Coverage for packages/workshop/src/livery/workshop/_update.py: 92%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop, packages/extensions/ty, packages/extensions/ruff, packages/extensions/python, packages/extensions/pytest, packages/extensions/pyrefly, packages/extensions/nanobind, packages/extensions/mypy, packages/extensions/mkdocstrings, packages/extensions/lfs, packages/extensions/docs, packages/extensions/cpp, packages/extensions/coverage, packages/extensions/conan, packages/extensions/cmake, packages/extensions/claude, packages/extensions/clang-tidy, packages/extensions/clang-format, packages/extensions/changelog, packages/extensions/basedpyright ran.

1"""The update family's file movers.
2
3The pieces ``workflow.update`` drives: raise ``[[depends]]`` floors
4to the latest released tags, and read the newest release per package
5from the tags. The driver that branches, commits, and submits lives
6beside this module.
7"""
8
9from __future__ import annotations(empty)
10
11import re(empty)
12from pathlib import Path(empty)
13
14from livery import git(empty)
15from livery.footman import fail(empty)
16from livery.workshop._packages import discover_packages, release_tag(empty)
17
18
19def latest_released(tags: tuple[str, ...]) -> dict[str, str]:(empty)
20 """The newest released version per package path, from *tags*."""
21 latest: dict[str, tuple[int, int, int]] = {}
22 for tag in tags:
23 found = release_tag(tag)
24 if found is None:
25 continue
26 path, text = found
27 major, minor, patch = (int(part) for part in text.split("."))
28 version = (major, minor, patch)
29 if version > latest.get(path, (-1, -1, -1)):29 ↛ 22line 29 didn't jump to line 22 because the condition on line 29 was always true
30 latest[path] = version
31 return {path: ".".join(map(str, v)) for path, v in latest.items()}
32
33
34def bump_floors(root: Path, *, only: tuple[str, ...] = ()) -> list[str]:(empty)
35 """Raise floors to the latest released tags; what changed.
36
37 A floor names the oldest version a dependant accepts; this raises
38 it to the newest release so instances move together. Both homes
39 move in step: the ``[[depends]]`` edge in ``workshop.toml`` and the
40 ``>=`` constraint in ``pyproject.toml``. *only* scopes the move
41 to floors on the named distributions (``livery-forge``); empty
42 moves every floor.
43 """
44 packages = discover_packages(root)
45 dist_names = {p.path: p.name for p in packages}
46 released = latest_released(git.tags(root))
47 changed = []
48 for package in packages:
49 for edge in package.depends:
50 if only and dist_names.get(edge.path, "") not in only:
51 continue
52 newest = released.get(edge.path, "")
53 if not edge.floor or not newest or newest == edge.floor:
54 continue
55 contract = package.directory / "workshop.toml"
56 text = contract.read_text("utf-8")
57 scoped = _bump_edge_floor(text, edge.path, edge.floor, newest)
58 contract.write_text(scoped, encoding="utf-8")
59 pyproject = package.directory / "pyproject.toml"
60 text = pyproject.read_text("utf-8")
61 pyproject.write_text(
62 text.replace(f">={edge.floor}", f">={newest}"), encoding="utf-8"
63 )
64 changed.append(
65 f"{package.path}: floor on {edge.path} {edge.floor} -> {newest}"
66 )
67 return changed
68
69
70def _bump_edge_floor(text: str, dep_path: str, old: str, new: str) -> str:(empty)
71 """The contract text with one edge's floor raised, scoped to its block."""
72 anchor = text.find(f'path = "{dep_path}"')
73 if anchor == -1:73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true
74 fail(f"no [[depends]] edge on {dep_path} found to bump")
75 tail = text[anchor:]
76 bumped, count = re.subn(
77 rf'floor = "{re.escape(old)}"', f'floor = "{new}"', tail, count=1
78 )
79 if count != 1:79 ↛ 80line 79 didn't jump to line 80 because the condition on line 79 was never true
80 fail(f"the edge on {dep_path} has no floor {old!r} line to bump")
81 return text[:anchor] + bumped