//python:repositories.bzl
This file contains macros to be called during WORKSPACE evaluation.
- is_standalone_interpreter(rctx, python_interpreter_path, logger=None)
Query a python interpreter target for whether or not it’s a rules_rust provided toolchain
- Args:
rctx– (repository_ctx)The repository rule’s context object.
python_interpreter_path– (path)A path representing the interpreter.
logger– (default None)Optional logger to use for operations.
- Returns:
boolWhether or not the target is from a rules_python generated toolchain.
- py_repositories(transition_settings=[])
Runtime dependencies that users must install.
This function should be loaded and called in the user’s
WORKSPACE. Withbzlmodenabled, this function is not needed sinceMODULE.bazelhandles transitive deps.- Args:
transition_settings– (default [])A list of labels that terminal rules transition on by default.
- python_register_multi_toolchains(name, python_versions, default_version=None, minor_mapping=None, **kwargs)
Convenience macro for registering multiple Python toolchains.
- Args:
base name for each name in
python_register_toolchainscall.python_versions– (list[str])the Python versions.
default_version– (str) (default None)the default Python version. If not set, the first version in python_versions is used.
minor_mapping– (dict[str,str]) (default None)mapping between
X.YtoX.Y.Zformat. Defaults to the value in//python:versions.bzl.kwargs– passed to eachpython_register_toolchainscall.
- python_register_toolchains(name, python_version, register_toolchains=True, register_coverage_tool=False, set_python_version_constraint=False, tool_versions=None, platforms={'aarch64-apple-darwin': struct(arch='aarch64', compatible_with=['@platforms//os:macos', '@platforms//cpu:aarch64'], flag_values={}, os_name='osx', target_settings=['@@//python/config_settings:_is_py_freethreaded_no']), 'aarch64-apple-darwin-freethreaded': struct(arch='aarch64', compatible_with=['@platforms//os:macos', '@platforms//cpu:aarch64'], flag_values={}, os_name='osx', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes']), 'aarch64-pc-windows-msvc': struct(arch='aarch64', compatible_with=['@platforms//os:windows', '@platforms//cpu:aarch64'], flag_values={}, os_name='windows', target_settings=['@@//python/config_settings:_is_py_freethreaded_no']), 'aarch64-pc-windows-msvc-freethreaded': struct(arch='aarch64', compatible_with=['@platforms//os:windows', '@platforms//cpu:aarch64'], flag_values={}, os_name='windows', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes']), 'aarch64-unknown-linux-gnu': struct(arch='aarch64', compatible_with=['@platforms//os:linux', '@platforms//cpu:aarch64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'aarch64-unknown-linux-gnu-freethreaded': struct(arch='aarch64', compatible_with=['@platforms//os:linux', '@platforms//cpu:aarch64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'arm64e-apple-darwin': struct(arch='aarch64', compatible_with=['@platforms//os:macos', '@platforms//cpu:arm64e'], flag_values={}, os_name='osx', target_settings=['@@//python/config_settings:_is_py_freethreaded_no']), 'arm64e-apple-darwin-freethreaded': struct(arch='aarch64', compatible_with=['@platforms//os:macos', '@platforms//cpu:arm64e'], flag_values={}, os_name='osx', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes']), 'armv7-unknown-linux-gnu': struct(arch='arm', compatible_with=['@platforms//os:linux', '@platforms//cpu:armv7'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'armv7-unknown-linux-gnu-freethreaded': struct(arch='arm', compatible_with=['@platforms//os:linux', '@platforms//cpu:armv7'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'i386-unknown-linux-gnu': struct(arch='x86_32', compatible_with=['@platforms//os:linux', '@platforms//cpu:i386'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'i386-unknown-linux-gnu-freethreaded': struct(arch='x86_32', compatible_with=['@platforms//os:linux', '@platforms//cpu:i386'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'ppc64le-unknown-linux-gnu': struct(arch='ppc', compatible_with=['@platforms//os:linux', '@platforms//cpu:ppc'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'ppc64le-unknown-linux-gnu-freethreaded': struct(arch='ppc', compatible_with=['@platforms//os:linux', '@platforms//cpu:ppc'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'riscv64-unknown-linux-gnu': struct(arch='riscv64', compatible_with=['@platforms//os:linux', '@platforms//cpu:riscv64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'riscv64-unknown-linux-gnu-freethreaded': struct(arch='riscv64', compatible_with=['@platforms//os:linux', '@platforms//cpu:riscv64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 's390x-unknown-linux-gnu': struct(arch='s390x', compatible_with=['@platforms//os:linux', '@platforms//cpu:s390x'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 's390x-unknown-linux-gnu-freethreaded': struct(arch='s390x', compatible_with=['@platforms//os:linux', '@platforms//cpu:s390x'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'x86_64-apple-darwin': struct(arch='x86_64', compatible_with=['@platforms//os:macos', '@platforms//cpu:x86_64'], flag_values={}, os_name='osx', target_settings=['@@//python/config_settings:_is_py_freethreaded_no']), 'x86_64-apple-darwin-freethreaded': struct(arch='x86_64', compatible_with=['@platforms//os:macos', '@platforms//cpu:x86_64'], flag_values={}, os_name='osx', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes']), 'x86_64-pc-windows-msvc': struct(arch='x86_64', compatible_with=['@platforms//os:windows', '@platforms//cpu:x86_64'], flag_values={}, os_name='windows', target_settings=['@@//python/config_settings:_is_py_freethreaded_no']), 'x86_64-pc-windows-msvc-freethreaded': struct(arch='x86_64', compatible_with=['@platforms//os:windows', '@platforms//cpu:x86_64'], flag_values={}, os_name='windows', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes']), 'x86_64-unknown-linux-gnu': struct(arch='x86_64', compatible_with=['@platforms//os:linux', '@platforms//cpu:x86_64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'x86_64-unknown-linux-gnu-freethreaded': struct(arch='x86_64', compatible_with=['@platforms//os:linux', '@platforms//cpu:x86_64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_glibc']), 'x86_64-unknown-linux-musl': struct(arch='x86_64', compatible_with=['@platforms//os:linux', '@platforms//cpu:x86_64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_no', '@@//python/config_settings:_is_py_linux_libc_musl']), 'x86_64-unknown-linux-musl-freethreaded': struct(arch='x86_64', compatible_with=['@platforms//os:linux', '@platforms//cpu:x86_64'], flag_values={}, os_name='linux', target_settings=['@@//python/config_settings:_is_py_freethreaded_yes', '@@//python/config_settings:_is_py_linux_libc_musl'])}, minor_mapping=None, **kwargs)
Convenience macro for users which does typical setup.
With
bzlmodenabled, this function is not needed sincerules_pythonis handling everything. In order to override the default behaviour from the root module one can see the docs for thepythonextension.Create a repository for each built-in platform like “python_3_8_linux_amd64” - this repository is lazily fetched when Python is needed for that platform.
Create a repository exposing toolchains for each platform like “python_platforms”.
Register a toolchain pointing at each platform.
Users can avoid this macro and do these steps themselves, if they want more control.
- Args:
base name for all created repos, e.g. “python_3_8”.
python_version– (str)the Python version.
register_toolchains– (bool) (default True)Whether or not to register the downloaded toolchains.
register_coverage_tool– (bool) (default False)Whether or not to register the downloaded coverage tool to the toolchains.
set_python_version_constraint– (bool) (default False)When set to
True,target_compatible_withfor the toolchains will include a version constraint.tool_versions– (dict) (default None)contains a mapping of version with SHASUM and platform info. If not supplied, the defaults in python/versions.bzl will be used.
platforms– (dict[str,struct]) (default {“aarch64-apple-darwin”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:macos”, “@platforms//cpu:aarch64”], flag_values = {}, os_name = “osx”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”]), “aarch64-apple-darwin-freethreaded”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:macos”, “@platforms//cpu:aarch64”], flag_values = {}, os_name = “osx”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”]), “aarch64-pc-windows-msvc”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:windows”, “@platforms//cpu:aarch64”], flag_values = {}, os_name = “windows”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”]), “aarch64-pc-windows-msvc-freethreaded”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:windows”, “@platforms//cpu:aarch64”], flag_values = {}, os_name = “windows”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”]), “aarch64-unknown-linux-gnu”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:aarch64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “aarch64-unknown-linux-gnu-freethreaded”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:aarch64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “arm64e-apple-darwin”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:macos”, “@platforms//cpu:arm64e”], flag_values = {}, os_name = “osx”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”]), “arm64e-apple-darwin-freethreaded”: struct(arch = “aarch64”, compatible_with = [“@platforms//os:macos”, “@platforms//cpu:arm64e”], flag_values = {}, os_name = “osx”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”]), “armv7-unknown-linux-gnu”: struct(arch = “arm”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:armv7”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “armv7-unknown-linux-gnu-freethreaded”: struct(arch = “arm”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:armv7”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “i386-unknown-linux-gnu”: struct(arch = “x86_32”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:i386”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “i386-unknown-linux-gnu-freethreaded”: struct(arch = “x86_32”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:i386”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “ppc64le-unknown-linux-gnu”: struct(arch = “ppc”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:ppc”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “ppc64le-unknown-linux-gnu-freethreaded”: struct(arch = “ppc”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:ppc”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “riscv64-unknown-linux-gnu”: struct(arch = “riscv64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:riscv64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “riscv64-unknown-linux-gnu-freethreaded”: struct(arch = “riscv64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:riscv64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “s390x-unknown-linux-gnu”: struct(arch = “s390x”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:s390x”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “s390x-unknown-linux-gnu-freethreaded”: struct(arch = “s390x”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:s390x”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “x86_64-apple-darwin”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:macos”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “osx”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”]), “x86_64-apple-darwin-freethreaded”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:macos”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “osx”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”]), “x86_64-pc-windows-msvc”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:windows”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “windows”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”]), “x86_64-pc-windows-msvc-freethreaded”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:windows”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “windows”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”]), “x86_64-unknown-linux-gnu”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “x86_64-unknown-linux-gnu-freethreaded”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_glibc”]), “x86_64-unknown-linux-musl”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_no”, “@@//python/config_settings:_is_py_linux_libc_musl”]), “x86_64-unknown-linux-musl-freethreaded”: struct(arch = “x86_64”, compatible_with = [“@platforms//os:linux”, “@platforms//cpu:x86_64”], flag_values = {}, os_name = “linux”, target_settings = [“@@//python/config_settings:_is_py_freethreaded_yes”, “@@//python/config_settings:_is_py_linux_libc_musl”])})platforms to create toolchain repositories for. Keys are platform names, and values are platform_info structs. Note that only a subset is created, depending on what’s available in
tool_versions.minor_mapping– (dict[str,str]) (default None)contains a mapping from
X.YtoX.Y.Zversion.kwargs– passed to eachpython_repositorycall.
- Returns:
On workspace, returns None.
On bzlmod, returns a
dict[str, platform_info], which is the subset ofplatformsthat it created repositories for.
- repo rule python_repository(name, platform, python_version, release_filename, repo_mapping, sha256, auth_patterns={}, coverage_tool='', distutils=None, distutils_content='', ignore_root_user_error=True, netrc='', patch_strip=1, patches=[], strip_prefix='', url='', urls=[])
Fetches the external tools needed for the Python toolchain.
- Attributes:
A unique name for this repository.
mandatory
The platform name for the Python interpreter tarball.
mandatory
python_version– (str)The Python version.
mandatory
release_filename– (str)The filename of the interpreter to be downloaded
mandatory
repo_mapping– (dict[str,str])In
WORKSPACEcontext only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.For example, an entry
"@foo": "@bar"declares that, for any time this repository depends on@foo(such as a dependency on@foo//some:target, it should actually resolve that dependency within globally-declared@bar(@bar//some:target).This attribute is not supported in
MODULE.bazelcontext (when invoking a repository rule inside a module extension’s implementation function).optional
The SHA256 integrity hash for the Python interpreter tarball.
mandatory
auth_patterns– (dict[str,str]) (default {})Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive
optional
coverage_tool– (str) (default “”)This is a target to use for collecting code coverage information from
py_binaryandpy_testtargets.The target is accepted as a string by the python_repository and evaluated within the context of the toolchain repository.
For more information see
py_runtime.coverage_tool.optional
distutils– (label) (default None)A distutils.cfg file to be included in the Python installation. Either distutils or distutils_content can be specified, but not both.
optional
distutils_content– (str) (default “”)A distutils.cfg file content to be included in the Python installation. Either distutils or distutils_content can be specified, but not both.
optional
ignore_root_user_error– (bool) (default True)Noop, will be removed in the next major release
optional
.netrc file to use for authentication; mirrors the eponymous attribute from http_archive
optional
patch_strip– (int) (default 1)Same as the –strip argument of Unix patch.
Note
In the future the default value will be set to
0, to mimic the well known function defaults (e.g.single_version_overrideforMODULE.bazelfiles.Added in version 0.36.0.
optional
patches– (list[label]) (default [])A list of patch files to apply to the unpacked interpreter
optional
strip_prefix– (str) (default “”)A directory prefix to strip from the extracted files.
optional
The URL of the interpreter to download. Exactly one of url and urls must be set.
optional
urls– (list[str]) (default [])The URL of the interpreter to download. Exactly one of url and urls must be set.
optional
- Envvars:
RULES_PYTHON_REPO_DEBUG