Repository navigation
Expand file tree
/
Copy pathpackage
More file actions
executable file
·134 lines (116 loc) · 4.44 KB
/
Copy pathpackage
File metadata and controls
executable file
·134 lines (116 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
# Copyright 2026 Katteli Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Build the testflows.machine SDK into dist/: a wheel and a source archive.
./package
twine upload dist/*
The version is testflows/machine/v1/_version.py. Both files are checked for
exactly what they may carry, the SDK's own sources, so dist/ holds nothing
twine should not publish. Needs the `build` package: pip install build.
"""
import argparse
import importlib.util
import shutil
import subprocess
import sys
import tarfile
import zipfile
from pathlib import Path
HERE = Path(__file__).resolve().parent
DIST = HERE / "dist"
#: The distribution's name in file names, as the wheel and archive spell it.
DIST_NAME = "testflows_machine"
#: The one package the SDK ships.
PACKAGE = "testflows/machine/v1/"
#: Files the source archive may carry beside the package, at its top level.
SDIST_FILES = {
"README.md",
"LICENSE",
"NOTICE",
"pyproject.toml",
"PKG-INFO",
"setup.cfg",
}
def build() -> None:
"""Build the wheel and the source archive into a fresh dist/."""
# Stale output first: a build/ left here would pass for the build package.
for stale in (DIST, HERE / "build"):
shutil.rmtree(stale, ignore_errors=True)
for info in HERE.glob("*.egg-info"):
shutil.rmtree(info)
if importlib.util.find_spec("build") is None:
raise SystemExit(
"The build package is not installed.\n"
f" Install it {sys.executable} -m pip install build"
)
try:
subprocess.run(
[sys.executable, "-m", "build", "--outdir", str(DIST), str(HERE)],
check=True,
)
except subprocess.CalledProcessError as exc:
raise SystemExit(f"The build failed: {exc}") from exc
def one(pattern: str) -> Path:
found = sorted(DIST.glob(pattern))
if len(found) != 1:
raise SystemExit(f"expected one {pattern} in dist/, found {found}")
return found[0]
def check_wheel(wheel: Path) -> None:
"""Refuse a wheel that is not pure Python with only the SDK's package."""
if not wheel.name.endswith("-py3-none-any.whl"):
raise SystemExit(f"the wheel is not pure Python: {wheel.name}")
names = [
n
for n in zipfile.ZipFile(wheel).namelist()
if not n.endswith("/") and not n.startswith(f"{DIST_NAME}-")
]
stray = [n for n in names if not (n.startswith(PACKAGE) and n.endswith(".py"))]
if stray or not names:
raise SystemExit(f"the wheel carries files outside {PACKAGE}: {stray}")
def check_sdist(sdist: Path) -> None:
"""Refuse a source archive carrying anything but the SDK's sources."""
with tarfile.open(sdist) as archive:
members = [m.name for m in archive.getmembers() if m.isfile()]
stray = []
for name in members:
inside = name.split("/", 1)[1] if "/" in name else name
if inside.startswith(PACKAGE) and inside.endswith(".py"):
continue
if inside.startswith("examples/") and inside.endswith(".py"):
continue
if inside in SDIST_FILES or inside.split("/", 1)[0].endswith(".egg-info"):
continue
stray.append(inside)
if stray:
raise SystemExit(f"the source archive carries files it may not: {stray}")
def check_dist() -> None:
"""Refuse a dist/ holding anything but the one wheel and one archive."""
others = sorted(
p.name for p in DIST.iterdir() if not p.name.endswith((".whl", ".tar.gz"))
)
if others:
raise SystemExit(f"dist/ holds files that are not packages: {others}")
def main() -> None:
argparse.ArgumentParser(description=(__doc__ or "").splitlines()[0]).parse_args()
build()
wheel = one(f"{DIST_NAME}-*.whl")
sdist = one(f"{DIST_NAME}-*.tar.gz")
check_wheel(wheel)
check_sdist(sdist)
check_dist()
print(wheel.relative_to(HERE))
print(sdist.relative_to(HERE))
print("Publish them twine upload dist/*")
if __name__ == "__main__":
main()