API Reference

Basic

trailrunner.walk(path, *, excludes=None)

Generate all significant file paths, starting from the given path.

Finds the project root and any associated gitignore. Filters any paths that match a gitignore pattern. Recurses into subdirectories, and otherwise only includes files that match the trailrunner.core.INCLUDE_PATTERN regex.

Optional excludes parameter allows supplying an extra set of paths (or gitignore-style patterns) to exclude from the final results.

Returns a generator that yields each significant file as the tree is walked.

Parameters
Return type

Iterator[pathlib.Path]

trailrunner.run(paths, func)

Run a given function once for each path, using a process pool for concurrency.

For each path given, func will be called with path as the only argument. To pass any other positional or keyword arguments, use functools.partial.

Results from each path will be returned as a dictionary mapping path to result.

Uses a process pool with “spawned” processes that share no state with the parent process, to enforce consistent behavior on Linux, macOS, and Windows, where forked processes are not possible.

Parameters
Return type

Dict[pathlib.Path, T]

trailrunner.walk_and_run(paths, func, *, excludes=None)

Walks each path given, and runs the given function on all gathered paths.

See walk() for details on how paths are gathered, and run() for how functions are run for each gathered path.

Parameters
Return type

Dict[pathlib.Path, T]

Advanced

class trailrunner.Trailrunner(*, context=None, executor_factory=None)

Self-contained Trailrunner instance with configurable multiprocessing semantics.

The basic API functions above are lightweight wrappers calling their respective methods on fresh instances of the Trailrunner class with no arguments.

By default, uses a process pool executor with “spawn” child processes, to enforce consistent behavior when running functions on Linux, macOS, and Windows. This can be overridden for each individual instance by passing an executor factory during initialization.

__init__(*, context=None, executor_factory=None)
Parameters
  • context (Optional[multiprocessing.context.BaseContext]) – multiprocessing context used by the default process pool executor. Ignored if DEFAULT_EXECUTOR is set, or executor_factory is passed. Not supported in Python 3.6 or older.

  • executor_factory (Optional[Callable[[], concurrent.futures._base.Executor]]) – Alternative executor factory. Must be a function that takes no arguments, and returns an instance of concurrent.futures.Executor.

run(paths, func)

Run a given function once for each path, using an executor for concurrency.

For each path given, func will be called with path as the only argument. To pass any other positional or keyword arguments, use functools.partial.

Results from each path will be returned as a dictionary mapping path to result.

Parameters
Return type

Dict[pathlib.Path, T]

walk(path, *, excludes=None)

Generate all significant file paths, starting from the given path.

Finds the project root and any associated gitignore. Filters any paths that match a gitignore pattern. Recurses into subdirectories, and otherwise only includes files that match the trailrunner.core.INCLUDE_PATTERN regex.

Optional excludes parameter allows supplying an extra set of paths (or gitignore-style patterns) to exclude from the final results.

Returns a generator that yields each significant file as the tree is walked.

Parameters
Return type

Iterator[pathlib.Path]

walk_and_run(paths, func, *, excludes=None)

Walks each path given, and runs the given function on all gathered paths.

See Trailrunner.walk() for details on how paths are gathered, and Trailrunner.run() for how functions are run for each gathered path.

Parameters
Return type

Dict[pathlib.Path, T]

DEFAULT_EXECUTOR: Optional[Callable[], concurrent.futures._base.Executor]] = None

Global override for the default executor, used when executor_factory is not passed to new instances. This is intended primarily for use in unit testing, when it is desirable to force using thread pools by default.

Prefer explicitly passing executor_factory when possible to avoid conflicting behavior with other packages that also use trailrunner.

Utilities

trailrunner.core.INCLUDE_PATTERN: str = '.+\\.pyi?$'

Regex pattern used to match against filenames that should be included in results when recursively walking directories. Any files not matching this regex will be ignored.

trailrunner.core.ROOT_MARKERS: List[pathlib.Path] = [PosixPath('pyproject.toml'), PosixPath('.git'), PosixPath('.hg')]

List of pathlib.Path objects used to “mark” the root of project. This is used by gitignore() for finding and reading a .gitignore file from the project root of a given path or directory.

trailrunner.core.project_root(path)

Find the project root, looking upward from the given path.

Looks through all parent paths until either the root is reached, or a directory is found that contains any of ROOT_MARKERS.

Parameters

path (pathlib.Path) –

Return type

pathlib.Path

trailrunner.core.gitignore(path)

Generate a PathSpec object for a .gitignore file in the given directory.

If none is found, an empty PathSpec is returned. If the path is not a directory, ValueError is raised.

Parameters

path (pathlib.Path) –

Return type

pathspec.pathspec.PathSpec