|
6 | 6 | import subprocess |
7 | 7 | from pathlib import Path |
8 | 8 |
|
| 9 | +# Flavor paths are names, not full paths: these say what they hang from. |
| 10 | +UNDER_BASEDIR = ("requirements", "source_dirs", "wheelhousereq") |
| 11 | +UNDER_ROOT = ("toolsdirs",) |
| 12 | + |
9 | 13 | def load_builds(config_file): |
10 | 14 | with open(config_file, "rb") as f: |
11 | 15 | config = tomllib.load(f) |
12 | | - builds = config["builds"] |
13 | 16 | python_versions = config.get("pythons", {}) |
| 17 | + # A file may still spell out one [[builds]] block per build; otherwise the |
| 18 | + # builds are the (python, flavor) pairs [pythons] asks for. |
| 19 | + builds = config["builds"] if "builds" in config else expand_builds(config) |
14 | 20 | return builds, python_versions |
15 | 21 |
|
16 | | -def declared_file(build, key, default): |
17 | | - """Path named by the build for `key`, else `default`. |
| 22 | +def expand_builds(config): |
| 23 | + """One build per flavor listed by each [pythons."3XX"], paths derived. |
18 | 24 |
|
19 | | - A path a build spells out must exist: pip_install() skips a missing |
20 | | - requirements file without failing, which would drop the packages silently. |
| 25 | + A flavor names its files relative to the build directory, so a new Python |
| 26 | + minor is one [pythons] block. Where a Python needs its own file - a |
| 27 | + requirements list under another name, say - [pythons."3XX".overrides.flavor] |
| 28 | + replaces that flavor's entries for that Python alone. |
21 | 29 | """ |
| 30 | + defaults = config.get("defaults", {}) |
| 31 | + flavors = config.get("flavors", {}) |
| 32 | + builds = [] |
| 33 | + for target, vinfo in config.get("pythons", {}).items(): |
| 34 | + root = Path(vinfo.get("root_dir_for_builds", defaults.get("root_dir_for_builds", ""))) |
| 35 | + basedir = root / f"bd{target}" |
| 36 | + overrides = vinfo.get("overrides", {}) |
| 37 | + for flavor in vinfo.get("builds", []): |
| 38 | + if flavor not in flavors: |
| 39 | + raise KeyError(f"python {target} builds {flavor!r}, which has no [flavors.{flavor}]") |
| 40 | + build = {**defaults, "name": flavor, "python_target": target, "flavor": flavor} |
| 41 | + for key, value in {**flavors[flavor], **overrides.get(flavor, {})}.items(): |
| 42 | + if key in UNDER_BASEDIR: |
| 43 | + value = str(basedir / value) |
| 44 | + elif key in UNDER_ROOT: |
| 45 | + value = str(root / value) |
| 46 | + build[key] = value |
| 47 | + builds.append(build) |
| 48 | + return builds |
| 49 | + |
| 50 | +def select_builds(builds, wanted): |
| 51 | + """Builds asked for on the command line: "315", "315:slim", ":slim".""" |
| 52 | + if not wanted: |
| 53 | + return builds |
| 54 | + kept = [] |
| 55 | + for spec in wanted: |
| 56 | + target, _, flavor = spec.partition(":") |
| 57 | + matching = [b for b in builds |
| 58 | + if (not target or b["python_target"] == target) |
| 59 | + and (not flavor or b["flavor"] == flavor)] |
| 60 | + if not matching: |
| 61 | + raise SystemExit(f"no build matches {spec!r}") |
| 62 | + kept += [b for b in matching if b not in kept] |
| 63 | + return kept |
| 64 | + |
| 65 | +def must_exist(path, build, key): |
| 66 | + """pip_install() skips a missing requirements file without failing, which |
| 67 | + would drop those packages silently. Say so instead.""" |
| 68 | + if path and not Path(path).exists(): |
| 69 | + raise FileNotFoundError(f"build {build['name']!r} needs {key} = {path!r}, which does not exist") |
| 70 | + return path |
| 71 | + |
| 72 | +def declared_file(build, key, default): |
| 73 | + """Path named by the build for `key`, else `default`.""" |
22 | 74 | declared = build.get(key) |
23 | | - if declared is None: |
24 | | - return str(default) |
25 | | - if not Path(declared).exists(): |
26 | | - raise FileNotFoundError(f"build {build['name']!r} sets {key} = {declared!r}, which does not exist") |
27 | | - return str(declared) |
28 | | - |
29 | | -def run_build(build, python_versions): |
30 | | - print(f"\n=== Building WinPython: {build['name']} ===") |
| 75 | + return str(default) if declared is None else must_exist(str(declared), build, key) |
| 76 | + |
| 77 | +def run_build(build, python_versions, dry_run=False): |
| 78 | + print(f"\n=== Building WinPython: {build['python_target']} {build['name']} ===") |
31 | 79 | print(build) |
32 | 80 |
|
33 | 81 | root_dir_for_builds = build["root_dir_for_builds"] |
34 | 82 | my_python_target = build["python_target"] |
35 | 83 | my_flavor = build["flavor"] |
36 | 84 | my_arch = str(build["arch"]) |
37 | 85 | my_create_installer = build.get("create_installer", "True") |
38 | | - my_requirements = build.get("requirements", "") |
| 86 | + my_requirements = must_exist(build.get("requirements", ""), build, "requirements") |
39 | 87 | my_source_dirs = build.get("source_dirs", "") |
40 | 88 | my_find_links = build.get("find_links", "") |
41 | 89 | my_toolsdirs = build.get("toolsdirs", "") |
42 | | - wheelhousereq = build.get("wheelhousereq", "") |
| 90 | + wheelhousereq = must_exist(build.get("wheelhousereq", ""), build, "wheelhousereq") |
43 | 91 | # "pip" (default, what ships) | "none" | "parallel" | "parallel-N" |
44 | 92 | my_bytecode = build.get("bytecode", "pip") |
45 | 93 |
|
@@ -89,15 +137,18 @@ def run_build(build, python_versions): |
89 | 137 | "--create-installer", my_create_installer, |
90 | 138 | ] |
91 | 139 |
|
92 | | - print("Running build command:") |
| 140 | + print("Dry run, build command:" if dry_run else "Running build command:") |
93 | 141 | print(" ".join(build_cmd)) |
94 | | - subprocess.run(build_cmd, cwd=os.getcwd(), check=False) |
| 142 | + if not dry_run: |
| 143 | + subprocess.run(build_cmd, check=False) |
95 | 144 |
|
96 | 145 | def main(): |
97 | | - config_file = sys.argv[1] if len(sys.argv) > 1 else "winpython_buildsNOT.toml" |
| 146 | + args = [a for a in sys.argv[1:] if a != "--dry-run"] |
| 147 | + dry_run = len(args) != len(sys.argv) - 1 |
| 148 | + config_file = args[0] if args else "winpython_builds.toml" |
98 | 149 | builds, python_versions = load_builds(config_file) |
99 | | - for build in builds: |
100 | | - run_build(build, python_versions) |
| 150 | + for build in select_builds(builds, args[1:]): |
| 151 | + run_build(build, python_versions, dry_run) |
101 | 152 |
|
102 | 153 | if __name__ == "__main__": |
103 | 154 | main() |
0 commit comments