1"""Which packages a change to a file outside every package is about.
3The affected engine ([livery.workshop._graph.affected_from_paths][])
4reads a changed root file as affecting everything, since the root
5files configure every gate. Adding or removing a package changes root
6files too: the composed `pyproject.toml` names the member, CODEOWNERS
7its directory, `uv.lock` its entry. This module attributes such a
8change to the packages it is about, so the package's own gate runs and
9the others' do not.
11A root file's change is explained by the package set when removing
12every entry that names an added or removed package, from both
13versions, leaves the same text. `uv.lock`'s change is attributed per
14member: each workspace member whose resolved closure differs is
15affected, and the root project's own change must lie inside the
16closures of affected members. Anything else is unexplained, and the
17caller runs everything, as before.
18"""
20from __future__ import annotations
22import json
23import re
24import tomllib
25from collections.abc import Iterable
26from dataclasses import dataclass
27from pathlib import Path
28from typing import Any, cast
30from livery import git
31from livery.workshop._packages import PACKAGES_DIR, Package
33#: The lock every python member resolves through.
34LOCK = "uv.lock"
37@dataclass(frozen=True)
38class Delta:
39 """The packages that exist on one side of a change and not the other.
41 Attributes:
42 added: The packages the change adds, as discovery sees them now.
43 removed: The removed packages' paths and distribution names,
44 as their contracts said before the change.
45 """
47 added: tuple[Package, ...]
48 removed: tuple[tuple[str, str], ...]
50 @property
51 def tokens(self) -> tuple[str, ...]:
52 """Every path and distribution name that names a package of the delta."""
53 names = [
54 token for package in self.added for token in (package.path, package.name)
55 ]
56 names += [token for path, name in self.removed for token in (path, name)]
57 return tuple(token for token in names if token)
59 def holds(self, path: str) -> bool:
60 """Whether *path* lies under a removed package's directory."""
61 return any(path.startswith(f"{removed}/") for removed, _ in self.removed)
64def package_delta(root: Path, before: str, packages: Iterable[Package]) -> Delta:
65 """The packages added and removed between *before* and the working tree.
67 One listing of *before*'s tree answers which contracts existed
68 then; a package's contract is ``packages/<name>/workshop.toml`` or,
69 in a group directory, ``packages/<group>/<name>/workshop.toml``.
70 """
71 listed = git.run(root, "ls-tree", "-r", "--name-only", before, "--", PACKAGES_DIR)
72 contracts = {
73 line.removesuffix("/workshop.toml")
74 for line in listed.splitlines()
75 if line.endswith("/workshop.toml") and len(line.split("/")) in (3, 4)
76 }
77 now = list(packages)
78 present = {package.path for package in now}
79 added = tuple(package for package in now if package.path not in contracts)
80 removed: list[tuple[str, str]] = []
81 for path in sorted(contracts - present):
82 try:
83 contract = tomllib.loads(git.file_at(root, before, f"{path}/workshop.toml"))
84 except tomllib.TOMLDecodeError:
85 contract = {}
86 removed.append((path, str(contract.get("name") or "")))
87 return Delta(added, tuple(removed))
90def _boundary(token: str) -> str:
91 """*token* as a pattern that does not match inside a longer name."""
92 return rf"(?<![\w.-]){re.escape(token)}(?![\w.-])"
95def _without(text: str, tokens: tuple[str, ...]) -> str:
96 """*text* with every entry naming one of *tokens* removed, whitespace dropped.
98 A quoted entry naming a token goes on its own, so a list on one
99 line keeps its other entries; a line that still names a token
100 after that (a table key, a CODEOWNERS or ignore line) goes whole.
101 Whitespace is dropped and commas collapsed, so the list a removal
102 leaves reads the same as one that never held the entry.
103 """
104 for token in tokens:
105 text = re.sub(rf'"/?{re.escape(token)}(?:[/\[][^"]*)?"', "", text)
106 patterns = [re.compile(_boundary(token)) for token in tokens]
107 kept = [
108 line
109 for line in text.splitlines()
110 if not any(pattern.search(line) for pattern in patterns)
111 ]
112 flat = re.sub(r"\s+", "", "\n".join(kept))
113 flat = re.sub(r",+", ",", flat)
114 return re.sub(r"\[,", "[", re.sub(r",\]", "]", flat))
117def explained(before: str, now: str, delta: Delta) -> bool:
118 """Whether a root file's change names only *delta*'s packages."""
119 tokens = delta.tokens
120 if not tokens:
121 return False
122 return _without(before, tokens) == _without(now, tokens)
125def _lock(text: str) -> dict[str, Any] | None:
126 """The parsed lock, or None when there is none or it does not parse."""
127 if not text:127 ↛ 128line 127 didn't jump to line 128 because the condition on line 127 was never true
128 return None
129 try:
130 return tomllib.loads(text)
131 except tomllib.TOMLDecodeError:
132 return None
135Node = tuple[str, str, str]
138def _closures(
139 lock: dict[str, Any],
140) -> tuple[dict[str, frozenset[Node]], frozenset[Node]]:
141 """Each workspace member's resolved closure, by package path, and the root's.
143 A node is a locked entry: name, version and source. A member is an
144 entry whose source is ``editable`` (its path); the root is the
145 ``virtual`` entry. Every dependency section counts: runtime, every
146 extra, every development group.
147 """
148 entries = cast("list[dict[str, Any]]", lock.get("package") or [])
149 by_name: dict[str, list[dict[str, Any]]] = {}
150 for entry in entries:
151 by_name.setdefault(str(entry.get("name", "")), []).append(entry)
153 def node(entry: dict[str, Any]) -> Node:
154 source = json.dumps(entry.get("source") or {}, sort_keys=True)
155 return (str(entry.get("name", "")), str(entry.get("version", "")), source)
157 def edges(entry: dict[str, Any]) -> list[str]:
158 named = list(cast("list[dict[str, Any]]", entry.get("dependencies") or []))
159 for section in ("optional-dependencies", "dev-dependencies"):
160 groups = cast("dict[str, list[dict[str, Any]]]", entry.get(section) or {})
161 for group in groups.values():
162 named.extend(group)
163 return [str(item.get("name", "")) for item in named]
165 def closure(start: dict[str, Any]) -> frozenset[Node]:
166 seen: dict[Node, dict[str, Any]] = {node(start): start}
167 stack = [start]
168 while stack:
169 for name in edges(stack.pop()):
170 for entry in by_name.get(name, []):
171 key = node(entry)
172 if key not in seen:
173 seen[key] = entry
174 stack.append(entry)
175 return frozenset(seen)
177 members: dict[str, frozenset[Node]] = {}
178 root: frozenset[Node] = frozenset()
179 for entry in entries:
180 source = cast("dict[str, Any]", entry.get("source") or {})
181 if "editable" in source:
182 members[str(source["editable"])] = closure(entry)
183 elif "virtual" in source:
184 root = closure(entry)
185 return members, root
188def lock_affected(before: str, now: str, delta: Delta) -> set[str] | None:
189 """The package paths whose resolution the lock's change moves; None for everything.
191 Everything when either side does not parse, when anything beside
192 the member list and the entries changed (the resolution's
193 settings), or when the root project's closure changed outside the
194 closures of the affected members: a tool only the root resolves
195 configures every gate.
196 """
197 old, new = _lock(before), _lock(now)
198 if old is None or new is None:
199 return None
200 settings = {key for key in (*old, *new) if key not in ("package", "manifest")}
201 if any(old.get(key) != new.get(key) for key in settings):
202 return None
203 tokens = delta.tokens
204 old_manifest = json.dumps(old.get("manifest") or {}, sort_keys=True)
205 new_manifest = json.dumps(new.get("manifest") or {}, sort_keys=True)
206 if old_manifest != new_manifest and (206 ↛ 209line 206 didn't jump to line 209 because the condition on line 206 was never true
207 not tokens or _without(old_manifest, tokens) != _without(new_manifest, tokens)
208 ):
209 return None
210 old_members, old_root = _closures(old)
211 new_members, new_root = _closures(new)
212 affected = {
213 path
214 for path in new_members
215 if new_members[path] != old_members.get(path, frozenset())
216 }
217 covered: set[Node] = set()
218 for path in affected | {path for path, _ in delta.removed}:
219 covered |= new_members.get(path, frozenset())
220 covered |= old_members.get(path, frozenset())
221 moved = (old_root ^ new_root) - covered
222 if moved:
223 return None
224 return affected