check_dependency_lock.py (21208B)
1 #!/usr/bin/env -S uv run --locked --script 2 # /// script 3 # requires-python = ">=3.11" 4 # dependencies = ["PyYAML==6.0.3"] 5 # /// 6 """Check the current Pixi v7 baseline without solving or installing its graph. 7 8 Run from the repository root with ``uv run --locked --script 9 tools/check_dependency_lock.py --mode baseline``. Baseline checks the existing 10 graph; success does not assert that a future dependency migration has happened. 11 The adjacent script lock is generated with ``uv lock --script``. 12 """ 13 14 from __future__ import annotations 15 16 import argparse 17 from dataclasses import dataclass 18 from pathlib import Path 19 import re 20 import sys 21 import tomllib 22 from urllib.parse import parse_qsl, urlsplit 23 24 import yaml 25 26 27 PLATFORMS = frozenset({"linux-64", "osx-arm64"}) 28 MOJO_VERSION = "1.0.0b1" 29 MOJO_PACKAGES = frozenset({"mojo", "mojo-compiler", "mojo-python"}) 30 JSON_REVISION = "56025d73631054d7803143fa5e68c5de578e33f5" 31 SOURCE_REPOSITORIES = { 32 "json": "https://github.com/triesap/mojo_json", 33 "morph": "https://github.com/triesap/mojo_morph", 34 "flare": "https://github.com/triesap/mojo_flare", 35 } 36 COMMIT = re.compile(r"[0-9a-f]{40}\Z") 37 NAME = re.compile(r"[a-z0-9_][a-z0-9_.-]*\Z") 38 LOCATOR = re.compile(r"([a-z0-9_][a-z0-9_.-]*)\[([0-9a-f]{8})\] @ git\+(.+)\Z") 39 MAX_INPUT_BYTES = 8 * 1024 * 1024 40 41 42 class InvalidLock(ValueError): 43 """An input violates a supported schema or dependency invariant.""" 44 45 46 def require(condition: bool, where: str, message: str) -> None: 47 if not condition: 48 raise InvalidLock(f"{where}: {message}") 49 50 51 def mapping(value: object, where: str) -> dict: 52 require(type(value) is dict, where, "expected a mapping") 53 require(all(type(key) is str for key in value), where, "expected string keys") 54 return value 55 56 57 def shape(value: object, where: str, required: set[str], optional: set[str] = frozenset()) -> dict: 58 result = mapping(value, where) 59 require(required <= result.keys(), where, "missing required fields; review the supported v7 schema") 60 require(result.keys() <= required | optional, where, "unsupported fields; extend the checker explicitly") 61 return result 62 63 64 def sequence(value: object, where: str, *, nonempty: bool = True) -> list: 65 require(type(value) is list, where, "expected a list") 66 require(not nonempty or bool(value), where, "expected a nonempty list") 67 return value 68 69 70 def string(value: object, where: str) -> str: 71 require(type(value) is str and bool(value) and value == value.strip(), where, "expected a nonblank string without surrounding whitespace") 72 require(not any(ord(char) < 32 or ord(char) == 127 for char in value), where, "control characters are forbidden") 73 return value 74 75 76 def strings(value: object, where: str, *, nonempty: bool = False) -> list[str]: 77 return [string(item, f"{where}[{index}]") for index, item in enumerate(sequence(value, where, nonempty=nonempty))] 78 79 80 class StrictLoader(yaml.SafeLoader): 81 """Reject ambiguous mappings, aliases and excessive YAML nesting.""" 82 83 def compose_node(self, parent, index): 84 event = self.peek_event() 85 require(not isinstance(event, yaml.AliasEvent) and getattr(event, "anchor", None) is None, "lock YAML", "anchors and aliases are unsupported") 86 require(getattr(event, "tag", None) is None, "lock YAML", "explicit YAML tags are unsupported") 87 self._node_count = getattr(self, "_node_count", 0) + 1 88 self._depth = getattr(self, "_depth", 0) + 1 89 require(self._node_count <= 100_000 and self._depth <= 64, "lock YAML", "document exceeds supported structural limits") 90 try: 91 return super().compose_node(parent, index) 92 finally: 93 self._depth -= 1 94 95 def construct_mapping(self, node, deep=False): 96 require(isinstance(node, yaml.MappingNode), "lock YAML", "expected a mapping node") 97 result = {} 98 for key_node, value_node in node.value: 99 key = self.construct_object(key_node, deep=deep) 100 require(type(key) is str, "lock YAML", "mapping keys must be strings") 101 require(key not in result, "lock YAML", f"duplicate mapping key at line {key_node.start_mark.line + 1}") 102 result[key] = self.construct_object(value_node, deep=deep) 103 return result 104 105 106 def read_text(path: Path, where: str) -> str: 107 try: 108 with path.open("rb") as stream: 109 data = stream.read(MAX_INPUT_BYTES + 1) 110 require(len(data) <= MAX_INPUT_BYTES, where, "input exceeds 8 MiB") 111 return data.decode("utf-8") 112 except (OSError, UnicodeError) as exc: 113 raise InvalidLock(f"{where}: cannot read a UTF-8 input") from exc 114 115 116 def web_url(value: object, where: str): 117 text = string(value, where) 118 require(not any(char.isspace() for char in text) and "\\" not in text, where, "malformed URL") 119 try: 120 parsed = urlsplit(text) 121 require(parsed.scheme == "https" and bool(parsed.hostname), where, "expected an absolute HTTPS URL") 122 require(parsed.username is None and parsed.password is None, where, "URL credentials are forbidden") 123 require(parsed.port in (None, 443), where, "unsupported URL port") 124 except ValueError as exc: 125 raise InvalidLock(f"{where}: malformed URL") from exc 126 return parsed 127 128 129 def repository(value: object, where: str, *, locked: bool = False) -> tuple[str, str | None]: 130 parsed = web_url(value, where) 131 path = parsed.path.rstrip("/") 132 if path.endswith(".git"): 133 path = path[:-4] 134 require(bool(path) and not any(part in ("", ".", "..") for part in path[1:].split("/")), where, "malformed repository path") 135 require("%" not in path, where, "escaped repository paths are unsupported") 136 identity = f"https://{parsed.hostname.lower()}{path}" 137 if not locked: 138 require(not parsed.query and not parsed.fragment, where, "manifest repository URL must not contain a ref") 139 return identity, None 140 try: 141 query = parse_qsl(parsed.query, keep_blank_values=True, strict_parsing=True) 142 except ValueError as exc: 143 raise InvalidLock(f"{where}: malformed Git query") from exc 144 require(len(query) == 1 and query[0][0] == "rev", where, "Git source requires only a rev query; branch/tag refs are forbidden") 145 revision = query[0][1] 146 require(COMMIT.fullmatch(revision) is not None, where, "Git rev must be a full lowercase 40-digit commit") 147 require(parsed.fragment == revision, where, "resolved Git commit must exactly match rev") 148 return identity, revision 149 150 151 @dataclass(frozen=True) 152 class Package: 153 kind: str 154 locator: str 155 name: str 156 version: str 157 platform: str 158 source: tuple[str, str] | None = None 159 160 161 def conda_identity(value: object, where: str) -> tuple[str, str, str]: 162 parsed = web_url(value, where) 163 require(not parsed.query and not parsed.fragment and "%" not in parsed.path, where, "Conda URL must identify a package without query, fragment or escapes") 164 parts = parsed.path.split("/") 165 require(len(parts) >= 3 and parts[-2] in PLATFORMS | {"noarch"}, where, "unsupported or missing Conda subdir") 166 filename = parts[-1] 167 suffix = next((item for item in (".conda", ".tar.bz2") if filename.endswith(item)), None) 168 require(suffix is not None, where, "expected a Conda package archive") 169 fields = filename[:-len(suffix)].rsplit("-", 2) 170 require(len(fields) == 3 and NAME.fullmatch(fields[0]) is not None and bool(fields[1]) and bool(fields[2]), where, "malformed Conda package filename") 171 return fields[0], fields[1], parts[-2] 172 173 174 def source_identity(value: object, where: str) -> tuple[str, tuple[str, str]]: 175 match = LOCATOR.fullmatch(string(value, where)) 176 require(match is not None, where, "expected a v7 name[variant] @ git+HTTPS locator") 177 name = match.group(1) 178 identity, revision = repository(match.group(3), where, locked=True) 179 return name, (identity, revision) 180 181 182 def manifest_policy(value: object) -> dict[str, tuple[str, str]]: 183 manifest = shape(value, "manifest", {"workspace", "dependencies", "tasks"}) 184 workspace = shape(manifest["workspace"], "manifest.workspace", {"name", "version", "description", "authors", "channels", "platforms", "preview"}) 185 for field in ("name", "version", "description"): 186 string(workspace[field], f"manifest.workspace.{field}") 187 for field in ("authors", "channels", "preview"): 188 strings(workspace[field], f"manifest.workspace.{field}", nonempty=True) 189 platforms = strings(workspace["platforms"], "manifest.workspace.platforms", nonempty=True) 190 require(len(platforms) == len(PLATFORMS) and set(platforms) == PLATFORMS, "manifest.workspace.platforms", "baseline requires exactly linux-64 and osx-arm64") 191 mapping(manifest["tasks"], "manifest.tasks") 192 dependencies = shape(manifest["dependencies"], "manifest.dependencies", {"mojo", *SOURCE_REPOSITORIES}) 193 require(dependencies["mojo"] == f"=={MOJO_VERSION}", "manifest.dependencies.mojo", "baseline requires exact Mojo ==1.0.0b1; migration needs a separate explicit policy") 194 sources = {} 195 for index, name in enumerate(sorted(SOURCE_REPOSITORIES)): 196 where = f"manifest source[{index}]" 197 spec = shape(dependencies[name], where, {"git", "rev"}) 198 identity, _ = repository(spec["git"], where) 199 revision = string(spec["rev"], where) 200 require(COMMIT.fullmatch(revision) is not None, where, "Git rev must be a full lowercase 40-digit commit; branch/tag refs are forbidden") 201 require(identity == SOURCE_REPOSITORIES[name], where, "repository differs from the independently defined baseline source") 202 if name == "json": 203 require(revision == JSON_REVISION, where, "JSON revision differs from the independently defined baseline revision") 204 sources[name] = (identity, revision) 205 return sources 206 207 208 def requirements(value: object, where: str) -> list[str]: 209 values = strings(value, where) 210 mojo_names = set() 211 for index, value in enumerate(values): 212 item_where = f"{where}[{index}]" 213 match = re.fullmatch(r"([a-z0-9_][a-z0-9_.-]*)(?: ([A-Za-z0-9*<>=!~.,+_| -]+))?", value) 214 require(match is not None, item_where, "unsupported dependency match specification") 215 name = match.group(1) 216 if name in MOJO_PACKAGES: 217 require(match.group(2) == f"=={MOJO_VERSION}", item_where, "Mojo dependency must exactly match the baseline root pin") 218 require(name not in mojo_names, item_where, "duplicate Mojo dependency") 219 mojo_names.add(name) 220 require(name != "uuid", item_where, "runtime UUID is outside the baseline; native libuuid is distinct") 221 return values 222 223 224 def metadata(record: dict, where: str) -> None: 225 for field in ("depends", "constrains"): 226 if field in record: 227 requirements(record[field], f"{where}.{field}") 228 for field in ("license", "license_family", "python_site_packages_path"): 229 if field in record: 230 string(record[field], f"{where}.{field}") 231 for field in ("build_number", "size", "timestamp"): 232 if field in record: 233 require(type(record[field]) is int and record[field] >= 0, f"{where}.{field}", "expected a nonnegative integer") 234 if "noarch" in record: 235 require(record["noarch"] in ("python", "generic"), f"{where}.noarch", "unsupported noarch type") 236 if "run_exports" in record: 237 exports = shape(record["run_exports"], f"{where}.run_exports", set(), {"weak", "strong", "noarch", "weak_constrains", "strong_constrains"}) 238 for index, value in enumerate(exports.values()): 239 requirements(value, f"{where}.run_exports[{index}]") 240 241 242 def package_record(value: object, index: int, sources: dict) -> Package: 243 where = f"lock.packages[{index}]" 244 record = mapping(value, where) 245 if "conda" in record: 246 record = shape(record, where, {"conda", "sha256", "md5"}, {"depends", "constrains", "build_number", "license", "license_family", "size", "timestamp", "noarch", "run_exports", "python_site_packages_path"}) 247 for field, length in (("sha256", 64), ("md5", 32)): 248 require(type(record[field]) is str and re.fullmatch(rf"[0-9a-f]{{{length}}}", record[field]) is not None, f"{where}.{field}", "expected a lowercase archive digest") 249 name, version, platform = conda_identity(record["conda"], where) 250 require(name not in SOURCE_REPOSITORIES, where, "a source-owned runtime package must not be replaced by a binary Conda package") 251 package = Package("conda", record["conda"], name, version, platform) 252 else: 253 record = shape(record, where, {"conda_source", "version", "build", "subdir", "variants", "depends"}, {"license", "license_family", "constrains", "build_packages", "host_packages"}) 254 name, source = source_identity(record["conda_source"], where) 255 require(name in sources, where, "unsupported runtime Git source; baseline allows only JSON, Morph and Flare") 256 require(source == sources[name], where, "Git source/revision differs from its exact manifest pin") 257 platform = string(record["subdir"], f"{where}.subdir") 258 require(platform in PLATFORMS, where, "unsupported source platform") 259 variants = shape(record["variants"], f"{where}.variants", {"target_platform"}) 260 require(variants["target_platform"] == platform, where, "source variant and subdir disagree") 261 string(record["build"], f"{where}.build") 262 package = Package("conda_source", record["conda_source"], name, string(record["version"], f"{where}.version"), platform, source) 263 require(f"mojo =={MOJO_VERSION}" in strings(record["depends"], f"{where}.depends", nonempty=True), where, "source package must declare the baseline exact Mojo dependency") 264 require(package.name != "uuid", where, "runtime UUID is outside the baseline; native libuuid is a distinct allowed package") 265 if package.name in MOJO_PACKAGES: 266 require(package.version == MOJO_VERSION, where, "Mojo/compiler/python version differs from the exact baseline root pin") 267 metadata(record, where) 268 return package 269 270 271 def package_references(value: object, where: str, packages: dict, platform: str) -> list[Package]: 272 result = [] 273 seen = set() 274 names = set() 275 for index, item in enumerate(sequence(value, where)): 276 item_where = f"{where}[{index}]" 277 item = mapping(item, item_where) 278 require(len(item) == 1 and next(iter(item)) in {"conda", "conda_source"}, item_where, "expected exactly one supported package locator") 279 kind, locator = next(iter(item.items())) 280 string(locator, item_where) 281 key = (kind, locator) 282 require(key in packages, item_where, "package reference has no corresponding package record") 283 require(key not in seen, item_where, "duplicate package reference") 284 package = packages[key] 285 require(package.name not in names, item_where, "multiple records select the same package name on one platform") 286 require(package.platform in {platform, "noarch"}, item_where, "referenced package belongs to another platform") 287 seen.add(key) 288 names.add(package.name) 289 result.append(package) 290 return result 291 292 293 def check_lock(value: object, sources: dict) -> None: 294 lock = shape(value, "lock", {"version", "platforms", "environments", "packages"}) 295 require(type(lock["version"]) is int and lock["version"] == 7, "lock.version", "only actual Pixi YAML v7 is supported") 296 platforms = [] 297 for index, value in enumerate(sequence(lock["platforms"], "lock.platforms")): 298 item = shape(value, f"lock.platforms[{index}]", {"name"}) 299 platforms.append(string(item["name"], f"lock.platforms[{index}].name")) 300 require(len(platforms) == len(PLATFORMS) and set(platforms) == PLATFORMS, "lock.platforms", "baseline requires exactly linux-64 and osx-arm64") 301 records = sequence(lock["packages"], "lock.packages") 302 packages = {} 303 source_platforms = set() 304 for index, record in enumerate(records): 305 package = package_record(record, index, sources) 306 key = (package.kind, package.locator) 307 require(key not in packages, f"lock.packages[{index}]", "duplicate package record") 308 packages[key] = package 309 if package.source is not None: 310 identity = (package.name, package.platform) 311 require(identity not in source_platforms, f"lock.packages[{index}]", "multiple source records for one runtime package/platform") 312 source_platforms.add(identity) 313 require(source_platforms == {(name, platform) for name in sources for platform in PLATFORMS}, "lock.packages", "missing source package/platform records") 314 # Two JSON variant records represent one canonical repository/commit, not 315 # two sources. Every source record was compared to the independent policy. 316 json_sources = {package.source for package in packages.values() if package.name == "json"} 317 require(json_sources == {sources["json"]}, "lock.packages", "expected one canonical JSON repository/revision") 318 environments = shape(lock["environments"], "lock.environments", {"default"}) 319 environment = shape(environments["default"], "lock.environments.default", {"channels", "packages"}) 320 channels = [] 321 for index, value in enumerate(sequence(environment["channels"], "lock default channels")): 322 item = shape(value, f"lock default channels[{index}]", {"url"}) 323 parsed = web_url(item["url"], f"lock default channels[{index}]") 324 require(not parsed.query and not parsed.fragment, f"lock default channels[{index}]", "channel URL must not contain a query or fragment") 325 channels.append(item["url"].rstrip("/")) 326 require(len(set(channels)) == len(channels), "lock default channels", "duplicate channel") 327 selections = shape(environment["packages"], "lock default packages", set(PLATFORMS)) 328 selected_sources = set() 329 for platform in sorted(PLATFORMS): 330 selected = package_references(selections[platform], f"lock default packages.{platform}", packages, platform) 331 names = {package.name for package in selected} 332 require(MOJO_PACKAGES <= names, f"lock default packages.{platform}", "missing Mojo, mojo-compiler or mojo-python package") 333 actual_sources = {package.name for package in selected if package.source is not None} 334 require(actual_sources == sources.keys(), f"lock default packages.{platform}", "missing required runtime source selection") 335 selected_sources.update((package.name, package.platform) for package in selected if package.source is not None) 336 require(selected_sources == source_platforms, "lock default packages", "source records and runtime selections disagree") 337 # Inspect every recorded build/host package reference too; unresolved refs, 338 # foreign platforms and hidden alternate source records cannot pass here. 339 for index, record in enumerate(records): 340 package = packages[("conda_source", record["conda_source"])] if "conda_source" in record else None 341 for field in ("build_packages", "host_packages"): 342 if field in record: 343 package_references(record[field], f"lock.packages[{index}].{field}", packages, package.platform) 344 345 346 class Parser(argparse.ArgumentParser): 347 def error(self, message): 348 self.exit(2, "dependency lock: invalid command line; use --help\n") 349 350 351 def main(argv: list[str] | None = None) -> int: 352 parser = Parser(description=__doc__) 353 parser.add_argument("--mode", choices=("baseline",), required=True, help="explicitly select the existing graph policy") 354 parser.add_argument("--manifest", type=Path, default=Path("pixi.toml"), help="manifest input (default: pixi.toml)") 355 parser.add_argument("--lock", type=Path, default=Path("pixi.lock"), help="lock input (default: pixi.lock)") 356 parser.add_argument("--self-test", action="store_true", help="run isolated synthetic baseline acceptance and rejection cases") 357 args = parser.parse_args(argv) 358 if args.self_test: 359 from check_dependency_lock_self_test import run 360 361 return run(sys.modules[__name__]) 362 try: 363 try: 364 manifest = tomllib.loads(read_text(args.manifest, "manifest input")) 365 except tomllib.TOMLDecodeError as exc: 366 raise InvalidLock("manifest TOML: malformed document") from exc 367 try: 368 lock = yaml.load(read_text(args.lock, "lock input"), Loader=StrictLoader) 369 except yaml.YAMLError as exc: 370 mark = getattr(exc, "problem_mark", None) 371 location = f" at line {mark.line + 1}, column {mark.column + 1}" if mark is not None else "" 372 raise InvalidLock(f"lock YAML: malformed or unsupported document{location}") from exc 373 check_lock(lock, manifest_policy(manifest)) 374 except InvalidLock as exc: 375 print(f"dependency lock: FAIL: {exc}", file=sys.stderr) 376 return 1 377 except (RecursionError, TypeError, ValueError, KeyError, OverflowError): 378 print("dependency lock: FAIL: malformed or unsupported input shape", file=sys.stderr) 379 return 1 380 print("dependency lock: baseline PASS (YAML v7; 2 platforms; one JSON source; immutable runtime Git refs; Mojo 1.0.0b1)") 381 return 0 382 383 384 if __name__ == "__main__": 385 raise SystemExit(main())