Skip to content

livery.workshop._invoke

livery.workshop._invoke

How a step's path list reaches its tool: in how many calls.

The paths go on the tool's command line, where a step's template puts them (livery.workshop._words.run_words). batches splits a path list into the fewest calls the platform's command line allows, and run_batched runs each call and merges their failures into one refusal, so a long list never fails on the operating system's limit and never stops at the first failing call.

Functions:

Name Description
batches

paths in the fewest calls whose arguments stay within limit bytes.

run_batched

Run call once per batch of paths; one refusal naming every failure.

Attributes:

Name Type Description
ARGV_LIMIT

ARGV_LIMIT module-attribute

ARGV_LIMIT = 30000 if sys.platform == 'win32' else 200000

batches

batches(paths: Sequence[str], *, limit: int = ARGV_LIMIT) -> tuple[tuple[str, ...], ...]

paths in the fewest calls whose arguments stay within limit bytes.

In order: each call takes paths until the next would not fit. A path longer than the limit on its own goes in a call of its own, since there is no smaller way to pass it.

Source code in packages/workshop/src/livery/workshop/_invoke.py
def batches(
    paths: Sequence[str], *, limit: int = ARGV_LIMIT
) -> tuple[tuple[str, ...], ...]:
    """*paths* in the fewest calls whose arguments stay within *limit* bytes.

    In order: each call takes paths until the next would not fit. A
    path longer than the limit on its own goes in a call of its own,
    since there is no smaller way to pass it.
    """
    calls: list[tuple[str, ...]] = []
    current: list[str] = []
    size = 0
    for path in paths:
        length = len(path.encode("utf-8")) + 1  # the separating space or NUL
        if current and size + length > limit:
            calls.append(tuple(current))
            current, size = [], 0
        current.append(path)
        size += length
    if current:
        calls.append(tuple(current))
    return tuple(calls)

run_batched

run_batched(paths: Sequence[str], call: Callable[[tuple[str, ...]], None], *, limit: int = ARGV_LIMIT) -> None

Run call once per batch of paths; one refusal naming every failure.

Every batch runs even when an earlier one fails, so one gate shows every finding. An empty paths runs nothing.

Raises:

Type Description
Failed

when any batch failed, with each failure's reason.

Source code in packages/workshop/src/livery/workshop/_invoke.py
def run_batched(
    paths: Sequence[str],
    call: Callable[[tuple[str, ...]], None],
    *,
    limit: int = ARGV_LIMIT,
) -> None:
    """Run *call* once per batch of *paths*; one refusal naming every failure.

    Every batch runs even when an earlier one fails, so one gate shows
    every finding. An empty *paths* runs nothing.

    Raises:
        Failed: when any batch failed, with each failure's reason.
    """
    reasons: list[str] = []
    for batch in batches(paths, limit=limit):
        try:
            call(batch)
        except Failed as failure:
            reasons.append(str(failure))
    if len(reasons) == 1:
        fail(reasons[0])
    if reasons:
        fail(f"{len(reasons)} calls failed:\n" + "\n".join(reasons))