Coverage for packages/workshop/src/livery/workshop/_tools.py: 92%
Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop/tests/test_workshop_workflow.py, packages/workshop/tests/test_workshop_update.py, packages/workshop/tests/test_workshop_sync_locked.py, packages/workshop/tests/test_workshop_sync.py, packages/workshop/tests/test_workshop_submit.py, packages/workshop/tests/test_workshop_repair.py, packages/workshop/tests/test_workshop_release_driver.py, packages/workshop/tests/test_workshop_prose.py, packages/workshop/tests/test_workshop_plugin_tools.py, packages/workshop/tests/test_workshop_fallbacks.py, packages/workshop/tests/test_workshop_extension_mount.py, packages/workshop/tests/test_workshop_env.py, packages/workshop/tests/test_workshop_credential_traces.py, packages/workshop/tests/test_workshop_balance.py, packages/workshop/tests/test_tools_restub.py, packages/workshop/tests/test_tools_materialise.py, packages/workshop/tests/test_kind_tools.py, packages/extensions/pytest/tests/test_pytest.py, packages/extensions/nanobind/tests/test_nanobind_kind.py, packages/extensions/mkdocstrings/tests/test_mkdocstrings.py, packages/extensions/lfs/tests/test_lfs.py, packages/extensions/docs/tests/test_taskref.py, packages/extensions/docs/tests/test_llms.py, packages/extensions/docs/tests/test_docs_verbs.py, packages/extensions/docs/tests/test_docs_site.py, packages/extensions/docs/tests/test_docs_phase.py, packages/extensions/docs/tests/test_docs.py, packages/extensions/cpp/tests/test_extensions_cpp.py, packages/extensions/conan/tests/test_conan_publish.py, packages/extensions/conan/tests/test_conan_kind.py, packages/extensions/cmake/tests/test_measured_run.py, packages/extensions/cmake/tests/test_cpp_kind.py, packages/extensions/agents/tests/test_agents.py ran.
909 statements
837 run
72 missing
4 excluded
19 partial
?
Keys r m x p show or hide the lines of a kindj k the next or previous highlighted chunk0 the top of the page1 the first highlighted chunk[ ] the previous or next fileu the index? show or hide this help
1 """The tools a workspace requires: six declaration sites, one lock, receipts.
3 A tool requirement is a name with a floor, `git` or `git>=2.40`, and
4 six sites declare them: the base, under `[toolroom] requires` in its
5 contract file, for uv, which makes the venv every step runs in; the
6 steps that judge a present package, each naming its tools; a listed
7 extension, the workspace's or one a package's set holds, under
8 `[toolroom] requires` in its `extension.toml`, for the tools it runs
9 (cmake's cmake and ninja, conan's conan), and in a `[for.<target>]`
10 table, for what it needs while that table applies; a plugin the
11 project mounts, through its `workshop.tools` entry point; a package
12 instance, in its `workshop.toml` under `[toolroom] requires`, for what
13 its extensions cannot know; and the project, in the root contract's
14 `[toolroom] requires`, for what belongs to the repository. The sites'
15 requirements union, and `toolroom.lock` at the root holds one version
16 per tool for the whole repository, the newest the catalogue lists that
17 satisfies every floor and resolves on every supported host. The root
18 contract's `[workspace] hosts` names those hosts, every host key when
19 it is absent, and `[toolroom] index` names where the catalogue is read
20 from: the published index by URL, a directory holding one, or the
21 records that build one.
23 Entering the environment materialises the bundle the sites require, on
24 `fm sync` and on `fm toolroom.add`: each locked tool is supplied through
25 the machine's store in one of three modes, its entry points linked
26 into the checkout's bin directory, its own directories on PATH, or no
27 PATH at all, and a receipt under `.workshop/receipts/` records the
28 exact version, the host, the deployment's digest and what reached
29 PATH. A receipt is a checkout's statement of what it installed, as the
30 release train's receipt is a release's; `fm env.check` reads them
31 back and names the drift when the lock's deployment has moved under
34 The stubs the type checkers read are materialised too, into `typings/`
35 at the root, pyright's default stub path and a search path the rendered
36 configuration hands the other three checkers: one rendering per tool
37 the lock holds, at the locked version, as `livery.toolroom.stubs`
38 modules with `livery.toolroom.handles` beside them declaring each
39 handle, which the installed tools package's index imports. Both live
40 under the `livery.toolroom` namespace package, beside the tools package
41 and never inside its directory: a tree inside it would shadow the
42 package for a checker run on explicit paths. The store renders each
43 stub from the locked version's own surface, from the records or from
44 the index, so no source holds a stub. A tool the workspace does not
45 deploy gets no stub. `fm toolroom.restub` writes them, and so do `fm sync`
49 from __future__ import annotations 32 ctx
52 from typing import TYPE_CHECKING 32 ctx
54 if TYPE_CHECKING : 32 ctx
55 from livery . strongroom import Source
56 from livery . toolroom . store import (
67 from livery . workshop . _packages import Package
75 from dataclasses import dataclass , field , replace 32 ctx
76 from pathlib import Path 32 ctx
77 from typing import Any , NoReturn , cast 32 ctx
79 from livery . footman import Failed , fail , prog , run 32 ctx
80 from livery . workshop . _contract import load_contract 32 ctx
81 from livery . workshop . _packages import discover_packages 32 ctx
83 WORKSPACE = " workspace " 32 ctx
84 """The contract table the supported hosts live under.""" 32 ctx
87 DEFAULT_HOSTS : tuple [ str , . . . ] = ( ) 32 ctx
88 """The hosts supported when `[workspace] hosts` is absent; empty for 32 ctx
89 every host key, which the store names and which is read only then."""
91 VERDICT_PHASES = ( " format " , " lint " , " typecheck " , " typecomplete " , " test " ) 32 ctx
92 """The phases whose tools take no host allowance: the version is the verdict.""" 32 ctx
94 PINNED_TOOLS = ( " uv " , ) 32 ctx
95 """The tools no allowance reaches by name: the entry pins uv.""" 32 ctx
97 BASE_SITE = " the base " 32 ctx
98 """The site of the base's own requirements, the ones every workspace has.""" 32 ctx
100 TYPINGS = " typings " 32 ctx
101 """The directory under the root the stubs are written into, pyright's default.""" 32 ctx
103 TYPINGS_PACKAGE = ( " livery " , " toolroom " ) 32 ctx
104 """The namespace package the stubs and handles live under, beside the tools package.""" 32 ctx
107 def tools_table ( path : Path ) - > dict [ str , object ] : 32 ctx
108 """The `[toolroom]` table of the contract at *path*, judged; empty when absent."""
109 from livery . toolroom . store import TABLE 75 ctx
111 if not path . is_file ( ) : 75 ctx
113 return dict ( load_contract ( path ) . get ( TABLE ) or { } ) 74 ctx
116 def _requires ( table : dict [ str , object ] , * , site : str ) - > list [ Requirement ] : 32 ctx
117 from livery . toolroom . store import LockError , Requirement 71 ctx
119 declared = cast ( " list[str] " , table . get ( " requires " , [ ] ) ) 71 ctx
121 return [ Requirement . parse ( text , site = site ) for text in declared ] 71 ctx
122 except LockError as error : 2 ctx
123 fail ( str ( error ) ) 2 ctx
126 @functools . cache 32 ctx
127 def base_requirements ( ) - > tuple [ Requirement , . . . ] : 32 ctx
128 """The tools the base requires in every workspace, at the site `BASE_SITE`.
130 Read from the `[toolroom] requires` of the base's contract file,
131 which spells them as an extension's `extension.toml` does: uv,
132 which makes the venv every step runs in, so a workspace with no
133 package requires it too.
136 Failed: when an entry is not a requirement, naming it.
138 from livery . toolroom . store import TABLE 9 ctx
139 from livery . workshop . _contract_keys import base_contract 9 ctx
141 data = base_contract ( ) 9 ctx
142 return tuple ( _requires ( dict ( data . get ( TABLE , { } ) ) , site = BASE_SITE ) ) 9 ctx
145 def requirements ( root : Path ) - > tuple [ Requirement , . . . ] : 32 ctx
146 """Every requirement the six sites declare, in site order.
148 The base's come first, at the site `BASE_SITE`: a workspace
149 requires uv with or without a package, since its own `tasks.py`
150 and tests run in the venv uv makes. The steps that judge a present
151 package bring the tools they run, each requirement naming
152 `step <name>` as its site, and so do the steps that judge the
153 root's own files, which are judged as a package holding the root's
155 An extension's site is `extension <name>`, read from its
156 ``extension.toml``'s ``[toolroom] requires`` and from each of its
157 ``[for.<target>]`` tables that applies now: cmake's build tools
158 are required while a package's set holds cmake, and a measurer's
159 tool while the workspace lists what it measures for. An extension
160 that neither the workspace lists nor a package's set holds
161 declares nothing here, since listing is the only activation
162 channel. A plugin the project mounts through its direct
163 dependencies is `plugin <name>`, read from its entry module
164 ([livery.workshop._tools.plugin_tools][]).
166 from livery . toolroom . store import LockError , Requirement 71 ctx
167 from livery . workshop . _extensions import extension_tools 71 ctx
169 packages = discover_packages ( root ) if ( root / " packages " ) . is_dir ( ) else ( ) 71 ctx
170 found : list [ Requirement ] = list ( base_requirements ( ) ) 71 ctx
171 # The steps that judge a present package bring their tools, each
172 # naming its step: unregistering a step removes its tool.
173 from livery . workshop . _steps import tools_for 71 ctx
175 for tool , step in tools_for ( _held ( root , packages ) ) : 71 ctx
176 found . append ( Requirement . parse ( tool , site = f " step { step } " ) ) 40 ctx
178 declared_by_extension = extension_tools ( root ) 70 ctx
179 except RuntimeError as error :
181 for extension , declared in declared_by_extension . items ( ) : 70 ctx
184 Requirement . parse ( text , site = f " extension { extension } " )
187 except LockError as error :
189 for plugin , declared in plugin_tools ( root ) . items ( ) : 70 ctx
192 Requirement . parse ( text , site = f " plugin { plugin } " ) for text in declared
194 except LockError as error :
196 for package in packages : 70 ctx
197 contract = package . directory / " workshop.toml " 62 ctx
198 found + = _requires ( tools_table ( contract ) , site = f " { package . path } /workshop.toml " ) 62 ctx
199 found + = _requires ( tools_table ( root / " workshop.toml " ) , site = " workshop.toml " ) 70 ctx
200 return tuple ( found ) 69 ctx
203 def _held ( root : Path , packages : tuple [ Package , . . . ] ) - > tuple [ str , . . . ] : 32 ctx
204 """The extensions the root's own files and the *packages* hold.
206 The root's own files, its `tasks.py` and tests, hold the root's
207 set, and are judged as a package holding it is.
209 from livery . workshop . _composition import extension_set 72 ctx
210 from livery . workshop . _extensions import root_set 72 ctx
212 held = ( name for package in packages for name in extension_set ( package ) ) 72 ctx
213 return tuple ( dict . fromkeys ( ( * root_set ( root ) , * held ) ) ) 72 ctx
216 def plugin_tools ( root : Path ) - > dict [ str , tuple [ str , . . . ] ] : 32 ctx
217 """The tools each plugin the project mounts declares, by its entry point name.
219 The plugins are the ``footman.builtin`` names the project's direct
220 dependencies offer, as footman's project rung mounts them. A plugin
221 declares the tools its verbs need in a data module that its
222 ``workshop.tools`` entry point names, under the plugin's own name:
223 ``"livery.forge" = "livery.forge._dev_tools:TOOLS"``, a tuple of
224 requirement strings, ``("docker?",)`` for one it can do without.
225 Loading it imports that module alone, never the plugin's tasks.
228 Failed: when a declaration is not a tuple or list of strings,
231 from livery . footman import installed_entry_points , project_builtins 73 ctx
233 declared = { entry . name : entry for entry in installed_entry_points ( " workshop.tools " ) } 73 ctx
234 found : dict [ str , tuple [ str , . . . ] ] = { } 73 ctx
235 for name in project_builtins ( root ) : 73 ctx
236 entry = declared . get ( name ) 9 ctx
237 if entry is None : 9 ctx
239 value : object = entry . load ( ) 7 ctx
240 if not isinstance ( value , ( tuple , list ) ) or not all ( 7 ctx
241 isinstance ( text , str )
242 for text in value # pyright: ignore[reportUnknownVariableType]
245 f " plugin { name } : its workshop.tools entry point ( { entry . value } ) is "
246 ' not a tuple of requirement strings, ( " docker? " ,) '
248 found [ name ] = tuple ( str ( text ) for text in value ) # pyright: ignore[reportUnknownVariableType, reportUnknownArgumentType] 6 ctx
252 def host_allowed ( root : Path ) - > tuple [ str , . . . ] : 32 ctx
253 """The tools whose copy on the machine may serve: the root contract's.
255 The root contract's `[toolroom] host-allowed`, each name once,
256 sorted. A name no site requires yet is kept and named by the lock
257 as an allowance ahead of its requirement: a workspace may allow
258 `cmake` before its first C++ package arrives, and the lock's line
259 is where a misspelling shows. A tool a step reads its verdict from
260 refuses, naming the steps that read it: a linter that varies by
261 machine makes the gate disagree with CI. `uv` refuses by name,
262 since the entry pins it. A tool an extension that writes release
263 notes requires refuses, naming the extension: a release's entries
264 must not depend on the machine. A download whose executable is
265 not named like the tool, or that has none, refuses too: the
266 allowance finds the tool on PATH by its name.
268 from livery . toolroom . store import LockError , Requirement 52 ctx
269 from livery . workshop . _extensions import notes_tools 52 ctx
270 from livery . workshop . _steps import step_for , tools_for 52 ctx
272 declared = cast ( 52 ctx
273 " list[str] " , tools_table ( root / " workshop.toml " ) . get ( " host-allowed " , [ ] )
275 names = tuple ( sorted ( set ( declared ) ) ) 52 ctx
278 packages = discover_packages ( root ) if ( root / " packages " ) . is_dir ( ) else ( ) 6 ctx
279 verdicts : dict [ str , set [ str ] ] = { } 6 ctx
280 for tool , step in tools_for ( _held ( root , packages ) ) : 6 ctx
281 if step_for ( step ) . phase in VERDICT_PHASES : 281 ↛ 280line 281 didn't jump to line 280 because the condition on line 281 was always true 6 ctx
282 verdicts . setdefault ( tool , set ( ) ) . add ( step ) 6 ctx
283 writers : dict [ str , str ] = { } 6 ctx
284 for extension , requires in notes_tools ( root ) . items ( ) : 6 ctx
285 for line in requires : 1 ctx
287 parsed = Requirement . parse ( line , site = f " extension { extension } " ) 1 ctx
288 except LockError as error :
290 writers [ parsed . name ] = extension 1 ctx
291 for name in names : 6 ctx
292 where = f " workshop.toml: [toolroom] host-allowed names { name } " 6 ctx
293 if name in PINNED_TOOLS : 6 ctx
294 fail ( f " { where } , which takes no allowance: the entry pins { name } " ) 1 ctx
295 if name in writers : 6 ctx
297 f " { where } , which takes no allowance: extension { writers [ name ] } "
298 " writes the release notes with it "
300 if name in verdicts : 6 ctx
301 readers = sorted ( verdicts [ name ] ) 1 ctx
303 f " { where } , whose version is a verdict: { ' , ' . join ( readers ) } "
304 f " { ' reads ' if len ( readers ) == 1 else ' read ' } it "
309 def unrequired_allowances ( root : Path ) - > tuple [ str , . . . ] : 32 ctx
310 """The host-allowed names no site requires yet, sorted: allowances ahead of need."""
311 required = { requirement . name for requirement in requirements ( root ) } 48 ctx
312 return tuple ( name for name in host_allowed ( root ) if name not in required ) 48 ctx
315 def _host_probe_gap ( listing : Catalogue , name : str , version : str , host : str ) - > str : 32 ctx
316 """Why *name* cannot be found on PATH by its own name; empty when it can."""
317 from livery . toolroom . store import DOWNLOAD_KINDS , CatalogueError , RecordError 4 ctx
319 listed = listing . listed ( name ) 4 ctx
320 if listed . kind not in DOWNLOAD_KINDS : 320 ↛ 321line 320 didn't jump to line 321 because the condition on line 320 was never true 4 ctx
323 deployment = listing . deployment ( name , version , host ) 4 ctx
324 except ( CatalogueError , RecordError ) :
326 if not deployment . entry_points : 326 ↛ 327line 326 didn't jump to line 327 because the condition on line 326 was never true 4 ctx
328 f " { name } has no executable; the allowance finds a tool on PATH by its name "
330 first = Path ( deployment . entry_points [ 0 ] ) . name 4 ctx
331 if first . lower ( ) . removesuffix ( " .exe " ) != name : 4 ctx
333 f " { name } ' s executable is { first } ; the allowance finds a tool on PATH by "
339 def tool_names ( root : Path , host : str = " " ) - > tuple [ str , . . . ] : 32 ctx
340 """The tools the sites require, each once, in the order first declared.
342 With *host*, the tools required there: a requirement scoped to other
343 hosts alone (``dotnet_coverage@windows``) requires nothing on it.
348 for requirement in requirements ( root )
349 if not host or requirement . on ( ( host , ) )
354 def this_host ( ) - > str : 32 ctx
355 """This machine's host key as the store names it: ``macos-arm``."""
356 import platform 13 ctx
358 from livery . toolroom . store import host_key 13 ctx
360 return host_key ( platform . system ( ) , platform . machine ( ) ) 13 ctx
363 def supported_hosts ( root : Path ) - > tuple [ str , . . . ] : 32 ctx
364 """The hosts the workspace supports and locks for, in host-key order.
366 `[workspace] hosts` names them in the requirement scope tokens:
367 a platform (`macos`), a host key (`linux-arm`), and either with `!`
368 to remove it, a list of removals alone starting from every host.
369 Absent, every host key is supported.
371 Raises a refusal for a token that is neither a platform nor a host
372 key, and for a list that is empty or leaves no host.
374 from livery . toolroom . store import HOSTS , Scope , SpecError 54 ctx
376 every = DEFAULT_HOSTS or HOSTS 54 ctx
377 path = root / " workshop.toml " 54 ctx
378 table = load_contract ( path ) . get ( WORKSPACE ) if path . is_file ( ) else None 54 ctx
379 declared = cast ( " dict[str, object] " , table or { } ) . get ( " hosts " ) 53 ctx
380 if declared is None : 53 ctx
382 where = " workshop.toml: [workspace] hosts " 5 ctx
383 tokens = cast ( " list[str] " , declared ) 5 ctx
385 fail ( f " { where } is empty; name the hosts, or drop the key for every host " ) 1 ctx
387 scope = Scope . parse ( " , " . join ( tokens ) , where = where , spelled = str ( tokens ) ) 5 ctx
388 except SpecError as error : 1 ctx
389 fail ( str ( error ) ) 1 ctx
390 hosts = scope . hosts ( every ) 5 ctx
392 fail ( f " { where } { tokens } leaves no host; name at least one host " ) 1 ctx
396 def index_source ( root : Path ) - > str : 32 ctx
397 """Where the catalogue is read from, as `[toolroom] index` names it.
399 Defaults to the published tool index when `[toolroom] index` is
400 omitted. A URL is read as an index; a path, relative to the root,
401 is a directory holding an index or the records that build one.
403 from livery . toolroom . store import CatalogueError 56 ctx
404 from livery . toolroom . store import index_source as declared_source 56 ctx
407 return declared_source ( tools_table ( root / " workshop.toml " ) , root ) 56 ctx
408 except CatalogueError as error : 2 ctx
409 fail ( str ( error ) ) 1 ctx
412 def _index_label ( root : Path ) - > str : 32 ctx
413 """Portable label of *root*'s primary catalogue for lock and collision messages."""
414 from livery . toolroom . store import DEFAULT_INDEX 56 ctx
416 table = tools_table ( root / " workshop.toml " ) 56 ctx
417 if " index " not in table : 417 ↛ 418line 417 didn't jump to line 418 because the condition on line 417 was never true 56 ctx
419 declared = table [ " index " ] 56 ctx
420 return declared if isinstance ( declared , str ) and declared else DEFAULT_INDEX 56 ctx
423 def _local_records ( root : Path , primary : str ) - > Path | None : 32 ctx
424 """Local ``records/`` overlay when it holds records distinct from *primary*."""
425 from livery . toolroom . store import LOCAL_RECORDS , is_records 55 ctx
427 candidate = root / LOCAL_RECORDS 55 ctx
428 if not is_records ( str ( candidate ) ) : 55 ctx
430 if " :// " not in primary and candidate . resolve ( ) == Path ( primary ) . resolve ( ) : 49 ctx
432 return candidate 1 ctx
435 def has_index ( root : Path ) - > bool : 32 ctx
436 """Whether *root* has a valid catalogue source (explicit or the default index)."""
437 table = tools_table ( root / " workshop.toml " ) 8 ctx
438 if " index " not in table : 8 ctx
440 declared = table [ " index " ] 4 ctx
441 return isinstance ( declared , str ) and bool ( declared ) 4 ctx
444 def receipt_gap ( tool : str , receipt : ToolReceipt , bin_dir : Path ) - > str : 32 ctx
445 """Why what *receipt* claims does not hold here; empty when it does.
447 A receipt is the store's own record of what it installed for this
448 checkout, so it is the thing to check: looking for an executable by
449 a tool's name instead answers a different question, and answers it
450 wrongly for a tool that is not a program.
452 What a receipt claims depends on how the tool was supplied. One
453 that puts directories on PATH claims they exist and that each entry
454 point runs from one of them, the venv's own bin, or PATH, since a
455 name is not always a binary's (``git_cliff`` installs
456 ``git-cliff``). One that carries environment values claims each
457 value that is a path is there: the cmake-conan provider is a file
458 CMake reads, and no program by that name exists anywhere. One that
459 claims nothing was verified on the host rather than installed, so
460 the host must still answer for it.
463 The first claim that does not hold, as a line to print after
464 the tool's name, or empty when every claim does.
466 for spelled in receipt . paths : 4 ctx
467 if not Path ( spelled ) . is_dir ( ) : 3 ctx
468 return f " MISSING (the receipt ' s path { spelled } is not there) " 1 ctx
469 for name in receipt . entry_points : 4 ctx
470 if not _entry_point_runs ( name , receipt , bin_dir ) : 4 ctx
471 return f " MISSING (the receipt names { name } , which does not run) " 1 ctx
472 for key , value in sorted ( receipt . env . items ( ) ) : 4 ctx
473 # A value that is a path is checked, and a path is judged by
474 # `is_absolute`: a Windows receipt names `C:\...`, which starts
475 # with a drive and not a slash.
476 named = Path ( value ) 1 ctx
477 if named . is_absolute ( ) and not named . exists ( ) : 1 ctx
478 return f " MISSING ( { key } names { value } , which is not there) " 1 ctx
479 # A receipt claiming nothing was verified on the host rather than
480 # installed, so the host has to answer for the tool now.
481 claims_nothing = not ( receipt . paths or receipt . entry_points or receipt . env ) 4 ctx
482 if claims_nothing and not ( shutil . which ( tool ) or ( bin_dir / tool ) . is_file ( ) ) : 4 ctx
483 return " MISSING (verified on the host, and not on PATH now) " 1 ctx
487 def _entry_point_runs ( name : str , receipt : ToolReceipt , bin_dir : Path ) - > bool : 32 ctx
488 """Whether *name* runs: from the receipt's paths, the venv's bin, or PATH."""
489 own = ( Path ( spelled ) / name for spelled in receipt . paths ) 4 ctx
491 any ( candidate . is_file ( ) for candidate in own )
492 or ( bin_dir / name ) . is_file ( )
493 or bool ( shutil . which ( name ) )
497 def store_cannot_supply ( root : Path ) - > str : 32 ctx
498 """Why the store supplies nothing here; empty when it can.
500 The sites require tools whatever a contract says, because the
501 base requires uv: a workspace with no package still runs in the
502 venv uv makes. Nothing is supplied until a lock names versions,
503 and nothing can be locked until the contract names a catalogue to
504 resolve against, so the two are one question with one answer a
507 Empty when a lock is present, and empty for a workspace that
508 requires no tool at all.
510 required = tool_names ( root ) 6 ctx
511 if not required or current_lock ( root ) is not None : 6 ctx
513 named = " , " . join ( required [ : 4 ] ) 6 ctx
514 rest = f " , and { len ( required ) - 4 } more " if len ( required ) > 4 else " " 6 ctx
516 f " the tools this workspace requires are not locked: { named } { rest } . "
517 " Nothing is materialised from a store without a lock, so the "
518 " first verb that reaches for one fails with no executable found. "
520 if not has_index ( root ) : 6 ctx
523 " Name the catalogue to resolve against in workshop.toml: " ,
526 ' index = " <the published index \' s URL, or a directory of records> " ' ,
530 f " Then ` { prog ( ) } toolroom.lock` writes the lock and "
531 f " ` { prog ( ) } sync` supplies what it names. " ,
533 return " \n " . join ( lines ) 6 ctx
536 def catalogue ( root : Path , * , offline : bool = False ) - > Catalogue : 32 ctx
537 """The catalogue the repository resolves against.
539 Reads `[toolroom] index` (or the published index by default). When
540 *root* also carries a local `records/` directory holding records
541 distinct from the primary source, those records are layered over the
542 primary catalogue; any colliding tool name refuses naming both.
544 from livery . toolroom . store import LOCAL_RECORDS , Catalogue , CatalogueError 56 ctx
546 primary = index_source ( root ) 56 ctx
547 label = _index_label ( root ) 56 ctx
548 base = _read_catalogue ( primary , offline = offline ) 56 ctx
549 local = _local_records ( root , primary ) 55 ctx
551 _read_catalogue ( str ( local ) , offline = offline ) if local is not None else None
553 key = ( id ( base ) , label , id ( overlay ) if overlay is not None else 0 ) 55 ctx
554 cached = _LAYERED_CATALOGUES . get ( key ) 55 ctx
555 if cached is not None : 55 ctx
557 tagged = base . with_source ( label ) 55 ctx
558 if overlay is None : 55 ctx
559 _LAYERED_CATALOGUES [ key ] = tagged 54 ctx
562 layered = Catalogue . layer ( 1 ctx
564 overlay . with_source ( LOCAL_RECORDS ) ,
566 overlay_source = LOCAL_RECORDS ,
568 except CatalogueError as error : 1 ctx
569 fail ( str ( error ) ) 1 ctx
570 _LAYERED_CATALOGUES [ key ] = layered 1 ctx
574 #: The catalogues this process read, by source, mode and the source's
575 #: identity: one read serves every caller in the process. A sync asks
576 #: for the catalogue five times (the lock judged, the tools supplied,
577 #: the stubs written); an index by URL answers each read over the
578 #: network, and a records directory loads every record each time.
579 _CATALOGUES : dict [ tuple [ Any , . . . ] , Catalogue ] = { } 32 ctx
580 _LAYERED_CATALOGUES : dict [ tuple [ Any , . . . ] , Catalogue ] = { } 32 ctx
583 def _source_stamp ( source : str ) - > tuple [ Any , . . . ] | None : 32 ctx
584 """What identifies the catalogue at *source* now.
586 None for an index by URL, whose pointer stands for the process. For
587 a directory index, the pointer file's modification time and size,
588 which the verbs that build an index move; for a records directory,
589 every record file's. A pointer that is not there is None too, and
590 the read refuses by name.
592 if " :// " in source : 592 ↛ 593line 592 didn't jump to line 593 because the condition on line 592 was never true 56 ctx
594 from livery . toolroom . store import POINTER , records_in 56 ctx
596 directory = Path ( source ) 56 ctx
597 pointer = directory / POINTER 56 ctx
598 if pointer . is_file ( ) : 56 ctx
599 found = pointer . stat ( ) 6 ctx
600 return ( found . st_mtime_ns , found . st_size ) 6 ctx
602 for path in records_in ( directory ) : 51 ctx
604 found = path . stat ( ) 50 ctx
607 stamps . append ( ( path . name , found . st_mtime_ns , found . st_size ) ) 50 ctx
608 return tuple ( stamps ) or None 51 ctx
611 def forget_catalogues ( ) - > None : 32 ctx
612 """Drop every catalogue this process read, so the next read is fresh."""
613 _CATALOGUES . clear ( ) 3 ctx
614 _LAYERED_CATALOGUES . clear ( ) 3 ctx
617 def _read_catalogue ( source : str , * , offline : bool ) - > Catalogue : 32 ctx
618 """The catalogue at *source*, records or index, with no build first.
620 A source is read once per process: a second read answers from the
621 first until the source's files change.
623 from livery . toolroom . store import Catalogue , CatalogueError 56 ctx
625 key = ( source , offline , _source_stamp ( source ) ) 56 ctx
626 held = _CATALOGUES . get ( key ) 56 ctx
627 if held is not None : 56 ctx
630 listing = Catalogue . at ( source , home = _home ( ) , offline = offline ) 56 ctx
631 except CatalogueError as error : 1 ctx
632 fail ( str ( error ) ) 1 ctx
633 _CATALOGUES [ key ] = listing 55 ctx
637 def _home ( ) - > Home : 32 ctx
638 return store_home ( ) 57 ctx
641 def store_home ( ) - > Home : 32 ctx
642 """The tool store's home on this machine, where every checkout's tools are.
644 The ``toolroom`` directory in the runner's data directory, one per
645 machine and shared by every checkout on it, as a
646 [livery.toolroom.store.Home][].
648 from livery . footman import data_dir 59 ctx
649 from livery . toolroom . store import Home 59 ctx
651 return Home . under ( data_dir ( ) ) 59 ctx
654 def lock_path ( root : Path ) - > Path : 32 ctx
655 """The lock's file, `toolroom.lock` at the root."""
656 from livery . toolroom . store import LOCK_FILE 130 ctx
658 return root / LOCK_FILE 130 ctx
661 def current_lock ( root : Path ) - > Lock | None : 32 ctx
662 """The lock as committed, or `None` when the repository has none yet."""
663 from livery . toolroom . store import Lock , LockError 130 ctx
665 path = lock_path ( root ) 130 ctx
666 if not path . is_file ( ) : 130 ctx
669 return Lock . load ( path ) 44 ctx
670 except LockError as error : 1 ctx
671 fail ( str ( error ) ) 1 ctx
674 def lock_is_current ( root : Path , * , offline : bool = False ) - > tuple [ bool , str ] : 32 ctx
675 """Whether `toolroom.lock` is what the sites resolve to now; and what moved.
677 The question `--locked` and `--check` ask, and nothing is written to
678 answer it. An entry that still satisfies every floor stands, so a
679 newer version published since the lock was written moves nothing:
680 that is what an upgrade is for. What moves the answer is a
681 requirement the lock does not hold, or holds at a version that no
684 The graphs are not compared: a delegated tool's graph is resolved
685 when its version enters the lock and written beside it, so a fresh
686 resolution never carries one, and reading its absence as a move
687 would move every delegated entry on every check.
690 Whether the lock on disk is current, and why not when it is
691 not: no lock at all, a requirement nothing satisfies, or the
692 tools whose entries would move.
694 from livery . toolroom . store import LOCK_FILE , LockError , resolve_lock 6 ctx
696 held = current_lock ( root ) 6 ctx
697 if held is None : 6 ctx
698 return False , f " there is no { LOCK_FILE } " 3 ctx
699 listing = catalogue ( root , offline = offline ) 5 ctx
701 fresh = resolve_lock ( 5 ctx
703 with_runtimes ( tuple ( requirements ( root ) ) , listing ) ,
704 hosts = supported_hosts ( root ) ,
706 host_allowed = host_allowed ( root ) ,
708 except LockError as error : 1 ctx
709 return False , str ( error ) 1 ctx
710 current , resolved = _entries ( held ) , _entries ( fresh ) 5 ctx
711 if fresh . hosts == held . hosts and resolved == current : 5 ctx
713 names = set ( resolved ) | set ( current ) 3 ctx
714 moved = sorted ( name for name in names if resolved . get ( name ) != current . get ( name ) ) 3 ctx
715 return False , f " the lock would move: { ' , ' . join ( moved ) } " 3 ctx
718 def _entries ( lock : Lock ) - > dict [ str , dict [ str , Any ] ] : 32 ctx
719 """The lock's entries without their graphs: what the current check compares."""
721 name : { key : value for key , value in entry . to_json ( ) . items ( ) if key != " graph " }
722 for name , entry in lock . tools . items ( )
729 upgrade : tuple [ str , . . . ] = ( ) ,
730 relock : tuple [ str , . . . ] = ( ) ,
731 offline : bool = False ,
733 """Resolve the sites' requirements and write the lock; the lock written.
735 An entry the lock already holds stands unless it is named in
736 *upgrade* or no longer satisfies; a refusal names the tool, each
737 floor with its site, and the host at fault. A delegated tool whose
738 version entered the lock has its graph resolved with it, and one
739 named in *relock* has its graph resolved again though its version
740 stands, which is what an install the graph could not satisfy asks
743 from livery . toolroom . store import LockError , resolve_lock 52 ctx
745 listing = catalogue ( root , offline = offline ) 52 ctx
746 kept = current_lock ( root ) 52 ctx
747 allowed = host_allowed ( root ) 52 ctx
749 lock = resolve_lock ( 51 ctx
751 with_runtimes ( tuple ( requirements ( root ) ) , listing ) ,
752 hosts = supported_hosts ( root ) ,
755 host_allowed = allowed ,
757 except LockError as error : 4 ctx
758 fail ( str ( error ) ) 3 ctx
759 # What the resolution left out, an optional tool it could not serve.
760 for note in lock . notes : 760 ↛ 761line 760 didn't jump to line 761 because the loop on line 760 never started 49 ctx
762 for name in allowed : 49 ctx
763 if name not in lock . tools : 5 ctx
765 for host in lock . tools [ name ] . on or lock . hosts : 4 ctx
766 if gap := _host_probe_gap ( listing , name , lock . tools [ name ] . version , host ) : 4 ctx
767 fail ( f " workshop.toml: [toolroom] host-allowed: { gap } " ) 1 ctx
768 lock , notes = with_graphs ( root , lock , listing , kept = kept , relock = relock ) 48 ctx
769 lock . save ( lock_path ( root ) ) 48 ctx
770 for note in notes : 48 ctx
771 print ( f " { note } " ) 46 ctx
772 for name in unrequired_allowances ( root ) : 48 ctx
773 print ( f " host-allowed: { name } is allowed and no site requires it yet " ) 1 ctx
774 for note in unreached_scopes ( root , lock ) : 48 ctx
775 print ( f " { note } " ) 1 ctx
779 def unreached_scopes ( root : Path , lock : Lock ) - > list [ str ] : 32 ctx
780 """One line per scoped requirement no locked host matches, and so locks nothing."""
781 lines : list [ str ] = [ ] 48 ctx
782 seen : set [ str ] = set ( ) 48 ctx
783 for requirement in requirements ( root ) : 48 ctx
784 if not requirement . hosts or requirement . name in lock . tools : 48 ctx
786 spelled = str ( requirement ) 1 ctx
787 if spelled in seen or requirement . on ( lock . hosts ) : 787 ↛ 788line 787 didn't jump to line 788 because the condition on line 787 was never true 1 ctx
789 seen . add ( spelled ) 1 ctx
791 f " { spelled } ( { requirement . site } ): no locked host is "
792 f " { ' , ' . join ( requirement . hosts ) } ; not locked "
797 def resolve_graph ( 32 ctx
805 ) - > tuple [ Graph | None , str ] :
806 """Resolve *package* at *version* and write its graph; the graph or why not.
808 A `pypi` graph is uv's universal hashed requirements, resolved
809 against the workspace's Python floor, so one file covers every
810 interpreter the workspace supports. An `npm` graph is the
811 runtime's own lockfile, resolved without installing anything.
812 Neither runs unless what resolves it is here: a checkout that has
813 not materialised its tools yet locks the version and says the
816 from livery . strongroom import digest_of
817 from livery . toolroom . store import Graph
819 directory = graphs_dir ( root )
820 directory . mkdir ( parents = True , exist_ok = True )
821 target = directory / _graph_file ( name , kind )
822 writer = _pypi_graph if kind == " pypi " else _npm_graph
823 by , why = writer ( root , package , version , target , runtime = runtime )
826 return Graph ( target . name , digest_of ( target . read_bytes ( ) ) , by = by ) , " "
829 def _pypi_graph ( 32 ctx
830 root : Path , package : str , version : str , target : Path , * , runtime : str
832 """Uv's universal hashed requirements for *package*; what resolved it."""
834 import livery . toolroom . tools as toolroom 1 ctx
835 from livery . workshop . _pythons import python_floor 1 ctx
837 with tempfile . TemporaryDirectory ( ) as scratch : 1 ctx
838 wanted = Path ( scratch ) / " in.txt " 1 ctx
839 wanted . write_text ( f " { package } == { version } \n " , encoding = " utf-8 " ) 1 ctx
840 done = toolroom . uv . opts ( nofail = True , recorded = False ) ( 1 ctx
846 # The header names the command that wrote it, the output
847 # path included, and each annotation names the input file,
848 # whose directory is new on every run: either would make two
849 # resolutions of one version differ in bytes, and the lock
853 f " --python-version= { python_floor ( root ) } " ,
854 f " --output-file= { target } " ,
857 if done . code != 0 : 857 ↛ 858line 857 didn't jump to line 858 because the condition on line 857 was never true 1 ctx
858 return " " , f " uv could not resolve it (exit { done . code } ) "
859 return f " uv { _tool_version ( root , ' uv ' ) } " , " " 1 ctx
863 root : Path , package : str , version : str , target : Path , * , runtime : str
865 """The runtime's own lockfile for *package*, resolved without installing."""
866 if runtime != " node " :
867 return " " , f " a graph of a { runtime } tool is not written yet "
868 held = tool_receipts ( root ) . get ( " node " )
870 return " " , " node is not materialised here; the next lock writes it "
871 node = Path ( held . tool_dir ) / " bin " / exe ( " node " )
872 if not node . is_file ( ) :
873 node = Path ( held . tool_dir ) / exe ( " node " )
874 if not node . is_file ( ) :
875 return " " , f " the node receipt names no executable under { held . tool_dir } "
876 from livery . toolroom . store import npm_cli
878 with tempfile . TemporaryDirectory ( ) as scratch :
879 where = Path ( scratch )
880 ( where / " package.json " ) . write_text (
885 " dependencies " : { package : version } ,
896 " --package-lock-only " ,
905 written = where / " package-lock.json "
906 if done . code != 0 or not written . is_file ( ) :
907 return " " , f " npm could not resolve it (exit { done . code } ) "
908 target . write_bytes ( written . read_bytes ( ) )
909 return f " node { held . version } " , " "
912 def _tool_version ( root : Path , name : str ) - > str : 32 ctx
913 """The version the lock pins for *name*, or empty when it holds none."""
914 lock = current_lock ( root ) 1 ctx
915 held = lock . tools . get ( name ) if lock is not None else None 1 ctx
916 return held . version if held is not None else " " 1 ctx
919 def exe ( name : str ) - > str : 32 ctx
920 """*name* as this platform spells an executable."""
921 return f " { name } .exe " if sys . platform == " win32 " else name 1 ctx
924 def _graph_path ( root : Path , locked : Locked ) - > Path | None : 32 ctx
925 """The graph file the lock names for this version, when it is here and whole.
927 A graph named but missing, or whose bytes are not the ones the
928 lock recorded, is not installed from: the tool installs the way
929 it did, and the next lock writes the graph again.
931 from livery . strongroom import digest_of 22 ctx
933 if locked . graph is None : 22 ctx
935 path = graphs_dir ( root ) / locked . graph . file 2 ctx
936 if not path . is_file ( ) or digest_of ( path . read_bytes ( ) ) != locked . graph . digest : 2 ctx
941 def graphs_dir ( root : Path ) - > Path : 32 ctx
942 """Where the resolved graphs live, beside the lock."""
943 from livery . toolroom . store import GRAPHS 49 ctx
945 return root / GRAPHS 49 ctx
948 def _graph_file ( name : str , kind : str ) - > str : 32 ctx
949 """The graph's file name for *name*: the installer's own format."""
950 return f " { name } .txt " if kind == " pypi " else f " { name } .json "
953 def _kept_graph ( 32 ctx
954 root : Path , name : str , locked : Locked , kept : Lock | None
956 """The graph the lock already had for this version, when it still stands.
958 A graph is resolved once, when a version enters the lock, and kept
959 after: a lock names artefacts and their hashes, so an install
960 re-runs no resolution and a newer installer installs the same
961 graph. A version that moved, a graph never written, and a file
962 edited or gone since are each resolved again.
964 from livery . strongroom import digest_of 46 ctx
966 before = kept . tools . get ( name ) if kept is not None else None 46 ctx
967 if before is None or before . version != locked . version or before . graph is None : 46 ctx
969 path = graphs_dir ( root ) / before . graph . file 2 ctx
970 if not path . is_file ( ) or digest_of ( path . read_bytes ( ) ) != before . graph . digest : 2 ctx
972 return before . graph 2 ctx
975 def with_graphs ( 32 ctx
980 kept : Lock | None = None ,
981 relock : tuple [ str , . . . ] = ( ) ,
982 ) - > tuple [ Lock , list [ str ] ] :
983 """*lock* with a graph per delegated tool, and what to say about it.
985 A `pypi` tool's graph is uv's hashed requirements, a `npm` tool's
986 is its runtime's lockfile, each written under `GRAPHS` beside the
987 lock and named there by its digest; a `dotnet` tool has none, since
988 its package carries its dependencies and the version pins the
989 whole. A graph that cannot be
990 resolved now, the runtime it needs being absent on a checkout
991 that has not materialised yet, leaves the tool as it was: it
992 installs the way it did and says so, and the next lock writes it.
994 A tool named in *relock* is resolved again though its version
995 stands. Nothing here re-resolves on its own: a resolve is a fresh
996 answer from an index, so it moves the lock, and a lock moves when
997 a person says so, never as a side effect of installing.
999 from livery . toolroom . store import CatalogueError 48 ctx
1001 directory = graphs_dir ( root ) 48 ctx
1002 tools : dict [ str , Locked ] = { } 48 ctx
1003 notes : list [ str ] = [ ] 48 ctx
1004 wanted : set [ str ] = set ( ) 48 ctx
1005 for name , locked in lock . tools . items ( ) : 48 ctx
1007 kind = listing . listed ( name ) . kind 48 ctx
1008 except CatalogueError :
1009 tools [ name ] = locked
1011 if kind not in ( " pypi " , " npm " ) : 48 ctx
1012 tools [ name ] = locked 34 ctx
1014 standing = None if name in relock else _kept_graph ( root , name , locked , kept ) 46 ctx
1015 if standing is not None : 46 ctx
1016 wanted . add ( standing . file ) 2 ctx
1017 tools [ name ] = replace ( locked , graph = standing ) 2 ctx
1019 listed = listing . listed ( name ) 46 ctx
1020 graph , why = resolve_graph ( 46 ctx
1023 listed . package or name ,
1026 runtime = runtime_of ( listed ) ,
1028 if graph is None : 46 ctx
1029 notes . append ( f " graphs: { name } { locked . version } : { why } " ) 44 ctx
1030 tools [ name ] = replace ( locked , graph = None ) 44 ctx
1032 wanted . add ( graph . file ) 2 ctx
1033 notes . append ( f " graphs: { name } { locked . version } resolved by { graph . by } " ) 2 ctx
1034 tools [ name ] = replace ( locked , graph = graph ) 2 ctx
1035 if directory . is_dir ( ) : 48 ctx
1036 # A graph the lock no longer names is a file nothing installs
1037 # from: it goes with the tool or the version it belonged to.
1038 for stale in sorted ( directory . iterdir ( ) ) : 2 ctx
1039 if stale . is_file ( ) and stale . name not in wanted : 2 ctx
1041 notes . append ( f " graphs: removed { stale . name } " ) 1 ctx
1042 return replace ( lock , tools = tools ) , notes 48 ctx
1045 def runtime_of ( listed : Listed ) - > str : 32 ctx
1046 """The runtime a tool runs on: an `npm` tool's record's, node unless it says bun.
1048 A `dotnet` tool runs on dotnet, the SDK. Empty for every other
1049 kind, which runs on nothing the lock supplies.
1051 if listed . kind == " npm " : 52 ctx
1052 return listed . runtime or " node " 9 ctx
1053 if listed . kind == " dotnet " : 52 ctx
1054 return " dotnet " 3 ctx
1058 def with_runtimes ( 32 ctx
1059 found : tuple [ Requirement , . . . ] , listing : Catalogue
1060 ) - > tuple [ Requirement , . . . ] :
1061 """*found*, plus each runtime a tool among them runs on and nothing names.
1063 An `npm` or `dotnet` tool is installed through its runtime and
1064 runs on it, so the runtime is the tool's dependency and the lock
1065 holds it as such, on the hosts the tool is required on: the site
1066 named is the tool that needs it. A runtime some site requires on
1067 every host needs no more; otherwise one requirement per distinct
1068 scope is added, and the lock unions them. A requirement the
1069 catalogue does not list is left for the resolver to refuse by
1072 from livery . toolroom . store import CatalogueError , Requirement 51 ctx
1074 everywhere = { requirement . name for requirement in found if not requirement . hosts } 51 ctx
1075 present = { ( requirement . name , requirement . hosts ) for requirement in found } 51 ctx
1076 added : list [ Requirement ] = [ ] 51 ctx
1077 for requirement in found : 51 ctx
1079 listed = listing . listed ( requirement . name ) 51 ctx
1080 except CatalogueError : 2 ctx
1082 runtime = runtime_of ( listed ) 51 ctx
1085 or runtime in everywhere
1086 or ( runtime , requirement . hosts ) in present
1089 present . add ( ( runtime , requirement . hosts ) ) 11 ctx
1093 site = f " { requirement . name } ( { listed . kind } ) " ,
1094 hosts = requirement . hosts ,
1097 return ( * found , * added ) 51 ctx
1100 def declare ( root : Path , text : str ) - > bool : 32 ctx
1101 """Add *text* to the project's `[toolroom] requires`; whether the contract changed.
1103 A requirement already declared at the project site, in the same
1104 spelling, is left as it is. The contract is edited in place: the
1105 `[toolroom]` table gains the entry, or is added at the end with it.
1107 from livery . toolroom . store import TABLE , LockError , Requirement 5 ctx
1110 Requirement . parse ( text , site = " workshop.toml " ) 5 ctx
1111 except LockError as error : 2 ctx
1112 fail ( str ( error ) ) 2 ctx
1113 path = root / " workshop.toml " 4 ctx
1114 declared = tools_table ( path ) . get ( " requires " , [ ] ) 4 ctx
1115 if isinstance ( declared , list ) and text in declared : 4 ctx
1117 source = path . read_text ( encoding = " utf-8 " ) if path . is_file ( ) else " " 4 ctx
1118 lines = source . split ( " \n " ) 4 ctx
1120 ( n for n , line in enumerate ( lines ) if line . strip ( ) == f " [ { TABLE } ] " ) , None
1122 if header is None : 4 ctx
1123 trimmed = source . rstrip ( " \n " ) 1 ctx
1124 path . write_text ( 1 ctx
1125 ( trimmed + " \n \n " if trimmed else " " )
1126 + f ' [ { TABLE } ] \n requires = [ " { text } " ] \n ' ,
1131 ( n for n in range ( header + 1 , len ( lines ) ) if lines [ n ] . startswith ( " [ " ) ) ,
1134 for n in range ( header + 1 , end ) : 4 ctx
1135 if lines [ n ] . split ( " = " , 1 ) [ 0 ] . strip ( ) == " requires " : 4 ctx
1136 if lines [ n ] . rstrip ( ) . endswith ( " ] " ) : 3 ctx
1137 body = lines [ n ] . split ( " = " , 1 ) [ 1 ] . strip ( ) [ 1 : - 1 ] . strip ( ) 3 ctx
1138 items = f " { body } , " if body else " " 3 ctx
1139 lines [ n ] = f ' requires = [ { items } " { text } " ] ' 3 ctx
1141 close = next ( m for m in range ( n + 1 , end ) if lines [ m ] . strip ( ) == " ] " ) 1 ctx
1142 lines . insert ( close , f ' " { text } " , ' ) 1 ctx
1143 path . write_text ( " \n " . join ( lines ) , encoding = " utf-8 " ) 3 ctx
1145 lines . insert ( header + 1 , f ' requires = [ " { text } " ] ' ) 4 ctx
1146 path . write_text ( " \n " . join ( lines ) , encoding = " utf-8 " ) 4 ctx
1150 # --- receipts and materialisation -------------------------------------------------
1152 RECEIPTS = " .workshop/receipts " 32 ctx
1153 """Where a checkout keeps its receipts, one file per tool, gitignored.""" 32 ctx
1155 BIN = " .workshop/bin " 32 ctx
1156 """The checkout's bin directory: the entry points of every tool in `link` mode.""" 32 ctx
1158 RECEIPT_SCHEMA = 1 32 ctx
1159 """A receipt's shape. Bumped when a reader must know.""" 32 ctx
1162 @dataclass ( frozen = True ) 32 ctx
1163 class ToolReceipt : 32 ctx
1164 """What this checkout installed of one tool, as its file records it.
1166 `fm sync` writes one receipt per tool under the checkout's
1167 ``.workshop/receipts/``; [livery.workshop.tool_receipts][] reads
1171 tool: The tool's name.
1172 version: The exact version installed, the lock's.
1173 host: The host key it was installed for, this machine's.
1174 kind: The installer kind.
1175 mode: How it reached PATH, one of
1176 [livery.toolroom.store.MODES][].
1177 deployment: The digest of the deployment installed; empty for a
1178 delegated kind, whose installer resolves the host.
1179 tool_dir: Where the tool is, under the machine's store home
1180 ([livery.workshop.store_home][]).
1181 paths: The absolute directories the tool puts on PATH in `path`
1182 mode; empty otherwise.
1183 env: The variables the tool sets, resolved against its directory.
1184 entry_points: The executables' names the tool puts on PATH:
1185 linked into the checkout's bin directory in `link` mode,
1186 found on `paths` in `path` mode; empty in `none` mode. A
1187 check resolves the tool by these, never by its name, since
1188 a name (`git_cliff`) is not always a binary (`git-cliff`).
1189 source: `store` for a tool the store supplied, `host` for a
1190 copy already on the machine that served under the
1191 allowance; `version` is the lock's either way, what the
1192 stubs render for and the drift check speaks of.
1193 answered: The version the host's copy printed when it served;
1194 empty for a store install.
1195 stamp: The host copy's identity when it served: its `path`,
1196 `size` and `mtime`, so a sync that finds the same file
1197 probes nothing; empty for a store install.
1205 deployment : str = " " 32 ctx
1206 tool_dir : str = " " 32 ctx
1207 paths : tuple [ str , . . . ] = ( ) 32 ctx
1208 env : dict [ str , str ] = field ( default_factory = dict ) 32 ctx
1209 entry_points : tuple [ str , . . . ] = ( ) 32 ctx
1210 source : str = " store " 32 ctx
1211 answered : str = " " 32 ctx
1212 stamp : dict [ str , str ] = field ( default_factory = dict ) 32 ctx
1214 def to_json ( self ) - > dict [ str , Any ] : 32 ctx
1215 """The receipt as a JSON object."""
1217 " schema " : RECEIPT_SCHEMA ,
1219 " version " : self . version ,
1223 " deployment " : self . deployment ,
1224 " tool_dir " : self . tool_dir ,
1225 " paths " : list ( self . paths ) ,
1226 " env " : dict ( self . env ) ,
1227 " entry_points " : list ( self . entry_points ) ,
1228 " source " : self . source ,
1229 " answered " : self . answered ,
1230 " stamp " : dict ( self . stamp ) ,
1234 def load ( cls , path : Path ) - > ToolReceipt : 32 ctx
1235 """The receipt at *path*.
1238 ValueError: for a file that is not a receipt of this schema,
1242 data = json . loads ( path . read_text ( encoding = " utf-8 " ) ) 39 ctx
1243 except ( OSError , ValueError ) as error : 3 ctx
1244 raise ValueError ( f " { path } : not a receipt ( { error } ) " ) from None 3 ctx
1245 if not isinstance ( data , dict ) or data . get ( " schema " ) != RECEIPT_SCHEMA : 37 ctx
1246 raise ValueError ( f " { path } : not a receipt of schema { RECEIPT_SCHEMA } " ) 1 ctx
1250 str ( data [ " version " ] ) ,
1254 str ( data . get ( " deployment " , " " ) ) ,
1255 str ( data . get ( " tool_dir " , " " ) ) ,
1256 tuple ( str ( p ) for p in data . get ( " paths " , [ ] ) ) ,
1257 { str ( k ) : str ( v ) for k , v in data . get ( " env " , { } ) . items ( ) } ,
1258 tuple ( str ( e ) for e in data . get ( " entry_points " , [ ] ) ) ,
1259 str ( data . get ( " source " , " store " ) ) ,
1260 str ( data . get ( " answered " , " " ) ) ,
1261 { str ( k ) : str ( v ) for k , v in data . get ( " stamp " , { } ) . items ( ) } ,
1263 except ( KeyError , TypeError , AttributeError ) as error : 1 ctx
1264 raise ValueError ( f " { path } : not a receipt ( { error } ) " ) from None 1 ctx
1267 def _runtimes_first ( 32 ctx
1268 wanted : tuple [ str , . . . ] , lock : Lock , listing : Catalogue
1269 ) - > tuple [ str , . . . ] :
1270 """*wanted* with each runtime ahead of the tools that run on it.
1272 An `npm` tool is installed through its runtime's executable, so the
1273 runtime is supplied first and its path handed on; a runtime the
1274 lock holds joins a narrowed *wanted* that names such a tool without
1277 needed : list [ str ] = [ ] 24 ctx
1278 for name in wanted : 24 ctx
1279 runtime = runtime_of ( listing . listed ( name ) ) 24 ctx
1280 if runtime and runtime not in needed : 24 ctx
1281 needed . append ( runtime ) 4 ctx
1282 if not needed : 24 ctx
1284 rest = tuple ( name for name in wanted if name not in needed ) 4 ctx
1285 return ( * ( runtime for runtime in needed if runtime in lock . tools ) , * rest ) 4 ctx
1288 def receipts_dir ( root : Path ) - > Path : 32 ctx
1289 """The checkout's receipts directory."""
1290 return root / RECEIPTS 73 ctx
1293 def bin_dir ( root : Path ) - > Path : 32 ctx
1294 """The checkout's bin directory."""
1295 return root / BIN 23 ctx
1298 def tool_receipts ( root : Path ) - > dict [ str , ToolReceipt ] : 32 ctx
1299 """What the checkout at *root* installed of each tool, by the tool's name.
1301 The receipts the last sync wrote, each a
1302 [livery.workshop.ToolReceipt][]: a receipt names the version the
1303 lock pins and where the tool is on this machine. Empty for a
1304 checkout no sync has supplied.
1307 ValueError: for a file under the receipts directory that is not
1308 a receipt, naming it.
1310 directory = receipts_dir ( root ) 55 ctx
1311 if not directory . is_dir ( ) : 55 ctx
1314 for path in sorted ( directory . glob ( " *.json " ) ) : 39 ctx
1315 receipt = ToolReceipt . load ( path ) 38 ctx
1316 found [ receipt . tool ] = receipt 35 ctx
1321 root : Path , name : str , kind : str , declared : str = " " , * , paths : tuple [ str , . . . ] = ( )
1323 """The mode *name* materialises in: the project's say, the record's, the kind's.
1325 *paths* are the deployment's, which decide a download's default.
1326 Raises a refusal naming the override when it is not one of `MODES`.
1328 from livery . toolroom . store import MODES , default_mode 19 ctx
1330 overrides = cast ( 19 ctx
1331 " dict[str, str] " , tools_table ( root / " workshop.toml " ) . get ( " modes " , { } )
1333 chosen = overrides . get ( name , declared ) or default_mode ( kind , paths ) 18 ctx
1334 if chosen not in MODES : 1334 ↛ 1335line 1334 didn't jump to line 1335 because the condition on line 1334 was never true 18 ctx
1336 f " workshop.toml: [toolroom] modes names { chosen !r} for { name } ; the modes "
1337 f " are { ' , ' . join ( MODES ) } "
1339 return str ( chosen ) 18 ctx
1342 def sources ( root : Path ) - > tuple [ Source , . . . ] : 32 ctx
1343 """The tiers consulted before an origin, `[toolroom] sources`: folders or URLs."""
1344 from livery . strongroom import FolderSource , HttpSource 25 ctx
1346 declared = cast ( " list[str] " , tools_table ( root / " workshop.toml " ) . get ( " sources " , [ ] ) ) 25 ctx
1347 found : list [ Source ] = [ ] 24 ctx
1348 for entry in declared : 24 ctx
1349 if " :// " in entry : 1349 ↛ 1350line 1349 didn't jump to line 1350 because the condition on line 1349 was never true 15 ctx
1350 found . append ( HttpSource ( entry ) )
1352 found . append ( FolderSource ( root / entry ) ) 15 ctx
1353 return tuple ( found ) 24 ctx
1356 @dataclass ( frozen = True ) 32 ctx
1357 class Materialised : 32 ctx
1358 """What one materialisation did to one tool.
1361 receipt: The receipt written, or `None` when the tool could not
1362 be supplied and *strict* was off.
1363 installed: Whether the store installed it on this call.
1364 failure: Why the tool could not be supplied; empty when it was.
1365 note: What the sync should say beyond the receipt: why a host
1366 copy was passed over for the store's version; empty
1370 receipt : ToolReceipt | None 32 ctx
1371 installed : bool 32 ctx
1372 failure : str = " " 32 ctx
1373 note : str = " " 32 ctx
1376 def _stamp ( found : Path ) - > dict [ str , str ] : 32 ctx
1377 """The identity of *found* a later sync compares before probing; empty when gone."""
1379 stat = found . stat ( ) 1 ctx
1384 " size " : str ( stat . st_size ) ,
1385 " mtime " : str ( stat . st_mtime_ns ) ,
1389 def _fresh_host_receipt ( root : Path , name : str ) - > ToolReceipt | None : 32 ctx
1390 """*name*'s receipt when a host copy served and the same file is still there."""
1391 path = receipts_dir ( root ) / f " { name } .json " 2 ctx
1392 if not path . is_file ( ) : 2 ctx
1395 receipt = ToolReceipt . load ( path ) 2 ctx
1398 if receipt . source != " host " or not receipt . stamp . get ( " path " ) : 2 ctx
1400 if _stamp ( Path ( receipt . stamp [ " path " ] ) ) != receipt . stamp : 1 ctx
1405 def site_floors ( root : Path ) - > dict [ str , tuple [ str , str ] ] : 32 ctx
1406 """The highest floor the sites declare per tool, with the site that asked.
1408 A tool no site floors is absent. The lock already honours these
1409 floors when it chooses a version; the check of a system tool on a
1410 machine honours them here, since the machine's own copy is never
1413 from livery . toolroom . store import version_key 23 ctx
1415 highest : dict [ str , tuple [ str , str ] ] = { } 23 ctx
1416 for requirement in requirements ( root ) : 23 ctx
1417 if not requirement . floor : 23 ctx
1419 held = highest . get ( requirement . name ) 3 ctx
1420 if held is None or version_key ( requirement . floor ) > version_key ( held [ 0 ] ) : 1420 ↛ 1416line 1420 didn't jump to line 1416 because the condition on line 1420 was always true 3 ctx
1421 highest [ requirement . name ] = ( requirement . floor , requirement . site ) 3 ctx
1422 return highest 23 ctx
1425 def materialise ( 32 ctx
1427 names : tuple [ str , . . . ] = ( ) ,
1429 offline : bool = False ,
1430 strict : bool = True ,
1431 ) - > tuple [ Materialised , . . . ] :
1432 """Supply every locked tool, or *names* alone, and write their receipts.
1434 The bundle is what the sites require: every tool the lock holds
1435 for this host; one locked for other hosts alone is skipped, and
1436 named it refuses. A downloaded kind is supplied from the
1437 catalogue's deployment for
1438 this host through the store's sources and the origin; a delegated
1439 kind through its installer. A system tool is the machine's own,
1440 held to the highest of the record's floor and the sites' floors;
1441 the refusal names the site whose floor it is under. A tool the
1442 lock marks `allow-host` is looked for on PATH first: a copy that
1443 satisfies the floor serves, its receipt saying `host` and the
1444 version that answered, and the store installs nothing for it; a
1445 copy that is absent or below the floor is passed over with a
1446 note, and the locked version is supplied as for any other tool. A
1447 host receipt whose file is unchanged since it was written is
1448 reused without a probe. Tools in
1449 `link` mode are linked into the checkout's bin directory together,
1450 so a name two tools offer goes to the first. A receipt is written
1451 per tool supplied, and on a materialise of the whole bundle the
1452 receipt of a tool the lock no longer holds is removed, so its
1453 paths leave the environment with it.
1455 Raises a refusal naming the tool when the lock does not hold it.
1456 A tool the store cannot supply refuses too, naming the reason,
1457 unless *strict* is off: then it is reported in its outcome and the
1458 others are supplied, which is what `sync` wants, since one tool's
1459 origin being unreachable must not stop the environment from
1460 entering. A tool the lock marks optional is reported and never
1461 refused, strict or not: the verbs that need it name its absence
1464 from livery . toolroom . store import ( 24 ctx
1475 lock = current_lock ( root ) 24 ctx
1476 if lock is None : 24 ctx
1477 fail ( f " no { LOCK_FILE } : lock the tools first with ` { prog ( ) } toolroom.lock` " ) 1 ctx
1478 listing = catalogue ( root , offline = offline ) 24 ctx
1479 store = Store ( _home ( ) , sources = sources ( root ) , offline = offline ) 24 ctx
1480 host = store . host 24 ctx
1481 supported = supported_hosts ( root ) 24 ctx
1482 if host not in supported : 24 ctx
1483 # A runner's label is free text on every forge, so the host
1484 # itself is where an unsupported runner can be told apart.
1486 f " this host ( { host } ) is not one the workspace supports; "
1487 f " [workspace] hosts supports { ' , ' . join ( supported ) } "
1489 if strict or os . environ . get ( " CI " ) : 2 ctx
1491 print ( f " { reason } " ) 1 ctx
1492 here = lock . on_host ( host ) 24 ctx
1493 wanted = names or here 24 ctx
1495 for name , entry in sorted ( lock . tools . items ( ) ) : 18 ctx
1496 if entry . optional and name not in here : 1496 ↛ 1497line 1496 didn't jump to line 1497 because the condition on line 1496 was never true 18 ctx
1498 f " { name } : optional, and not locked for this host ( { host } ); "
1499 " the verbs that need it say so "
1501 for name in wanted : 24 ctx
1502 if name not in lock . tools : 24 ctx
1504 f " { name } is not in { LOCK_FILE } ; the lock holds "
1505 f " { ' , ' . join ( sorted ( lock . tools ) ) or ' nothing ' } "
1507 if name not in here : 23 ctx
1509 f " { name } is locked for { ' , ' . join ( lock . tools [ name ] . on ) } and not "
1510 f " for this host ( { host } ); the sites require it there alone "
1512 floors = site_floors ( root ) 23 ctx
1513 done_by_name : dict [ str , Materialised ] = { } 23 ctx
1514 linked : list [ tuple [ ToolReceipt , Ensured ] ] = [ ] 23 ctx
1515 wanted = _runtimes_first ( wanted , lock , listing ) 23 ctx
1516 runtimes : dict [ str , Path ] = { } 23 ctx
1517 requests : list [ SupplyRequest ] = [ ] 23 ctx
1518 pending_meta : dict [ str , tuple [ str , str , Deployment | None , str ] ] = { } 23 ctx
1519 for name in wanted : 23 ctx
1520 locked = lock . tools [ name ] 23 ctx
1521 listed = listing . listed ( name ) 23 ctx
1522 floor , site = listed . min_version , " " 23 ctx
1523 asked = floors . get ( name ) if listed . kind == " system-check " else None 23 ctx
1524 if asked is not None and ( 23 ctx
1525 not floor or version_key ( asked [ 0 ] ) > version_key ( floor )
1527 floor , site = asked 1 ctx
1529 if locked . allow_host : 23 ctx
1530 fresh = _fresh_host_receipt ( root , name ) 2 ctx
1531 if fresh is not None : 2 ctx
1532 if name in RUNTIMES : 1532 ↛ 1533line 1532 didn't jump to line 1533 because the condition on line 1532 was never true 1 ctx
1533 runtimes [ name ] = Path ( fresh . stamp [ " path " ] )
1534 done_by_name [ name ] = Materialised ( fresh , False ) 1 ctx
1536 asked = floors . get ( name ) 2 ctx
1537 host_floor = asked [ 0 ] if asked is not None else listed . min_version 2 ctx
1539 served = store . supply ( 2 ctx
1540 name , " system-check " , locked . version , None , min_version = host_floor
1542 except StoreError as error : 1 ctx
1543 note = f " { error } ; the store ' s { locked . version } serves " 1 ctx
1545 found = served . tool_dir / exe ( name ) 1 ctx
1546 if name in RUNTIMES : 1546 ↛ 1547line 1546 didn't jump to line 1547 because the condition on line 1546 was never true 1 ctx
1547 runtimes [ name ] = found
1548 receipt = ToolReceipt ( 1 ctx
1554 tool_dir = str ( served . tool_dir ) ,
1556 answered = served . answered ,
1557 stamp = _stamp ( found ) ,
1559 done_by_name [ name ] = Materialised ( receipt , False ) 1 ctx
1561 deployment : Deployment | None = None 22 ctx
1562 if listed . kind in DOWNLOAD_KINDS : 22 ctx
1564 deployment = listing . deployment ( name , locked . version , host ) 18 ctx
1565 except CatalogueError as error : 2 ctx
1566 # A host outside the lock's set, or one the version has
1567 # no build for: the tool is reported and the others are
1568 # supplied, as any tool the store cannot supply is.
1569 reason = f " { name } { locked . version } : not locked for { host } : { error } " 2 ctx
1570 if strict and not locked . optional : 2 ctx
1572 done_by_name [ name ] = Materialised ( None , False , reason ) 1 ctx
1574 pending_meta [ name ] = ( floor , site , deployment , note ) 22 ctx
1575 requests . append ( 22 ctx
1581 package = listed . package ,
1583 runtime = listed . runtime ,
1584 runtime_exe = runtimes . get ( runtime_of ( listed ) ) ,
1585 graph = _graph_path ( root , locked ) ,
1588 supplied = { result . name : result for result in store . supply_many ( requests ) } 23 ctx
1589 done : list [ Materialised ] = [ ] 23 ctx
1590 for name in wanted : 23 ctx
1591 already = done_by_name . get ( name ) 23 ctx
1592 if already is not None : 23 ctx
1593 done . append ( already ) 2 ctx
1595 locked = lock . tools [ name ] 22 ctx
1596 listed = listing . listed ( name ) 22 ctx
1597 floor , site , deployment , note = pending_meta [ name ] 22 ctx
1598 outcome = supplied [ name ] 22 ctx
1599 if outcome . ensured is None : 22 ctx
1600 reason = outcome . error 7 ctx
1601 if site and " below the floor " in reason : 7 ctx
1602 reason + = f " ; { site } requires { name } >= { floor } " 1 ctx
1603 # An optional tool the host cannot supply is named, never
1604 # refused: the verbs that need it say so when they run.
1605 if strict and not locked . optional : 7 ctx
1607 done . append ( Materialised ( None , False , reason ) ) 7 ctx
1609 ensured = outcome . ensured 18 ctx
1610 mode = mode_of ( 18 ctx
1615 paths = deployment . paths if deployment is not None else ( ) ,
1617 receipt = ToolReceipt ( 18 ctx
1623 str ( deployment . digest ( ) ) if deployment is not None else " " ,
1624 str ( ensured . tool_dir ) ,
1625 tuple ( str ( p ) for p in ensured . paths ) if mode == " path " else ( ) ,
1627 tuple ( Path ( e ) . name for e in ensured . deployment . entry_points )
1628 if mode in ( " link " , " path " )
1631 if mode == " link " : 18 ctx
1632 linked . append ( ( receipt , ensured ) ) 1 ctx
1633 done . append ( Materialised ( receipt , ensured . installed , note = note ) ) 18 ctx
1634 if linked or bin_dir ( root ) . is_dir ( ) : 23 ctx
1635 store . link ( [ ensured for _ , ensured in linked ] , bin_dir ( root ) ) 1 ctx
1636 receipts_dir ( root ) . mkdir ( parents = True , exist_ok = True ) 23 ctx
1637 for made in done : 23 ctx
1638 if made . receipt is None : 23 ctx
1640 path = receipts_dir ( root ) / f " { made . receipt . tool } .json " 19 ctx
1641 path . write_text ( 19 ctx
1642 json . dumps ( made . receipt . to_json ( ) , indent = 2 ) + " \n " , encoding = " utf-8 "
1645 # A tool that left the lock, or is locked for other hosts
1646 # alone, leaves the environment: a receipt kept past its tool
1647 # would keep the tool's directory on PATH ahead of whatever
1649 for stale in receipts_dir ( root ) . glob ( " *.json " ) : 18 ctx
1650 if stale . stem not in here : 16 ctx
1652 return tuple ( done ) 23 ctx
1655 def typings_dir ( root : Path ) - > Path : 32 ctx
1656 """The typings directory the stubs are written under."""
1657 return root / TYPINGS 21 ctx
1660 def stubs_dir ( root : Path ) - > Path : 32 ctx
1661 """The `stubs` package under the typings directory's `livery.toolroom`."""
1662 return typings_dir ( root ) . joinpath ( * TYPINGS_PACKAGE ) / " stubs " 21 ctx
1665 def stubs_present ( root : Path ) - > int : 32 ctx
1666 """How many tool stubs the typings directory holds."""
1667 return sum ( 1 for p in stubs_dir ( root ) . glob ( " *.pyi " ) if p . stem != " __init__ " ) 4 ctx
1670 @dataclass ( frozen = True ) 32 ctx
1672 """What `write_stubs` did.
1675 written: The tools whose stub changed on disk, or was new.
1676 kept: The tools whose stub was already what the catalogue holds.
1677 skipped: Per tool the catalogue lists and no stub was written
1679 removed: Stubs of tools the catalogue no longer lists.
1682 written : tuple [ str , . . . ] 32 ctx
1683 kept : tuple [ str , . . . ] 32 ctx
1684 skipped : dict [ str , str ] 32 ctx
1685 removed : tuple [ str , . . . ] 32 ctx
1688 def _no_command ( listing : Catalogue , name : str , version : str ) - > bool : 32 ctx
1689 """Whether *name* is a download with no entry point: no command, so no stub."""
1690 import platform 21 ctx
1692 from livery . toolroom . store import CatalogueError , RecordError , host_key 21 ctx
1694 if listing . listed ( name ) . kind != " download " : 21 ctx
1697 deployment = listing . deployment ( 12 ctx
1698 name , version , host_key ( platform . system ( ) , platform . machine ( ) )
1700 except ( CatalogueError , RecordError ) :
1702 return not deployment . entry_points 12 ctx
1705 def write_stubs ( root : Path , * , offline : bool = False ) - > Stubbed : 32 ctx
1706 """Write the stubs of the locked tools into the typings directory.
1708 One stub per tool `toolroom.lock` holds, rendered by the store from the
1709 locked version's own surface, from the records or from the index:
1710 a tool the workspace does not deploy gets no stub, and its handle
1711 types as a bare `Tool`. The `handles` module beside the stubs
1712 declares the handles, one import and one `name: Class[Result]` per
1713 stub written, and the installed tools package's index imports every
1714 name from it. The handles cannot live in the stubs package's own
1715 index: a package's index binds its submodules, and `uv` would name
1716 `uv.pyi` rather than the handle. Nothing is written inside the
1717 tools package's own directory, and a tree left there is removed:
1718 it shadows the package for a checker run on explicit paths. Without
1719 a lock nothing is written.
1721 A receipt under `.workshop/state/` names the lock and the source
1722 the last write rendered from, a records directory by its stat
1723 fingerprint and an index by its pointer. When both stand and every file it
1724 wrote is there, nothing is read and nothing is written, which is
1725 the steady state of every sync; otherwise a stub already on disk
1726 as the source renders it is kept, so a checker's cache stands.
1728 from livery . toolroom . store import CatalogueError 21 ctx
1730 source = index_source ( root ) 21 ctx
1731 local = _local_records ( root , source ) 21 ctx
1732 lock = current_lock ( root ) 21 ctx
1733 locked = dict ( lock . tools ) if lock is not None else { } 21 ctx
1734 shutil . rmtree ( 21 ctx
1735 typings_dir ( root ) . joinpath ( * TYPINGS_PACKAGE , " tools " ) , ignore_errors = True
1737 directory = stubs_dir ( root ) 21 ctx
1738 directory . mkdir ( parents = True , exist_ok = True ) 21 ctx
1742 seen = source_mark ( source ) 21 ctx
1743 if local is not None : 21 ctx
1744 seen = f " { seen } + { source_mark ( str ( local ) ) } " 1 ctx
1745 except CatalogueError as error :
1747 standing = _stubs_standing ( root , seen , sorted ( locked ) ) 21 ctx
1748 if standing is not None : 21 ctx
1749 return standing 3 ctx
1750 listing = catalogue ( root , offline = offline ) if locked else None 21 ctx
1751 written : list [ str ] = [ ] 21 ctx
1752 kept : list [ str ] = [ ] 21 ctx
1753 skipped : dict [ str , str ] = { } 21 ctx
1754 declared : list [ str ] = [ ] 21 ctx
1755 for name in sorted ( locked ) : 21 ctx
1756 version = locked [ name ] . version 21 ctx
1757 assert listing is not None 21 ctx
1758 if _no_command ( listing , name , version ) : 21 ctx
1761 text = listing . stub ( name , version ) 21 ctx
1762 except CatalogueError as error : 14 ctx
1763 # A tool/version genuinely never observed has no underlying
1764 # cause and is skipped; an unreadable object in the index
1765 # (missing blob/tree, transport error) fails closed rather
1766 # than silently falling back to a bare stub and writing a
1768 if error . __cause__ is not None : 14 ctx
1769 fail ( str ( error ) ) 1 ctx
1770 skipped [ name ] = str ( error ) 13 ctx
1772 path = directory / f " { name } .pyi " 19 ctx
1773 if path . is_file ( ) and path . read_text ( encoding = " utf-8 " ) == text : 19 ctx
1774 kept . append ( name ) 6 ctx
1776 path . write_text ( text , encoding = " utf-8 " ) 18 ctx
1777 written . append ( name ) 18 ctx
1778 declared . append ( name ) 19 ctx
1779 removed : list [ str ] = [ ] 20 ctx
1780 for stale in sorted ( directory . glob ( " *.pyi " ) ) : 20 ctx
1781 if stale . stem != " __init__ " and stale . stem not in declared : 19 ctx
1783 removed . append ( stale . stem ) 2 ctx
1784 _write_if_changed ( directory / " __init__.pyi " , " " ) 20 ctx
1785 _write_if_changed ( handles_path ( root ) , handles_index ( declared ) ) 20 ctx
1786 if seen is not None : 20 ctx
1787 _write_stubs_receipt ( root , seen , sorted ( locked ) , declared ) 20 ctx
1788 return Stubbed ( tuple ( written ) , tuple ( kept ) , skipped , tuple ( removed ) ) 20 ctx
1791 def source_mark ( source : str ) - > str : 32 ctx
1792 """What the catalogue source is right now, in one digest, without reading it whole.
1794 A directory of records is its stat fingerprint; an index, by
1795 directory or URL, is the digest of its pointer document.
1797 from livery . strongroom import canonical , digest_of 21 ctx
1798 from livery . toolroom . store import is_records , read_pointer , tree_fingerprint 21 ctx
1800 if is_records ( source ) : 21 ctx
1801 return tree_fingerprint ( [ source ] ) 18 ctx
1802 return str ( digest_of ( canonical ( read_pointer ( source ) ) ) ) 4 ctx
1805 STUBS_RECEIPT = " .workshop/state/stubs.json " 32 ctx
1806 """The receipt naming the lock and the source the last stub write rendered from. 32 ctx
1808 Under `state/` rather than `receipts/`: that directory is the tool
1809 receipts, which `tool_receipts()` reads as a set and refuses a file of
1810 another shape in. Each kind of thing under `.workshop/` keeps its own
1811 directory, so no walk of one kind can reach another.
1815 def _stubs_receipt_path ( root : Path ) - > Path : 32 ctx
1816 return root / STUBS_RECEIPT 21 ctx
1819 def _lock_digest ( root : Path ) - > str : 32 ctx
1820 return " sha256: " + hashlib . sha256 ( lock_path ( root ) . read_bytes ( ) ) . hexdigest ( ) 20 ctx
1823 def _stubs_standing ( root : Path , stubs : str , locked : list [ str ] ) - > Stubbed | None : 32 ctx
1824 """The last write's answer when the lock, the source and the files stand."""
1826 held = json . loads ( _stubs_receipt_path ( root ) . read_text ( encoding = " utf-8 " ) ) 21 ctx
1827 except ( OSError , ValueError ) : 20 ctx
1829 if not isinstance ( held , dict ) or held . get ( " schema " ) != 1 : 1829 ↛ 1830line 1829 didn't jump to line 1830 because the condition on line 1829 was never true 8 ctx
1831 if held . get ( " source " ) != stubs or held . get ( " lock " ) != _lock_digest ( root ) : 8 ctx
1833 if held . get ( " locked " ) != locked : 1833 ↛ 1834line 1833 didn't jump to line 1834 because the condition on line 1833 was never true 6 ctx
1835 written = held . get ( " written " ) 6 ctx
1836 if not isinstance ( written , list ) or not all ( isinstance ( n , str ) for n in written ) : 1836 ↛ 1837line 1836 didn't jump to line 1837 because the condition on line 1836 was never true 6 ctx
1838 # A locked tool with no stub is named on every write, so a write that
1839 # skipped one is never answered from the receipt.
1840 if written != locked : 6 ctx
1842 present = { p . stem for p in stubs_dir ( root ) . glob ( " *.pyi " ) } - { " __init__ " } 3 ctx
1843 if present != set ( written ) or not handles_path ( root ) . is_file ( ) : 3 ctx
1845 return Stubbed ( ( ) , tuple ( written ) , { } , ( ) ) 3 ctx
1848 def _write_stubs_receipt ( 32 ctx
1849 root : Path , stubs : str , locked : list [ str ] , written : list [ str ]
1851 path = _stubs_receipt_path ( root ) 20 ctx
1852 path . parent . mkdir ( parents = True , exist_ok = True ) 20 ctx
1856 " lock " : _lock_digest ( root ) ,
1860 path . write_text ( json . dumps ( document , indent = 2 ) + " \n " , encoding = " utf-8 " ) 20 ctx
1863 def handles_path ( root : Path ) - > Path : 32 ctx
1864 """The `handles.pyi` module beside the stubs, declaring the typed handles."""
1865 return typings_dir ( root ) . joinpath ( * TYPINGS_PACKAGE ) / " handles.pyi " 20 ctx
1868 def handles_index ( names : list [ str ] ) - > str : 32 ctx
1869 """The `handles` module declaring the handles of *names*, in order.
1871 The installed tools package's own index imports every name from
1872 this module, so the handle `uv` types as `Uv[Result]` exactly
1873 when `stubs/uv.pyi` is beside it.
1875 from livery . toolroom . store import class_name 20 ctx
1878 f " # Rendered by ` { prog ( ) } toolroom.restub`: the handles this workspace " ,
1879 " # locks. Do not edit by hand. " ,
1880 " from livery.toolroom.tools import Result " ,
1883 f " from livery.toolroom.stubs. { name } import "
1884 f " { class_name ( name ) } as { class_name ( name ) } "
1887 lines . append ( " " ) 20 ctx
1888 lines + = [ f " { name } : { class_name ( name ) } [Result] " for name in names ] 20 ctx
1889 return " \n " . join ( lines ) + " \n " 20 ctx
1892 def _write_if_changed ( path : Path , text : str ) - > None : 32 ctx
1893 if path . is_file ( ) and path . read_text ( encoding = " utf-8 " ) == text : 20 ctx
1895 path . write_text ( text , encoding = " utf-8 " ) 19 ctx
1898 def stub_lines ( root : Path , * , offline : bool = False , strict : bool = True ) - > list [ str ] : 32 ctx
1899 """Write the stubs and say what happened, as `sync` and the lock verbs print it.
1901 A refusal is raised when *strict*; otherwise it is the one line
1902 returned, naming the reason, since a lock written or a bundle
1903 materialised stands whether or not the stubs could follow.
1906 made = write_stubs ( root , offline = offline ) 2 ctx
1909 made = write_stubs ( root , offline = offline ) 13 ctx
1910 except Failed as refusal :
1911 return [ f " stubs: not written: { refusal } " ]
1912 held = len ( made . written ) + len ( made . kept ) 14 ctx
1913 line = f " stubs: { held } in { TYPINGS } / " 14 ctx
1914 if made . written : 14 ctx
1915 line + = f " , wrote { len ( made . written ) } " 11 ctx
1916 if made . removed : 1916 ↛ 1917line 1916 didn't jump to line 1917 because the condition on line 1916 was never true 14 ctx
1917 line + = f " , removed { ' , ' . join ( made . removed ) } "
1918 lines = [ line ] 14 ctx
1919 lines + = [ f " stubs: { name } : { why } " for name , why in made . skipped . items ( ) ] 14 ctx
1923 def emission ( root : Path ) - > tuple [ tuple [ str , . . . ] , dict [ str , str ] ] : 32 ctx
1924 """What the receipts add to an entered shell: PATH entries and variables.
1926 The checkout's bin directory first when any receipt links into it,
1927 then each `path` receipt's directories in tool order, then the
1928 variables, a later receipt's value winning.
1930 held = tool_receipts ( root ) 18 ctx
1931 paths : list [ str ] = [ ] 17 ctx
1932 env : dict [ str , str ] = { } 17 ctx
1933 if any ( r . mode == " link " for r in held . values ( ) ) : 17 ctx
1934 paths . append ( str ( bin_dir ( root ) ) ) 1 ctx
1935 for receipt in held . values ( ) : 17 ctx
1936 for entry in receipt . paths : 9 ctx
1937 if entry not in paths : 1937 ↛ 1936line 1937 didn't jump to line 1936 because the condition on line 1937 was always true 8 ctx
1938 paths . append ( entry ) 8 ctx
1939 env . update ( receipt . env ) 9 ctx
1940 return tuple ( paths ) , env 17 ctx
1943 def drift ( root : Path ) - > dict [ str , str ] : 32 ctx
1944 """Per tool the sites require, what `fm env.check` says of its receipt.
1946 An empty string is a tool whose receipt matches the lock; anything
1947 else names the problem: no receipt, a receipt at another version
1948 than the lock, or a deployment the lock has moved under it.
1950 lock = current_lock ( root ) 10 ctx
1951 held = tool_receipts ( root ) 10 ctx
1952 found : dict [ str , str ] = { } 10 ctx
1953 for name in tool_names ( root , this_host ( ) ) : 10 ctx
1954 if lock is None or name not in lock . tools : 9 ctx
1955 found [ name ] = f " not locked; run ` { prog ( ) } toolroom.lock` " 5 ctx
1957 locked = lock . tools [ name ] 6 ctx
1958 receipt = held . get ( name ) 6 ctx
1959 if receipt is None : 6 ctx
1960 found [ name ] = ( 4 ctx
1961 f " no receipt for { locked . version } ; run ` { prog ( ) } sync` to materialise "
1964 if receipt . version != locked . version : 5 ctx
1965 found [ name ] = ( 1 ctx
1966 f " receipt { receipt . version } , lock { locked . version } ; run ` { prog ( ) } sync` "
1969 expected = locked . hosts . get ( receipt . host ) 5 ctx
1970 if receipt . source == " host " : 5 ctx
1972 if expected is not None and str ( expected ) != receipt . deployment : 4 ctx
1973 found [ name ] = ( 1 ctx
1974 f " DRIFT: the deployment of { locked . version } on { receipt . host } moved "
1975 f " under the receipt ( { receipt . deployment [ : 19 ] } ... is now "
1976 f " { str ( expected ) [ : 19 ] } ...); run ` { prog ( ) } sync` "
1979 found [ name ] = " " 4 ctx
1983 def undeployed ( name : str ) - > NoReturn : 32 ctx
1984 """Refuse the task: *name* is a tool of the store and is not on ``PATH``.
1986 Call it where a step spawns a tool of the store and the spawn
1987 raises ``OSError``, or where ``shutil.which`` finds nothing for a
1988 tool the step spawns by its path. A toolroom handle spawns its tool
1989 by name from ``PATH``, and the store's tools are on ``PATH`` only
1990 after the machine has deployed them and the shell has entered the
1991 environment. A traceback from the spawn names nothing a person can
1992 act on. This refusal names the tool and the next action: run
1993 ``fm sync``, then the ``env.emit`` line it prints, then run the task
1999 name: The tool's name, as its handle spawns it.
2002 Failed: always, naming *name* and the sync that deploys it.
2005 f " { name } is not on PATH: it is a tool of the store, so enter the "
2006 f " environment (` { prog ( ) } sync`, then the printed "
2007 " env.emit line) and re-run "