Coverage for packages/workshop/src/livery/workshop/_invoke.py: 100%

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"""How a step's path list reaches its tool: in how many calls.
2
3The paths go on the tool's command line, where a step's template puts
4them ([livery.workshop._words.run_words][]). `batches` splits a path
5list into the fewest calls the platform's command line allows, and
6`run_batched` runs each call and merges their failures into one
7refusal, so a long list never fails on the operating system's limit
8and never stops at the first failing call.
9"""
10
11from __future__ import annotations(empty)
12
13import sys(empty)
14from collections.abc import Callable, Sequence(empty)
15
16from livery.footman import Failed, fail(empty)
17
18#: The bytes of path arguments one call may carry. Windows caps a whole
19#: command line at 32767 characters; POSIX systems allow far more, and
20#: the figure stays well under the smallest of them (macOS's ARG_MAX of
21#: 1 MiB less the environment).
22ARGV_LIMIT = 30_000 if sys.platform == "win32" else 200_000(empty)
23
24
25def batches((empty)
26 paths: Sequence[str], *, limit: int = ARGV_LIMIT
27) -> tuple[tuple[str, ...], ...]:
28 """*paths* in the fewest calls whose arguments stay within *limit* bytes.
29
30 In order: each call takes paths until the next would not fit. A
31 path longer than the limit on its own goes in a call of its own,
32 since there is no smaller way to pass it.
33 """
34 calls: list[tuple[str, ...]] = []
35 current: list[str] = []
36 size = 0
37 for path in paths:
38 length = len(path.encode("utf-8")) + 1 # the separating space or NUL
39 if current and size + length > limit:
40 calls.append(tuple(current))
41 current, size = [], 0
42 current.append(path)
43 size += length
44 if current:
45 calls.append(tuple(current))
46 return tuple(calls)
47
48
49def run_batched((empty)
50 paths: Sequence[str],
51 call: Callable[[tuple[str, ...]], None],
52 *,
53 limit: int = ARGV_LIMIT,
54) -> None:
55 """Run *call* once per batch of *paths*; one refusal naming every failure.
56
57 Every batch runs even when an earlier one fails, so one gate shows
58 every finding. An empty *paths* runs nothing.
59
60 Raises:
61 Failed: when any batch failed, with each failure's reason.
62 """
63 reasons: list[str] = []
64 for batch in batches(paths, limit=limit):
65 try:
66 call(batch)
67 except Failed as failure:
68 reasons.append(str(failure))
69 if len(reasons) == 1:
70 fail(reasons[0])
71 if reasons:
72 fail(f"{len(reasons)} calls failed:\n" + "\n".join(reasons))