Skip to content

livery.workshop._influence

livery.workshop._influence

Which files a step or CI job reads, and which of them a change touched.

A step or CI job declares its inputs on its record (livery.workshop._steps.StepRecord inputs, or livery.workshop._points.Job inputs): every entry of reads or widens names one set of files through category, channel, or one of the base's four named sets (NAMED_SETS), optionally narrowed by suffixes and, on a category read, ignore rule codes.

Two widenings hold for every workspace step. A run that knows nothing of what changed (the whole gate) judges everything. A step whose own code changed judges everything, since its rule moved, not the files: the member package that ships it changed under its sources, or, for a step installed from an index, the lock moved.

Classes:

Name Description
Changes

What changed since the tree a gate measures from.

InputEntry

One set of files a step or CI job reads or widens on.

Inputs

The files a step or CI job reads and widens on, named by sets.

Selection

What one step judges in a run.

Functions:

Name Description
sets_of

The named sets of NAMED_SETS that contain path inside root.

reads

Whether inputs read path.

provider_of

The member package whose sources ship extension, by its path; empty if none.

select

What a step with inputs judges for changes.

Attributes:

Name Type Description
LOCK
NAMED_SETS
WHOLE
SKIP

LOCK module-attribute

LOCK = 'uv.lock'

NAMED_SETS module-attribute

NAMED_SETS = frozenset({'graph', 'extension-sources', 'declarations', 'tasks'})

WHOLE module-attribute

WHOLE = Selection(whole=True)

SKIP module-attribute

SKIP = Selection(whole=False)

Changes dataclass

Changes(root: Path, paths: tuple[str, ...], before: str = '')

What changed since the tree a gate measures from.

Attributes:

Name Type Description
root Path

The workspace root.

paths tuple[str, ...]

The changed paths, root-relative posix, deleted ones included.

before str

The commit or tree the paths were taken from; empty when the caller named the paths itself.

Methods:

Name Description
removed

The changed paths the working tree no longer holds: deleted or moved.

text_before

path where the change is measured from; empty if unknown.

root instance-attribute

root: Path

paths instance-attribute

paths: tuple[str, ...]

before class-attribute instance-attribute

before: str = ''

removed

removed() -> tuple[str, ...]

The changed paths the working tree no longer holds: deleted or moved.

Source code in packages/workshop/src/livery/workshop/_influence.py
def removed(self) -> tuple[str, ...]:
    """The changed paths the working tree no longer holds: deleted or moved."""
    return tuple(path for path in self.paths if not (self.root / path).exists())

text_before

text_before(path: str) -> str

path where the change is measured from; empty if unknown.

Source code in packages/workshop/src/livery/workshop/_influence.py
def text_before(self, path: str) -> str:
    """*path* where the change is measured from; empty if unknown."""
    if not self.before:
        return ""

    return git.file_at(self.root, self.before, path)

InputEntry dataclass

InputEntry(category: str = '', channel: str = '', set: str = '', suffixes: tuple[str, ...] = (), ignore: tuple[str, ...] = ())

One set of files a step or CI job reads or widens on.

Exactly one of category, channel or set is non-empty.

Attributes:

Name Type Description
category str

A category name (source, test, prose, ...).

channel str

A channel name (generated, composed, ...).

set str

One of NAMED_SETS (graph, extension-sources, declarations, tasks).

suffixes tuple[str, ...]

Optional file suffixes (each starting with .) that narrow the set; empty reads every file in the set.

ignore tuple[str, ...]

