runfiles.runfiles — rules_python 0.0.0 documentation (original) (raw)

Runfiles lookup library for Bazel-built Python binaries and tests.

See @rules_python//python/runfiles/README.md for usage instructions.

Module Contents

Classes

Runfiles Returns the runtime location of runfiles.
_DirectoryBased Runfiles strategy that appends runfiles paths to the runfiles root.
_ManifestBased Runfiles strategy that parses a runfiles-manifest to look up runfiles.

Functions

Create
CreateDirectoryBased
CreateManifestBased
_FindPythonRunfilesRoot Finds the root of the Python runfiles tree.
_ParseRepoMapping Parses the repository mapping manifest.

Data

API

runfiles.runfiles.Create(env: Optional[Dict[str, str]] = None) → Optional[runfiles.runfiles.Runfiles]

runfiles.runfiles.CreateDirectoryBased(runfiles_dir_path: str) → runfiles.runfiles.Runfiles

runfiles.runfiles.CreateManifestBased(manifest_path: str) → runfiles.runfiles.Runfiles

class runfiles.runfiles.Runfiles(strategy: Union[runfiles.runfiles._ManifestBased, runfiles.runfiles._DirectoryBased])

Returns the runtime location of runfiles.

Runfiles are data-dependencies of Bazel-built binaries and tests.

static Create(env: Optional[Dict[str, str]] = None) → Optional[runfiles.runfiles.Runfiles]

Returns a new Runfiles instance.

The returned object is either:

If env contains “RUNFILES_MANIFEST_FILE” with non-empty value, this method returns a manifest-based implementation. The object eagerly reads and caches the whole manifest file upon instantiation; this may be relevant for performance consideration.

Otherwise, if env contains “RUNFILES_DIR” with non-empty value (checked in this priority order), this method returns a directory-based implementation.

If neither cases apply, this method returns null.

Args: env: {string: string}; optional; the map of environment variables. If None, this function uses the environment variable map of this process. Raises: IOError: if some IO error occurs.

static CreateDirectoryBased(runfiles_dir_path: str) → runfiles.runfiles.Runfiles

static CreateManifestBased(manifest_path: str) → runfiles.runfiles.Runfiles

CurrentRepository(frame: int = 1) → str

Returns the canonical name of the caller’s Bazel repository.

For example, this function returns ‘’ (the empty string) when called from the main repository and a string of the form ‘rules_python~0.13.0` when called from code in the repository corresponding to the rules_python Bazel module.

More information about the difference between canonical repository names and the @repo part of labels is available at: https://bazel.build/build/bzlmod#repository-names

NOTE: This function inspects the callstack to determine where in the runfiles the caller is located to determine which repository it came from. This may fail or produce incorrect results depending on who the caller is, for example if it is not represented by a Python source file. Use the frame argument to control the stack lookup.

Args: frame: int; the stack frame to return the repository name for. Defaults to 1, the caller of the CurrentRepository function.

Returns: The canonical name of the Bazel repository containing the file containing the frame-th caller of this function

Raises: ValueError: if the caller cannot be determined or the caller’s file path is not contained in the Python runfiles tree

EnvVars() → Dict[str, str]

Returns environment variables for subprocesses.

The caller should set the returned key-value pairs in the environment of subprocesses in case those subprocesses are also Bazel-built binaries that need to use runfiles.

Returns: {string: string}; a dict; keys are environment variable names, values are the values for these environment variables

Rlocation(path: str, source_repo: Optional[str] = None) → Optional[str]

Returns the runtime path of a runfile.

Runfiles are data-dependencies of Bazel-built binaries and tests.

The returned path may not be valid. The caller should check the path’s validity and that the path exists.

The function may return None. In that case the caller can be sure that the rule does not know about this data-dependency.

Args: path: string; runfiles-root-relative path of the runfile source_repo: string; optional; the canonical name of the repository whose repository mapping should be used to resolve apparent to canonical repository names in path. If None (default), the repository mapping of the repository containing the caller of this method is used. Explicitly setting this parameter should only be necessary for libraries that want to wrap the runfiles library. UseCurrentRepository to obtain canonical repository names. Returns: the path to the runfile, which the caller should check for existence, or None if the method doesn’t know about this runfile Raises: TypeError: if path is not a string ValueError: if path is None or empty, or it’s absolute or not normalized

__init__(strategy: Union[runfiles.runfiles._ManifestBased, runfiles.runfiles._DirectoryBased]) → None

class runfiles.runfiles._DirectoryBased(path: str)

Runfiles strategy that appends runfiles paths to the runfiles root.

EnvVars() → Dict[str, str]

RlocationChecked(path: str) → str

__init__(path: str) → None

runfiles.runfiles._FindPythonRunfilesRoot() → str

Finds the root of the Python runfiles tree.

class runfiles.runfiles._ManifestBased(path: str)

Runfiles strategy that parses a runfiles-manifest to look up runfiles.

EnvVars() → Dict[str, str]

RlocationChecked(path: str) → Optional[str]

Returns the runtime path of a runfile.

_GetRunfilesDir() → str

static _LoadRunfiles(path: str) → Dict[str, str]

Loads the runfiles manifest.

__init__(path: str) → None

runfiles.runfiles._ParseRepoMapping(repo_mapping_path: Optional[str]) → Dict[Tuple[str, str], str]

Parses the repository mapping manifest.

runfiles.runfiles._Runfiles = None