|
| 1 | +"""Install the published Model Fit policy and update the checked-in distribution.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +PACKAGE_DIR = Path(__file__).resolve().parent |
| 10 | +ROOT = PACKAGE_DIR.parents[2] |
| 11 | +PACKAGE = "@atls/skill-model-fit" |
| 12 | +POLICY = "references/assessment-policy.md" |
| 13 | + |
| 14 | + |
| 15 | +def version(value: str) -> tuple[int, ...]: |
| 16 | + if not re.fullmatch(r"(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)", value): |
| 17 | + raise ValueError("Expected an exact Model Fit package version") |
| 18 | + return tuple(map(int, value.split("."))) |
| 19 | + |
| 20 | + |
| 21 | +def update(tag: str) -> None: |
| 22 | + prefix = f"{PACKAGE}@" |
| 23 | + if not tag.startswith(prefix): |
| 24 | + raise ValueError("Expected a Model Fit package release tag") |
| 25 | + requested = tag.removeprefix(prefix) |
| 26 | + version(requested) |
| 27 | + selected = json.loads( |
| 28 | + subprocess.check_output( |
| 29 | + ["npm", "view", PACKAGE, "version", "--json"], cwd=PACKAGE_DIR, text=True |
| 30 | + ) |
| 31 | + ) |
| 32 | + if version(selected) < version(requested): |
| 33 | + raise ValueError("The registry has not published the dispatched version") |
| 34 | + tag = prefix + selected |
| 35 | + path = PACKAGE_DIR / "package.json" |
| 36 | + current = json.loads(path.read_text())["dependencies"][PACKAGE] |
| 37 | + if version(selected) < version(current): |
| 38 | + return |
| 39 | + subprocess.run( |
| 40 | + [ |
| 41 | + "npm", |
| 42 | + "install", |
| 43 | + "--package-lock-only", |
| 44 | + "--save-exact", |
| 45 | + "--ignore-scripts", |
| 46 | + "--no-audit", |
| 47 | + "--no-fund", |
| 48 | + tag, |
| 49 | + ], |
| 50 | + cwd=PACKAGE_DIR, |
| 51 | + check=True, |
| 52 | + ) |
| 53 | + subprocess.run( |
| 54 | + ["npm", "ci", "--ignore-scripts", "--no-audit", "--no-fund"], |
| 55 | + cwd=PACKAGE_DIR, |
| 56 | + check=True, |
| 57 | + ) |
| 58 | + installed = PACKAGE_DIR / "node_modules" / PACKAGE |
| 59 | + manifest = json.loads((installed / "package.json").read_text()) |
| 60 | + if manifest["name"] != PACKAGE or manifest["version"] != selected: |
| 61 | + raise ValueError("Installed package differs from the requested release") |
| 62 | + (ROOT / "skills/model-fit" / POLICY).write_bytes((installed / POLICY).read_bytes()) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + parser = argparse.ArgumentParser(description=__doc__) |
| 67 | + parser.add_argument("tag") |
| 68 | + update(parser.parse_args().tag) |
0 commit comments