Rule codes the step withholds in this category (written into a tool's per-file ignores); valid on category reads only.

Methods:

Name Description
matches_suffix

Whether path carries one of suffixes, or suffixes is empty.

category class-attribute instance-attribute

category: str = ''

channel class-attribute instance-attribute

channel: str = ''

set class-attribute instance-attribute

set: str = ''

suffixes class-attribute instance-attribute

suffixes: tuple[str, ...] = ()

ignore class-attribute instance-attribute

ignore: tuple[str, ...] = ()

matches_suffix

matches_suffix(path: str) -> bool

Whether path carries one of suffixes, or suffixes is empty.

Source code in packages/workshop/src/livery/workshop/_influence.py
def matches_suffix(self, path: str) -> bool:
    """Whether *path* carries one of *suffixes*, or *suffixes* is empty."""
    return not self.suffixes or any(path.endswith(s) for s in self.suffixes)

Inputs dataclass

Inputs(reads: tuple[InputEntry, ...], per_file: bool = True, widens: tuple[InputEntry, ...] = (), on_removal: bool = False, widen: Callable[[Changes], bool] | None = None)

The files a step or CI job reads and widens on, named by sets.

Attributes:

Name Type Description
reads tuple[InputEntry, ...]

The file sets the step judges.

per_file bool

Whether a workspace step judges each changed file it reads on its own; otherwise a change to any of them runs it whole.

widens tuple[InputEntry, ...]

File sets the step does not judge whose change runs it over everything it reads.

on_removal bool

Whether a deleted or moved file anywhere runs it whole: a link checker's, since a link in an unchanged page may point at what went away.

widen Callable[[Changes], bool] | None

The step's own further reason to run whole, given the changes: a link checker's removed heading.

reads instance-attribute

reads: tuple[InputEntry, ...]

per_file class-attribute instance-attribute

per_file: bool = True

widens class-attribute instance-attribute

widens: tuple[InputEntry, ...] = ()

on_removal class-attribute instance-attribute

on_removal: bool = False

widen class-attribute instance-attribute

widen: Callable[[Changes], bool] | None = None

Selection dataclass

Selection(whole: bool, files: tuple[str, ...] = ())

What one step judges in a run.

Attributes:

Name Type Description
whole bool

Whether it judges everything it reads.

files tuple[str, ...]

Otherwise, the changed files it judges; with whole false and no files, the step does not run.

whole instance-attribute

whole: bool

files class-attribute instance-attribute

files: tuple[str, ...] = ()

runs property

runs: bool

Whether the step runs at all.

sets_of

sets_of(root: Path, path: str, *, packages: Sequence[Package] | None = None, package_dirs: frozenset[str] | None = None, providers: Sequence[str] | None = None) -> frozenset[str]

The named sets of NAMED_SETS that contain path inside root.

Source code in packages/workshop/src/livery/workshop/_influence.py
def sets_of(
    root: Path,
    path: str,
    *,
    packages: Sequence[Package] | None = None,
    package_dirs: frozenset[str] | None = None,
    providers: Sequence[str] | None = None,
) -> frozenset[str]:
    """The named sets of `NAMED_SETS` that contain *path* inside *root*."""
    from livery.workshop._packages import discover_packages

    found: set[str] = set()
    not_ignored = not _is_prose_or_licence(path)
    if not_ignored:
        dirs = _package_dirs(root) if package_dirs is None else package_dirs
        parent = path.rpartition("/")[0]
        if path == "workshop.toml" or path.startswith("src/") or parent in dirs:
            found.add("graph")
        if providers is None:
            pkgs: Sequence[Package] = ()
            if packages is not None:
                pkgs = packages
            elif (root / "workshop.toml").is_file() and (root / "packages").is_dir():
                try:
                    pkgs = discover_packages(root)
                except ValueError:
                    pkgs = ()
            providers = _extension_providers(root, pkgs)
        if any(path.startswith(f"{provider}/src/") for provider in providers):
            found.add("extension-sources")
    if matches("packages/**/src/**/extension.toml", path):
        found.add("declarations")
    if path == "tasks.py":
        found.add("tasks")
    return frozenset(found)

reads

reads(inputs: Inputs, root: Path, path: str) -> bool

Whether inputs read path.

Source code in packages/workshop/src/livery/workshop/_influence.py
def reads(inputs: Inputs, root: Path, path: str) -> bool:
    """Whether *inputs* read *path*."""
    index = _PathIndex(root, (path,))
    return bool(index.matched(inputs.reads))

provider_of

provider_of(extension: str, packages: tuple[Package, ...]) -> str

The member package whose sources ship extension, by its path; empty if none.

An extension installed from an index has no member: its code moves with the lock instead.

Source code in packages/workshop/src/livery/workshop/_influence.py
def provider_of(extension: str, packages: tuple[Package, ...]) -> str:
    """The member package whose sources ship *extension*, by its path; empty if none.

    An extension installed from an index has no member: its code moves
    with the lock instead.
    """
    try:
        spec = importlib.util.find_spec(extension)
    except (ImportError, ValueError):
        return ""
    if spec is None:
        return ""
    places = [*(spec.submodule_search_locations or ())]
    if spec.origin:
        places.append(str(Path(spec.origin).parent))
    for place in places:
        resolved = Path(place).resolve()
        for package in packages:
            if resolved.is_relative_to(package.directory.resolve()):
                return package.path
    return ""

select

select(inputs: Inputs, changes: Changes | None, *, provider: str = '', packages: Sequence[Package] | None = None) -> Selection

What a step with inputs judges for changes.

Everything when nothing is known of what changed, when the step's own code changed (provider, the member that ships it, changed under src/; for a step with no member, the lock moved), or when a widening input changed; else the changed files it reads, each on its own, or everything when it does not judge per file; nothing when no file it reads changed.

Source code in packages/workshop/src/livery/workshop/_influence.py
def select(
    inputs: Inputs,
    changes: Changes | None,
    *,
    provider: str = "",
    packages: Sequence[Package] | None = None,
) -> Selection:
    """What a step with *inputs* judges for *changes*.

    Everything when nothing is known of what changed, when the step's
    own code changed (*provider*, the member that ships it, changed
    under ``src/``; for a step with no member, the lock moved), or
    when a widening input changed; else the changed files it reads,
    each on its own, or everything when it does not judge per file;
    nothing when no file it reads changed.
    """
    if changes is None:
        return WHOLE
    paths = changes.paths
    if provider:
        if any(path.startswith(f"{provider}/src/") for path in paths):
            return WHOLE
    elif LOCK in paths:
        return WHOLE
    index = _PathIndex(changes.root, paths, packages=packages)
    if index.matched(inputs.widens):
        return WHOLE
    if inputs.on_removal and changes.removed():
        return WHOLE
    if inputs.widen is not None and inputs.widen(changes):
        return WHOLE
    touched = index.matched(inputs.reads)
    if not touched:
        return SKIP
    return Selection(whole=False, files=touched) if inputs.per_file else WHOLE