diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 3b70734eff..7e9d4dea53 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -51,9 +51,11 @@ buildifier: test_flags: - "--noenable_bzlmod" - "--enable_workspace" + - "--test_tag_filters=-integration-test" build_flags: - "--noenable_bzlmod" - "--enable_workspace" + - "--build_tag_filters=-integration-test" bazel: 7.x .common_bazelinbazel_config: &common_bazelinbazel_config build_flags: diff --git a/.bazelignore b/.bazelignore index e10af2035d..fb999097f5 100644 --- a/.bazelignore +++ b/.bazelignore @@ -25,6 +25,7 @@ examples/pip_parse/bazel-pip_parse examples/pip_parse_vendored/bazel-pip_parse_vendored examples/pip_repository_annotations/bazel-pip_repository_annotations examples/py_proto_library/bazel-py_proto_library +gazelle/bazel-gazelle tests/integration/compile_pip_requirements/bazel-compile_pip_requirements tests/integration/ignore_root_user_error/bazel-ignore_root_user_error tests/integration/local_toolchains/bazel-local_toolchains diff --git a/.bazelrc b/.bazelrc index 4e6f2fa187..7e744fb67a 100644 --- a/.bazelrc +++ b/.bazelrc @@ -4,8 +4,8 @@ # (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it) # To update these lines, execute # `bazel run @rules_bazel_integration_test//tools:update_deleted_packages` -build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma -query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma +build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single +query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single test --test_output=errors diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..2737b0f184 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +# Set default charset +[*] +charset = utf-8 + +# Line width +[*] +max_line_length = 100 + +# 4 space indentation +[*.{py,bzl}] +indent_style = space +indent_size = 4 + +# different overrides for git commit messages +[.git/COMMIT_EDITMSG] +max_line_length = 72 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b451e89fa..67a02fc6c0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: - --profile - black - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 25.1.0 hooks: - id: black - repo: local diff --git a/.python-version b/.python-version new file mode 100644 index 0000000000..2c20ac9bea --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.13.3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1378853626..e48e3d4f3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,95 @@ BEGIN_UNRELEASED_TEMPLATE END_UNRELEASED_TEMPLATE --> +{#v0-0-0} +## Unreleased + +[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0 + +{#v0-0-0-changed} +### Changed + +* If using the (deprecated) autodetecting/runtime_env toolchain, then the Python + version specified at build-time *must* match the Python version used at + runtime (the {obj}`--@rules_python//python/config_settings:python_version` + flag and the {attr}`python_version` attribute control the build-time version + for a target). If they don't match, dependencies won't be importable. (Such a + misconfiguration was unlikely to work to begin with; this is called out as an + FYI). +* (rules) {obj}`--bootstrap_impl=script` is the default for non-Windows. +* (rules) On Windows, {obj}`--bootstrap_impl=system_python` is forced. This + allows setting `--bootstrap_impl=script` in bazelrc for mixed-platform + environments. +* (rules) {obj}`compile_pip_requirements` now generates a `.test` target. The + `_test` target is deprecated and will be removed in the next major release. + ([#2794](https://github.com/bazel-contrib/rules_python/issues/2794) +* (py_wheel) py_wheel always creates zip64-capable wheel zips +* (providers) (experimental) {obj}`PyInfo.venv_symlinks` replaces + `PyInfo.site_packages_symlinks` + +{#v0-0-0-fixed} +### Fixed + +* (rules) PyInfo provider is now advertised by py_test, py_binary, and py_library; + this allows aspects using required_providers to function correctly. + ([#2506](https://github.com/bazel-contrib/rules_python/issues/2506)). +* Fixes when using {obj}`--bootstrap_impl=script`: + * `compile_pip_requirements` now works with it + * The `sys._base_executable` value will reflect the underlying interpreter, + not venv interpreter. + * The {obj}`//python/runtime_env_toolchains:all` toolchain now works with it. +* (rules) Better handle flakey platform.win32_ver() calls by calling them + multiple times. +* (tools/wheelmaker.py) Extras are now preserved in Requires-Dist metadata when using requires_file + to specify the requirements. +* (pypi) Use bazel downloader for direct URL references and correctly detect the filenames from + various URL formats - URL encoded version strings get correctly resolved, sha256 value can be + also retrieved from the URL as opposed to only the `--hash` parameter. Fixes + [#2363](https://github.com/bazel-contrib/rules_python/issues/2363). +* (pypi) `whl_library` now infers file names from its `urls` attribute correctly. +* (pypi) When running under `bazel test`, be sure that temporary `requirements` file + remains writable. +* (py_test, py_binary) Allow external files to be used for main +* (pypi) Correctly aggregate the sources when the hashes specified in the lockfile differ + by platform even though the same version is used. Fixes [#2648](https://github.com/bazel-contrib/rules_python/issues/2648). +* (pypi) `compile_pip_requirements` test rule works behind the proxy + +{#v0-0-0-added} +### Added +* Repo utilities `execute_unchecked`, `execute_checked`, and `execute_checked_stdout` now + support `log_stdout` and `log_stderr` keyword arg booleans. When these are `True` + (the default), the subprocess's stdout/stderr will be logged. +* (toolchains) Local toolchains can be activated with custom flags. See + [Conditionally using local toolchains] docs for how to configure. +* (pypi) Starlark-based evaluation of environment markers (requirements.txt conditionals) + available (not enabled by default) for improved multi-platform build support. + Set the `RULES_PYTHON_ENABLE_PIPSTAR=1` environment variable to enable it. +* (utils) Add a way to run a REPL for any `rules_python` target that returns + a `PyInfo` provider. +* (uv) Handle `.netrc` and `auth_patterns` auth when downloading `uv`. Work towards + [#1975](https://github.com/bazel-contrib/rules_python/issues/1975). +* (toolchains) Arbitrary python-build-standalone runtimes can be registered + and activated with custom flags. See the [Registering custom runtimes] + docs and {obj}`single_version_platform_override()` API docs for more + information. +* (rules) Added support for a using constraints files with `compile_pip_requirements`. + Useful when an intermediate dependency needs to be upgraded to pull in + security patches. + +{#v0-0-0-removed} +### Removed +* Nothing removed. + +{#1-4-1} +## [1.4.1] - 2025-05-08 + +[1.4.1]: https://github.com/bazel-contrib/rules_python/releases/tag/1.4.1 + +{#1-4-1-fixed} +### Fixed +* (pypi) Fix a typo not allowing users to benefit from using the downloader when the hashes in the + requirements file are not present. Fixes + [#2863](https://github.com/bazel-contrib/rules_python/issues/2863). {#1-4-0} ## [1.4.0] - 2025-04-19 @@ -66,8 +155,6 @@ END_UNRELEASED_TEMPLATE * 3.12.9 * 3.13.2 * (pypi) Use `xcrun xcodebuild --showsdks` to find XCode root. -* (pypi) The `bzlmod` extension will now generate smaller lock files for when - using `experimental_index_url`. * (toolchains) Remove all but `3.8.20` versions of the Python `3.8` interpreter who has reached EOL. If users still need other versions of the `3.8` interpreter, please supply the URLs manually {bzl:obj}`python.toolchain` or {bzl:obj}`python_register_toolchains` calls. @@ -104,6 +191,13 @@ END_UNRELEASED_TEMPLATE * (packaging) An empty `requires_file` is treated as if it were omitted, resulting in a valid `METADATA` file. * (rules) py_wheel and sphinxdocs rules now propagate `target_compatible_with` to all targets they create. [PR #2788](https://github.com/bazel-contrib/rules_python/pull/2788). +* (pypi) Correctly handle `METADATA` entries when `python_full_version` is used in + the environment marker. + Fixes [#2319](https://github.com/bazel-contrib/rules_python/issues/2319). +* (pypi) Correctly handle `python_version` parameter and transition the requirement + locking to the right interpreter version when using + {obj}`compile_pip_requirements` rule. + See [#2819](https://github.com/bazel-contrib/rules_python/pull/2819). {#1-4-0-added} ### Added @@ -113,7 +207,7 @@ END_UNRELEASED_TEMPLATE packages through SimpleAPI unless they are pulled through direct URL references. Fixes [#2023](https://github.com/bazel-contrib/rules_python/issues/2023). In case you see issues with `rules_python` being too eager to fetch the SimpleAPI - metadata, you can use the newly added {attr}`pip.parse.experimental_skip_sources` + metadata, you can use the newly added {attr}`pip.parse.simpleapi_skip` to skip metadata fetching for those packages. * (uv) A {obj}`lock` rule that is the replacement for the {obj}`compile_pip_requirements`. This may still have rough corners @@ -125,7 +219,7 @@ END_UNRELEASED_TEMPLATE please check the {obj}`uv.configure` tag class. * Add support for riscv64 linux platform. * (toolchains) Add python 3.13.2 and 3.12.9 toolchains -* (providers) (experimental) {obj}`PyInfo.site_packages_symlinks` field added to +* (providers) (experimental) `PyInfo.site_packages_symlinks` field added to allow specifying links to create within the venv site packages (only applicable with {obj}`--bootstrap_impl=script`) ([#2156](https://github.com/bazelbuild/rules_python/issues/2156)). @@ -174,7 +268,7 @@ END_UNRELEASED_TEMPLATE {#v1-3-0-added} ### Added -* (python) {attr}`python.defaults` has been added to allow users to +* (python) {obj}`python.defaults` has been added to allow users to set the default python version in the root module by reading the default version number from a file or an environment variable. * {obj}`//python/bin:python`: convenience target for directly running an @@ -194,7 +288,7 @@ END_UNRELEASED_TEMPLATE and py_library rules ([#1647](https://github.com/bazel-contrib/rules_python/issues/1647)) * (rules) Added env-var to allow additional interpreter args for stage1 bootstrap. - See {obj}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable. + See {any}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable. Only applicable for {obj}`--bootstrap_impl=script`. * (rules) Added {obj}`interpreter_args` attribute to `py_binary` and `py_test`, which allows pass arguments to the interpreter before the regular args. @@ -300,7 +394,7 @@ END_UNRELEASED_TEMPLATE values. Fixes [#2466](https://github.com/bazel-contrib/rules_python/issues/2466). * (py_proto_library) Fix import paths in Bazel 8. * (whl_library) Now the changes to the dependencies are correctly tracked when - PyPI packages used in {bzl:obj}`whl_library` during the `repository_rule` phase + PyPI packages used in `whl_library` during the repository rule phase change. Fixes [#2468](https://github.com/bazel-contrib/rules_python/issues/2468). + (gazelle) Gazelle no longer ignores `setup.py` files by default. To restore this behavior, apply the `# gazelle:python_ignore_files setup.py` directive. @@ -319,7 +413,7 @@ END_UNRELEASED_TEMPLATE * (pypi) Freethreaded packages are now fully supported in the {obj}`experimental_index_url` usage or the regular `pip.parse` usage. To select the free-threaded interpreter in the repo phase, please use - the documented [env](/environment-variables.html) variables. + the documented [env](environment-variables) variables. Fixes [#2386](https://github.com/bazel-contrib/rules_python/issues/2386). * (toolchains) Use the latest astrahl-sh toolchain release [20241206] for Python versions: * 3.9.21 @@ -413,7 +507,7 @@ Other changes: for the latest toolchain versions for each minor Python version. You can control the toolchain selection by using the {bzl:obj}`//python/config_settings:py_linux_libc` build flag. -* (providers) Added {obj}`py_runtime_info.site_init_template` and +* (providers) Added {obj}`PyRuntimeInfo.site_init_template` and {obj}`PyRuntimeInfo.site_init_template` for specifying the template to use to initialize the interpreter via venv startup hooks. * (runfiles) (Bazel 7.4+) Added support for spaces and newlines in runfiles paths @@ -611,8 +705,8 @@ Other changes: * (bzlmod) The default value for the {obj}`--python_version` flag will now be always set to the default python toolchain version value. * (bzlmod) correctly wire the {attr}`pip.parse.extra_pip_args` all the - way to {obj}`whl_library`. What is more we will pass the `extra_pip_args` to - {obj}`whl_library` for `sdist` distributions when using + way to `whl_library`. What is more we will pass the `extra_pip_args` to + `whl_library` for `sdist` distributions when using {attr}`pip.parse.experimental_index_url`. See [#2239](https://github.com/bazel-contrib/rules_python/issues/2239). * (whl_filegroup): Provide per default also the `RECORD` file @@ -660,8 +754,8 @@ Other changes: {#v0-37-0-removed} ### Removed -* (precompiling) {obj}`--precompile_add_to_runfiles` has been removed. -* (precompiling) {obj}`--pyc_collection` has been removed. The `pyc_collection` +* (precompiling) `--precompile_add_to_runfiles` has been removed. +* (precompiling) `--pyc_collection` has been removed. The `pyc_collection` attribute now bases its default on {obj}`--precompile`. * (precompiling) The {obj}`precompile=if_generated_source` value has been removed. * (precompiling) The {obj}`precompile_source_retention=omit_if_generated_source` value has been removed. @@ -713,7 +807,7 @@ Other changes: in extra_requires in py_wheel rule. * (rules) Prevent pytest from trying run the generated stage2 bootstrap .py file when using {obj}`--bootstrap_impl=script` -* (toolchain) The {bzl:obj}`gen_python_config_settings` has been fixed to include +* (toolchain) The `gen_python_config_settings` has been fixed to include the flag_values from the platform definitions. {#v0-36-0-added} @@ -1128,9 +1222,9 @@ Other changes: depend on legacy labels instead of the hub repo aliases and you use the `experimental_requirement_cycles`, now is a good time to migrate. -[python_default_visibility]: gazelle/README.md#directive-python_default_visibility +[python_default_visibility]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_default_visibility [test_file_pattern_issue]: https://github.com/bazel-contrib/rules_python/issues/1816 -[test_file_pattern_docs]: gazelle/README.md#directive-python_test_file_pattern +[test_file_pattern_docs]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_test_file_pattern [20240224]: https://github.com/indygreg/python-build-standalone/releases/tag/20240224. [20240415]: https://github.com/indygreg/python-build-standalone/releases/tag/20240415. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 17558e1b23..324801cfc3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -68,7 +68,7 @@ to the actual rules_python project and begin the code review process. ## Developer guide For more more details, guidance, and tips for working with the code base, -see [DEVELOPING.md](DEVELOPING.md) +see [docs/devguide.md](./devguide) ## Formatting @@ -173,6 +173,55 @@ The `legacy_foo` arg was removed ::: ``` +## Style and idioms + +For the most part, we just accept whatever the code formatters do, so there +isn't much style to enforce. + +Some miscellanous style, idioms, and conventions we have are: + +### Markdown/Sphinx Style + +* Use colons for prose sections of text, e.g. `:::{note}`, not backticks. +* Use backticks for code blocks. +* Max line length: 100. + +### BUILD/bzl Style + +* When a macro generates public targets, use a dot (`.`) to separate the + user-provided name from the generted name. e.g. `foo(name="x")` generates + `x.test`. The `.` is our convention to communicate that it's a generated + target, and thus one should look for `name="x"` when searching for the + definition. +* The different build phases shouldn't load code that defines objects that + aren't valid for their phase. e.g. + * The bzlmod phase shouldn't load code defining regular rules or providers. + * The repository phase shouldn't load code defining module extensions, regular + rules, or providers. + * The loading phase shouldn't load code defining module extensions or + repository rules. + * Loading utility libraries or generic code is OK, but should strive to load + code that is usable for its phase. e.g. loading-phase code shouldn't + load utility code that is predominately only usable to the bzlmod phase. +* Providers should be in their own files. This allows implementing a custom rule + that implements the provider without loading a specific implementation. +* One rule per file is preferred, but not required. The goal is that defining an + e.g. library shouldn't incur loading all the code for binaries, tests, + packaging, etc; things that may be niche or uncommonly used. +* Separate files should be used to expose public APIs. This ensures our public + API is well defined and prevents accidentally exposing a package-private + symbol as a public symbol. + + :::{note} + The public API file's docstring becomes part of the user-facing docs. That + file's docstring must be used for module-level API documentation. + ::: +* Repository rules should have name ending in `_repo`. This helps distinguish + them from regular rules. +* Each bzlmod extension, the "X" of `use_repo("//foo:foo.bzl", "X")` should be + in its own file. The path given in the `use_repo()` expression is the identity + Bazel uses and cannot be changed. + ## Generated files Some checked-in files are generated and need to be updated when a new PR is diff --git a/MODULE.bazel b/MODULE.bazel index c649896344..144e130c1b 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -98,7 +98,11 @@ internal_dev_deps = use_extension( "internal_dev_deps", dev_dependency = True, ) -use_repo(internal_dev_deps, "buildkite_config", "wheel_for_testing") +use_repo( + internal_dev_deps, + "buildkite_config", + "rules_python_runtime_env_tc_info", +) # Add gazelle plugin so that we can run the gazelle example as an e2e integration # test and include the distribution files. @@ -121,6 +125,22 @@ dev_python.override( register_all_versions = True, ) +# For testing an arbitrary runtime triggered by a custom flag. +# See //tests/toolchains:custom_platform_toolchain_test +dev_python.single_version_platform_override( + platform = "linux-x86-install-only-stripped", + python_version = "3.13.1", + sha256 = "56817aa976e4886bec1677699c136cb01c1cdfe0495104c0d8ef546541864bbb", + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + target_settings = [ + "@@//tests/support:is_custom_runtime_linux-x86-install-only-stripped", + ], + urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250115/cpython-3.13.1+20250115-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"], +) + dev_pip = use_extension( "//python/extensions:pip.bzl", "pip", @@ -130,6 +150,7 @@ dev_pip.parse( download_only = True, experimental_index_url = "https://pypi.org/simple", hub_name = "dev_pip", + parallel_download = False, python_version = "3.11", requirements_lock = "//docs:requirements.txt", ) diff --git a/WORKSPACE b/WORKSPACE index 3ad83ca04b..dddc5105ed 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -78,7 +78,7 @@ python_register_multi_toolchains( python_versions = PYTHON_VERSIONS, ) -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # Used for Bazel CI http_archive( @@ -155,14 +155,3 @@ pip_parse( load("@dev_pip//:requirements.bzl", docs_install_deps = "install_deps") docs_install_deps() - -# This wheel is purely here to validate the wheel extraction code. It's not -# intended for anything else. -http_file( - name = "wheel_for_testing", - downloaded_file_path = "numpy-1.25.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", - sha256 = "0d60fbae8e0019865fc4784745814cff1c421df5afee233db6d88ab4f14655a2", - urls = [ - "https://files.pythonhosted.org/packages/50/67/3e966d99a07d60a21a21d7ec016e9e4c2642a86fea251ec68677daf71d4d/numpy-1.25.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", - ], -) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 25da682012..852c4d4fa6 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -113,13 +113,17 @@ sphinx_stardocs( "//python/private:builders_util_bzl", "//python/private:py_binary_rule_bzl", "//python/private:py_cc_toolchain_rule_bzl", + "//python/private:py_info_bzl", "//python/private:py_library_rule_bzl", "//python/private:py_runtime_rule_bzl", "//python/private:py_test_rule_bzl", "//python/private:rule_builders_bzl", "//python/private/api:py_common_api_bzl", "//python/private/pypi:config_settings_bzl", + "//python/private/pypi:env_marker_info_bzl", "//python/private/pypi:pkg_aliases_bzl", + "//python/private/pypi:whl_config_setting_bzl", + "//python/private/pypi:whl_library_bzl", "//python/uv:lock_bzl", "//python/uv:uv_bzl", "//python/uv:uv_toolchain_bzl", diff --git a/docs/_includes/py_console_script_binary.md b/docs/_includes/py_console_script_binary.md index aa356e0e94..d327091630 100644 --- a/docs/_includes/py_console_script_binary.md +++ b/docs/_includes/py_console_script_binary.md @@ -48,6 +48,26 @@ py_console_script_binary( ) ``` +#### Adding a Shebang Line + +You can specify a shebang line for the generated binary, useful for Unix-like +systems where the shebang line determines which interpreter is used to execute +the script, per [PEP441]: + +```starlark +load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary") + +py_console_script_binary( + name = "black", + pkg = "@pip//black", + shebang = "#!/usr/bin/env python3", +) +``` + +Note that to execute via the shebang line, you need to ensure the specified +Python interpreter is available in the environment. + + #### Using a specific Python Version directly from a Toolchain :::{deprecated} 1.1.0 The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules. @@ -70,4 +90,5 @@ py_console_script_binary( ``` [specification]: https://packaging.python.org/en/latest/specifications/entry-points/ -[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule \ No newline at end of file +[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule +[PEP441]: https://peps.python.org/pep-0441/#minimal-tooling-the-zipapp-module diff --git a/docs/api/rules_python/python/bin/index.md b/docs/api/rules_python/python/bin/index.md index 8bea6b54bd..873b644341 100644 --- a/docs/api/rules_python/python/bin/index.md +++ b/docs/api/rules_python/python/bin/index.md @@ -10,7 +10,8 @@ A target to directly run a Python interpreter. By default, it uses the Python version that toolchain resolution matches -(typically the one marked `is_default=True` in `MODULE.bazel`). +(typically the one set with `python.defaults(python_version = ...)` in +`MODULE.bazel`). This runs a Python interpreter in a similar manner as when running `python3` on the command line. It can be invoked using `bazel run`. Remember that in diff --git a/docs/api/rules_python/python/config_settings/index.md b/docs/api/rules_python/python/config_settings/index.md index ed6444298e..ae84d40b13 100644 --- a/docs/api/rules_python/python/config_settings/index.md +++ b/docs/api/rules_python/python/config_settings/index.md @@ -159,6 +159,18 @@ Values: ::: :::: +::::{bzl:flag} pip_env_marker_config +The target that provides the values for pip env marker evaluation. + +Default: `//python/config_settings:_pip_env_marker_default_config` + +This flag points to a target providing {obj}`EnvMarkerInfo`, which determines +the values used when environment markers are resolved at build time. + +:::{versionadded} VERSION_NEXT_FEATURE +::: +:::: + ::::{bzl:flag} pip_whl Set what distributions are used in the `pip` integration. @@ -233,6 +245,10 @@ Values: ::::{bzl:flag} bootstrap_impl Determine how programs implement their startup process. +The default for this depends on the platform: +* Windows: `system_python` (**always** used) +* Other: `script` + Values: * `system_python`: Use a bootstrap that requires a system Python available in order to start programs. This requires @@ -257,6 +273,11 @@ instead. :::{versionadded} 0.33.0 ::: +:::{versionchanged} VERSION_NEXT_FEATURE +* The default for non-Windows changed from `system_python` to `script`. +* On Windows, the value is forced to `system_python`. +::: + :::: ::::{bzl:flag} current_config diff --git a/docs/conf.py b/docs/conf.py index f58baf5183..8537d9996c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -87,10 +87,12 @@ "api/sphinxdocs/sphinx": "/api/sphinxdocs/sphinxdocs/sphinx.html", "api/sphinxdocs/sphinx_stardoc": "/api/sphinxdocs/sphinxdocs/sphinx_stardoc.html", "api/sphinxdocs/readthedocs": "/api/sphinxdocs/sphinxdocs/readthedocs.html", - "api/sphinxdocs/index": "/api/sphinxdocs/sphinxdocs/index.html", + "api/sphinxdocs/index": "sphinxdocs/index.html", "api/sphinxdocs/private/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/private/sphinx_docs_library.html", "api/sphinxdocs/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/sphinx_docs_library.html", "api/sphinxdocs/inventories/index": "/api/sphinxdocs/sphinxdocs/inventories/index.html", + "pip.html": "pypi/index.html", + "pypi-dependencies.html": "pypi/index.html", } # Adapted from the template code: @@ -125,6 +127,12 @@ primary_domain = None # The default is 'py', which we don't make much use of nitpicky = True +nitpick_ignore_regex = [ + # External xrefs aren't setup: ignore missing xref warnings + # External xrefs to sphinx isn't setup: ignore missing xref warnings + ("py:.*", "(sphinx|docutils|ast|enum|collections|typing_extensions).*"), +] + # --- Intersphinx configuration intersphinx_mapping = { @@ -133,7 +141,9 @@ # --- Extlinks configuration extlinks = { + "gh-issue": (f"https://github.com/bazel-contrib/rules_python/issues/%s", "#%s issue"), "gh-path": (f"https://github.com/bazel-contrib/rules_python/tree/main/%s", "%s"), + "gh-pr": (f"https://github.com/bazel-contrib/rules_python/pulls/%s", "#%s PR"), } # --- MyST configuration diff --git a/DEVELOPING.md b/docs/devguide.md similarity index 96% rename from DEVELOPING.md rename to docs/devguide.md index 83026c1dbc..f233611cad 100644 --- a/DEVELOPING.md +++ b/docs/devguide.md @@ -1,4 +1,4 @@ -# For Developers +# Dev Guide This document covers tips and guidance for working on the rules_python code base. A primary audience for it is first time contributors. @@ -39,7 +39,7 @@ more important for tests to balance understandability and maintainability. ### sh_py_run_test -The [`sh_py_run_test`](tests/support/sh_py_run_test.bzl) rule is a helper to +The {gh-path}`sh_py_run_test ` for any behaviour differences :hidden: self getting-started -pypi-dependencies +pypi/index Toolchains -pip coverage precompiling gazelle +REPL Extending Contributing +devguide support Changelog api/index diff --git a/docs/pip.md b/docs/pip.md deleted file mode 100644 index 43d8fc4978..0000000000 --- a/docs/pip.md +++ /dev/null @@ -1,4 +0,0 @@ -(pip-integration)= -# Pip Integration - -See [PyPI dependencies](./pypi-dependencies). diff --git a/docs/pypi-dependencies.md b/docs/pypi-dependencies.md deleted file mode 100644 index 6cc0da6cb4..0000000000 --- a/docs/pypi-dependencies.md +++ /dev/null @@ -1,458 +0,0 @@ -:::{default-domain} bzl -::: - -# Using dependencies from PyPI - -Using PyPI packages (aka "pip install") involves two main steps. - -1. [Installing third party packages](#installing-third-party-packages) -2. [Using third party packages as dependencies](#using-third-party-packages) - -{#installing-third-party-packages} -## Installing third party packages - -### Using bzlmod - -To add pip dependencies to your `MODULE.bazel` file, use the `pip.parse` -extension, and call it to create the central external repo and individual wheel -external repos. Include in the `MODULE.bazel` the toolchain extension as shown -in the first bzlmod example above. - -```starlark -pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") -pip.parse( - hub_name = "my_deps", - python_version = "3.11", - requirements_lock = "//:requirements_lock_3_11.txt", -) -use_repo(pip, "my_deps") -``` -For more documentation, including how the rules can update/create a requirements -file, see the bzlmod examples under the {gh-path}`examples` folder or the documentation -for the {obj}`@rules_python//python/extensions:pip.bzl` extension. - -```{note} -We are using a host-platform compatible toolchain by default to setup pip dependencies. -During the setup phase, we create some symlinks, which may be inefficient on Windows -by default. In that case use the following `.bazelrc` options to improve performance if -you have admin privileges: - - startup --windows_enable_symlinks - -This will enable symlinks on Windows and help with bootstrap performance of setting up the -hermetic host python interpreter on this platform. Linux and OSX users should see no -difference. -``` - -### Using a WORKSPACE file - -To add pip dependencies to your `WORKSPACE`, load the `pip_parse` function and -call it to create the central external repo and individual wheel external repos. - -```starlark -load("@rules_python//python:pip.bzl", "pip_parse") - -# Create a central repo that knows about the dependencies needed from -# requirements_lock.txt. -pip_parse( - name = "my_deps", - requirements_lock = "//path/to:requirements_lock.txt", -) -# Load the starlark macro, which will define your dependencies. -load("@my_deps//:requirements.bzl", "install_deps") -# Call it to define repos for your requirements. -install_deps() -``` - -(vendoring-requirements)= -#### Vendoring the requirements.bzl file - -In some cases you may not want to generate the requirements.bzl file as a repository rule -while Bazel is fetching dependencies. For example, if you produce a reusable Bazel module -such as a ruleset, you may want to include the requirements.bzl file rather than make your users -install the WORKSPACE setup to generate it. -See https://github.com/bazel-contrib/rules_python/issues/608 - -This is the same workflow as Gazelle, which creates `go_repository` rules with -[`update-repos`](https://github.com/bazelbuild/bazel-gazelle#update-repos) - -To do this, use the "write to source file" pattern documented in -https://blog.aspect.dev/bazel-can-write-to-the-source-folder -to put a copy of the generated requirements.bzl into your project. -Then load the requirements.bzl file directly rather than from the generated repository. -See the example in rules_python/examples/pip_parse_vendored. - -(per-os-arch-requirements)= -### Requirements for a specific OS/Architecture - -In some cases you may need to use different requirements files for different OS, Arch combinations. This is enabled via the `requirements_by_platform` attribute in `pip.parse` extension and the `pip_parse` repository rule. The keys of the dictionary are labels to the file and the values are a list of comma separated target (os, arch) tuples. - -For example: -```starlark - # ... - requirements_by_platform = { - "requirements_linux_x86_64.txt": "linux_x86_64", - "requirements_osx.txt": "osx_*", - "requirements_linux_exotic.txt": "linux_exotic", - "requirements_some_platforms.txt": "linux_aarch64,windows_*", - }, - # For the list of standard platforms that the rules_python has toolchains for, default to - # the following requirements file. - requirements_lock = "requirements_lock.txt", -``` - -In case of duplicate platforms, `rules_python` will raise an error as there has -to be unambiguous mapping of the requirement files to the (os, arch) tuples. - -An alternative way is to use per-OS requirement attributes. -```starlark - # ... - requirements_windows = "requirements_windows.txt", - requirements_darwin = "requirements_darwin.txt", - # For the remaining platforms (which is basically only linux OS), use this file. - requirements_lock = "requirements_lock.txt", -) -``` - -### pip rules - -Note that since `pip_parse` and `pip.parse` are executed at evaluation time, -Bazel has no information about the Python toolchain and cannot enforce that the -interpreter used to invoke `pip` matches the interpreter used to run -`py_binary` targets. By default, `pip_parse` uses the system command -`"python3"`. To override this, pass in the `python_interpreter` attribute or -`python_interpreter_target` attribute to `pip_parse`. The `pip.parse` `bzlmod` extension -by default uses the hermetic python toolchain for the host platform. - -You can have multiple `pip_parse`s in the same workspace, or use the pip -extension multiple times when using bzlmod. This configuration will create -multiple external repos that have no relation to one another and may result in -downloading the same wheels numerous times. - -As with any repository rule, if you would like to ensure that `pip_parse` is -re-executed to pick up a non-hermetic change to your environment (e.g., updating -your system `python` interpreter), you can force it to re-execute by running -`bazel sync --only [pip_parse name]`. - -{#using-third-party-packages} -## Using third party packages as dependencies - -Each extracted wheel repo contains a `py_library` target representing -the wheel's contents. There are two ways to access this library. The -first uses the `requirement()` function defined in the central -repo's `//:requirements.bzl` file. This function maps a pip package -name to a label: - -```starlark -load("@my_deps//:requirements.bzl", "requirement") - -py_library( - name = "mylib", - srcs = ["mylib.py"], - deps = [ - ":myotherlib", - requirement("some_pip_dep"), - requirement("another_pip_dep"), - ] -) -``` - -The reason `requirement()` exists is to insulate from -changes to the underlying repository and label strings. However, those -labels have become directly used, so aren't able to easily change regardless. - -On the other hand, using `requirement()` has several drawbacks; see -[this issue][requirements-drawbacks] for an enumeration. If you don't -want to use `requirement()`, you can use the library -labels directly instead. For `pip_parse`, the labels are of the following form: - -```starlark -@{name}//{package} -``` - -Here `name` is the `name` attribute that was passed to `pip_parse` and -`package` is the pip package name with characters that are illegal in -Bazel label names (e.g. `-`, `.`) replaced with `_`. If you need to -update `name` from "old" to "new", then you can run the following -buildozer command: - -```shell -buildozer 'substitute deps @old//([^/]+) @new//${1}' //...:* -``` - -[requirements-drawbacks]: https://github.com/bazel-contrib/rules_python/issues/414 - -### Entry points - -If you would like to access [entry points][whl_ep], see the `py_console_script_binary` rule documentation, -which can help you create a `py_binary` target for a particular console script exposed by a package. - -[whl_ep]: https://packaging.python.org/specifications/entry-points/ - -### 'Extras' dependencies - -Any 'extras' specified in the requirements lock file will be automatically added -as transitive dependencies of the package. In the example above, you'd just put -`requirement("useful_dep")` or `@pypi//useful_dep`. - -### Consuming Wheel Dists Directly - -If you need to depend on the wheel dists themselves, for instance, to pass them -to some other packaging tool, you can get a handle to them with the -`whl_requirement` macro. For example: - -```starlark -load("@pypi//:requirements.bzl", "whl_requirement") - -filegroup( - name = "whl_files", - data = [ - # This is equivalent to "@pypi//boto3:whl" - whl_requirement("boto3"), - ] -) -``` - -### Creating a filegroup of files within a whl - -The rule {obj}`whl_filegroup` exists as an easy way to extract the necessary files -from a whl file without the need to modify the `BUILD.bazel` contents of the -whl repositories generated via `pip_repository`. Use it similarly to the `filegroup` -above. See the API docs for more information. - -(advance-topics)= -## Advanced topics - -(circular-deps)= -### Circular dependencies - -Sometimes PyPi packages contain dependency cycles -- for instance a particular -version `sphinx` (this is no longer the case in the latest version as of -2024-06-02) depends on `sphinxcontrib-serializinghtml`. When using them as -`requirement()`s, ala - -``` -py_binary( - name = "doctool", - ... - deps = [ - requirement("sphinx"), - ], -) -``` - -Bazel will protest because it doesn't support cycles in the build graph -- - -``` -ERROR: .../external/pypi_sphinxcontrib_serializinghtml/BUILD.bazel:44:6: in alias rule @pypi_sphinxcontrib_serializinghtml//:pkg: cycle in dependency graph: - //:doctool (...) - @pypi//sphinxcontrib_serializinghtml:pkg (...) -.-> @pypi_sphinxcontrib_serializinghtml//:pkg (...) -| @pypi_sphinxcontrib_serializinghtml//:_pkg (...) -| @pypi_sphinx//:pkg (...) -| @pypi_sphinx//:_pkg (...) -`-- @pypi_sphinxcontrib_serializinghtml//:pkg (...) -``` - -The `experimental_requirement_cycles` argument allows you to work around these -issues by specifying groups of packages which form cycles. `pip_parse` will -transparently fix the cycles for you and provide the cyclic dependencies -simultaneously. - -```starlark -pip_parse( - ... - experimental_requirement_cycles = { - "sphinx": [ - "sphinx", - "sphinxcontrib-serializinghtml", - ] - }, -) -``` - -`pip_parse` supports fixing multiple cycles simultaneously, however cycles must -be distinct. `apache-airflow` for instance has dependency cycles with a number -of its optional dependencies, which means those optional dependencies must all -be a part of the `airflow` cycle. For instance -- - -```starlark -pip_parse( - ... - experimental_requirement_cycles = { - "airflow": [ - "apache-airflow", - "apache-airflow-providers-common-sql", - "apache-airflow-providers-postgres", - "apache-airflow-providers-sqlite", - ] - } -) -``` - -Alternatively, one could resolve the cycle by removing one leg of it. - -For example while `apache-airflow-providers-sqlite` is "baked into" the Airflow -package, `apache-airflow-providers-postgres` is not and is an optional feature. -Rather than listing `apache-airflow[postgres]` in your `requirements.txt` which -would expose a cycle via the extra, one could either _manually_ depend on -`apache-airflow` and `apache-airflow-providers-postgres` separately as -requirements. Bazel rules which need only `apache-airflow` can take it as a -dependency, and rules which explicitly want to mix in -`apache-airflow-providers-postgres` now can. - -Alternatively, one could use `rules_python`'s patching features to remove one -leg of the dependency manually. For instance by making -`apache-airflow-providers-postgres` not explicitly depend on `apache-airflow` or -perhaps `apache-airflow-providers-common-sql`. - - -(bazel-downloader)= -### Multi-platform support - -Multi-platform support of cross-building the wheels can be done in two ways - either -using {bzl:attr}`experimental_index_url` for the {bzl:obj}`pip.parse` bzlmod tag class -or by using the {bzl:attr}`pip.parse.download_only` setting. In this section we -are going to outline quickly how one can use the latter option. - -Let's say you have 2 requirements files: -``` -# requirements.linux_x86_64.txt ---platform=manylinux_2_17_x86_64 ---python-version=39 ---implementation=cp ---abi=cp39 - -foo==0.0.1 --hash=sha256:deadbeef -bar==0.0.1 --hash=sha256:deadb00f -``` - -``` -# requirements.osx_aarch64.txt contents ---platform=macosx_10_9_arm64 ---python-version=39 ---implementation=cp ---abi=cp39 - -foo==0.0.3 --hash=sha256:deadbaaf -``` - -With these 2 files your {bzl:obj}`pip.parse` could look like: -``` -pip.parse( - hub_name = "pip", - python_version = "3.9", - # Tell `pip` to ignore sdists - download_only = True, - requirements_by_platform = { - "requirements.linux_x86_64.txt": "linux_x86_64", - "requirements.osx_aarch64.txt": "osx_aarch64", - }, -) -``` - -With this, the `pip.parse` will create a hub repository that is going to -support only two platforms - `cp39_osx_aarch64` and `cp39_linux_x86_64` and it -will only use `wheels` and ignore any sdists that it may find on the PyPI -compatible indexes. - -```{note} -This is only supported on `bzlmd`. -``` - -(bazel-downloader)= -### Bazel downloader and multi-platform wheel hub repository. - -The `bzlmod` `pip.parse` call supports pulling information from `PyPI` (or a -compatible mirror) and it will ensure that the [bazel -downloader][bazel_downloader] is used for downloading the wheels. This allows -the users to use the [credential helper](#credential-helper) to authenticate -with the mirror and it also ensures that the distribution downloads are cached. -It also avoids using `pip` altogether and results in much faster dependency -fetching. - -This can be enabled by `experimental_index_url` and related flags as shown in -the {gh-path}`examples/bzlmod/MODULE.bazel` example. - -When using this feature during the `pip` extension evaluation you will see the accessed indexes similar to below: -```console -Loading: 0 packages loaded - currently loading: docs/ - Fetching module extension pip in @@//python/extensions:pip.bzl; starting - Fetching https://pypi.org/simple/twine/ -``` - -This does not mean that `rules_python` is fetching the wheels eagerly, but it -rather means that it is calling the PyPI server to get the Simple API response -to get the list of all available source and wheel distributions. Once it has -got all of the available distributions, it will select the right ones depending -on the `sha256` values in your `requirements_lock.txt` file. If `sha256` hashes -are not present in the requirements file, we will fallback to matching by version -specified in the lock file. The compatible distribution URLs will be then -written to the `MODULE.bazel.lock` file. Currently users wishing to use the -lock file with `rules_python` with this feature have to set an environment -variable `RULES_PYTHON_OS_ARCH_LOCK_FILE=0` which will become default in the -next release. - -Fetching the distribution information from the PyPI allows `rules_python` to -know which `whl` should be used on which target platform and it will determine -that by parsing the `whl` filename based on [PEP600], [PEP656] standards. This -allows the user to configure the behaviour by using the following publicly -available flags: -* {obj}`--@rules_python//python/config_settings:py_linux_libc` for selecting the Linux libc variant. -* {obj}`--@rules_python//python/config_settings:pip_whl` for selecting `whl` distribution preference. -* {obj}`--@rules_python//python/config_settings:pip_whl_osx_arch` for selecting MacOS wheel preference. -* {obj}`--@rules_python//python/config_settings:pip_whl_glibc_version` for selecting the GLIBC version compatibility. -* {obj}`--@rules_python//python/config_settings:pip_whl_muslc_version` for selecting the musl version compatibility. -* {obj}`--@rules_python//python/config_settings:pip_whl_osx_version` for selecting MacOS version compatibility. - -[bazel_downloader]: https://bazel.build/rules/lib/builtins/repository_ctx#download -[pep600]: https://peps.python.org/pep-0600/ -[pep656]: https://peps.python.org/pep-0656/ - -(credential-helper)= -### Credential Helper - -The "use Bazel downloader for python wheels" experimental feature includes support for the Bazel -[Credential Helper][cred-helper-design]. - -Your python artifact registry may provide a credential helper for you. Refer to your index's docs -to see if one is provided. - -See the [Credential Helper Spec][cred-helper-spec] for details. - -[cred-helper-design]: https://github.com/bazelbuild/proposals/blob/main/designs/2022-06-07-bazel-credential-helpers.md -[cred-helper-spec]: https://github.com/EngFlow/credential-helper-spec/blob/main/spec.md - - -#### Basic Example: - -The simplest form of a credential helper is a bash script that accepts an arg and spits out JSON to -stdout. For a service like Google Artifact Registry that uses ['Basic' HTTP Auth][rfc7617] and does -not provide a credential helper that conforms to the [spec][cred-helper-spec], the script might -look like: - -```bash -#!/bin/bash -# cred_helper.sh -ARG=$1 # but we don't do anything with it as it's always "get" - -# formatting is optional -echo '{' -echo ' "headers": {' -echo ' "Authorization": ["Basic dGVzdDoxMjPCow=="]' -echo ' }' -echo '}' -``` - -Configure Bazel to use this credential helper for your python index `example.com`: - -``` -# .bazelrc -build --credential_helper=example.com=/full/path/to/cred_helper.sh -``` - -Bazel will call this file like `cred_helper.sh get` and use the returned JSON to inject headers -into whatever HTTP(S) request it performs against `example.com`. - -[rfc7617]: https://datatracker.ietf.org/doc/html/rfc7617 diff --git a/docs/pypi/circular-dependencies.md b/docs/pypi/circular-dependencies.md new file mode 100644 index 0000000000..d22f5b36a7 --- /dev/null +++ b/docs/pypi/circular-dependencies.md @@ -0,0 +1,82 @@ +:::{default-domain} bzl +::: + +# Circular dependencies + +Sometimes PyPi packages contain dependency cycles -- for instance a particular +version `sphinx` (this is no longer the case in the latest version as of +2024-06-02) depends on `sphinxcontrib-serializinghtml`. When using them as +`requirement()`s, ala + +```starlark +py_binary( + name = "doctool", + ... + deps = [ + requirement("sphinx"), + ], +) +``` + +Bazel will protest because it doesn't support cycles in the build graph -- + +``` +ERROR: .../external/pypi_sphinxcontrib_serializinghtml/BUILD.bazel:44:6: in alias rule @pypi_sphinxcontrib_serializinghtml//:pkg: cycle in dependency graph: + //:doctool (...) + @pypi//sphinxcontrib_serializinghtml:pkg (...) +.-> @pypi_sphinxcontrib_serializinghtml//:pkg (...) +| @pypi_sphinxcontrib_serializinghtml//:_pkg (...) +| @pypi_sphinx//:pkg (...) +| @pypi_sphinx//:_pkg (...) +`-- @pypi_sphinxcontrib_serializinghtml//:pkg (...) +``` + +The `experimental_requirement_cycles` attribute allows you to work around these +issues by specifying groups of packages which form cycles. `pip_parse` will +transparently fix the cycles for you and provide the cyclic dependencies +simultaneously. + +```starlark + ... + experimental_requirement_cycles = { + "sphinx": [ + "sphinx", + "sphinxcontrib-serializinghtml", + ] + }, +) +``` + +`pip_parse` supports fixing multiple cycles simultaneously, however cycles must +be distinct. `apache-airflow` for instance has dependency cycles with a number +of its optional dependencies, which means those optional dependencies must all +be a part of the `airflow` cycle. For instance -- + +```starlark + ... + experimental_requirement_cycles = { + "airflow": [ + "apache-airflow", + "apache-airflow-providers-common-sql", + "apache-airflow-providers-postgres", + "apache-airflow-providers-sqlite", + ] + } +) +``` + +Alternatively, one could resolve the cycle by removing one leg of it. + +For example while `apache-airflow-providers-sqlite` is "baked into" the Airflow +package, `apache-airflow-providers-postgres` is not and is an optional feature. +Rather than listing `apache-airflow[postgres]` in your `requirements.txt` which +would expose a cycle via the extra, one could either _manually_ depend on +`apache-airflow` and `apache-airflow-providers-postgres` separately as +requirements. Bazel rules which need only `apache-airflow` can take it as a +dependency, and rules which explicitly want to mix in +`apache-airflow-providers-postgres` now can. + +Alternatively, one could use `rules_python`'s patching features to remove one +leg of the dependency manually. For instance by making +`apache-airflow-providers-postgres` not explicitly depend on `apache-airflow` or +perhaps `apache-airflow-providers-common-sql`. diff --git a/docs/pypi/download-workspace.md b/docs/pypi/download-workspace.md new file mode 100644 index 0000000000..48710095a4 --- /dev/null +++ b/docs/pypi/download-workspace.md @@ -0,0 +1,107 @@ +:::{default-domain} bzl +::: + +# Download (WORKSPACE) + +This documentation page covers how to download the PyPI dependencies in the legacy `WORKSPACE` setup. + +To add pip dependencies to your `WORKSPACE`, load the `pip_parse` function and +call it to create the central external repo and individual wheel external repos. + +```starlark +load("@rules_python//python:pip.bzl", "pip_parse") + +# Create a central repo that knows about the dependencies needed from +# requirements_lock.txt. +pip_parse( + name = "my_deps", + requirements_lock = "//path/to:requirements_lock.txt", +) + +# Load the starlark macro, which will define your dependencies. +load("@my_deps//:requirements.bzl", "install_deps") + +# Call it to define repos for your requirements. +install_deps() +``` + +## Interpreter selection + +Note that pip parse runs before the Bazel before decides which Python toolchain to use, it cannot +enforce that the interpreter used to invoke `pip` matches the interpreter used to run `py_binary` +targets. By default, `pip_parse` uses the system command `"python3"`. To override this, pass in the +{attr}`pip_parse.python_interpreter` attribute or {attr}`pip_parse.python_interpreter_target`. + +You can have multiple `pip_parse`s in the same workspace. This configuration will create multiple +external repos that have no relation to one another and may result in downloading the same wheels +numerous times. + +As with any repository rule, if you would like to ensure that `pip_parse` is +re-executed to pick up a non-hermetic change to your environment (e.g., updating +your system `python` interpreter), you can force it to re-execute by running +`bazel sync --only [pip_parse name]`. + +(per-os-arch-requirements)= +## Requirements for a specific OS/Architecture + +In some cases you may need to use different requirements files for different OS, Arch combinations. +This is enabled via the {attr}`pip_parse.requirements_by_platform` attribute. The keys of the +dictionary are labels to the file and the values are a list of comma separated target (os, arch) +tuples. + +For example: +```starlark + # ... + requirements_by_platform = { + "requirements_linux_x86_64.txt": "linux_x86_64", + "requirements_osx.txt": "osx_*", + "requirements_linux_exotic.txt": "linux_exotic", + "requirements_some_platforms.txt": "linux_aarch64,windows_*", + }, + # For the list of standard platforms that the rules_python has toolchains for, default to + # the following requirements file. + requirements_lock = "requirements_lock.txt", +``` + +In case of duplicate platforms, `rules_python` will raise an error as there has +to be unambiguous mapping of the requirement files to the (os, arch) tuples. + +An alternative way is to use per-OS requirement attributes. +```starlark + # ... + requirements_windows = "requirements_windows.txt", + requirements_darwin = "requirements_darwin.txt", + # For the remaining platforms (which is basically only linux OS), use this file. + requirements_lock = "requirements_lock.txt", +) +``` + +:::{note} +If you are using a universal lock file but want to restrict the list of platforms that +the lock file will be evaluated against, consider using the aforementioned +`requirements_by_platform` attribute and listing the platforms explicitly. +::: + +(vendoring-requirements)= +## Vendoring the requirements.bzl file + +:::{note} +For `bzlmod`, refer to standard `bazel vendor` usage if you want to really vendor it, otherwise +just use the `pip` extension as you would normally. + +However, be aware that there are caveats when doing so. +::: + +In some cases you may not want to generate the requirements.bzl file as a repository rule +while Bazel is fetching dependencies. For example, if you produce a reusable Bazel module +such as a ruleset, you may want to include the `requirements.bzl` file rather than make your users +install the `WORKSPACE` setup to generate it, see {gh-issue}`608`. + +This is the same workflow as Gazelle, which creates `go_repository` rules with +[`update-repos`](https://github.com/bazelbuild/bazel-gazelle#update-repos) + +To do this, use the "write to source file" pattern documented in + +to put a copy of the generated `requirements.bzl` into your project. +Then load the requirements.bzl file directly rather than from the generated repository. +See the example in {gh-path}`examples/pip_parse_vendored`. diff --git a/docs/pypi/download.md b/docs/pypi/download.md new file mode 100644 index 0000000000..18d6699ab3 --- /dev/null +++ b/docs/pypi/download.md @@ -0,0 +1,302 @@ +:::{default-domain} bzl +::: + +# Download (bzlmod) + +:::{seealso} +For WORKSPACE instructions see [here](./download-workspace). +::: + +To add PyPI dependencies to your `MODULE.bazel` file, use the `pip.parse` +extension, and call it to create the central external repo and individual wheel +external repos. Include in the `MODULE.bazel` the toolchain extension as shown +in the first bzlmod example above. + +```starlark +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") + +pip.parse( + hub_name = "my_deps", + python_version = "3.13", + requirements_lock = "//:requirements_lock_3_11.txt", +) + +use_repo(pip, "my_deps") +``` + +For more documentation, see the bzlmod examples under the {gh-path}`examples` folder or the documentation +for the {obj}`@rules_python//python/extensions:pip.bzl` extension. + +:::note} +We are using a host-platform compatible toolchain by default to setup pip dependencies. +During the setup phase, we create some symlinks, which may be inefficient on Windows +by default. In that case use the following `.bazelrc` options to improve performance if +you have admin privileges: + + startup --windows_enable_symlinks + +This will enable symlinks on Windows and help with bootstrap performance of setting up the +hermetic host python interpreter on this platform. Linux and OSX users should see no +difference. +::: + +## Interpreter selection + +The {obj}`pip.parse` `bzlmod` extension by default uses the hermetic python toolchain for the host +platform, but you can customize the interpreter using {attr}`pip.parse.python_interpreter` and +{attr}`pip.parse.python_interpreter_target`. + +You can use the pip extension multiple times. This configuration will create +multiple external repos that have no relation to one another and may result in +downloading the same wheels numerous times. + +As with any repository rule or extension, if you would like to ensure that `pip_parse` is +re-executed to pick up a non-hermetic change to your environment (e.g., updating your system +`python` interpreter), you can force it to re-execute by running `bazel sync --only [pip_parse +name]`. + +(per-os-arch-requirements)= +## Requirements for a specific OS/Architecture + +In some cases you may need to use different requirements files for different OS, Arch combinations. +This is enabled via the `requirements_by_platform` attribute in `pip.parse` extension and the +{obj}`pip.parse` tag class. The keys of the dictionary are labels to the file and the values are a +list of comma separated target (os, arch) tuples. + +For example: +```starlark + # ... + requirements_by_platform = { + "requirements_linux_x86_64.txt": "linux_x86_64", + "requirements_osx.txt": "osx_*", + "requirements_linux_exotic.txt": "linux_exotic", + "requirements_some_platforms.txt": "linux_aarch64,windows_*", + }, + # For the list of standard platforms that the rules_python has toolchains for, default to + # the following requirements file. + requirements_lock = "requirements_lock.txt", +``` + +In case of duplicate platforms, `rules_python` will raise an error as there has +to be unambiguous mapping of the requirement files to the (os, arch) tuples. + +An alternative way is to use per-OS requirement attributes. +```starlark + # ... + requirements_windows = "requirements_windows.txt", + requirements_darwin = "requirements_darwin.txt", + # For the remaining platforms (which is basically only linux OS), use this file. + requirements_lock = "requirements_lock.txt", +) +``` + +:::{note} +If you are using a universal lock file but want to restrict the list of platforms that +the lock file will be evaluated against, consider using the aforementioned +`requirements_by_platform` attribute and listing the platforms explicitly. +::: + +## Multi-platform support + +Historically the {obj}`pip_parse` and {obj}`pip.parse` have been only downloading/building +Python dependencies for the host platform that the `bazel` commands are executed on. Over +the years people started needing support for building containers and usually that involves +fetching dependencies for a particular target platform that may be other than the host +platform. + +Multi-platform support of cross-building the wheels can be done in two ways: +1. using {attr}`experimental_index_url` for the {bzl:obj}`pip.parse` bzlmod tag class +2. using {attr}`pip.parse.download_only` setting. + +:::{warning} +This will not for sdists with C extensions, but pure Python sdists may still work using the first +approach. +::: + +### Using `download_only` attribute + +Let's say you have 2 requirements files: +``` +# requirements.linux_x86_64.txt +--platform=manylinux_2_17_x86_64 +--python-version=39 +--implementation=cp +--abi=cp39 + +foo==0.0.1 --hash=sha256:deadbeef +bar==0.0.1 --hash=sha256:deadb00f +``` + +``` +# requirements.osx_aarch64.txt contents +--platform=macosx_10_9_arm64 +--python-version=39 +--implementation=cp +--abi=cp39 + +foo==0.0.3 --hash=sha256:deadbaaf +``` + +With these 2 files your {bzl:obj}`pip.parse` could look like: +```starlark +pip.parse( + hub_name = "pip", + python_version = "3.9", + # Tell `pip` to ignore sdists + download_only = True, + requirements_by_platform = { + "requirements.linux_x86_64.txt": "linux_x86_64", + "requirements.osx_aarch64.txt": "osx_aarch64", + }, +) +``` + +With this, the `pip.parse` will create a hub repository that is going to +support only two platforms - `cp39_osx_aarch64` and `cp39_linux_x86_64` and it +will only use `wheels` and ignore any sdists that it may find on the PyPI +compatible indexes. + +:::{warning} +Because bazel is not aware what exactly is downloaded, the same wheel may be downloaded +multiple times. +::: + +:::{note} +This will only work for wheel-only setups, i.e. all of your dependencies need to have wheels +available on the PyPI index that you use. +::: + +### Customizing `Requires-Dist` resolution + +:::{note} +Currently this is disabled by default, but you can turn it on using +{envvar}`RULES_PYTHON_ENABLE_PIPSTAR` environment variable. +::: + +In order to understand what dependencies to pull for a particular package +`rules_python` parses the `whl` file [`METADATA`][metadata]. +Packages can express dependencies via `Requires-Dist` and they can add conditions using +"environment markers", which represent the Python version, OS, etc. + +While the PyPI integration provides reasonable defaults to support most +platforms and environment markers, the values it uses can be customized in case +more esoteric configurations are needed. + +To customize the values used, you need to do two things: +1. Define a target that returns {obj}`EnvMarkerInfo` +2. Set the {obj}`//python/config_settings:pip_env_marker_config` flag to + the target defined in (1). + +The keys and values should be compatible with the [PyPA dependency specifiers +specification](https://packaging.python.org/en/latest/specifications/dependency-specifiers/). +This is not strictly enforced, however, so you can return a subset of keys or +additional keys, which become available during dependency evaluation. + +[metadata]: https://packaging.python.org/en/latest/specifications/core-metadata/ + +(bazel-downloader)= +### Bazel downloader and multi-platform wheel hub repository. + +:::{warning} +This is currently still experimental and whilst it has been proven to work in quite a few +environments, the APIs are still being finalized and there may be changes to the APIs for this +feature without much notice. + +The issues that you can subscribe to for updates are: +* {gh-issue}`260` +* {gh-issue}`1357` +::: + +The {obj}`pip` extension supports pulling information from `PyPI` (or a compatible mirror) and it +will ensure that the [bazel downloader][bazel_downloader] is used for downloading the wheels. + +This provides the following benefits: +* Integration with the [credential_helper](#credential-helper) to authenticate with private + mirrors. +* Cache the downloaded wheels speeding up the consecutive re-initialization of the repositories. +* Reuse the same instance of the wheel for multiple target platforms. +* Allow using transitions and targeting free-threaded and musl platforms more easily. +* Avoids `pip` for wheel fetching and results in much faster dependency fetching. + +To enable the feature specify {attr}`pip.parse.experimental_index_url` as shown in +the {gh-path}`examples/bzlmod/MODULE.bazel` example. + +Similar to [uv](https://docs.astral.sh/uv/configuration/indexes/), one can override the +index that is used for a single package. By default we first search in the index specified by +{attr}`pip.parse.experimental_index_url`, then we iterate through the +{attr}`pip.parse.experimental_extra_index_urls` unless there are overrides specified via +{attr}`pip.parse.experimental_index_url_overrides`. + +When using this feature during the `pip` extension evaluation you will see the accessed indexes similar to below: +```console +Loading: 0 packages loaded + Fetching module extension @@//python/extensions:pip.bzl%pip; Fetch package lists from PyPI index + Fetching https://pypi.org/simple/jinja2/ + +``` + +This does not mean that `rules_python` is fetching the wheels eagerly, but it +rather means that it is calling the PyPI server to get the Simple API response +to get the list of all available source and wheel distributions. Once it has +got all of the available distributions, it will select the right ones depending +on the `sha256` values in your `requirements_lock.txt` file. If `sha256` hashes +are not present in the requirements file, we will fallback to matching by version +specified in the lock file. + +Fetching the distribution information from the PyPI allows `rules_python` to +know which `whl` should be used on which target platform and it will determine +that by parsing the `whl` filename based on [PEP600], [PEP656] standards. This +allows the user to configure the behaviour by using the following publicly +available flags: +* {obj}`--@rules_python//python/config_settings:py_linux_libc` for selecting the Linux libc variant. +* {obj}`--@rules_python//python/config_settings:pip_whl` for selecting `whl` distribution preference. +* {obj}`--@rules_python//python/config_settings:pip_whl_osx_arch` for selecting MacOS wheel preference. +* {obj}`--@rules_python//python/config_settings:pip_whl_glibc_version` for selecting the GLIBC version compatibility. +* {obj}`--@rules_python//python/config_settings:pip_whl_muslc_version` for selecting the musl version compatibility. +* {obj}`--@rules_python//python/config_settings:pip_whl_osx_version` for selecting MacOS version compatibility. + +[bazel_downloader]: https://bazel.build/rules/lib/builtins/repository_ctx#download +[pep600]: https://peps.python.org/pep-0600/ +[pep656]: https://peps.python.org/pep-0656/ + +(credential-helper)= +## Credential Helper + +The [Bazel downloader](#bazel-downloader) usage allows for the Bazel +[Credential Helper][cred-helper-design]. +Your python artifact registry may provide a credential helper for you. +Refer to your index's docs to see if one is provided. + +The simplest form of a credential helper is a bash script that accepts an arg and spits out JSON to +stdout. For a service like Google Artifact Registry that uses ['Basic' HTTP Auth][rfc7617] and does +not provide a credential helper that conforms to the [spec][cred-helper-spec], the script might +look like: + +```bash +#!/bin/bash +# cred_helper.sh +ARG=$1 # but we don't do anything with it as it's always "get" + +# formatting is optional +echo '{' +echo ' "headers": {' +echo ' "Authorization": ["Basic dGVzdDoxMjPCow=="]' +echo ' }' +echo '}' +``` + +Configure Bazel to use this credential helper for your python index `example.com`: + +``` +# .bazelrc +build --credential_helper=example.com=/full/path/to/cred_helper.sh +``` + +Bazel will call this file like `cred_helper.sh get` and use the returned JSON to inject headers +into whatever HTTP(S) request it performs against `example.com`. + +See the [Credential Helper Spec][cred-helper-spec] for more details. + +[rfc7617]: https://datatracker.ietf.org/doc/html/rfc7617 +[cred-helper-design]: https://github.com/bazelbuild/proposals/blob/main/designs/2022-06-07-bazel-credential-helpers.md +[cred-helper-spec]: https://github.com/EngFlow/credential-helper-spec/blob/main/spec.md diff --git a/docs/pypi/index.md b/docs/pypi/index.md new file mode 100644 index 0000000000..c300124398 --- /dev/null +++ b/docs/pypi/index.md @@ -0,0 +1,27 @@ +:::{default-domain} bzl +::: + +# Using PyPI + +Using PyPI packages (aka "pip install") involves the following main steps. + +1. [Generating requirements file](./lock) +2. Installing third party packages in [bzlmod](./download) or [WORKSPACE](./download-workspace). +3. [Using third party packages as dependencies](./use) + +With the advanced topics covered separately: +* Dealing with [circular dependencies](./circular-dependencies). + +```{toctree} +lock +download +download-workspace +use +``` + +## Advanced topics + +```{toctree} +circular-dependencies +patch +``` diff --git a/docs/pypi/lock.md b/docs/pypi/lock.md new file mode 100644 index 0000000000..c9376036fb --- /dev/null +++ b/docs/pypi/lock.md @@ -0,0 +1,46 @@ +:::{default-domain} bzl +::: + +# Lock + +:::{note} +Currently `rules_python` only supports `requirements.txt` format. +::: + +## requirements.txt + +### pip compile + +Generally, when working on a Python project, you'll have some dependencies that themselves have other dependencies. You might also specify dependency bounds instead of specific versions. So you'll need to generate a full list of all transitive dependencies and pinned versions for every dependency. + +Typically, you'd have your project dependencies specified in `pyproject.toml` or `requirements.in` and generate the full pinned list of dependencies in `requirements_lock.txt`, which you can manage with the {obj}`compile_pip_requirements`: + +```starlark +load("@rules_python//python:pip.bzl", "compile_pip_requirements") + +compile_pip_requirements( + name = "requirements", + src = "requirements.in", + requirements_txt = "requirements_lock.txt", +) +``` + +This rule generates two targets: +- `bazel run [name].update` will regenerate the `requirements_txt` file +- `bazel test [name]_test` will test that the `requirements_txt` file is up to date + +Once you generate this fully specified list of requirements, you can install the requirements ([bzlmod](./download)/[WORKSPACE](./download-workspace)). + +:::{warning} +If you're specifying dependencies in `pyproject.toml`, make sure to include the `[build-system]` configuration, with pinned dependencies. `compile_pip_requirements` will use the build system specified to read your project's metadata, and you might see non-hermetic behavior if you don't pin the build system. + +Not specifying `[build-system]` at all will result in using a default `[build-system]` configuration, which uses unpinned versions ([ref](https://peps.python.org/pep-0518/#build-system-table)). +::: + +### uv pip compile (bzlmod only) + +We also have experimental setup for the `uv pip compile` way of generating lock files. +This is well tested with the public PyPI index, but you may hit some rough edges with private +mirrors. + +For more documentation see {obj}`lock` documentation. diff --git a/docs/pypi/patch.md b/docs/pypi/patch.md new file mode 100644 index 0000000000..f341bd1091 --- /dev/null +++ b/docs/pypi/patch.md @@ -0,0 +1,10 @@ +:::{default-domain} bzl +::: + +# Patching wheels + +Sometimes the wheels have to be patched to: +* Workaround the lack of a standard `site-packages` layout ({gh-issue}`2156`) +* Include certain PRs of your choice on top of wheels and avoid building from sdist, + +You can patch the wheels by using the {attr}`pip.override.patches` attribute. diff --git a/docs/pypi/use.md b/docs/pypi/use.md new file mode 100644 index 0000000000..7a16b7d9e9 --- /dev/null +++ b/docs/pypi/use.md @@ -0,0 +1,133 @@ +:::{default-domain} bzl +::: + +# Use in BUILD.bazel files + +Once you have setup the dependencies, you are ready to start using them in your `BUILD.bazel` +files. If you haven't done so yet, set it up by following the following docs: +1. [WORKSPACE](./download-workspace) +1. [bzlmod](./download) + +To refer to targets in a hub repo `pypi`, you can do one of two things: +```starlark +py_library( + name = "my_lib", + deps = [ + "@pypi//numpy", + ], +) +``` + +Or use the `requirement` helper that needs to be loaded from the `hub` repo itself: +```starlark +load("@pypi//:requirements.bzl", "requirement") + +py_library( + deps = [ + requirement("numpy") + ], +) +``` + +Note, that the usage of the `requirement` helper is not advised and can be problematic. See the +[notes below](#requirement-helper). + +Note, that the hub repo contains the following targets for each package: +* `@pypi//numpy` which is a shorthand for `@pypi//numpy:numpy`. This is an {obj}`alias` to + `@pypi//numpy:pkg`. +* `@pypi//numpy:pkg` - the {obj}`py_library` target automatically generated by the repository + rules. +* `@pypi//numpy:data` - the {obj}`filegroup` that is for all of the extra files that are included + as data in the `pkg` target. +* `@pypi//numpy:dist_info` - the {obj}`filegroup` that is for all of the files in the `.distinfo` directory. +* `@pypi//numpy:whl` - the {obj}`filegroup` that is the `.whl` file itself which includes all of + the transitive dependencies via the {attr}`filegroup.data` attribute. + +## Entry points + +If you would like to access [entry points][whl_ep], see the `py_console_script_binary` rule documentation, +which can help you create a `py_binary` target for a particular console script exposed by a package. + +[whl_ep]: https://packaging.python.org/specifications/entry-points/ + +## 'Extras' dependencies + +Any 'extras' specified in the requirements lock file will be automatically added +as transitive dependencies of the package. In the example above, you'd just put +`requirement("useful_dep")` or `@pypi//useful_dep`. + +## Consuming Wheel Dists Directly + +If you need to depend on the wheel dists themselves, for instance, to pass them +to some other packaging tool, you can get a handle to them with the +`whl_requirement` macro. For example: + +```starlark +load("@pypi//:requirements.bzl", "whl_requirement") + +filegroup( + name = "whl_files", + data = [ + # This is equivalent to "@pypi//boto3:whl" + whl_requirement("boto3"), + ] +) +``` + +## Creating a filegroup of files within a whl + +The rule {obj}`whl_filegroup` exists as an easy way to extract the necessary files +from a whl file without the need to modify the `BUILD.bazel` contents of the +whl repositories generated via `pip_repository`. Use it similarly to the `filegroup` +above. See the API docs for more information. + +(requirement-helper)= +## A note about using the requirement helper + +Each extracted wheel repo contains a `py_library` target representing +the wheel's contents. There are two ways to access this library. The +first uses the `requirement()` function defined in the central +repo's `//:requirements.bzl` file. This function maps a pip package +name to a label: + +```starlark +load("@my_deps//:requirements.bzl", "requirement") + +py_library( + name = "mylib", + srcs = ["mylib.py"], + deps = [ + ":myotherlib", + requirement("some_pip_dep"), + requirement("another_pip_dep"), + ] +) +``` + +The reason `requirement()` exists is to insulate from +changes to the underlying repository and label strings. However, those +labels have become directly used, so aren't able to easily change regardless. + +On the other hand, using `requirement()` helper has several drawbacks: + +- It doesn't work with `buildifier` +- It doesn't work with `buildozer` +- It adds extra layer on top of normal mechanisms to refer to targets. +- It does not scale well as each type of target needs a new macro to be loaded and imported. + +If you don't want to use `requirement()`, you can use the library labels directly instead. For +`pip_parse`, the labels are of the following form: + +```starlark +@{name}//{package} +``` + +Here `name` is the `name` attribute that was passed to `pip_parse` and +`package` is the pip package name with characters that are illegal in +Bazel label names (e.g. `-`, `.`) replaced with `_`. If you need to +update `name` from "old" to "new", then you can run the following +`buildozer` command: + +```shell +buildozer 'substitute deps @old//([^/]+) @new//${1}' //...:* +``` diff --git a/docs/repl.md b/docs/repl.md new file mode 100644 index 0000000000..edcf37e811 --- /dev/null +++ b/docs/repl.md @@ -0,0 +1,66 @@ +# Getting a REPL or Interactive Shell + +rules_python provides a REPL to help with debugging and developing. The goal of +the REPL is to present an environment identical to what a {bzl:obj}`py_binary` creates +for your code. + +## Usage + +Start the REPL with the following command: +```console +$ bazel run @rules_python//python/bin:repl +Python 3.11.11 (main, Mar 17 2025, 21:02:09) [Clang 20.1.0 ] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> +``` + +Settings like `//python/config_settings:python_version` will influence the exact +behaviour. +```console +$ bazel run @rules_python//python/bin:repl --@rules_python//python/config_settings:python_version=3.13 +Python 3.13.2 (main, Mar 17 2025, 21:02:54) [Clang 20.1.0 ] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> +``` + +See [//python/config_settings](api/rules_python/python/config_settings/index) +and [Environment Variables](environment-variables) for more settings. + +## Importing Python targets + +The `//python/bin:repl_dep` command line flag gives the REPL access to a target +that provides the {bzl:obj}`PyInfo` provider. + +```console +$ bazel run @rules_python//python/bin:repl --@rules_python//python/bin:repl_dep=@rules_python//tools:wheelmaker +Python 3.11.11 (main, Mar 17 2025, 21:02:09) [Clang 20.1.0 ] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> import tools.wheelmaker +>>> +``` + +## Customizing the shell + +By default, the `//python/bin:repl` target will invoke the shell from the `code` +module. It's possible to switch to another shell by writing a custom "stub" and +pointing the target at the necessary dependencies. + +### IPython Example + +For an IPython shell, create a file as follows. + +```python +import IPython +IPython.start_ipython() +``` + +Assuming the file is called `ipython_stub.py` and the `pip.parse` hub's name is +`my_deps`, set this up in the .bazelrc file: +``` +# Allow the REPL stub to import ipython. In this case, @my_deps is the hub name +# of the pip.parse() call. +build --@rules_python//python/bin:repl_stub_dep=@my_deps//ipython + +# Point the REPL at the stub created above. +build --@rules_python//python/bin:repl_stub=//path/to:ipython_stub.py +``` diff --git a/docs/requirements.txt b/docs/requirements.txt index 5e308b00f4..87c13aa8ba 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,6 +1,5 @@ # This file was autogenerated by uv via the following command: # bazel run //docs:requirements.update ---index-url https://pypi.org/simple absl-py==2.2.2 \ --hash=sha256:bf25b2c2eed013ca456918c453d687eab4e8309fba81ee2f4c1a6aa2494175eb \ @@ -10,9 +9,9 @@ alabaster==1.0.0 \ --hash=sha256:c00dca57bca26fa62a6d7d0a9fcce65f3e026e9bfe33e9c538fd3fbb2144fd9e \ --hash=sha256:fc6786402dc3fcb2de3cabd5fe455a2db534b371124f1f21de8731783dec828b # via sphinx -astroid==3.3.6 \ - --hash=sha256:6aaea045f938c735ead292204afdb977a36e989522b7833ef6fea94de743f442 \ - --hash=sha256:db676dc4f3ae6bfe31cda227dc60e03438378d7a896aec57422c95634e8d722f +astroid==3.3.9 \ + --hash=sha256:622cc8e3048684aa42c820d9d218978021c3c3d174fb03a9f0d615921744f550 \ + --hash=sha256:d05bfd0acba96a7bd43e222828b7d9bc1e138aaeb0649707908d3702a9831248 # via sphinx-autodoc2 babel==2.17.0 \ --hash=sha256:0c54cffb19f690cdcc52a3b50bcbf71e07a808d1c80d549f2459b9d2cf0afb9d \ @@ -223,9 +222,9 @@ myst-parser==4.0.0 \ --hash=sha256:851c9dfb44e36e56d15d05e72f02b80da21a9e0d07cba96baf5e2d476bb91531 \ --hash=sha256:b9317997552424448c6096c2558872fdb6f81d3ecb3a40ce84a7518798f3f28d # via rules-python-docs (docs/pyproject.toml) -packaging==24.2 \ - --hash=sha256:09abb1bccd265c01f4a3aa3f7a7db064b36514d2cba19a2f694fe6150451a759 \ - --hash=sha256:c228a6dc5e932d346bc5739379109d49e8853dd8223571c7c5b55260edc0b97f +packaging==25.0 \ + --hash=sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484 \ + --hash=sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f # via # readthedocs-sphinx-ext # sphinx @@ -319,9 +318,9 @@ sphinx-reredirects==0.1.6 \ --hash=sha256:c491cba545f67be9697508727818d8626626366245ae64456fe29f37e9bbea64 \ --hash=sha256:efd50c766fbc5bf40cd5148e10c00f2c00d143027de5c5e48beece93cc40eeea # via rules-python-docs (docs/pyproject.toml) -sphinx-rtd-theme==3.0.1 \ - --hash=sha256:921c0ece75e90633ee876bd7b148cfaad136b481907ad154ac3669b6fc957916 \ - --hash=sha256:a4c5745d1b06dfcb80b7704fe532eb765b44065a8fad9851e4258c8804140703 +sphinx-rtd-theme==3.0.2 \ + --hash=sha256:422ccc750c3a3a311de4ae327e82affdaf59eb695ba4936538552f3b00f4ee13 \ + --hash=sha256:b7457bc25dda723b20b086a670b9953c859eab60a2a03ee8eb2bb23e176e5f85 # via rules-python-docs (docs/pyproject.toml) sphinxcontrib-applehelp==2.0.0 \ --hash=sha256:2f29ef331735ce958efa4734873f084941970894c6090408b079c61b2e1c06d1 \ diff --git a/docs/toolchains.md b/docs/toolchains.md index 320e16335b..57d43d27f1 100644 --- a/docs/toolchains.md +++ b/docs/toolchains.md @@ -1,6 +1,7 @@ :::{default-domain} bzl ::: +(configuring-toolchains)= # Configuring Python toolchains and runtimes This documents how to configure the Python toolchain and runtimes for different @@ -43,7 +44,8 @@ you should read the dev-only library module section. bazel_dep(name="rules_python", version=...) python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain(python_version = "3.12", is_default = True) +python.defaults(python_version = "3.12") +python.toolchain(python_version = "3.12") ``` ### Library modules @@ -71,7 +73,8 @@ python = use_extension( dev_dependency = True ) -python.toolchain(python_version = "3.12", is_default=True) +python.defaults(python_version = "3.12") +python.toolchain(python_version = "3.12") ``` #### Library modules without version constraints @@ -160,9 +163,13 @@ Multiple versions can be specified and used within a single build. # MODULE.bazel python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.defaults( + # The environment variable takes precedence if set. + python_version = "3.11", + python_version_env = "BAZEL_PYTHON_VERSION", +) python.toolchain( python_version = "3.11", - is_default = True, ) python.toolchain( @@ -236,6 +243,73 @@ existing attributes: * Adding additional Python versions via {bzl:obj}`python.single_version_override` or {bzl:obj}`python.single_version_platform_override`. +### Registering custom runtimes + +Because the python-build-standalone project has _thousands_ of prebuilt runtimes +available, rules_python only includes popular runtimes in its built in +configurations. If you want to use a runtime that isn't already known to +rules_python then {obj}`single_version_platform_override()` can be used to do +so. In short, it allows specifying an arbitrary URL and using custom flags +to control when a runtime is used. + +In the example below, we register a particular python-build-standalone runtime +that is activated for Linux x86 builds when the custom flag +`--//:runtime=my-custom-runtime` is set. + +``` +# File: MODULE.bazel +bazel_dep(name = "bazel_skylib", version = "1.7.1.") +bazel_dep(name = "rules_python", version = "1.5.0") +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.single_version_platform_override( + platform = "my-platform", + python_version = "3.13.3", + sha256 = "01d08b9bc8a96698b9d64c2fc26da4ecc4fa9e708ce0a34fb88f11ab7e552cbd", + os_name = "linux", + arch = "x86_64", + target_settings = [ + "@@//:runtime=my-custom-runtime", + ], + urls = ["https://github.com/astral-sh/python-build-standalone/releases/download/20250409/cpython-3.13.3+20250409-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"], +) +# File: //:BUILD.bazel +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") +string_flag( + name = "custom_runtime", + build_setting_default = "", +) +config_setting( + name = "is_custom_runtime_linux-x86-install-only-stripped", + flag_values = { + ":custom_runtime": "linux-x86-install-only-stripped", + }, +) +``` + +Notes: +- While any URL and archive can be used, it's assumed their content looks how + a python-build-standalone archive looks. +- A "version aware" toolchain is registered, which means the Python version flag + must also match (e.g. `--@rules_python//python/config_settings:python_version=3.13.3` + must be set -- see `minor_mapping` and `is_default` for controls and docs + about version matching and selection). +- The `target_compatible_with` attribute can be used to entirely specify the + arg of the same name the toolchain uses. +- The labels in `target_settings` must be absolute; `@@` refers to the main repo. +- The `target_settings` are `config_setting` targets, which means you can + customize how matching occurs. + +:::{seealso} +See {obj}`//python/config_settings` for flags rules_python already defines +that can be used with `target_settings`. Some particular ones of note are: +{flag}`--py_linux_libc` and {flag}`--py_freethreaded`, among others. +::: + +:::{versionadded} VERSION_NEXT_FEATURE +Added support for custom platform names, `target_compatible_with`, and +`target_settings` with `single_version_platform_override`. +::: + ### Using defined toolchains from WORKSPACE It is possible to use toolchains defined in `MODULE.bazel` in `WORKSPACE`. For example @@ -263,7 +337,8 @@ bazel_dep(name = "rules_python", version = "0.40.0") python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain(is_default = True, python_version = "3.10") +python.defaults(python_version = "3.10") +python.toolchain(python_version = "3.10") use_repo(python, "python_3_10", "python_3_10_host") ``` @@ -339,9 +414,10 @@ runtime metadata (Python version, headers, ABI flags, etc) that the regular remotely downloaded runtimes contain, which makes it possible to build e.g. C extensions (unlike the autodetecting and runtime environment toolchains). -For simple cases, some rules are provided that will introspect -a Python installation and create an appropriate Bazel definition from -it. To do this, three pieces need to be wired together: +For simple cases, the {obj}`local_runtime_repo` and +{obj}`local_runtime_toolchains_repo` rules are provided that will introspect a +Python installation and create an appropriate Bazel definition from it. To do +this, three pieces need to be wired together: 1. Specify a path or command to a Python interpreter (multiple can be defined). 2. Create toolchains for the runtimes in (1) @@ -376,15 +452,14 @@ local_runtime_repo( local_runtime_toolchains_repo( name = "local_toolchains", runtimes = ["local_python3"], + # TIP: The `target_settings` arg can be used to activate them based on + # command line flags; see docs below. ) # Step 3: Register the toolchains register_toolchains("@local_toolchains//:all", dev_dependency = True) ``` -Note that `register_toolchains` will insert the local toolchain earlier in the -toolchain ordering, so it will take precedence over other registered toolchains. - :::{important} Be sure to set `dev_dependency = True`. Using a local toolchain only makes sense for the root module. @@ -396,6 +471,72 @@ downstream modules. Multiple runtimes and/or toolchains can be defined, which allows for multiple Python versions and/or platforms to be configured in a single `MODULE.bazel`. +Note that `register_toolchains` will insert the local toolchain earlier in the +toolchain ordering, so it will take precedence over other registered toolchains. +To better control when the toolchain is used, see [Conditionally using local +toolchains] + +### Conditionally using local toolchains + +By default, a local toolchain has few constraints and is early in the toolchain +ordering, which means it will usually be used no matter what. This can be +problematic for CI (where it shouldn't be used), expensive for CI (CI must +initialize/download the repository to determine its Python version), and +annoying for iterative development (enabling/disabling it requires modifying +MODULE.bazel). + +These behaviors can be mitigated, but it requires additional configuration +to avoid triggering the local toolchain repository to initialize (i.e. run +local commands and perform downloads). + +The two settings to change are +{obj}`local_runtime_toolchains_repo.target_compatible_with` and +{obj}`local_runtime_toolchains_repo.target_settings`, which control how Bazel +decides if a toolchain should match. By default, they point to targets *within* +the local runtime repository (trigger repo initialization). We have to override +them to *not* reference the local runtime repository at all. + +In the example below, we reconfigure the local toolchains so they are only +activated if the custom flag `--//:py=local` is set and the target platform +matches the Bazel host platform. The net effect is CI won't use the local +toolchain (nor initialize its repository), and developers can easily +enable/disable the local toolchain with a command line flag. + +``` +# File: MODULE.bazel +bazel_dep(name = "bazel_skylib", version = "1.7.1") + +local_runtime_toolchains_repo( + name = "local_toolchains", + runtimes = ["local_python3"], + target_compatible_with = { + "local_python3": ["HOST_CONSTRAINTS"], + }, + target_settings = { + "local_python3": ["@//:is_py_local"] + } +) + +# File: BUILD.bazel +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") + +config_setting( + name = "is_py_local", + flag_values = {":py": "local"}, +) + +string_flag( + name = "py", + build_setting_default = "", +) +``` + +:::{tip} +Easily switching between *multiple* local toolchains can be accomplished by +adding additional `:is_py_X` targets and setting `--//:py` to match. +to easily switch between different local toolchains. +::: + ## Runtime environment toolchain @@ -424,7 +565,7 @@ locally installed Python. ### Autodetecting toolchain The autodetecting toolchain is a deprecated toolchain that is built into Bazel. -It's name is a bit misleading: it doesn't autodetect anything. All it does is +**It's name is a bit misleading: it doesn't autodetect anything**. All it does is use `python3` from the environment a binary runs within. This provides extremely limited functionality to the rules (at build time, nothing is knowable about the Python runtime). @@ -691,3 +832,13 @@ a fixed version. The `python` target does not provide access to any modules from `py_*` targets on its own. Please file a feature request if this is desired. ::: + +### Differences from `//python/bin:repl` + +The `//python/bin:python` target provides access to the underlying interpreter +without any hermeticity guarantees. + +The [`//python/bin:repl` target](repl) provides an environment indentical to +what `py_binary` provides. That means it handles things like the +[`PYTHONSAFEPATH`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSAFEPATH) +environment variable automatically. The `//python/bin:python` target will not. diff --git a/examples/bzlmod/MODULE.bazel b/examples/bzlmod/MODULE.bazel index 69e384e42b..841c096dcf 100644 --- a/examples/bzlmod/MODULE.bazel +++ b/examples/bzlmod/MODULE.bazel @@ -28,10 +28,13 @@ bazel_dep(name = "rules_rust", version = "0.54.1") # We next initialize the python toolchain using the extension. # You can set different Python versions in this block. python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.defaults( + # Use python.defaults if you have defined multiple toolchain versions. + python_version = "3.9", + python_version_env = "BAZEL_PYTHON_VERSION", +) python.toolchain( configure_coverage_tool = True, - # Only set when you have multiple toolchain versions. - is_default = True, python_version = "3.9", ) diff --git a/examples/bzlmod/entry_points/BUILD.bazel b/examples/bzlmod/entry_points/BUILD.bazel index a0939cb65b..4ca5b53568 100644 --- a/examples/bzlmod/entry_points/BUILD.bazel +++ b/examples/bzlmod/entry_points/BUILD.bazel @@ -1,4 +1,3 @@ -load("@python_versions//3.9:defs.bzl", py_console_script_binary_3_9 = "py_console_script_binary") load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary") # This is how you can define a `pylint` entrypoint which uses the default python version. @@ -24,10 +23,11 @@ py_console_script_binary( ], ) -# A specific Python version can be forced by using the generated version-aware -# wrappers, e.g. to force Python 3.9: -py_console_script_binary_3_9( +# A specific Python version can be forced by passing `python_version` +# attribute, e.g. to force Python 3.9: +py_console_script_binary( name = "yamllint", pkg = "@pip//yamllint:pkg", + python_version = "3.9", visibility = ["//entry_points:__subpackages__"], ) diff --git a/examples/bzlmod/other_module/MODULE.bazel b/examples/bzlmod/other_module/MODULE.bazel index 959501abc2..f9d6706120 100644 --- a/examples/bzlmod/other_module/MODULE.bazel +++ b/examples/bzlmod/other_module/MODULE.bazel @@ -25,14 +25,16 @@ PYTHON_NAME_39 = "python_3_9" PYTHON_NAME_311 = "python_3_11" python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.defaults( + # In a submodule this is ignored + python_version = "3.11", +) python.toolchain( configure_coverage_tool = True, python_version = "3.9", ) python.toolchain( configure_coverage_tool = True, - # In a submodule this is ignored - is_default = True, python_version = "3.11", ) diff --git a/examples/bzlmod/py_proto_library/foo_external/py_binary_with_proto.py b/examples/bzlmod/py_proto_library/foo_external/py_binary_with_proto.py index be34264b5a..67e798bb8f 100644 --- a/examples/bzlmod/py_proto_library/foo_external/py_binary_with_proto.py +++ b/examples/bzlmod/py_proto_library/foo_external/py_binary_with_proto.py @@ -2,4 +2,5 @@ if __name__ == "__main__": import my_proto_pb2 + sys.exit(0) diff --git a/examples/bzlmod/requirements_lock_3_9.txt b/examples/bzlmod/requirements_lock_3_9.txt index c48f406451..8a6d41441a 100644 --- a/examples/bzlmod/requirements_lock_3_9.txt +++ b/examples/bzlmod/requirements_lock_3_9.txt @@ -262,9 +262,9 @@ s3cmd==2.1.0 \ --hash=sha256:49cd23d516b17974b22b611a95ce4d93fe326feaa07320bd1d234fed68cbccfa \ --hash=sha256:966b0a494a916fc3b4324de38f089c86c70ee90e8e1cae6d59102103a4c0cc03 # via -r examples/bzlmod/requirements.in -setuptools==65.6.3 \ - --hash=sha256:57f6f22bde4e042978bcd50176fdb381d7c21a9efa4041202288d3737a0c6a54 \ - --hash=sha256:a7620757bf984b58deaf32fc8a4577a9bbc0850cf92c20e1ce41c38c19e5fb75 +setuptools==78.1.1 \ + --hash=sha256:c3a9c4211ff4c309edb8b8c4f1cbfa7ae324c4ba9f91ff254e3d305b9fd54561 \ + --hash=sha256:fcc17fd9cd898242f6b4adfaca46137a9edef687f43e6f78469692a5e70d851d # via # babel # yamllint diff --git a/examples/bzlmod_build_file_generation/MODULE.bazel b/examples/bzlmod_build_file_generation/MODULE.bazel index 9bec25fcbb..b9b428d365 100644 --- a/examples/bzlmod_build_file_generation/MODULE.bazel +++ b/examples/bzlmod_build_file_generation/MODULE.bazel @@ -46,9 +46,13 @@ python = use_extension("@rules_python//python/extensions:python.bzl", "python") # We next initialize the python toolchain using the extension. # You can set different Python versions in this block. +python.defaults( + # The environment variable takes precedence if set. + python_version = "3.9", + python_version_env = "BAZEL_PYTHON_VERSION", +) python.toolchain( configure_coverage_tool = True, - is_default = True, python_version = "3.9", ) diff --git a/examples/multi_python_versions/MODULE.bazel b/examples/multi_python_versions/MODULE.bazel index 85140360bb..4e4a0473c2 100644 --- a/examples/multi_python_versions/MODULE.bazel +++ b/examples/multi_python_versions/MODULE.bazel @@ -17,8 +17,6 @@ python.defaults( ) python.toolchain( configure_coverage_tool = True, - # Only set when you have mulitple toolchain versions. - is_default = True, python_version = "3.9", ) python.toolchain( diff --git a/examples/multi_python_versions/requirements/requirements.in b/examples/multi_python_versions/requirements/requirements.in index 14774b465e..4d1474b9a2 100644 --- a/examples/multi_python_versions/requirements/requirements.in +++ b/examples/multi_python_versions/requirements/requirements.in @@ -1 +1 @@ -websockets +websockets ; python_full_version > "3.9.1" diff --git a/examples/multi_python_versions/requirements/requirements_lock_3_10.txt b/examples/multi_python_versions/requirements/requirements_lock_3_10.txt index 4910d13844..3a8453223f 100644 --- a/examples/multi_python_versions/requirements/requirements_lock_3_10.txt +++ b/examples/multi_python_versions/requirements/requirements_lock_3_10.txt @@ -4,7 +4,7 @@ # # bazel run //requirements:requirements_3_10.update # -websockets==11.0.3 \ +websockets==11.0.3 ; python_full_version > "3.9.1" \ --hash=sha256:01f5567d9cf6f502d655151645d4e8b72b453413d3819d2b6f1185abc23e82dd \ --hash=sha256:03aae4edc0b1c68498f41a6772d80ac7c1e33c06c6ffa2ac1c27a07653e79d6f \ --hash=sha256:0ac56b661e60edd453585f4bd68eb6a29ae25b5184fd5ba51e97652580458998 \ diff --git a/examples/multi_python_versions/requirements/requirements_lock_3_11.txt b/examples/multi_python_versions/requirements/requirements_lock_3_11.txt index 35666b54b1..f1fa8f56f5 100644 --- a/examples/multi_python_versions/requirements/requirements_lock_3_11.txt +++ b/examples/multi_python_versions/requirements/requirements_lock_3_11.txt @@ -4,7 +4,7 @@ # # bazel run //requirements:requirements_3_11.update # -websockets==11.0.3 \ +websockets==11.0.3 ; python_full_version > "3.9.1" \ --hash=sha256:01f5567d9cf6f502d655151645d4e8b72b453413d3819d2b6f1185abc23e82dd \ --hash=sha256:03aae4edc0b1c68498f41a6772d80ac7c1e33c06c6ffa2ac1c27a07653e79d6f \ --hash=sha256:0ac56b661e60edd453585f4bd68eb6a29ae25b5184fd5ba51e97652580458998 \ diff --git a/examples/multi_python_versions/requirements/requirements_lock_3_9.txt b/examples/multi_python_versions/requirements/requirements_lock_3_9.txt index 0001f88d48..3c696a865e 100644 --- a/examples/multi_python_versions/requirements/requirements_lock_3_9.txt +++ b/examples/multi_python_versions/requirements/requirements_lock_3_9.txt @@ -4,7 +4,7 @@ # # bazel run //requirements:requirements_3_9.update # -websockets==11.0.3 \ +websockets==11.0.3 ; python_full_version > "3.9.1" \ --hash=sha256:01f5567d9cf6f502d655151645d4e8b72b453413d3819d2b6f1185abc23e82dd \ --hash=sha256:03aae4edc0b1c68498f41a6772d80ac7c1e33c06c6ffa2ac1c27a07653e79d6f \ --hash=sha256:0ac56b661e60edd453585f4bd68eb6a29ae25b5184fd5ba51e97652580458998 \ diff --git a/examples/pip_parse/BUILD.bazel b/examples/pip_parse/BUILD.bazel index 8bdbd94b2c..6ed8d26286 100644 --- a/examples/pip_parse/BUILD.bazel +++ b/examples/pip_parse/BUILD.bazel @@ -57,6 +57,10 @@ py_console_script_binary( compile_pip_requirements( name = "requirements", src = "requirements.in", + constraints = [ + "constraints_certifi.txt", + "constraints_urllib3.txt", + ], requirements_txt = "requirements_lock.txt", requirements_windows = "requirements_windows.txt", ) diff --git a/examples/pip_parse/constraints_certifi.txt b/examples/pip_parse/constraints_certifi.txt new file mode 100644 index 0000000000..7dc4eac259 --- /dev/null +++ b/examples/pip_parse/constraints_certifi.txt @@ -0,0 +1 @@ +certifi>=2025.1.31 \ No newline at end of file diff --git a/examples/pip_parse/constraints_urllib3.txt b/examples/pip_parse/constraints_urllib3.txt new file mode 100644 index 0000000000..3818262552 --- /dev/null +++ b/examples/pip_parse/constraints_urllib3.txt @@ -0,0 +1 @@ +urllib3>1.26.18 diff --git a/examples/pip_parse/requirements_lock.txt b/examples/pip_parse/requirements_lock.txt index aeac61eff9..dc34b45a45 100644 --- a/examples/pip_parse/requirements_lock.txt +++ b/examples/pip_parse/requirements_lock.txt @@ -12,10 +12,12 @@ babel==2.13.1 \ --hash=sha256:33e0952d7dd6374af8dbf6768cc4ddf3ccfefc244f9986d4074704f2fbd18900 \ --hash=sha256:7077a4984b02b6727ac10f1f7294484f737443d7e2e66c5e4380e41a3ae0b4ed # via sphinx -certifi==2024.7.4 \ - --hash=sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b \ - --hash=sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90 - # via requests +certifi==2025.4.26 \ + --hash=sha256:0a816057ea3cdefcef70270d2c515e4506bbc954f417fa5ade2021213bb8f0c6 \ + --hash=sha256:30350364dfe371162649852c63336a15c70c6510c2ad5015b21c2345311805f3 + # via + # -c ./constraints_certifi.txt + # requests chardet==4.0.0 \ --hash=sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa \ --hash=sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5 @@ -218,10 +220,12 @@ sphinxcontrib-serializinghtml==1.1.9 \ # via # -r requirements.in # sphinx -urllib3==1.26.18 \ - --hash=sha256:34b97092d7e0a3a8cf7cd10e386f401b3737364026c45e622aa02903dffe0f07 \ - --hash=sha256:f8ecc1bba5667413457c529ab955bf8c67b45db799d159066261719e328580a0 - # via requests +urllib3==1.26.20 \ + --hash=sha256:0ed14ccfbf1c30a9072c7ca157e4319b70d65f623e91e7b32fadb2853431016e \ + --hash=sha256:40c2dc0c681e47eb8f90e7e27bf6ff7df2e677421fd46756da1161c39ca70d32 + # via + # -c ./constraints_urllib3.txt + # requests yamllint==1.28.0 \ --hash=sha256:89bb5b5ac33b1ade059743cf227de73daa34d5e5a474b06a5e17fc16583b0cf2 \ --hash=sha256:9e3d8ddd16d0583214c5fdffe806c9344086721f107435f68bad990e5a88826b diff --git a/examples/pip_parse/requirements_windows.txt b/examples/pip_parse/requirements_windows.txt index 61a6682047..78c1a45690 100644 --- a/examples/pip_parse/requirements_windows.txt +++ b/examples/pip_parse/requirements_windows.txt @@ -12,10 +12,12 @@ babel==2.13.1 \ --hash=sha256:33e0952d7dd6374af8dbf6768cc4ddf3ccfefc244f9986d4074704f2fbd18900 \ --hash=sha256:7077a4984b02b6727ac10f1f7294484f737443d7e2e66c5e4380e41a3ae0b4ed # via sphinx -certifi==2024.7.4 \ - --hash=sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b \ - --hash=sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90 - # via requests +certifi==2025.4.26 \ + --hash=sha256:0a816057ea3cdefcef70270d2c515e4506bbc954f417fa5ade2021213bb8f0c6 \ + --hash=sha256:30350364dfe371162649852c63336a15c70c6510c2ad5015b21c2345311805f3 + # via + # -c ./constraints_certifi.txt + # requests chardet==4.0.0 \ --hash=sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa \ --hash=sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5 @@ -222,10 +224,12 @@ sphinxcontrib-serializinghtml==1.1.9 \ # via # -r requirements.in # sphinx -urllib3==1.26.18 \ - --hash=sha256:34b97092d7e0a3a8cf7cd10e386f401b3737364026c45e622aa02903dffe0f07 \ - --hash=sha256:f8ecc1bba5667413457c529ab955bf8c67b45db799d159066261719e328580a0 - # via requests +urllib3==1.26.20 \ + --hash=sha256:0ed14ccfbf1c30a9072c7ca157e4319b70d65f623e91e7b32fadb2853431016e \ + --hash=sha256:40c2dc0c681e47eb8f90e7e27bf6ff7df2e677421fd46756da1161c39ca70d32 + # via + # -c ./constraints_urllib3.txt + # requests yamllint==1.28.0 \ --hash=sha256:89bb5b5ac33b1ade059743cf227de73daa34d5e5a474b06a5e17fc16583b0cf2 \ --hash=sha256:9e3d8ddd16d0583214c5fdffe806c9344086721f107435f68bad990e5a88826b diff --git a/examples/pip_repository_annotations/.bazelrc b/examples/pip_repository_annotations/.bazelrc index c16c5a24f2..9397bd31b8 100644 --- a/examples/pip_repository_annotations/.bazelrc +++ b/examples/pip_repository_annotations/.bazelrc @@ -5,4 +5,5 @@ try-import %workspace%/user.bazelrc # is in examples/bzlmod as the `whl_mods` feature. common --noenable_bzlmod common --enable_workspace +common --legacy_external_runfiles=false common --incompatible_python_disallow_native_rules diff --git a/examples/pip_repository_annotations/pip_repository_annotations_test.py b/examples/pip_repository_annotations/pip_repository_annotations_test.py index e41dd4f0f6..219be1ba03 100644 --- a/examples/pip_repository_annotations/pip_repository_annotations_test.py +++ b/examples/pip_repository_annotations/pip_repository_annotations_test.py @@ -21,7 +21,7 @@ import unittest from pathlib import Path -from rules_python.python.runfiles import runfiles +from python.runfiles import runfiles class PipRepositoryAnnotationsTest(unittest.TestCase): @@ -34,11 +34,7 @@ def wheel_pkg_dir(self) -> str: def test_build_content_and_data(self): r = runfiles.Create() - rpath = r.Rlocation( - "pip_repository_annotations_example/external/{}/generated_file.txt".format( - self.wheel_pkg_dir() - ) - ) + rpath = r.Rlocation("{}/generated_file.txt".format(self.wheel_pkg_dir())) generated_file = Path(rpath) self.assertTrue(generated_file.exists()) @@ -47,11 +43,7 @@ def test_build_content_and_data(self): def test_copy_files(self): r = runfiles.Create() - rpath = r.Rlocation( - "pip_repository_annotations_example/external/{}/copied_content/file.txt".format( - self.wheel_pkg_dir() - ) - ) + rpath = r.Rlocation("{}/copied_content/file.txt".format(self.wheel_pkg_dir())) copied_file = Path(rpath) self.assertTrue(copied_file.exists()) @@ -61,7 +53,7 @@ def test_copy_files(self): def test_copy_executables(self): r = runfiles.Create() rpath = r.Rlocation( - "pip_repository_annotations_example/external/{}/copied_content/executable{}".format( + "{}/copied_content/executable{}".format( self.wheel_pkg_dir(), ".exe" if platform.system() == "windows" else ".py", ) @@ -82,7 +74,7 @@ def test_data_exclude_glob(self): current_wheel_version = "0.38.4" r = runfiles.Create() - dist_info_dir = "pip_repository_annotations_example/external/{}/site-packages/wheel-{}.dist-info".format( + dist_info_dir = "{}/site-packages/wheel-{}.dist-info".format( self.wheel_pkg_dir(), current_wheel_version, ) @@ -113,11 +105,8 @@ def test_extra(self): # This test verifies that annotations work correctly for pip packages with extras # specified, in this case requests[security]. r = runfiles.Create() - rpath = r.Rlocation( - "pip_repository_annotations_example/external/{}/generated_file.txt".format( - self.requests_pkg_dir() - ) - ) + path = "{}/generated_file.txt".format(self.requests_pkg_dir()) + rpath = r.Rlocation(path) generated_file = Path(rpath) self.assertTrue(generated_file.exists()) diff --git a/examples/wheel/BUILD.bazel b/examples/wheel/BUILD.bazel index b434e67405..e52e0fc3a3 100644 --- a/examples/wheel/BUILD.bazel +++ b/examples/wheel/BUILD.bazel @@ -313,6 +313,17 @@ wheel; python_version == "3.11" or python_version == "3.12" # Example comment """.splitlines(), ) +write_file( + name = "requires_dist_depends_on_extras_file", + out = "requires_dist_depends_on_extras.txt", + content = """\ +# Requirements file +--index-url https://pypi.com + +extra_requires[example]==0.0.1 +""".splitlines(), +) + # py_wheel can use text files to specify their requirements. This # can be convenient for users of `compile_pip_requirements` who have # granular `requirements.in` files per package. This target shows @@ -374,6 +385,22 @@ py_wheel( deps = [":example_pkg"], ) +py_wheel( + name = "requires_dist_depends_on_extras", + distribution = "requires_dist_depends_on_extras", + requires = [ + "extra_requires[example]==0.0.1", + ], + version = "0.0.1", +) + +py_wheel( + name = "requires_dist_depends_on_extras_using_file", + distribution = "requires_dist_depends_on_extras_using_file", + requires_file = ":requires_dist_depends_on_extras.txt", + version = "0.0.1", +) + py_test( name = "wheel_test", srcs = ["wheel_test.py"], @@ -391,6 +418,8 @@ py_test( ":minimal_with_py_package", ":python_abi3_binary_wheel", ":python_requires_in_a_package", + ":requires_dist_depends_on_extras", + ":requires_dist_depends_on_extras_using_file", ":requires_files", ":use_rule_with_dir_in_outs", ], diff --git a/examples/wheel/lib/module_with_type_annotations.py b/examples/wheel/lib/module_with_type_annotations.py index 13e0895160..eda57bae6a 100644 --- a/examples/wheel/lib/module_with_type_annotations.py +++ b/examples/wheel/lib/module_with_type_annotations.py @@ -12,5 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. + def function(): return "qux" diff --git a/examples/wheel/test_publish.py b/examples/wheel/test_publish.py index 47134d11f3..7665629c19 100644 --- a/examples/wheel/test_publish.py +++ b/examples/wheel/test_publish.py @@ -104,7 +104,7 @@ def test_upload_and_query_simple_api(self):

Links for example-minimal-library

- example_minimal_library-0.0.1-py3-none-any.whl
+ example_minimal_library-0.0.1-py3-none-any.whl
""" self.assertEqual( diff --git a/examples/wheel/wheel_test.py b/examples/wheel/wheel_test.py index 9ec150301d..7f19ecd9f9 100644 --- a/examples/wheel/wheel_test.py +++ b/examples/wheel/wheel_test.py @@ -85,7 +85,7 @@ def test_py_library_wheel(self): ], ) self.assertFileSha256Equal( - filename, "0cbf4ec574676015af595f570caf4ae2812f994f6338e247b002b4e496b6fbd5" + filename, "ef5afd9f6c3ff569ef7e5b2799d3a2ec9675d029414f341e0abd7254d6b9a25d" ) def test_py_package_wheel(self): @@ -110,7 +110,7 @@ def test_py_package_wheel(self): ], ) self.assertFileSha256Equal( - filename, "22aff90dd3c8c30c3ce2b729bb793cab0bd2668a6810de232677a0354ce79cae" + filename, "39bec133cf79431e8d057eae550cd91aa9dfbddfedb53d98ebd36e3ade2753d0" ) def test_customized_wheel(self): @@ -144,6 +144,7 @@ def test_customized_wheel(self): "example_customized-0.0.1.dist-info/entry_points.txt" ) + print(record_contents) self.assertEqual( record_contents, # The entries are guaranteed to be sorted. @@ -151,7 +152,7 @@ def test_customized_wheel(self): "examples/wheel/lib/data,with,commas.txt",sha256=9vJKEdfLu8bZRArKLroPZJh1XKkK3qFMXiM79MBL2Sg,12 examples/wheel/lib/data.txt,sha256=9vJKEdfLu8bZRArKLroPZJh1XKkK3qFMXiM79MBL2Sg,12 examples/wheel/lib/module_with_data.py,sha256=8s0Khhcqz3yVsBKv2IB5u4l4TMKh7-c_V6p65WVHPms,637 -examples/wheel/lib/module_with_type_annotations.py,sha256=MM2cFQsCBaUnzGiEGT5r07jhKSaCVRh5Paw_YLyrS-w,636 +examples/wheel/lib/module_with_type_annotations.py,sha256=2p_0YFT0TBUufbGCAR_u2vtxF1nM0lf3dX4VGeUtYq0,637 examples/wheel/lib/module_with_type_annotations.pyi,sha256=fja3ql_WRJ1qO8jyZjWWrTTMcg1J7EpOQivOHY_8vI4,630 examples/wheel/lib/simple_module.py,sha256=z2hwciab_XPNIBNH8B1Q5fYgnJvQTeYf0ZQJpY8yLLY,637 examples/wheel/main.py,sha256=mFiRfzQEDwCHr-WVNQhOH26M42bw1UMF6IoqvtuDTrw,1047 @@ -205,7 +206,7 @@ def test_customized_wheel(self): second = second.main:s""", ) self.assertFileSha256Equal( - filename, "657a938a6fdd6f38bf73d1d91016ffff85d68cf29ca390692a3e9d923dd0e39e" + filename, "685f68fc6665f53c9b769fd1ba12cce9937ab7f40ef4e60c82ef2de8653935de" ) def test_filename_escaping(self): @@ -277,7 +278,7 @@ def test_custom_package_root_wheel(self): for line in record_contents.splitlines(): self.assertFalse(line.startswith("/")) self.assertFileSha256Equal( - filename, "d415edbf8f326161674c1fa260e364dd44f2a0311e2f596284320ea52d2a8bdb" + filename, "2fbfc3baaf6fccca0f97d02316b8344507fe6c8136991a66ee5f162235adb19f" ) def test_custom_package_root_multi_prefix_wheel(self): @@ -311,7 +312,7 @@ def test_custom_package_root_multi_prefix_wheel(self): for line in record_contents.splitlines(): self.assertFalse(line.startswith("/")) self.assertFileSha256Equal( - filename, "6b76a1178c90996feaf3f9417f350c4a67f90f4247647fd4fd552858dc372d4b" + filename, "3e67971ca1e8a9ba36a143df7532e641f5661c56235e41d818309316c955ba58" ) def test_custom_package_root_multi_prefix_reverse_order_wheel(self): @@ -345,7 +346,7 @@ def test_custom_package_root_multi_prefix_reverse_order_wheel(self): for line in record_contents.splitlines(): self.assertFalse(line.startswith("/")) self.assertFileSha256Equal( - filename, "f976f0bb1c7d753e8c41629d6b79fb09908c6ecd2fec006816879fc86b664f3f" + filename, "372ef9e11fb79f1952172993718a326b5adda192d94884b54377c34b44394982" ) def test_python_requires_wheel(self): @@ -370,7 +371,7 @@ def test_python_requires_wheel(self): """, ) self.assertFileSha256Equal( - filename, "f3b74ce429c3324b87f8d1cc7dc33be1493f54bb88d546a7d53be7587b82c1a7" + filename, "10a325ba8f77428b5cfcff6345d508f5eb77c140889eb62490d7382f60d4ebfe" ) def test_python_abi3_binary_wheel(self): @@ -435,7 +436,7 @@ def test_rule_creates_directory_and_is_included_in_wheel(self): ], ) self.assertFileSha256Equal( - filename, "d8e874b807e5574bd11a9312c58ce7fe7055afb80412d0d0e7ed21fc9223cd53" + filename, "85e44c43cc19ccae9fe2e1d629230203aa11791bed1f7f68a069fb58d1c93cd2" ) def test_rule_expands_workspace_status_keys_in_wheel_metadata(self): @@ -564,6 +565,56 @@ def test_extra_requires(self): requires, ) + def test_requires_dist_depends_on_extras(self): + filename = self._get_path("requires_dist_depends_on_extras-0.0.1-py3-none-any.whl") + + with zipfile.ZipFile(filename) as zf: + self.assertAllEntriesHasReproducibleMetadata(zf) + metadata_file = None + for f in zf.namelist(): + if os.path.basename(f) == "METADATA": + metadata_file = f + self.assertIsNotNone(metadata_file) + + requires = [] + with zf.open(metadata_file) as fp: + for line in fp: + if line.startswith(b"Requires-Dist:"): + requires.append(line.decode("utf-8").strip()) + + print(requires) + self.assertEqual( + [ + "Requires-Dist: extra_requires[example]==0.0.1", + ], + requires, + ) + + def test_requires_dist_depends_on_extras_file(self): + filename = self._get_path("requires_dist_depends_on_extras_using_file-0.0.1-py3-none-any.whl") + + with zipfile.ZipFile(filename) as zf: + self.assertAllEntriesHasReproducibleMetadata(zf) + metadata_file = None + for f in zf.namelist(): + if os.path.basename(f) == "METADATA": + metadata_file = f + self.assertIsNotNone(metadata_file) + + requires = [] + with zf.open(metadata_file) as fp: + for line in fp: + if line.startswith(b"Requires-Dist:"): + requires.append(line.decode("utf-8").strip()) + + print(requires) + self.assertEqual( + [ + "Requires-Dist: extra_requires[example]==0.0.1", + ], + requires, + ) + if __name__ == "__main__": unittest.main() diff --git a/gazelle/WORKSPACE b/gazelle/WORKSPACE index 14a124d5f2..ad428b10cd 100644 --- a/gazelle/WORKSPACE +++ b/gazelle/WORKSPACE @@ -42,6 +42,8 @@ load("//:internal_dev_deps.bzl", "internal_dev_deps") internal_dev_deps() +register_toolchains("@rules_python//python/runtime_env_toolchains:all") + load("//:deps.bzl", _py_gazelle_deps = "gazelle_deps") # gazelle:repository_macro deps.bzl%go_deps diff --git a/gazelle/python/testdata/dependency_resolution_order/__init__.py b/gazelle/python/testdata/dependency_resolution_order/__init__.py index e2d0a8a979..4b40aa9f54 100644 --- a/gazelle/python/testdata/dependency_resolution_order/__init__.py +++ b/gazelle/python/testdata/dependency_resolution_order/__init__.py @@ -22,9 +22,8 @@ # we can still override "third_party.foo.bar" import third_party.foo.bar -from third_party import baz - import third_party +from third_party import baz _ = sys _ = bar diff --git a/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py b/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py index eff06de5a7..eb6263b334 100644 --- a/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py +++ b/gazelle/python/testdata/py312_syntax/pep_695_type_parameter.py @@ -17,6 +17,5 @@ def search_one_more_level[T]( import _other_module - if __name__ == "__main__": pass diff --git a/internal_dev_deps.bzl b/internal_dev_deps.bzl index 87690be1ad..f2b33e279e 100644 --- a/internal_dev_deps.bzl +++ b/internal_dev_deps.bzl @@ -68,10 +68,10 @@ def rules_python_internal_deps(): http_archive( name = "rules_pkg", urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz", - "https://github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/1.0.1/rules_pkg-1.0.1.tar.gz", + "https://github.com/bazelbuild/rules_pkg/releases/download/1.0.1/rules_pkg-1.0.1.tar.gz", ], - sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2", + sha256 = "d20c951960ed77cb7b341c2a59488534e494d5ad1d30c4818c736d57772a9fef", ) http_archive( diff --git a/internal_dev_setup.bzl b/internal_dev_setup.bzl index fc38e3f9c5..c37c59a5da 100644 --- a/internal_dev_setup.bzl +++ b/internal_dev_setup.bzl @@ -24,6 +24,7 @@ load("@rules_shell//shell:repositories.bzl", "rules_shell_dependencies", "rules_ load("//:version.bzl", "SUPPORTED_BAZEL_VERSIONS") load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS") load("//python/private:pythons_hub.bzl", "hub_repo") # buildifier: disable=bzl-visibility +load("//python/private:runtime_env_repo.bzl", "runtime_env_repo") # buildifier: disable=bzl-visibility load("//python/private/pypi:deps.bzl", "pypi_deps") # buildifier: disable=bzl-visibility def rules_python_internal_setup(): @@ -33,13 +34,19 @@ def rules_python_internal_setup(): name = "pythons_hub", minor_mapping = MINOR_MAPPING, default_python_version = "", - toolchain_prefixes = [], - toolchain_python_versions = [], - toolchain_set_python_version_constraints = [], - toolchain_user_repository_names = [], python_versions = sorted(TOOL_VERSIONS.keys()), + toolchain_names = [], + toolchain_repo_names = {}, + toolchain_target_compatible_with_map = {}, + toolchain_target_settings_map = {}, + toolchain_platform_keys = {}, + toolchain_python_versions = {}, + toolchain_set_python_version_constraints = {}, + host_compatible_repo_names = [], ) + runtime_env_repo(name = "rules_python_runtime_env_tc_info") + pypi_deps() bazel_skylib_workspace() diff --git a/python/BUILD.bazel b/python/BUILD.bazel index 3389a0dacc..58cff5b99d 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -93,9 +93,9 @@ bzl_library( "//python/private:bzlmod_enabled_bzl", "//python/private:py_package.bzl", "//python/private:py_wheel_bzl", - "//python/private:py_wheel_normalize_pep440.bzl", "//python/private:stamp_bzl", "//python/private:util_bzl", + "//python/private:version.bzl", "@bazel_skylib//rules:native_binary", ], ) @@ -247,6 +247,7 @@ bzl_library( name = "versions_bzl", srcs = ["versions.bzl"], visibility = ["//:__subpackages__"], + deps = ["//python/private:platform_info_bzl"], ) # NOTE: Remember to add bzl_library targets to //tests:bzl_libraries diff --git a/python/api/api.bzl b/python/api/api.bzl index c8fb921c12..d41ec739cd 100644 --- a/python/api/api.bzl +++ b/python/api/api.bzl @@ -1,4 +1,23 @@ -"""Public, analysis phase APIs for Python rules.""" +"""Public, analysis phase APIs for Python rules. + +To use the analyis-time API, add the attributes to your rule, then +use `py_common.get()` to get the api object: + +``` +load("@rules_python//python/api:api.bzl", "py_common") + +def _impl(ctx): + py_api = py_common.get(ctx) + +myrule = rule( + implementation = _impl, + attrs = {...} | py_common.API_ATTRS +) +``` + +:::{versionadded} 0.37.0 +::: +""" load("//python/private/api:api.bzl", _py_common = "py_common") diff --git a/python/bin/BUILD.bazel b/python/bin/BUILD.bazel index 57bee34378..30af7d1b9f 100644 --- a/python/bin/BUILD.bazel +++ b/python/bin/BUILD.bazel @@ -1,4 +1,5 @@ load("//python/private:interpreter.bzl", _interpreter_binary = "interpreter_binary") +load("//python/private:repl.bzl", "py_repl_binary") filegroup( name = "distribution", @@ -22,3 +23,35 @@ label_flag( name = "python_src", build_setting_default = "//python:none", ) + +py_repl_binary( + name = "repl", + stub = ":repl_stub", + visibility = ["//visibility:public"], + deps = [ + ":repl_dep", + ":repl_stub_dep", + ], +) + +# The user can replace this with their own stub. E.g. they can use this to +# import ipython instead of the default shell. +label_flag( + name = "repl_stub", + build_setting_default = "repl_stub.py", +) + +# The user can modify this flag to make an interpreter shell library available +# for the stub. E.g. if they switch the stub for an ipython-based one, then they +# can point this at their version of ipython. +label_flag( + name = "repl_stub_dep", + build_setting_default = "//python/private:empty", +) + +# The user can modify this flag to make arbitrary PyInfo targets available for +# import on the REPL. +label_flag( + name = "repl_dep", + build_setting_default = "//python/private:empty", +) diff --git a/python/bin/repl_stub.py b/python/bin/repl_stub.py new file mode 100644 index 0000000000..1e21b26dc3 --- /dev/null +++ b/python/bin/repl_stub.py @@ -0,0 +1,32 @@ +"""Simulates the REPL that Python spawns when invoking the binary with no arguments. + +The code module is responsible for the default shell. + +The import and `ocde.interact()` call here his is equivalent to doing: + + $ python3 -m code + Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux + Type "help", "copyright", "credits" or "license" for more information. + (InteractiveConsole) + >>> + +The logic for PYTHONSTARTUP is handled in python/private/repl_template.py. +""" + +# Capture the globals from PYTHONSTARTUP so we can pass them on to the console. +console_locals = globals().copy() + +import code +import sys + +if sys.stdin.isatty(): + # Use the default options. + exitmsg = None +else: + # On a non-interactive console, we want to suppress the >>> and the exit message. + exitmsg = "" + sys.ps1 = "" + sys.ps2 = "" + +# We set the banner to an empty string because the repl_template.py file already prints the banner. +code.interact(local=console_locals, banner="", exitmsg=exitmsg) diff --git a/python/config_settings/BUILD.bazel b/python/config_settings/BUILD.bazel index 45354e24d9..ee15828fa5 100644 --- a/python/config_settings/BUILD.bazel +++ b/python/config_settings/BUILD.bazel @@ -11,6 +11,7 @@ load( "PrecompileSourceRetentionFlag", "VenvsSitePackages", "VenvsUseDeclareSymlinkFlag", + rp_string_flag = "string_flag", ) load( "//python/private/pypi:flags.bzl", @@ -87,14 +88,27 @@ string_flag( visibility = ["//visibility:public"], ) -string_flag( +rp_string_flag( name = "bootstrap_impl", - build_setting_default = BootstrapImplFlag.SYSTEM_PYTHON, + build_setting_default = BootstrapImplFlag.SCRIPT, + override = select({ + # Windows doesn't yet support bootstrap=script, so force disable it + ":_is_windows": BootstrapImplFlag.SYSTEM_PYTHON, + "//conditions:default": "", + }), values = sorted(BootstrapImplFlag.__members__.values()), # NOTE: Only public because it's an implicit dependency visibility = ["//visibility:public"], ) +# For some reason, @platforms//os:windows can't be directly used +# in the select() for the flag. But it can be used when put behind +# a config_setting(). +config_setting( + name = "_is_windows", + constraint_values = ["@platforms//os:windows"], +) + # This is used for pip and hermetic toolchain resolution. string_flag( name = "py_linux_libc", @@ -203,6 +217,22 @@ string_flag( visibility = ["//visibility:public"], ) +config_setting( + name = "is_venvs_site_packages", + flag_values = { + ":venvs_site_packages": VenvsSitePackages.YES, + }, + # NOTE: Only public because it is used in whl_library repos. + visibility = ["//visibility:public"], +) + define_pypi_internal_flags( name = "define_pypi_internal_flags", ) + +label_flag( + name = "pip_env_marker_config", + build_setting_default = ":_pip_env_marker_default_config", + # NOTE: Only public because it is used in pip hub repos. + visibility = ["//visibility:public"], +) diff --git a/python/extensions/python.bzl b/python/extensions/python.bzl index abd5080dd8..b8b755ebca 100644 --- a/python/extensions/python.bzl +++ b/python/extensions/python.bzl @@ -20,10 +20,8 @@ The simplest way to configure the toolchain with `rules_python` is as follows. ```starlark python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain( - is_default = True, - python_version = "3.11", -) +python.defaults(python_version = "3.11") +python.toolchain(python_version = "3.11") use_repo(python, "python_3_11") ``` diff --git a/python/features.bzl b/python/features.bzl index 917bd3800c..b678a45241 100644 --- a/python/features.bzl +++ b/python/features.bzl @@ -31,11 +31,11 @@ def _features_typedef(): ::: :::: - ::::{field} py_info_site_packages_symlinks + ::::{field} py_info_venv_symlinks - True if the `PyInfo.site_packages_symlinks` field is available. + True if the `PyInfo.venv_symlinks` field is available. - :::{versionadded} 1.4.0 + :::{versionadded} VERSION_NEXT_FEATURE ::: :::: @@ -61,7 +61,7 @@ features = struct( TYPEDEF = _features_typedef, # keep sorted precompile = True, - py_info_site_packages_symlinks = True, + py_info_venv_symlinks = True, uses_builtin_rules = not config.enable_pystar, version = _VERSION_PRIVATE if "$Format" not in _VERSION_PRIVATE else "", ) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index b63f446be3..b319919305 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -86,6 +86,7 @@ bzl_library( name = "runtime_env_toolchain_bzl", srcs = ["runtime_env_toolchain.bzl"], deps = [ + ":config_settings_bzl", ":py_exec_tools_toolchain_bzl", ":toolchain_types_bzl", "//python:py_runtime_bzl", @@ -138,7 +139,7 @@ bzl_library( name = "config_settings_bzl", srcs = ["config_settings.bzl"], deps = [ - ":semver_bzl", + ":version_bzl", "@bazel_skylib//lib:selects", "@bazel_skylib//rules:common_settings", ], @@ -240,17 +241,23 @@ bzl_library( ], ) +bzl_library( + name = "platform_info_bzl", + srcs = ["platform_info.bzl"], +) + bzl_library( name = "python_bzl", srcs = ["python.bzl"], deps = [ ":full_version_bzl", + ":platform_info_bzl", ":python_register_toolchains_bzl", ":pythons_hub_bzl", ":repo_utils_bzl", - ":semver_bzl", ":toolchains_repo_bzl", ":util_bzl", + ":version_bzl", "@bazel_features//:features", ], ) @@ -609,11 +616,6 @@ bzl_library( ], ) -bzl_library( - name = "semver_bzl", - srcs = ["semver.bzl"], -) - bzl_library( name = "sentinel_bzl", srcs = ["sentinel.bzl"], @@ -657,6 +659,11 @@ bzl_library( ], ) +bzl_library( + name = "version_bzl", + srcs = ["version.bzl"], +) + bzl_library( name = "version_label_bzl", srcs = ["version_label.bzl"], @@ -700,7 +707,7 @@ exports_files( "repack_whl.py", "py_package.bzl", "py_wheel.bzl", - "py_wheel_normalize_pep440.bzl", + "version.bzl", "reexports.bzl", "stamp.bzl", "util.bzl", @@ -816,6 +823,10 @@ current_interpreter_executable( visibility = ["//visibility:public"], ) +py_library( + name = "empty", +) + sentinel( name = "sentinel", ) diff --git a/python/private/api/api.bzl b/python/private/api/api.bzl index 06fb7294b9..44f9ab4e77 100644 --- a/python/private/api/api.bzl +++ b/python/private/api/api.bzl @@ -27,6 +27,17 @@ will depend on the target that is providing the API struct. }, ) +def _py_common_typedef(): + """Typedef for py_common. + + :::{field} API_ATTRS + :type: dict[str, Attribute] + + The attributes that rules must have for `py_common.get()` to work. + ::: + + """ + def _py_common_get(ctx): """Get the py_common API instance. @@ -45,6 +56,7 @@ def _py_common_get(ctx): return ctx.attr._py_common_api[ApiImplInfo].impl py_common = struct( + TYPEDEF = _py_common_typedef, get = _py_common_get, API_ATTRS = { "_py_common_api": attr.label( diff --git a/python/private/api/py_common_api.bzl b/python/private/api/py_common_api.bzl index 401b35973e..6fed245257 100644 --- a/python/private/api/py_common_api.bzl +++ b/python/private/api/py_common_api.bzl @@ -22,17 +22,40 @@ def _py_common_api_impl(ctx): py_common_api = rule( implementation = _py_common_api_impl, - doc = "Rule implementing py_common API.", + doc = "Internal Rule implementing py_common API.", ) +def _py_common_api_typedef(): + """The py_common API implementation. + + An instance of this object is obtained using {obj}`py_common.get()` + """ + def _merge_py_infos(transitive, *, direct = []): - builder = PyInfoBuilder() + """Merge PyInfo objects into a single PyInfo. + + This is a convenience wrapper around {obj}`PyInfoBuilder.merge_all`. For + more control over merging PyInfo objects, use {obj}`PyInfoBuilder`. + + Args: + transitive: {type}`list[PyInfo]` The PyInfo objects with info + considered indirectly provided by something (e.g. via + its deps attribute). + direct: {type}`list[PyInfo]` The PyInfo objects that are + considered directly provided by something (e.g. via + the srcs attribute). + + Returns: + {type}`PyInfo` A PyInfo containing the merged values. + """ + builder = PyInfoBuilder.new() builder.merge_all(transitive, direct = direct) return builder.build() # Exposed for doc generation, not directly used. # buildifier: disable=name-conventions PyCommonApi = struct( + TYPEDEF = _py_common_api_typedef, merge_py_infos = _merge_py_infos, - PyInfoBuilder = PyInfoBuilder, + PyInfoBuilder = PyInfoBuilder.new, ) diff --git a/python/private/attr_builders.bzl b/python/private/attr_builders.bzl index 57fe476109..be9fa22138 100644 --- a/python/private/attr_builders.bzl +++ b/python/private/attr_builders.bzl @@ -1222,7 +1222,7 @@ def _StringList_typedef(): ::: :::{field} default - :type: Value[list[str] | configuration_field] + :type: list[str] | configuration_field ::: :::{function} doc() -> str @@ -1237,6 +1237,9 @@ def _StringList_typedef(): :::{function} set_allow_empty(v: bool) ::: + :::{function} set_default(v: list[str] | configuration_field) + ::: + :::{function} set_doc(v: str) ::: diff --git a/python/private/attributes.bzl b/python/private/attributes.bzl index 8543caba7b..ad8cba2e6c 100644 --- a/python/private/attributes.bzl +++ b/python/private/attributes.bzl @@ -260,7 +260,7 @@ The order of this list can matter because it affects the order that information from dependencies is merged in, which can be relevant depending on the ordering mode of depsets that are merged. -* {obj}`PyInfo.site_packages_symlinks` uses topological ordering. +* {obj}`PyInfo.venv_symlinks` uses topological ordering. See {obj}`PyInfo` for more information about the ordering of its depsets and how its fields are merged. @@ -397,14 +397,14 @@ COVERAGE_ATTRS = { "_collect_cc_coverage": lambda: attrb.Label( default = "@bazel_tools//tools/test:collect_cc_coverage", executable = True, - cfg = "exec", + cfg = config.exec(exec_group = "test"), ), # Magic attribute to make coverage work. There's no # docs about this; see TestActionBuilder.java "_lcov_merger": lambda: attrb.Label( default = configuration_field(fragment = "coverage", name = "output_generator"), executable = True, - cfg = "exec", + cfg = config.exec(exec_group = "test"), ), } diff --git a/python/private/common.bzl b/python/private/common.bzl index 072a1bb296..e49dbad20c 100644 --- a/python/private/common.bzl +++ b/python/private/common.bzl @@ -378,7 +378,7 @@ def create_py_info( implicit_pyc_files, implicit_pyc_source_files, imports, - site_packages_symlinks = []): + venv_symlinks = []): """Create PyInfo provider. Args: @@ -396,7 +396,7 @@ def create_py_info( implicit_pyc_files: {type}`depset[File]` Implicitly generated pyc files that a binary can choose to include. imports: depset of strings; the import path values to propagate. - site_packages_symlinks: {type}`list[tuple[str, str]]` tuples of + venv_symlinks: {type}`list[tuple[str, str]]` tuples of `(runfiles_path, site_packages_path)` for symlinks to create in the consuming binary's venv site packages. @@ -405,8 +405,8 @@ def create_py_info( transitive sources collected from dependencies (the latter is only necessary for deprecated extra actions support). """ - py_info = PyInfoBuilder() - py_info.site_packages_symlinks.add(site_packages_symlinks) + py_info = PyInfoBuilder.new() + py_info.venv_symlinks.add(venv_symlinks) py_info.direct_original_sources.add(original_sources) py_info.direct_pyc_files.add(required_pyc_files) py_info.direct_pyi_files.add(ctx.files.pyi_srcs) diff --git a/python/private/config_settings.bzl b/python/private/config_settings.bzl index e5f9d865d1..aff5d016fb 100644 --- a/python/private/config_settings.bzl +++ b/python/private/config_settings.bzl @@ -18,7 +18,7 @@ load("@bazel_skylib//lib:selects.bzl", "selects") load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("//python/private:text_util.bzl", "render") -load(":semver.bzl", "semver") +load(":version.bzl", "version") _PYTHON_VERSION_FLAG = Label("//python/config_settings:python_version") _PYTHON_VERSION_MAJOR_MINOR_FLAG = Label("//python/config_settings:python_version_major_minor") @@ -31,6 +31,10 @@ If the value is missing, then the default value is being used, see documentation {docs_url}/python/config_settings """ +# Indicates something needs public visibility so that other generated code can +# access it, but it's not intended for general public usage. +_NOT_ACTUALLY_PUBLIC = ["//visibility:public"] + def construct_config_settings(*, name, default_version, versions, minor_mapping, documented_flags): # buildifier: disable=function-docstring """Create a 'python_version' config flag and construct all config settings used in rules_python. @@ -128,7 +132,30 @@ def construct_config_settings(*, name, default_version, versions, minor_mapping, # `whl_library` in the hub repo created by `pip.parse`. flag_values = {"current_config": "will-never-match"}, # Only public so that PyPI hub repo can access it - visibility = ["//visibility:public"], + visibility = _NOT_ACTUALLY_PUBLIC, + ) + + libc = Label("//python/config_settings:py_linux_libc") + native.config_setting( + name = "_is_py_linux_libc_glibc", + flag_values = {libc: "glibc"}, + visibility = _NOT_ACTUALLY_PUBLIC, + ) + native.config_setting( + name = "_is_py_linux_libc_musl", + flag_values = {libc: "glibc"}, + visibility = _NOT_ACTUALLY_PUBLIC, + ) + freethreaded = Label("//python/config_settings:py_freethreaded") + native.config_setting( + name = "_is_py_freethreaded_yes", + flag_values = {freethreaded: "yes"}, + visibility = _NOT_ACTUALLY_PUBLIC, + ) + native.config_setting( + name = "_is_py_freethreaded_no", + flag_values = {freethreaded: "no"}, + visibility = _NOT_ACTUALLY_PUBLIC, ) def _python_version_flag_impl(ctx): @@ -154,8 +181,8 @@ _python_version_flag = rule( def _python_version_major_minor_flag_impl(ctx): input = _flag_value(ctx.attr._python_version_flag) if input: - version = semver(input) - value = "{}.{}".format(version.major, version.minor) + ver = version.parse(input) + value = "{}.{}".format(ver.release[0], ver.release[1]) else: value = "" @@ -209,3 +236,42 @@ _current_config = rule( "_template": attr.string(default = _DEBUG_ENV_MESSAGE_TEMPLATE), }, ) + +def is_python_version_at_least(name, **kwargs): + flag_name = "_{}_flag".format(name) + native.config_setting( + name = name, + flag_values = { + flag_name: "yes", + }, + ) + _python_version_at_least( + name = flag_name, + visibility = ["//visibility:private"], + **kwargs + ) + +def _python_version_at_least_impl(ctx): + flag_value = ctx.attr._major_minor[config_common.FeatureFlagInfo].value + + # CI is, somehow, getting an empty string for the current flag value. + # How isn't clear. + if not flag_value: + return [config_common.FeatureFlagInfo(value = "no")] + + current = tuple([ + int(x) + for x in flag_value.split(".") + ]) + at_least = tuple([int(x) for x in ctx.attr.at_least.split(".")]) + + value = "yes" if current >= at_least else "no" + return [config_common.FeatureFlagInfo(value = value)] + +_python_version_at_least = rule( + implementation = _python_version_at_least_impl, + attrs = { + "at_least": attr.string(mandatory = True), + "_major_minor": attr.label(default = _PYTHON_VERSION_MAJOR_MINOR_FLAG), + }, +) diff --git a/python/private/flags.bzl b/python/private/flags.bzl index c53e4610ff..710402ba68 100644 --- a/python/private/flags.bzl +++ b/python/private/flags.bzl @@ -35,8 +35,38 @@ AddSrcsToRunfilesFlag = FlagEnum( is_enabled = _AddSrcsToRunfilesFlag_is_enabled, ) +def _string_flag_impl(ctx): + if ctx.attr.override: + value = ctx.attr.override + else: + value = ctx.build_setting_value + + if value not in ctx.attr.values: + fail(( + "Invalid value for {name}: got {value}, must " + + "be one of {allowed}" + ).format( + name = ctx.label, + value = value, + allowed = ctx.attr.values, + )) + + return [ + BuildSettingInfo(value = value), + config_common.FeatureFlagInfo(value = value), + ] + +string_flag = rule( + implementation = _string_flag_impl, + build_setting = config.string(flag = True), + attrs = { + "override": attr.string(), + "values": attr.string_list(), + }, +) + def _bootstrap_impl_flag_get_value(ctx): - return ctx.attr._bootstrap_impl_flag[BuildSettingInfo].value + return ctx.attr._bootstrap_impl_flag[config_common.FeatureFlagInfo].value # buildifier: disable=name-conventions BootstrapImplFlag = enum( @@ -124,12 +154,12 @@ def _venvs_site_packages_is_enabled(ctx): flag_value = ctx.attr.experimental_venvs_site_packages[BuildSettingInfo].value return flag_value == VenvsSitePackages.YES -# Decides if libraries try to use a site-packages layout using site_packages_symlinks +# Decides if libraries try to use a site-packages layout using venv_symlinks # buildifier: disable=name-conventions VenvsSitePackages = FlagEnum( - # Use site_packages_symlinks + # Use venv_symlinks YES = "yes", - # Don't use site_packages_symlinks + # Don't use venv_symlinks NO = "no", is_enabled = _venvs_site_packages_is_enabled, ) diff --git a/python/private/get_local_runtime_info.py b/python/private/get_local_runtime_info.py index 0207f56bef..19db3a2935 100644 --- a/python/private/get_local_runtime_info.py +++ b/python/private/get_local_runtime_info.py @@ -22,6 +22,7 @@ "micro": sys.version_info.micro, "include": sysconfig.get_path("include"), "implementation_name": sys.implementation.name, + "base_executable": sys._base_executable, } config_vars = [ diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index 64d721ecad..f944b0b914 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -20,7 +20,7 @@ load("//python:py_runtime_pair.bzl", "py_runtime_pair") load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain") load(":glob_excludes.bzl", "glob_excludes") load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain") -load(":semver.bzl", "semver") +load(":version.bzl", "version") _IS_FREETHREADED = Label("//python/config_settings:is_py_freethreaded") @@ -53,8 +53,11 @@ def define_hermetic_runtime_toolchain_impl( use. """ _ = name # @unused - version_info = semver(python_version) - version_dict = version_info.to_dict() + version_info = version.parse(python_version) + version_dict = { + "major": version_info.release[0], + "minor": version_info.release[1], + } native.filegroup( name = "files", srcs = native.glob( @@ -198,9 +201,9 @@ def define_hermetic_runtime_toolchain_impl( files = [":files"], interpreter = python_bin, interpreter_version_info = { - "major": str(version_info.major), - "micro": str(version_info.patch), - "minor": str(version_info.minor), + "major": str(version_info.release[0]), + "micro": str(version_info.release[2]), + "minor": str(version_info.release[1]), }, coverage_tool = select({ # Convert empty string to None diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index a5c4787161..cfe2fdfd77 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl @@ -20,6 +20,8 @@ settings for rules to later use. load(":repo_utils.bzl", "repo_utils") +_ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR" +_ENABLE_PIPSTAR_DEFAULT = "0" _ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR" _ENABLE_PYSTAR_DEFAULT = "1" _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS" @@ -28,6 +30,7 @@ _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0" _CONFIG_TEMPLATE = """\ config = struct( enable_pystar = {enable_pystar}, + enable_pipstar = {enable_pipstar}, enable_deprecation_warnings = {enable_deprecation_warnings}, BuiltinPyInfo = getattr(getattr(native, "legacy_globals", None), "PyInfo", {builtin_py_info_symbol}), BuiltinPyRuntimeInfo = getattr(getattr(native, "legacy_globals", None), "PyRuntimeInfo", {builtin_py_runtime_info_symbol}), @@ -84,6 +87,7 @@ def _internal_config_repo_impl(rctx): rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format( enable_pystar = enable_pystar, + enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT), enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT), builtin_py_info_symbol = builtin_py_info_symbol, builtin_py_runtime_info_symbol = builtin_py_runtime_info_symbol, diff --git a/python/private/internal_dev_deps.bzl b/python/private/internal_dev_deps.bzl index 2a3b84e7df..600c934ace 100644 --- a/python/private/internal_dev_deps.bzl +++ b/python/private/internal_dev_deps.bzl @@ -14,22 +14,11 @@ """Module extension for internal dev_dependency=True setup.""" load("@bazel_ci_rules//:rbe_repo.bzl", "rbe_preconfig") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") +load(":runtime_env_repo.bzl", "runtime_env_repo") def _internal_dev_deps_impl(mctx): _ = mctx # @unused - # This wheel is purely here to validate the wheel extraction code. It's not - # intended for anything else. - http_file( - name = "wheel_for_testing", - downloaded_file_path = "numpy-1.25.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", - sha256 = "0d60fbae8e0019865fc4784745814cff1c421df5afee233db6d88ab4f14655a2", - urls = [ - "https://files.pythonhosted.org/packages/50/67/3e966d99a07d60a21a21d7ec016e9e4c2642a86fea251ec68677daf71d4d/numpy-1.25.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", - ], - ) - # Creates a default toolchain config for RBE. # Use this as is if you are using the rbe_ubuntu16_04 container, # otherwise refer to RBE docs. @@ -37,6 +26,7 @@ def _internal_dev_deps_impl(mctx): name = "buildkite_config", toolchain = "ubuntu1804-bazel-java11", ) + runtime_env_repo(name = "rules_python_runtime_env_tc_info") internal_dev_deps = module_extension( implementation = _internal_dev_deps_impl, diff --git a/python/private/local_runtime_repo.bzl b/python/private/local_runtime_repo.bzl index fb1a8e29ac..ec0643e497 100644 --- a/python/private/local_runtime_repo.bzl +++ b/python/private/local_runtime_repo.bzl @@ -84,6 +84,20 @@ def _local_runtime_repo_impl(rctx): info = json.decode(exec_result.stdout) logger.info(lambda: _format_get_info_result(info)) + # We use base_executable because we want the path within a Python + # installation directory ("PYTHONHOME"). The problems with sys.executable + # are: + # * If we're in an activated venv, then we don't want the venv's + # `bin/python3` path to be used -- it isn't an actual Python installation. + # * If sys.executable is a wrapper (e.g. pyenv), then (1) it may not be + # located within an actual Python installation directory, and (2) it + # can interfer with Python recognizing when it's within a venv. + # + # In some cases, it may be a symlink (usually e.g. `python3->python3.12`), + # but we don't realpath() it to respect what it has decided is the + # appropriate path. + interpreter_path = info["base_executable"] + # NOTE: Keep in sync with recursive glob in define_local_runtime_toolchain_impl repo_utils.watch_tree(rctx, rctx.path(info["include"])) diff --git a/python/private/local_runtime_toolchains_repo.bzl b/python/private/local_runtime_toolchains_repo.bzl index adb3bb560d..004ca664ad 100644 --- a/python/private/local_runtime_toolchains_repo.bzl +++ b/python/private/local_runtime_toolchains_repo.bzl @@ -26,6 +26,9 @@ define_local_toolchain_suites( name = "toolchains", version_aware_repo_names = {version_aware_names}, version_unaware_repo_names = {version_unaware_names}, + repo_exec_compatible_with = {repo_exec_compatible_with}, + repo_target_compatible_with = {repo_target_compatible_with}, + repo_target_settings = {repo_target_settings}, ) """ @@ -39,6 +42,9 @@ def _local_runtime_toolchains_repo(rctx): rctx.file("BUILD.bazel", _TOOLCHAIN_TEMPLATE.format( version_aware_names = render.list(rctx.attr.runtimes), + repo_target_settings = render.string_list_dict(rctx.attr.target_settings), + repo_target_compatible_with = render.string_list_dict(rctx.attr.target_compatible_with), + repo_exec_compatible_with = render.string_list_dict(rctx.attr.exec_compatible_with), version_unaware_names = render.list(rctx.attr.default_runtimes or rctx.attr.runtimes), )) @@ -62,8 +68,36 @@ These will be defined as *version-unaware* toolchains. This means they will match any Python version. As such, they are registered after the version-aware toolchains defined by the `runtimes` attribute. +If not set, then the `runtimes` values will be used. + Note that order matters: it determines the toolchain priority within the package. +""", + ), + "exec_compatible_with": attr.string_list_dict( + doc = """ +Constraints that must be satisfied by an exec platform for a toolchain to be used. + +This is a `dict[str, list[str]]`, where the keys are repo names from the +`runtimes` or `default_runtimes` args, and the values are constraint +target labels (e.g. OS, CPU, etc). + +:::{note} +Specify `@//foo:bar`, not simply `//foo:bar` or `:bar`. The additional `@` is +needed because the strings are evaluated in a different context than where +they originate. +::: + +The list of settings become the {obj}`toolchain.exec_compatible_with` value for +each respective repo. + +This allows a local toolchain to only be used if certain exec platform +conditions are met, typically values from `@platforms`. + +See the [Local toolchains] docs for examples and further information. + +:::{versionadded} VERSION_NEXT_FEATURE +::: """, ), "runtimes": attr.string_list( @@ -76,6 +110,81 @@ are registered before `default_runtimes`. Note that order matters: it determines the toolchain priority within the package. +""", + ), + "target_compatible_with": attr.string_list_dict( + doc = """ +Constraints that must be satisfied for a toolchain to be used. + + +This is a `dict[str, list[str]]`, where the keys are repo names from the +`runtimes` or `default_runtimes` args, and the values are constraint +target labels (e.g. OS, CPU, etc), or the special string `"HOST_CONSTRAINTS"` +(which will be replaced with the current Bazel hosts's constraints). + +If a repo's entry is missing or empty, it defaults to the supported OS the +underlying runtime repository detects as compatible. + +:::{note} +Specify `@//foo:bar`, not simply `//foo:bar` or `:bar`. The additional `@` is +needed because the strings are evaluated in a different context than where +they originate. +::: + +The list of settings **becomes the** the {obj}`toolchain.target_compatible_with` +value for each respective repo; i.e. they _replace_ the auto-detected values +the local runtime itself computes. + +This allows a local toolchain to only be used if certain target platform +conditions are met, typically values from `@platforms`. + +See the [Local toolchains] docs for examples and further information. + +:::{seealso} +The `target_settings` attribute, which handles `config_setting` values, +instead of constraints. +::: + +:::{versionadded} VERSION_NEXT_FEATURE +::: +""", + ), + "target_settings": attr.string_list_dict( + doc = """ +Config settings that must be satisfied for a toolchain to be used. + +This is a `dict[str, list[str]]`, where the keys are repo names from the +`runtimes` or `default_runtimes` args, and the values are {obj}`config_setting()` +target labels. + +If a repo's entry is missing or empty, it will default to +`@//:is_match_python_version` (for repos in `runtimes`) or an empty list +(for repos in `default_runtimes`). + +:::{note} +Specify `@//foo:bar`, not simply `//foo:bar` or `:bar`. The additional `@` is +needed because the strings are evaluated in a different context than where +they originate. +::: + +The list of settings will be applied atop of any of the local runtime's +settings that are used for {obj}`toolchain.target_settings`. i.e. they are +evaluated first and guard the checking of the local runtime's auto-detected +conditions. + +This allows a local toolchain to only be used if certain flags or +config setting conditions are met. Such conditions can include user-defined +flags, platform constraints, etc. + +See the [Local toolchains] docs for examples and further information. + +:::{seealso} +The `target_compatible_with` attribute, which handles *constraint* values, +instead of `config_settings`. +::: + +:::{versionadded} VERSION_NEXT_FEATURE +::: """, ), "_rule_name": attr.string(default = "local_toolchains_repo"), diff --git a/python/private/platform_info.bzl b/python/private/platform_info.bzl new file mode 100644 index 0000000000..3f7dc00165 --- /dev/null +++ b/python/private/platform_info.bzl @@ -0,0 +1,34 @@ +"""Helper to define a struct used to define platform metadata.""" + +def platform_info( + *, + compatible_with = [], + flag_values = {}, + target_settings = [], + os_name, + arch): + """Creates a struct of platform metadata. + + This is just a helper to ensure structs are created the same and + the meaning/values are documented. + + Args: + compatible_with: list[str], where the values are string labels. These + are the target_compatible_with values to use with the toolchain + flag_values: dict[str|Label, Any] of config_setting.flag_values + compatible values. DEPRECATED -- use target_settings instead + target_settings: list[str], where the values are string labels. These + are the target_settings values to use with the toolchain. + os_name: str, the os name; must match the name used in `@platfroms//os` + arch: str, the cpu name; must match the name used in `@platforms//cpu` + + Returns: + A struct with attributes and values matching the args. + """ + return struct( + compatible_with = compatible_with, + flag_values = flag_values, + target_settings = target_settings, + os_name = os_name, + arch = arch, + ) diff --git a/python/private/py_console_script_binary.bzl b/python/private/py_console_script_binary.bzl index 7347ebe16a..d98457dbe1 100644 --- a/python/private/py_console_script_binary.bzl +++ b/python/private/py_console_script_binary.bzl @@ -52,22 +52,25 @@ def py_console_script_binary( entry_points_txt = None, script = None, binary_rule = py_binary, + shebang = "", **kwargs): """Generate a py_binary for a console_script entry_point. Args: - name: [`target-name`] The name of the resulting target. - pkg: {any}`simple label` the package for which to generate the script. - entry_points_txt: optional [`label`], the entry_points.txt file to parse + name: {type}`Name` The name of the resulting target. + pkg: {type}`Label` the package for which to generate the script. + entry_points_txt: {type}`label | None`, the entry_points.txt file to parse for available console_script values. It may be a single file, or a group of files, but must contain a file named `entry_points.txt`. If not specified, defaults to the `dist_info` target in the same package as the `pkg` Label. - script: [`str`], The console script name that the py_binary is going to be + script: {type}`str`, The console script name that the py_binary is going to be generated for. Defaults to the normalized name attribute. - binary_rule: {any}`rule callable`, The rule/macro to use to instantiate - the target. It's expected to behave like {any}`py_binary`. - Defaults to {any}`py_binary`. + binary_rule: {type}`callable`, The rule/macro to use to instantiate + the target. It's expected to behave like {obj}`py_binary`. + Defaults to {obj}`py_binary`. + shebang: {type}`str`, The shebang to use for the entry point python file. + Defaults to empty string. **kwargs: Extra parameters forwarded to `binary_rule`. """ main = "rules_python_entry_point_{}.py".format(name) @@ -81,6 +84,7 @@ def py_console_script_binary( out = main, console_script = script, console_script_guess = name, + shebang = shebang, visibility = ["//visibility:private"], ) diff --git a/python/private/py_console_script_gen.bzl b/python/private/py_console_script_gen.bzl index 7dd4dd2dad..de016036b2 100644 --- a/python/private/py_console_script_gen.bzl +++ b/python/private/py_console_script_gen.bzl @@ -42,6 +42,7 @@ def _py_console_script_gen_impl(ctx): args = ctx.actions.args() args.add("--console-script", ctx.attr.console_script) args.add("--console-script-guess", ctx.attr.console_script_guess) + args.add("--shebang", ctx.attr.shebang) args.add(entry_points_txt) args.add(ctx.outputs.out) @@ -81,6 +82,10 @@ py_console_script_gen = rule( doc = "Output file location.", mandatory = True, ), + "shebang": attr.string( + doc = "The shebang to use for the entry point python file.", + default = "", + ), "_tool": attr.label( default = ":py_console_script_gen_py", executable = True, diff --git a/python/private/py_console_script_gen.py b/python/private/py_console_script_gen.py index ffc4e81b3a..4b4f2f6986 100644 --- a/python/private/py_console_script_gen.py +++ b/python/private/py_console_script_gen.py @@ -44,7 +44,7 @@ _ENTRY_POINTS_TXT = "entry_points.txt" _TEMPLATE = """\ -import sys +{shebang}import sys # See @rules_python//python/private:py_console_script_gen.py for explanation if getattr(sys.flags, "safe_path", False): @@ -87,6 +87,7 @@ def run( out: pathlib.Path, console_script: str, console_script_guess: str, + shebang: str, ): """Run the generator @@ -94,6 +95,8 @@ def run( entry_points: The entry_points.txt file to be parsed. out: The output file. console_script: The console_script entry in the entry_points.txt file. + console_script_guess: The string used for guessing the console_script if it is not provided. + shebang: The shebang to use for the entry point python file. Defaults to empty string (no shebang). """ config = EntryPointsParser() config.read(entry_points) @@ -136,6 +139,7 @@ def run( with open(out, "w") as f: f.write( _TEMPLATE.format( + shebang=f"{shebang}\n" if shebang else "", module=module, attr=attr, entry_point=entry_point, @@ -154,6 +158,10 @@ def main(): required=True, help="The string used for guessing the console_script if it is not provided.", ) + parser.add_argument( + "--shebang", + help="The shebang to use for the entry point python file.", + ) parser.add_argument( "entry_points", metavar="ENTRY_POINTS_TXT", @@ -173,6 +181,7 @@ def main(): out=args.out, console_script=args.console_script, console_script_guess=args.console_script_guess, + shebang=args.shebang, ) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index dd3ad869fa..7c3e0cb757 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -54,7 +54,7 @@ load(":flags.bzl", "BootstrapImplFlag", "VenvsUseDeclareSymlinkFlag") load(":precompile.bzl", "maybe_precompile") load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo") load(":py_executable_info.bzl", "PyExecutableInfo") -load(":py_info.bzl", "PyInfo") +load(":py_info.bzl", "PyInfo", "VenvSymlinkKind") load(":py_internal.bzl", "py_internal") load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo") load(":reexports.bzl", "BuiltinPyInfo", "BuiltinPyRuntimeInfo") @@ -78,7 +78,6 @@ EXECUTABLE_ATTRS = dicts.add( AGNOSTIC_EXECUTABLE_ATTRS, PY_SRCS_ATTRS, IMPORTS_ATTRS, - COVERAGE_ATTRS, { "interpreter_args": lambda: attrb.StringList( doc = """ @@ -350,6 +349,7 @@ def _create_executable( main_py = main_py, imports = imports, runtime_details = runtime_details, + venv = venv, ) extra_runfiles = ctx.runfiles([stage2_bootstrap] + venv.files_without_interpreter) zip_main = _create_zip_main( @@ -538,11 +538,15 @@ def _create_venv(ctx, output_prefix, imports, runtime_details): ctx.actions.write(pyvenv_cfg, "") runtime = runtime_details.effective_runtime + venvs_use_declare_symlink_enabled = ( VenvsUseDeclareSymlinkFlag.get_value(ctx) == VenvsUseDeclareSymlinkFlag.YES ) + recreate_venv_at_runtime = False + bin_dir = "{}/bin".format(venv) - if not venvs_use_declare_symlink_enabled: + if not venvs_use_declare_symlink_enabled or not runtime.supports_build_time_venv: + recreate_venv_at_runtime = True if runtime.interpreter: interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path) else: @@ -553,17 +557,19 @@ def _create_venv(ctx, output_prefix, imports, runtime_details): # When the venv symlinks are disabled, the $venv/bin/python3 file isn't # needed or used at runtime. However, the zip code uses the interpreter # File object to figure out some paths. - interpreter = ctx.actions.declare_file("{}/bin/{}".format(venv, py_exe_basename)) + interpreter = ctx.actions.declare_file("{}/{}".format(bin_dir, py_exe_basename)) ctx.actions.write(interpreter, "actual:{}".format(interpreter_actual_path)) elif runtime.interpreter: + # Some wrappers around the interpreter (e.g. pyenv) use the program + # name to decide what to do, so preserve the name. py_exe_basename = paths.basename(runtime.interpreter.short_path) # Even though ctx.actions.symlink() is used, using # declare_symlink() is required to ensure that the resulting file # in runfiles is always a symlink. An RBE implementation, for example, # may choose to write what symlink() points to instead. - interpreter = ctx.actions.declare_symlink("{}/bin/{}".format(venv, py_exe_basename)) + interpreter = ctx.actions.declare_symlink("{}/{}".format(bin_dir, py_exe_basename)) interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path) rel_path = relative_path( @@ -576,7 +582,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details): ctx.actions.symlink(output = interpreter, target_path = rel_path) else: py_exe_basename = paths.basename(runtime.interpreter_path) - interpreter = ctx.actions.declare_symlink("{}/bin/{}".format(venv, py_exe_basename)) + interpreter = ctx.actions.declare_symlink("{}/{}".format(bin_dir, py_exe_basename)) ctx.actions.symlink(output = interpreter, target_path = runtime.interpreter_path) interpreter_actual_path = runtime.interpreter_path @@ -594,7 +600,8 @@ def _create_venv(ctx, output_prefix, imports, runtime_details): if "t" in runtime.abi_flags: version += "t" - site_packages = "{}/lib/python{}/site-packages".format(venv, version) + venv_site_packages = "lib/python{}/site-packages".format(version) + site_packages = "{}/{}".format(venv, venv_site_packages) pth = ctx.actions.declare_file("{}/bazel.pth".format(site_packages)) ctx.actions.write(pth, "import _bazel_site_init\n") @@ -612,87 +619,104 @@ def _create_venv(ctx, output_prefix, imports, runtime_details): }, computed_substitutions = computed_subs, ) - site_packages_symlinks = _create_site_packages_symlinks(ctx, site_packages) + + venv_dir_map = { + VenvSymlinkKind.BIN: bin_dir, + VenvSymlinkKind.LIB: site_packages, + } + venv_symlinks = _create_venv_symlinks(ctx, venv_dir_map) return struct( interpreter = interpreter, - recreate_venv_at_runtime = not venvs_use_declare_symlink_enabled, + recreate_venv_at_runtime = recreate_venv_at_runtime, # Runfiles root relative path or absolute path interpreter_actual_path = interpreter_actual_path, - files_without_interpreter = [pyvenv_cfg, pth, site_init] + site_packages_symlinks, + files_without_interpreter = [pyvenv_cfg, pth, site_init] + venv_symlinks, + # string; venv-relative path to the site-packages directory. + venv_site_packages = venv_site_packages, ) -def _create_site_packages_symlinks(ctx, site_packages): - """Creates symlinks within site-packages. +def _create_venv_symlinks(ctx, venv_dir_map): + """Creates symlinks within the venv. Args: ctx: current rule ctx - site_packages: runfiles-root-relative path to the site-packages directory + venv_dir_map: mapping of VenvSymlinkKind constants to the + venv path. Returns: {type}`list[File]` list of the File symlink objects created. """ - # maps site-package symlink to the runfiles path it should point to + # maps venv-relative path to the runfiles path it should point to entries = depset( # NOTE: Topological ordering is used so that dependencies closer to the # binary have precedence in creating their symlinks. This allows the # binary a modicum of control over the result. order = "topological", transitive = [ - dep[PyInfo].site_packages_symlinks + dep[PyInfo].venv_symlinks for dep in ctx.attr.deps if PyInfo in dep ], ).to_list() + link_map = _build_link_map(entries) + venv_files = [] + for kind, kind_map in link_map.items(): + base = venv_dir_map[kind] + for venv_path, link_to in kind_map.items(): + venv_link = ctx.actions.declare_symlink(paths.join(base, venv_path)) + venv_link_rf_path = runfiles_root_path(ctx, venv_link.short_path) + rel_path = relative_path( + # dirname is necessary because a relative symlink is relative to + # the directory the symlink resides within. + from_ = paths.dirname(venv_link_rf_path), + to = link_to, + ) + ctx.actions.symlink(output = venv_link, target_path = rel_path) + venv_files.append(venv_link) - sp_files = [] - for sp_dir_path, link_to in link_map.items(): - sp_link = ctx.actions.declare_symlink(paths.join(site_packages, sp_dir_path)) - sp_link_rf_path = runfiles_root_path(ctx, sp_link.short_path) - rel_path = relative_path( - # dirname is necessary because a relative symlink is relative to - # the directory the symlink resides within. - from_ = paths.dirname(sp_link_rf_path), - to = link_to, - ) - ctx.actions.symlink(output = sp_link, target_path = rel_path) - sp_files.append(sp_link) - return sp_files + return venv_files def _build_link_map(entries): + # dict[str kind, dict[str rel_path, str link_to_path]] link_map = {} - for link_to_runfiles_path, site_packages_path in entries: - if site_packages_path in link_map: + for entry in entries: + kind = entry.kind + kind_map = link_map.setdefault(kind, {}) + if entry.venv_path in kind_map: # We ignore duplicates by design. The dependency closer to the # binary gets precedence due to the topological ordering. continue else: - link_map[site_packages_path] = link_to_runfiles_path + kind_map[entry.venv_path] = entry.link_to_path # An empty link_to value means to not create the site package symlink. # Because of the topological ordering, this allows binaries to remove # entries by having an earlier dependency produce empty link_to values. - for sp_dir_path, link_to in link_map.items(): - if not link_to: - link_map.pop(sp_dir_path) + for kind, kind_map in link_map.items(): + for dir_path, link_to in kind_map.items(): + if not link_to: + kind_map.pop(dir_path) - # Remove entries that would be a child path of a created symlink. - # Earlier entries have precedence to match how exact matches are handled. + # dict[str kind, dict[str rel_path, str link_to_path]] keep_link_map = {} - for _ in range(len(link_map)): - if not link_map: - break - dirname, value = link_map.popitem() - keep_link_map[dirname] = value - - prefix = dirname + "/" # Add slash to prevent /X matching /XY - for maybe_suffix in link_map.keys(): - maybe_suffix += "/" # Add slash to prevent /X matching /XY - if maybe_suffix.startswith(prefix) or prefix.startswith(maybe_suffix): - link_map.pop(maybe_suffix) + # Remove entries that would be a child path of a created symlink. + # Earlier entries have precedence to match how exact matches are handled. + for kind, kind_map in link_map.items(): + keep_kind_map = keep_link_map.setdefault(kind, {}) + for _ in range(len(kind_map)): + if not kind_map: + break + dirname, value = kind_map.popitem() + keep_kind_map[dirname] = value + prefix = dirname + "/" # Add slash to prevent /X matching /XY + for maybe_suffix in kind_map.keys(): + maybe_suffix += "/" # Add slash to prevent /X matching /XY + if maybe_suffix.startswith(prefix) or prefix.startswith(maybe_suffix): + kind_map.pop(maybe_suffix) return keep_link_map def _map_each_identity(v): @@ -716,7 +740,8 @@ def _create_stage2_bootstrap( output_sibling, main_py, imports, - runtime_details): + runtime_details, + venv = None): output = ctx.actions.declare_file( # Prepend with underscore to prevent pytest from trying to # process the bootstrap for files starting with `test_` @@ -731,6 +756,14 @@ def _create_stage2_bootstrap( main_py_path = "{}/{}".format(ctx.workspace_name, main_py.short_path) else: main_py_path = "" + + # The stage2 bootstrap uses the venv site-packages location to fix up issues + # that occur when the toolchain doesn't support the build-time venv. + if venv and not runtime.supports_build_time_venv: + venv_rel_site_packages = venv.venv_site_packages + else: + venv_rel_site_packages = "" + ctx.actions.expand_template( template = template, output = output, @@ -741,6 +774,7 @@ def _create_stage2_bootstrap( "%main%": main_py_path, "%main_module%": ctx.attr.main_module, "%target%": str(ctx.label), + "%venv_rel_site_packages%": venv_rel_site_packages, "%workspace_name%": ctx.workspace_name, }, is_executable = True, @@ -766,6 +800,12 @@ def _create_stage1_bootstrap( python_binary_actual = venv.interpreter_actual_path if venv else "" + # Runtime may be None on Windows due to the --python_path flag. + if runtime and runtime.supports_build_time_venv: + resolve_python_binary_at_runtime = "0" + else: + resolve_python_binary_at_runtime = "1" + subs = { "%interpreter_args%": "\n".join([ '"{}"'.format(v) @@ -775,7 +815,9 @@ def _create_stage1_bootstrap( "%python_binary%": python_binary_path, "%python_binary_actual%": python_binary_actual, "%recreate_venv_at_runtime%": str(int(venv.recreate_venv_at_runtime)) if venv else "0", + "%resolve_python_binary_at_runtime%": resolve_python_binary_at_runtime, "%target%": str(ctx.label), + "%venv_rel_site_packages%": venv.venv_site_packages if venv else "", "%workspace_name%": ctx.workspace_name, } @@ -1854,6 +1896,8 @@ def create_base_executable_rule(): """ return create_executable_rule_builder().build() +_MaybeBuiltinPyInfo = [BuiltinPyInfo] if BuiltinPyInfo != None else [] + # NOTE: Exported publicly def create_executable_rule_builder(implementation, **kwargs): """Create a rule builder for an executable Python program. @@ -1874,10 +1918,10 @@ def create_executable_rule_builder(implementation, **kwargs): """ builder = ruleb.Rule( implementation = implementation, - attrs = EXECUTABLE_ATTRS, + attrs = EXECUTABLE_ATTRS | (COVERAGE_ATTRS if kwargs.get("test") else {}), exec_groups = dict(REQUIRED_EXEC_GROUP_BUILDERS), # Mutable copy fragments = ["py", "bazel_py"], - provides = [PyExecutableInfo], + provides = [PyExecutableInfo, PyInfo] + _MaybeBuiltinPyInfo, toolchains = [ ruleb.ToolchainType(TOOLCHAIN_TYPE), ruleb.ToolchainType(EXEC_TOOLS_TOOLCHAIN_TYPE, mandatory = False), diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index dc3cb24c51..2a2f4554e3 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl @@ -18,6 +18,64 @@ load(":builders.bzl", "builders") load(":reexports.bzl", "BuiltinPyInfo") load(":util.bzl", "define_bazel_6_provider") +def _VenvSymlinkKind_typedef(): + """An enum of types of venv directories. + + :::{field} BIN + :type: object + + Indicates to create paths under the directory that has binaries + within the venv. + ::: + + :::{field} LIB + :type: object + + Indicates to create paths under the venv's site-packages directory. + ::: + + :::{field} INCLUDE + :type: object + + Indicates to create paths under the venv's include directory. + ::: + """ + +# buildifier: disable=name-conventions +VenvSymlinkKind = struct( + TYPEDEF = _VenvSymlinkKind_typedef, + BIN = "BIN", + LIB = "LIB", + INCLUDE = "INCLUDE", +) + +# A provider is used for memory efficiency. +# buildifier: disable=name-conventions +VenvSymlinkEntry = provider( + doc = """ +An entry in `PyInfo.venv_symlinks` +""", + fields = { + "kind": """ +:type: str + +One of the {obj}`VenvSymlinkKind` values. It represents which directory within +the venv to create the path under. +""", + "link_to_path": """ +:type: str | None + +A runfiles-root relative path that `venv_path` will symlink to. If `None`, +it means to not create a symlink. +""", + "venv_path": """ +:type: str + +A path relative to the `kind` directory within the venv. +""", + }, +) + def _check_arg_type(name, required_type, value): """Check that a value is of an expected type.""" value_type = type(value) @@ -43,7 +101,7 @@ def _PyInfo_init( transitive_original_sources = depset(), direct_pyi_files = depset(), transitive_pyi_files = depset(), - site_packages_symlinks = depset()): + venv_symlinks = depset()): _check_arg_type("transitive_sources", "depset", transitive_sources) # Verify it's postorder compatible, but retain is original ordering. @@ -71,7 +129,6 @@ def _PyInfo_init( "has_py2_only_sources": has_py2_only_sources, "has_py3_only_sources": has_py2_only_sources, "imports": imports, - "site_packages_symlinks": site_packages_symlinks, "transitive_implicit_pyc_files": transitive_implicit_pyc_files, "transitive_implicit_pyc_source_files": transitive_implicit_pyc_source_files, "transitive_original_sources": transitive_original_sources, @@ -79,10 +136,15 @@ def _PyInfo_init( "transitive_pyi_files": transitive_pyi_files, "transitive_sources": transitive_sources, "uses_shared_libraries": uses_shared_libraries, + "venv_symlinks": venv_symlinks, } PyInfo, _unused_raw_py_info_ctor = define_bazel_6_provider( - doc = "Encapsulates information provided by the Python rules.", + doc = """Encapsulates information provided by the Python rules. + +Instead of creating this object directly, use {obj}`PyInfoBuilder` and +the {obj}`PyCommonApi` utilities. +""", init = _PyInfo_init, fields = { "direct_original_sources": """ @@ -142,34 +204,6 @@ A depset of import path strings to be added to the `PYTHONPATH` of executable Python targets. These are accumulated from the transitive `deps`. The order of the depset is not guaranteed and may be changed in the future. It is recommended to use `default` order (the default). -""", - "site_packages_symlinks": """ -:type: depset[tuple[str | None, str]] - -A depset with `topological` ordering. - -Tuples of `(runfiles_path, site_packages_path)`. Where -* `runfiles_path` is a runfiles-root relative path. It is the path that - has the code to make importable. If `None` or empty string, then it means - to not create a site packages directory with the `site_packages_path` - name. -* `site_packages_path` is a path relative to the site-packages directory of - the venv for whatever creates the venv (typically py_binary). It makes - the code in `runfiles_path` available for import. Note that this - is created as a "raw" symlink (via `declare_symlink`). - -:::{include} /_includes/experimental_api.md -::: - -:::{tip} -The topological ordering means dependencies earlier and closer to the consumer -have precedence. This allows e.g. a binary to add dependencies that override -values from further way dependencies, such as forcing symlinks to point to -specific paths or preventing symlinks from being created. -::: - -:::{versionadded} 1.4.0 -::: """, "transitive_implicit_pyc_files": """ :type: depset[File] @@ -258,6 +292,35 @@ Whether any of this target's transitive `deps` has a shared library file (such as a `.so` file). This field is currently unused in Bazel and may go away in the future. +""", + "venv_symlinks": """ +:type: depset[VenvSymlinkEntry] + +A depset with `topological` ordering. + + +Tuples of `(runfiles_path, site_packages_path)`. Where +* `runfiles_path` is a runfiles-root relative path. It is the path that + has the code to make importable. If `None` or empty string, then it means + to not create a site packages directory with the `site_packages_path` + name. +* `site_packages_path` is a path relative to the site-packages directory of + the venv for whatever creates the venv (typically py_binary). It makes + the code in `runfiles_path` available for import. Note that this + is created as a "raw" symlink (via `declare_symlink`). + +:::{include} /_includes/experimental_api.md +::: + +:::{tip} +The topological ordering means dependencies earlier and closer to the consumer +have precedence. This allows e.g. a binary to add dependencies that override +values from further way dependencies, such as forcing symlinks to point to +specific paths or preventing symlinks from being created. +::: + +:::{versionadded} VERSION_NEXT_FEATURE +::: """, }, ) @@ -265,7 +328,65 @@ This field is currently unused in Bazel and may go away in the future. # The "effective" PyInfo is what the canonical //python:py_info.bzl%PyInfo symbol refers to _EffectivePyInfo = PyInfo if (config.enable_pystar or BuiltinPyInfo == None) else BuiltinPyInfo -def PyInfoBuilder(): +def _PyInfoBuilder_typedef(): + """Builder for PyInfo. + + To create an instance, use {obj}`py_common.get()` and call `PyInfoBuilder()` + + :::{field} direct_original_sources + :type: DepsetBuilder[File] + ::: + + :::{field} direct_pyc_files + :type: DepsetBuilder[File] + ::: + + :::{field} direct_pyi_files + :type: DepsetBuilder[File] + ::: + + :::{field} imports + :type: DepsetBuilder[str] + ::: + + :::{field} transitive_implicit_pyc_files + :type: DepsetBuilder[File] + ::: + + :::{field} transitive_implicit_pyc_source_files + :type: DepsetBuilder[File] + ::: + + :::{field} transitive_original_sources + :type: DepsetBuilder[File] + ::: + + :::{field} transitive_pyc_files + :type: DepsetBuilder[File] + ::: + + :::{field} transitive_pyi_files + :type: DepsetBuilder[File] + ::: + + :::{field} transitive_sources + :type: DepsetBuilder[File] + ::: + + :::{field} venv_symlinks + :type: DepsetBuilder[tuple[str | None, str]] + + NOTE: This depset has `topological` order + ::: + """ + +def _PyInfoBuilder_new(): + """Creates an instance. + + Returns: + {type}`PyInfoBuilder` + """ + # buildifier: disable=uninitialized self = struct( _has_py2_only_sources = [False], @@ -296,40 +417,121 @@ def PyInfoBuilder(): transitive_pyc_files = builders.DepsetBuilder(), transitive_pyi_files = builders.DepsetBuilder(), transitive_sources = builders.DepsetBuilder(), - site_packages_symlinks = builders.DepsetBuilder(order = "topological"), + venv_symlinks = builders.DepsetBuilder(order = "topological"), ) return self def _PyInfoBuilder_get_has_py3_only_sources(self): + """Get the `has_py3_only_sources` value. + + Args: + self: implicitly added. + + Returns: + {type}`bool` + """ return self._has_py3_only_sources[0] def _PyInfoBuilder_get_has_py2_only_sources(self): + """Get the `has_py2_only_sources` value. + + Args: + self: implicitly added. + + Returns: + {type}`bool` + """ return self._has_py2_only_sources[0] def _PyInfoBuilder_set_has_py2_only_sources(self, value): + """Sets `has_py2_only_sources` to `value`. + + Args: + self: implicitly added. + value: {type}`bool` The value to set. + + Returns: + {type}`PyInfoBuilder` self + """ self._has_py2_only_sources[0] = value return self def _PyInfoBuilder_set_has_py3_only_sources(self, value): + """Sets `has_py3_only_sources` to `value`. + + Args: + self: implicitly added. + value: {type}`bool` The value to set. + + Returns: + {type}`PyInfoBuilder` self + """ self._has_py3_only_sources[0] = value return self def _PyInfoBuilder_merge_has_py2_only_sources(self, value): + """Sets `has_py2_only_sources` based on current and incoming `value`. + + Args: + self: implicitly added. + value: {type}`bool` Another `has_py2_only_sources` value. It will + be merged into this builder's state. + + Returns: + {type}`PyInfoBuilder` self + """ self._has_py2_only_sources[0] = self._has_py2_only_sources[0] or value return self def _PyInfoBuilder_merge_has_py3_only_sources(self, value): + """Sets `has_py3_only_sources` based on current and incoming `value`. + + Args: + self: implicitly added. + value: {type}`bool` Another `has_py3_only_sources` value. It will + be merged into this builder's state. + + Returns: + {type}`PyInfoBuilder` self + """ self._has_py3_only_sources[0] = self._has_py3_only_sources[0] or value return self def _PyInfoBuilder_merge_uses_shared_libraries(self, value): + """Sets `uses_shared_libraries` based on current and incoming `value`. + + Args: + self: implicitly added. + value: {type}`bool` Another `uses_shared_libraries` value. It will + be merged into this builder's state. + + Returns: + {type}`PyInfoBuilder` self + """ self._uses_shared_libraries[0] = self._uses_shared_libraries[0] or value return self def _PyInfoBuilder_get_uses_shared_libraries(self): + """Get the `uses_shared_libraries` value. + + Args: + self: implicitly added. + + Returns: + {type}`bool` + """ return self._uses_shared_libraries[0] def _PyInfoBuilder_set_uses_shared_libraries(self, value): + """Sets `uses_shared_libraries` to `value`. + + Args: + self: implicitly added. + value: {type}`bool` The value to set. + + Returns: + {type}`PyInfoBuilder` self + """ self._uses_shared_libraries[0] = value return self @@ -344,7 +546,7 @@ def _PyInfoBuilder_merge(self, *infos, direct = []): direct fields into this object's direct fields. Returns: - {type}`PyInfoBuilder` the current object + {type}`PyInfoBuilder` self """ return self.merge_all(list(infos), direct = direct) @@ -359,7 +561,7 @@ def _PyInfoBuilder_merge_all(self, transitive, *, direct = []): direct fields into this object's direct fields. Returns: - {type}`PyInfoBuilder` the current object + {type}`PyInfoBuilder` self """ for info in direct: # BuiltinPyInfo doesn't have this field @@ -382,7 +584,7 @@ def _PyInfoBuilder_merge_all(self, transitive, *, direct = []): self.transitive_original_sources.add(info.transitive_original_sources) self.transitive_pyc_files.add(info.transitive_pyc_files) self.transitive_pyi_files.add(info.transitive_pyi_files) - self.site_packages_symlinks.add(info.site_packages_symlinks) + self.venv_symlinks.add(info.venv_symlinks) return self @@ -392,11 +594,11 @@ def _PyInfoBuilder_merge_target(self, target): Args: self: implicitly added. target: {type}`Target` targets that provide PyInfo, or other relevant - providers, will be merged into this object. If a target doesn't provide - any relevant providers, it is ignored. + providers, will be merged into this object. If a target doesn't provide + any relevant providers, it is ignored. Returns: - {type}`PyInfoBuilder` the current object. + {type}`PyInfoBuilder` self. """ if PyInfo in target: self.merge(target[PyInfo]) @@ -410,18 +612,26 @@ def _PyInfoBuilder_merge_targets(self, targets): Args: self: implicitly added. targets: {type}`list[Target]` - targets that provide PyInfo, or other relevant - providers, will be merged into this object. If a target doesn't provide - any relevant providers, it is ignored. + targets that provide PyInfo, or other relevant + providers, will be merged into this object. If a target doesn't provide + any relevant providers, it is ignored. Returns: - {type}`PyInfoBuilder` the current object. + {type}`PyInfoBuilder` self. """ for t in targets: self.merge_target(t) return self def _PyInfoBuilder_build(self): + """Builds into a {obj}`PyInfo` object. + + Args: + self: implicitly added. + + Returns: + {type}`PyInfo` + """ if config.enable_pystar: kwargs = dict( direct_original_sources = self.direct_original_sources.build(), @@ -432,7 +642,7 @@ def _PyInfoBuilder_build(self): transitive_original_sources = self.transitive_original_sources.build(), transitive_pyc_files = self.transitive_pyc_files.build(), transitive_pyi_files = self.transitive_pyi_files.build(), - site_packages_symlinks = self.site_packages_symlinks.build(), + venv_symlinks = self.venv_symlinks.build(), ) else: kwargs = {} @@ -447,6 +657,15 @@ def _PyInfoBuilder_build(self): ) def _PyInfoBuilder_build_builtin_py_info(self): + """Builds into a Bazel-builtin PyInfo object, if available. + + Args: + self: implicitly added. + + Returns: + {type}`BuiltinPyInfo | None` None is returned if Bazel's + builtin PyInfo object is disabled. + """ if BuiltinPyInfo == None: return None @@ -457,3 +676,25 @@ def _PyInfoBuilder_build_builtin_py_info(self): transitive_sources = self.transitive_sources.build(), uses_shared_libraries = self._uses_shared_libraries[0], ) + +# Provided for documentation purposes +# buildifier: disable=name-conventions +PyInfoBuilder = struct( + TYPEDEF = _PyInfoBuilder_typedef, + new = _PyInfoBuilder_new, + build = _PyInfoBuilder_build, + build_builtin_py_info = _PyInfoBuilder_build_builtin_py_info, + get_has_py2_only_sources = _PyInfoBuilder_get_has_py2_only_sources, + get_has_py3_only_sources = _PyInfoBuilder_get_has_py3_only_sources, + get_uses_shared_libraries = _PyInfoBuilder_get_uses_shared_libraries, + merge = _PyInfoBuilder_merge, + merge_all = _PyInfoBuilder_merge_all, + merge_has_py2_only_sources = _PyInfoBuilder_merge_has_py2_only_sources, + merge_has_py3_only_sources = _PyInfoBuilder_merge_has_py3_only_sources, + merge_target = _PyInfoBuilder_merge_target, + merge_targets = _PyInfoBuilder_merge_targets, + merge_uses_shared_libraries = _PyInfoBuilder_merge_uses_shared_libraries, + set_has_py2_only_sources = _PyInfoBuilder_set_has_py2_only_sources, + set_has_py3_only_sources = _PyInfoBuilder_set_has_py3_only_sources, + set_uses_shared_libraries = _PyInfoBuilder_set_uses_shared_libraries, +) diff --git a/python/private/py_library.bzl b/python/private/py_library.bzl index 6b5882de5a..fabc880a8d 100644 --- a/python/private/py_library.bzl +++ b/python/private/py_library.bzl @@ -43,7 +43,9 @@ load( load(":flags.bzl", "AddSrcsToRunfilesFlag", "PrecompileFlag", "VenvsSitePackages") load(":precompile.bzl", "maybe_precompile") load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo") +load(":py_info.bzl", "PyInfo", "VenvSymlinkEntry", "VenvSymlinkKind") load(":py_internal.bzl", "py_internal") +load(":reexports.bzl", "BuiltinPyInfo") load(":rule_builders.bzl", "ruleb") load( ":toolchain_types.bzl", @@ -88,9 +90,9 @@ won't be understood as namespace packages; they'll be seen as regular packages. likely lead to conflicts with other targets that contribute to the namespace. :::{tip} -This attributes populates {obj}`PyInfo.site_packages_symlinks`, which is +This attributes populates {obj}`PyInfo.venv_symlinks`, which is a topologically ordered depset. This means dependencies closer and earlier -to a consumer have precedence. See {obj}`PyInfo.site_packages_symlinks` for +to a consumer have precedence. See {obj}`PyInfo.venv_symlinks` for more information. ::: @@ -153,9 +155,9 @@ def py_library_impl(ctx, *, semantics): runfiles = runfiles.build(ctx) imports = [] - site_packages_symlinks = [] + venv_symlinks = [] - imports, site_packages_symlinks = _get_imports_and_site_packages_symlinks(ctx, semantics) + imports, venv_symlinks = _get_imports_and_venv_symlinks(ctx, semantics) cc_info = semantics.get_cc_info_for_library(ctx) py_info, deps_transitive_sources, builtins_py_info = create_py_info( @@ -166,7 +168,7 @@ def py_library_impl(ctx, *, semantics): implicit_pyc_files = implicit_pyc_files, implicit_pyc_source_files = implicit_pyc_source_files, imports = imports, - site_packages_symlinks = site_packages_symlinks, + venv_symlinks = venv_symlinks, ) # TODO(b/253059598): Remove support for extra actions; https://github.com/bazelbuild/bazel/issues/16455 @@ -204,16 +206,16 @@ Source files are no longer added to the runfiles directly. ::: """ -def _get_imports_and_site_packages_symlinks(ctx, semantics): +def _get_imports_and_venv_symlinks(ctx, semantics): imports = depset() - site_packages_symlinks = depset() + venv_symlinks = depset() if VenvsSitePackages.is_enabled(ctx): - site_packages_symlinks = _get_site_packages_symlinks(ctx) + venv_symlinks = _get_venv_symlinks(ctx) else: imports = collect_imports(ctx, semantics) - return imports, site_packages_symlinks + return imports, venv_symlinks -def _get_site_packages_symlinks(ctx): +def _get_venv_symlinks(ctx): imports = ctx.attr.imports if len(imports) == 0: fail("When venvs_site_packages is enabled, exactly one `imports` " + @@ -251,6 +253,7 @@ def _get_site_packages_symlinks(ctx): repo_runfiles_dirname = None dirs_with_init = {} # dirname -> runfile path + venv_symlinks = [] for src in ctx.files.srcs: if src.extension not in PYTHON_FILE_EXTENSIONS: continue @@ -259,16 +262,20 @@ def _get_site_packages_symlinks(ctx): continue path = path.removeprefix(site_packages_root) dir_name, _, filename = path.rpartition("/") - if not dir_name: - # This would be e.g. `site-packages/__init__.py`, which isn't valid - # because it's not within a directory for an importable Python package. - # However, the pypi integration over-eagerly adds a pkgutil-style - # __init__.py file during the repo phase. Just ignore them for now. - continue - if filename.startswith("__init__."): + if dir_name and filename.startswith("__init__."): dirs_with_init[dir_name] = None repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0] + elif not dir_name: + repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0] + + # This would be files that do not have directories and we just need to add + # direct symlinks to them as is: + venv_symlinks.append(VenvSymlinkEntry( + kind = VenvSymlinkKind.LIB, + link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, filename), + venv_path = filename, + )) # Sort so that we encounter `foo` before `foo/bar`. This ensures we # see the top-most explicit package first. @@ -284,13 +291,13 @@ def _get_site_packages_symlinks(ctx): if not is_sub_package: first_level_explicit_packages.append(d) - site_packages_symlinks = [] for dirname in first_level_explicit_packages: - site_packages_symlinks.append(( - paths.join(repo_runfiles_dirname, site_packages_root, dirname), - dirname, + venv_symlinks.append(VenvSymlinkEntry( + kind = VenvSymlinkKind.LIB, + link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, dirname), + venv_path = dirname, )) - return site_packages_symlinks + return venv_symlinks def _repo_relative_short_path(short_path): # Convert `../+pypi+foo/some/file.py` to `some/file.py` @@ -299,6 +306,8 @@ def _repo_relative_short_path(short_path): else: return short_path +_MaybeBuiltinPyInfo = [BuiltinPyInfo] if BuiltinPyInfo != None else [] + # NOTE: Exported publicaly def create_py_library_rule_builder(): """Create a rule builder for a py_library. @@ -319,6 +328,7 @@ def create_py_library_rule_builder(): exec_groups = dict(REQUIRED_EXEC_GROUP_BUILDERS), attrs = LIBRARY_ATTRS, fragments = ["py"], + provides = [PyCcLinkParamsInfo, PyInfo] + _MaybeBuiltinPyInfo, toolchains = [ ruleb.ToolchainType(TOOLCHAIN_TYPE, mandatory = False), ruleb.ToolchainType(EXEC_TOOLS_TOOLCHAIN_TYPE, mandatory = False), diff --git a/python/private/py_package.bzl b/python/private/py_package.bzl index 1d866a9d80..adf2b6deef 100644 --- a/python/private/py_package.bzl +++ b/python/private/py_package.bzl @@ -34,7 +34,7 @@ def _path_inside_wheel(input_file): def _py_package_impl(ctx): inputs = builders.DepsetBuilder() - py_info = PyInfoBuilder() + py_info = PyInfoBuilder.new() for dep in ctx.attr.deps: inputs.add(dep[DefaultInfo].data_runfiles.files) inputs.add(dep[DefaultInfo].default_runfiles.files) diff --git a/python/private/py_repositories.bzl b/python/private/py_repositories.bzl index 46ca903df4..10bc06630b 100644 --- a/python/private/py_repositories.bzl +++ b/python/private/py_repositories.bzl @@ -39,11 +39,15 @@ def py_repositories(): name = "pythons_hub", minor_mapping = MINOR_MAPPING, default_python_version = "", - toolchain_prefixes = [], - toolchain_python_versions = [], - toolchain_set_python_version_constraints = [], - toolchain_user_repository_names = [], python_versions = sorted(TOOL_VERSIONS.keys()), + toolchain_names = [], + toolchain_repo_names = {}, + toolchain_target_compatible_with_map = {}, + toolchain_target_settings_map = {}, + toolchain_platform_keys = {}, + toolchain_python_versions = {}, + toolchain_set_python_version_constraints = {}, + host_compatible_repo_names = [], ) http_archive( name = "bazel_skylib", diff --git a/python/private/py_runtime_info.bzl b/python/private/py_runtime_info.bzl index 4297391068..d2ae17e360 100644 --- a/python/private/py_runtime_info.bzl +++ b/python/private/py_runtime_info.bzl @@ -67,7 +67,8 @@ def _PyRuntimeInfo_init( stage2_bootstrap_template = None, zip_main_template = None, abi_flags = "", - site_init_template = None): + site_init_template = None, + supports_build_time_venv = True): if (interpreter_path and interpreter) or (not interpreter_path and not interpreter): fail("exactly one of interpreter or interpreter_path must be specified") @@ -119,6 +120,7 @@ def _PyRuntimeInfo_init( "site_init_template": site_init_template, "stage2_bootstrap_template": stage2_bootstrap_template, "stub_shebang": stub_shebang, + "supports_build_time_venv": supports_build_time_venv, "zip_main_template": zip_main_template, } @@ -312,6 +314,28 @@ The following substitutions are made during template expansion: "Shebang" expression prepended to the bootstrapping Python stub script used when executing {obj}`py_binary` targets. Does not apply to Windows. +""", + "supports_build_time_venv": """ +:type: bool + +True if this toolchain supports the build-time created virtual environment. +False if not or unknown. If build-time venv creation isn't supported, then binaries may +fallback to non-venv solutions or creating a venv at runtime. + +In order to use the build-time created virtual environment, a toolchain needs +to meet two criteria: +1. Specifying the underlying executable (e.g. `/usr/bin/python3`, as reported by + `sys._base_executable`) for the venv executable (`$venv/bin/python3`, as reported + by `sys.executable`). This typically requires relative symlinking the venv + path to the underlying path at build time, or using the `PYTHONEXECUTABLE` + environment variable (Python 3.11+) at runtime. +2. Having the build-time created site-packages directory + (`/lib/python{version}/site-packages`) recognized by the runtime + interpreter. This typically requires the Python version to be known at + build-time and match at runtime. + +:::{versionadded} VERSION_NEXT_FEATURE +::: """, "zip_main_template": """ :type: File diff --git a/python/private/py_runtime_rule.bzl b/python/private/py_runtime_rule.bzl index a85f5b25f2..6dadcfeac3 100644 --- a/python/private/py_runtime_rule.bzl +++ b/python/private/py_runtime_rule.bzl @@ -130,6 +130,7 @@ def _py_runtime_impl(ctx): zip_main_template = ctx.file.zip_main_template, abi_flags = abi_flags, site_init_template = ctx.file.site_init_template, + supports_build_time_venv = ctx.attr.supports_build_time_venv, )) if not IS_BAZEL_7_OR_HIGHER: @@ -353,6 +354,17 @@ motivation. Does not apply to Windows. """, ), + "supports_build_time_venv": attr.bool( + doc = """ +Whether this runtime supports virtualenvs created at build time. + +See {obj}`PyRuntimeInfo.supports_build_time_venv` for docs. + +:::{versionadded} VERSION_NEXT_FEATURE +::: +""", + default = True, + ), "zip_main_template": attr.label( default = "//python/private:zip_main_template", allow_single_file = True, diff --git a/python/private/py_toolchain_suite.bzl b/python/private/py_toolchain_suite.bzl index a69be376b4..fa73d5daa3 100644 --- a/python/private/py_toolchain_suite.bzl +++ b/python/private/py_toolchain_suite.bzl @@ -15,6 +15,7 @@ """Create the toolchain defs in a BUILD.bazel file.""" load("@bazel_skylib//lib:selects.bzl", "selects") +load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS") load(":text_util.bzl", "render") load( ":toolchain_types.bzl", @@ -33,15 +34,20 @@ def py_toolchain_suite( python_version, set_python_version_constraint, flag_values, + target_settings = [], target_compatible_with = []): """For internal use only. Args: prefix: Prefix for toolchain target names. - user_repository_name: The name of the user repository. + user_repository_name: The name of the repository with the toolchain + implementation (it's assumed to have particular target names within + it). Does not include the leading "@". python_version: The full (X.Y.Z) version of the interpreter. set_python_version_constraint: True or False as a string. - flag_values: Extra flag values to match for this toolchain. + flag_values: Extra flag values to match for this toolchain. These + are prepended to target_settings. + target_settings: Extra target_settings to match for this toolchain. target_compatible_with: list constraints the toolchains are compatible with. """ @@ -81,7 +87,7 @@ def py_toolchain_suite( match_any = match_any, visibility = ["//visibility:private"], ) - target_settings = [name] + target_settings = [name] + target_settings else: fail(("Invalid set_python_version_constraint value: got {} {}, wanted " + "either the string 'True' or the string 'False'; " + @@ -95,9 +101,15 @@ def py_toolchain_suite( runtime_repo_name = user_repository_name, target_settings = target_settings, target_compatible_with = target_compatible_with, + exec_compatible_with = [], ) -def _internal_toolchain_suite(prefix, runtime_repo_name, target_compatible_with, target_settings): +def _internal_toolchain_suite( + prefix, + runtime_repo_name, + target_compatible_with, + target_settings, + exec_compatible_with): native.toolchain( name = "{prefix}_toolchain".format(prefix = prefix), toolchain = "@{runtime_repo_name}//:python_runtimes".format( @@ -106,6 +118,7 @@ def _internal_toolchain_suite(prefix, runtime_repo_name, target_compatible_with, toolchain_type = TARGET_TOOLCHAIN_TYPE, target_settings = target_settings, target_compatible_with = target_compatible_with, + exec_compatible_with = exec_compatible_with, ) native.toolchain( @@ -116,6 +129,7 @@ def _internal_toolchain_suite(prefix, runtime_repo_name, target_compatible_with, toolchain_type = PY_CC_TOOLCHAIN_TYPE, target_settings = target_settings, target_compatible_with = target_compatible_with, + exec_compatible_with = exec_compatible_with, ) native.toolchain( @@ -142,7 +156,13 @@ def _internal_toolchain_suite(prefix, runtime_repo_name, target_compatible_with, # call in python/repositories.bzl. Bzlmod doesn't need anything; it will # register `:all`. -def define_local_toolchain_suites(name, version_aware_repo_names, version_unaware_repo_names): +def define_local_toolchain_suites( + name, + version_aware_repo_names, + version_unaware_repo_names, + repo_exec_compatible_with, + repo_target_compatible_with, + repo_target_settings): """Define toolchains for `local_runtime_repo` backed toolchains. This generates `toolchain` targets that can be registered using `:all`. The @@ -156,24 +176,60 @@ def define_local_toolchain_suites(name, version_aware_repo_names, version_unawar version-aware toolchains defined. version_unaware_repo_names: `list[str]` of the repo names that will have version-unaware toolchains defined. + repo_target_settings: {type}`dict[str, list[str]]` mapping of repo names + to string labels that are added to the `target_settings` for the + respective repo's toolchain. + repo_target_compatible_with: {type}`dict[str, list[str]]` mapping of repo names + to string labels that are added to the `target_compatible_with` for + the respective repo's toolchain. + repo_exec_compatible_with: {type}`dict[str, list[str]]` mapping of repo names + to string labels that are added to the `exec_compatible_with` for + the respective repo's toolchain. """ + i = 0 for i, repo in enumerate(version_aware_repo_names, start = i): - prefix = render.left_pad_zero(i, 4) + target_settings = ["@{}//:is_matching_python_version".format(repo)] + + if repo_target_settings.get(repo): + selects.config_setting_group( + name = "_{}_user_guard".format(repo), + match_all = repo_target_settings.get(repo, []) + target_settings, + ) + target_settings = ["_{}_user_guard".format(repo)] _internal_toolchain_suite( - prefix = prefix, + prefix = render.left_pad_zero(i, 4), runtime_repo_name = repo, - target_compatible_with = ["@{}//:os".format(repo)], - target_settings = ["@{}//:is_matching_python_version".format(repo)], + target_compatible_with = _get_local_toolchain_target_compatible_with( + repo, + repo_target_compatible_with, + ), + target_settings = target_settings, + exec_compatible_with = repo_exec_compatible_with.get(repo, []), ) # The version unaware entries must go last because they will match any Python # version. for i, repo in enumerate(version_unaware_repo_names, start = i + 1): - prefix = render.left_pad_zero(i, 4) _internal_toolchain_suite( - prefix = prefix, + prefix = render.left_pad_zero(i, 4) + "_default", runtime_repo_name = repo, - target_settings = [], - target_compatible_with = ["@{}//:os".format(repo)], + target_compatible_with = _get_local_toolchain_target_compatible_with( + repo, + repo_target_compatible_with, + ), + # We don't call _get_local_toolchain_target_settings because that + # will add the version matching condition by default. + target_settings = repo_target_settings.get(repo, []), + exec_compatible_with = repo_exec_compatible_with.get(repo, []), ) + +def _get_local_toolchain_target_compatible_with(repo, repo_target_compatible_with): + if repo in repo_target_compatible_with: + target_compatible_with = repo_target_compatible_with[repo] + if "HOST_CONSTRAINTS" in target_compatible_with: + target_compatible_with.remove("HOST_CONSTRAINTS") + target_compatible_with.extend(HOST_CONSTRAINTS) + else: + target_compatible_with = ["@{}//:os".format(repo)] + return target_compatible_with diff --git a/python/private/py_wheel.bzl b/python/private/py_wheel.bzl index c196ca6ad0..ffc24f6846 100644 --- a/python/private/py_wheel.bzl +++ b/python/private/py_wheel.bzl @@ -16,8 +16,8 @@ load(":py_info.bzl", "PyInfo") load(":py_package.bzl", "py_package_lib") -load(":py_wheel_normalize_pep440.bzl", "normalize_pep440") load(":stamp.bzl", "is_stamping_enabled") +load(":version.bzl", "version") PyWheelInfo = provider( doc = "Information about a wheel produced by `py_wheel`", @@ -306,11 +306,11 @@ def _input_file_to_arg(input_file): def _py_wheel_impl(ctx): abi = _replace_make_variables(ctx.attr.abi, ctx) python_tag = _replace_make_variables(ctx.attr.python_tag, ctx) - version = _replace_make_variables(ctx.attr.version, ctx) + version_str = _replace_make_variables(ctx.attr.version, ctx) filename_segments = [ _escape_filename_distribution_name(ctx.attr.distribution), - normalize_pep440(version), + version.normalize(version_str), _escape_filename_segment(python_tag), _escape_filename_segment(abi), _escape_filename_segment(ctx.attr.platform), @@ -343,7 +343,7 @@ def _py_wheel_impl(ctx): args = ctx.actions.args() args.add("--name", ctx.attr.distribution) - args.add("--version", version) + args.add("--version", version_str) args.add("--python_tag", python_tag) args.add("--abi", abi) args.add("--platform", ctx.attr.platform) diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index 7297238cb4..d89dc6c228 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -18,6 +18,11 @@ package(default_visibility = ["//:__subpackages__"]) licenses(["notice"]) +exports_files( + srcs = ["namespace_pkg_tmpl.py"], + visibility = ["//visibility:public"], +) + filegroup( name = "distribution", srcs = glob( @@ -71,6 +76,23 @@ bzl_library( ], ) +bzl_library( + name = "env_marker_info_bzl", + srcs = ["env_marker_info.bzl"], +) + +bzl_library( + name = "env_marker_setting_bzl", + srcs = ["env_marker_setting.bzl"], + deps = [ + ":env_marker_info_bzl", + ":pep508_env_bzl", + ":pep508_evaluate_bzl", + "//python/private:toolchain_types_bzl", + "@bazel_skylib//rules:common_settings", + ], +) + bzl_library( name = "evaluate_markers_bzl", srcs = ["evaluate_markers.bzl"], @@ -99,10 +121,11 @@ bzl_library( ":whl_target_platforms_bzl", "//python/private:full_version_bzl", "//python/private:normalize_name_bzl", - "//python/private:semver_bzl", + "//python/private:version_bzl", "//python/private:version_label_bzl", "@bazel_features//:features", "@pythons_hub//:interpreters_bzl", + "@pythons_hub//:versions_bzl", ], ) @@ -110,6 +133,8 @@ bzl_library( name = "flags_bzl", srcs = ["flags.bzl"], deps = [ + ":env_marker_info.bzl", + ":pep508_env_bzl", "//python/private:enum_bzl", "@bazel_skylib//rules:common_settings", ], @@ -212,22 +237,11 @@ bzl_library( ], ) -bzl_library( - name = "pep508_bzl", - srcs = ["pep508.bzl"], - deps = [ - ":pep508_env_bzl", - ":pep508_evaluate_bzl", - ], -) - bzl_library( name = "pep508_deps_bzl", srcs = ["pep508_deps.bzl"], deps = [ - ":pep508_env_bzl", ":pep508_evaluate_bzl", - ":pep508_platform_bzl", ":pep508_requirement_bzl", "//python/private:normalize_name_bzl", ], @@ -238,6 +252,7 @@ bzl_library( srcs = ["pep508_env.bzl"], deps = [ ":pep508_platform_bzl", + "//python/private:version_bzl", ], ) @@ -246,7 +261,7 @@ bzl_library( srcs = ["pep508_evaluate.bzl"], deps = [ "//python/private:enum_bzl", - "//python/private:semver_bzl", + "//python/private:version_bzl", ], ) @@ -289,6 +304,7 @@ bzl_library( ":evaluate_markers_bzl", ":parse_requirements_bzl", ":pip_repository_attrs_bzl", + ":pypi_repo_utils_bzl", ":render_pkg_aliases_bzl", ":whl_config_setting_bzl", "//python/private:normalize_name_bzl", @@ -378,13 +394,13 @@ bzl_library( ":attrs_bzl", ":deps_bzl", ":generate_whl_library_build_bazel_bzl", - ":parse_whl_name_bzl", ":patch_whl_bzl", - ":pep508_deps_bzl", + ":pep508_requirement_bzl", ":pypi_repo_utils_bzl", ":whl_metadata_bzl", ":whl_target_platforms_bzl", "//python/private:auth_bzl", + "//python/private:bzlmod_enabled_bzl", "//python/private:envsubst_bzl", "//python/private:is_standalone_interpreter_bzl", "//python/private:repo_utils_bzl", diff --git a/python/private/pypi/attrs.bzl b/python/private/pypi/attrs.bzl index 9d88c1e32c..7ea19d106a 100644 --- a/python/private/pypi/attrs.bzl +++ b/python/private/pypi/attrs.bzl @@ -123,6 +123,9 @@ Warning: "experimental_target_platforms": attr.string_list( default = [], doc = """\ +*NOTE*: This will be removed in the next major version, so please consider migrating +to `bzlmod` and rely on {attr}`pip.parse.requirements_by_platform` for this feature. + A list of platforms that we will generate the conditional dependency graph for cross platform wheels by parsing the wheel metadata. This will generate the correct dependencies for packages like `sphinx` or `pylint`, which include @@ -207,7 +210,7 @@ If True, suppress printing stdout and stderr output to the terminal. If you would like to get more diagnostic output, set {envvar}`RULES_PYTHON_REPO_DEBUG=1 ` or -{envvar}`RULES_PYTHON_REPO_DEBUG_VERBOSITY= ` +{envvar}`RULES_PYTHON_REPO_DEBUG_VERBOSITY=INFO|DEBUG|TRACE ` """, ), # 600 is documented as default here: https://docs.bazel.build/versions/master/skylark/lib/repository_ctx.html#execute diff --git a/python/private/pypi/config.bzl.tmpl.bzlmod b/python/private/pypi/config.bzl.tmpl.bzlmod new file mode 100644 index 0000000000..c3ada70d27 --- /dev/null +++ b/python/private/pypi/config.bzl.tmpl.bzlmod @@ -0,0 +1,9 @@ +"""Extra configuration values that are exposed from the hub repository for spoke repositories to access. + +NOTE: This is internal `rules_python` API and if you would like to depend on it, please raise an issue +with your usecase. This may change in between rules_python versions without any notice. + +@generated by rules_python pip.parse bzlmod extension. +""" + +whl_map = %%WHL_MAP%% diff --git a/python/private/pypi/config_settings.bzl b/python/private/pypi/config_settings.bzl index 1045ffef35..d1b85d16c1 100644 --- a/python/private/pypi/config_settings.bzl +++ b/python/private/pypi/config_settings.bzl @@ -42,6 +42,8 @@ specialized is as follows: * `:is_cp3_abi3_` * `:is_cp3_cp3_` and `:is_cp3_cp3t_` +Optionally instead of `` there sometimes may be `.` used in order to fully specify the versions + The specialization of free-threaded vs non-free-threaded wheels is the same as they are just variants of each other. The same goes for the specialization of `musllinux` vs `manylinux`. diff --git a/python/private/pypi/dependency_resolver/dependency_resolver.py b/python/private/pypi/dependency_resolver/dependency_resolver.py index 293377dc6d..f3a339f929 100644 --- a/python/private/pypi/dependency_resolver/dependency_resolver.py +++ b/python/private/pypi/dependency_resolver/dependency_resolver.py @@ -15,14 +15,17 @@ "Set defaults for the pip-compile command to run it under Bazel" import atexit +import functools import os import shutil import sys from pathlib import Path -from typing import Optional, Tuple +from typing import List, Optional, Tuple import click import piptools.writer as piptools_writer +from pip._internal.exceptions import DistributionNotFound +from pip._vendor.resolvelib.resolvers import ResolutionImpossible from piptools.scripts.compile import cli from python.runfiles import runfiles @@ -82,7 +85,7 @@ def _locate(bazel_runfiles, file): @click.command(context_settings={"ignore_unknown_options": True}) @click.option("--src", "srcs", multiple=True, required=True) @click.argument("requirements_txt") -@click.argument("update_target_label") +@click.argument("target_label_prefix") @click.option("--requirements-linux") @click.option("--requirements-darwin") @click.option("--requirements-windows") @@ -90,7 +93,7 @@ def _locate(bazel_runfiles, file): def main( srcs: Tuple[str, ...], requirements_txt: str, - update_target_label: str, + target_label_prefix: str, requirements_linux: Optional[str], requirements_darwin: Optional[str], requirements_windows: Optional[str], @@ -148,13 +151,21 @@ def main( requirements_out = os.path.join( os.environ["TEST_TMPDIR"], os.path.basename(requirements_file) + ".out" ) + # Why this uses shutil.copyfileobj: + # # Those two files won't necessarily be on the same filesystem, so we can't use os.replace # or shutil.copyfile, as they will fail with OSError: [Errno 18] Invalid cross-device link. - shutil.copy(resolved_requirements_file, requirements_out) - - update_command = os.getenv("CUSTOM_COMPILE_COMMAND") or "bazel run %s" % ( - update_target_label, + # + # Further, shutil.copy preserves the source file's mode, and so if + # our source file is read-only (the default under Perforce Helix), + # this scratch file will also be read-only, defeating its purpose. + with open(resolved_requirements_file, "rb") as fsrc, open(requirements_out, "wb") as fdst: + shutil.copyfileobj(fsrc, fdst) + + update_command = ( + os.getenv("CUSTOM_COMPILE_COMMAND") or f"bazel run {target_label_prefix}.update" ) + test_command = f"bazel test {target_label_prefix}.test" os.environ["CUSTOM_COMPILE_COMMAND"] = update_command os.environ["PIP_CONFIG_FILE"] = os.getenv("PIP_CONFIG_FILE") or os.devnull @@ -168,6 +179,12 @@ def main( ) argv.extend(extra_args) + _run_pip_compile = functools.partial( + run_pip_compile, + argv, + srcs_relative=srcs_relative, + ) + if UPDATE: print("Updating " + requirements_file_relative) @@ -185,53 +202,68 @@ def main( # and we should copy the updated requirements back to the source tree. if not absolute_output_file.samefile(requirements_file_tree): atexit.register( - lambda: shutil.copy( - absolute_output_file, requirements_file_tree - ) + lambda: shutil.copy(absolute_output_file, requirements_file_tree) ) - cli(argv, standalone_mode = False) + _run_pip_compile(verbose_command=f"{update_command} -- --verbose") requirements_file_relative_path = Path(requirements_file_relative) content = requirements_file_relative_path.read_text() content = content.replace(absolute_path_prefix, "") requirements_file_relative_path.write_text(content) else: - # cli will exit(0) on success - try: - print("Checking " + requirements_file) - cli(argv) - print("cli() should exit", file=sys.stderr) + print("Checking " + requirements_file) + sys.stdout.flush() + _run_pip_compile(verbose_command=f"{test_command} --test_arg=--verbose") + golden = open(_locate(bazel_runfiles, requirements_file)).readlines() + out = open(requirements_out).readlines() + out = [line.replace(absolute_path_prefix, "") for line in out] + if golden != out: + import difflib + + print("".join(difflib.unified_diff(golden, out)), file=sys.stderr) + print( + f"Lock file out of date. Run '{update_command}' to update.", + file=sys.stderr, + ) + sys.exit(1) + + +def run_pip_compile( + args: List[str], + *, + srcs_relative: List[str], + verbose_command: str, +) -> None: + try: + cli(args, standalone_mode=False) + except DistributionNotFound as e: + if isinstance(e.__cause__, ResolutionImpossible): + # pip logs an informative error to stderr already + # just render the error and exit + print(e) + sys.exit(1) + else: + raise + except SystemExit as e: + if e.code == 0: + return # shouldn't happen, but just in case + elif e.code == 2: + print( + "pip-compile exited with code 2. This means that pip-compile found " + "incompatible requirements or could not find a version that matches " + f"the install requirement in one of {srcs_relative}.\n" + "Try re-running with verbose:\n" + f" {verbose_command}", + file=sys.stderr, + ) + sys.exit(1) + else: + print( + f"pip-compile unexpectedly exited with code {e.code}.\n" + "Try re-running with verbose:\n" + f" {verbose_command}", + file=sys.stderr, + ) sys.exit(1) - except SystemExit as e: - if e.code == 2: - print( - "pip-compile exited with code 2. This means that pip-compile found " - "incompatible requirements or could not find a version that matches " - f"the install requirement in one of {srcs_relative}.", - file=sys.stderr, - ) - sys.exit(1) - elif e.code == 0: - golden = open(_locate(bazel_runfiles, requirements_file)).readlines() - out = open(requirements_out).readlines() - out = [line.replace(absolute_path_prefix, "") for line in out] - if golden != out: - import difflib - - print("".join(difflib.unified_diff(golden, out)), file=sys.stderr) - print( - "Lock file out of date. Run '" - + update_command - + "' to update.", - file=sys.stderr, - ) - sys.exit(1) - sys.exit(0) - else: - print( - f"pip-compile unexpectedly exited with code {e.code}.", - file=sys.stderr, - ) - sys.exit(1) if __name__ == "__main__": diff --git a/python/private/pypi/env_marker_info.bzl b/python/private/pypi/env_marker_info.bzl new file mode 100644 index 0000000000..b483436d98 --- /dev/null +++ b/python/private/pypi/env_marker_info.bzl @@ -0,0 +1,26 @@ +"""Provider for implementing environment marker values.""" + +EnvMarkerInfo = provider( + doc = """ +The values to use during environment marker evaluation. + +:::{seealso} +The {obj}`--//python/config_settings:pip_env_marker_config` flag. +::: + +:::{versionadded} VERSION_NEXT_FEATURE +""", + fields = { + "env": """ +:type: dict[str, str] + +The values to use for environment markers when evaluating an expression. + +The keys and values should be compatible with the [PyPA dependency specifiers +specification](https://packaging.python.org/en/latest/specifications/dependency-specifiers/) + +Missing values will be set to the specification's defaults or computed using +available toolchain information. +""", + }, +) diff --git a/python/private/pypi/env_marker_setting.bzl b/python/private/pypi/env_marker_setting.bzl new file mode 100644 index 0000000000..2bfdf42ef0 --- /dev/null +++ b/python/private/pypi/env_marker_setting.bzl @@ -0,0 +1,140 @@ +"""Implement a flag for matching the dependency specifiers at analysis time.""" + +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") +load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") +load(":env_marker_info.bzl", "EnvMarkerInfo") +load(":pep508_env.bzl", "create_env", "set_missing_env_defaults") +load(":pep508_evaluate.bzl", "evaluate") + +# Use capitals to hint its not an actual boolean type. +_ENV_MARKER_TRUE = "TRUE" +_ENV_MARKER_FALSE = "FALSE" + +def env_marker_setting(*, name, expression, **kwargs): + """Creates an env_marker setting. + + Generated targets: + + * `is_{name}_true`: config_setting that matches when the expression is true. + * `{name}`: env marker target that evalutes the expression. + + Args: + name: {type}`str` target name + expression: {type}`str` the environment marker string to evaluate + **kwargs: {type}`dict` additional common kwargs. + """ + native.config_setting( + name = "is_{}_true".format(name), + flag_values = { + ":{}".format(name): _ENV_MARKER_TRUE, + }, + **kwargs + ) + _env_marker_setting( + name = name, + expression = expression, + **kwargs + ) + +def _env_marker_setting_impl(ctx): + env = create_env() + env.update( + ctx.attr._env_marker_config_flag[EnvMarkerInfo].env, + ) + + runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime + + if "python_version" not in env: + if runtime.interpreter_version_info: + version_info = runtime.interpreter_version_info + env["python_version"] = "{major}.{minor}".format( + major = version_info.major, + minor = version_info.minor, + ) + full_version = _format_full_version(version_info) + env["python_full_version"] = full_version + env["implementation_version"] = full_version + else: + env["python_version"] = _get_flag(ctx.attr._python_version_major_minor_flag) + full_version = _get_flag(ctx.attr._python_full_version_flag) + env["python_full_version"] = full_version + env["implementation_version"] = full_version + + if "implementation_name" not in env and runtime.implementation_name: + env["implementation_name"] = runtime.implementation_name + + set_missing_env_defaults(env) + if evaluate(ctx.attr.expression, env = env): + value = _ENV_MARKER_TRUE + else: + value = _ENV_MARKER_FALSE + return [config_common.FeatureFlagInfo(value = value)] + +_env_marker_setting = rule( + doc = """ +Evaluates an environment marker expression using target configuration info. + +See +https://packaging.python.org/en/latest/specifications/dependency-specifiers +for the specification of behavior. +""", + implementation = _env_marker_setting_impl, + attrs = { + "expression": attr.string( + mandatory = True, + doc = "Environment marker expression to evaluate.", + ), + "_env_marker_config_flag": attr.label( + default = "//python/config_settings:pip_env_marker_config", + providers = [EnvMarkerInfo], + ), + "_python_full_version_flag": attr.label( + default = "//python/config_settings:python_version", + providers = [config_common.FeatureFlagInfo], + ), + "_python_version_major_minor_flag": attr.label( + default = "//python/config_settings:python_version_major_minor", + providers = [config_common.FeatureFlagInfo], + ), + }, + provides = [config_common.FeatureFlagInfo], + toolchains = [ + TARGET_TOOLCHAIN_TYPE, + ], +) + +def _format_full_version(info): + """Format the full python interpreter version. + + Adapted from spec code at: + https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers + + Args: + info: The provider from the Python runtime. + + Returns: + a {type}`str` with the version + """ + kind = info.releaselevel + if kind == "final": + kind = "" + serial = "" + else: + kind = kind[0] if kind else "" + serial = str(info.serial) if info.serial else "" + + return "{major}.{minor}.{micro}{kind}{serial}".format( + v = info, + major = info.major, + minor = info.minor, + micro = info.micro, + kind = kind, + serial = serial, + ) + +def _get_flag(t): + if config_common.FeatureFlagInfo in t: + return t[config_common.FeatureFlagInfo].value + if BuildSettingInfo in t: + return t[BuildSettingInfo].value + fail("Should not occur: {} does not have necessary providers") diff --git a/python/private/pypi/evaluate_markers.bzl b/python/private/pypi/evaluate_markers.bzl index a0223abdc8..191933596e 100644 --- a/python/private/pypi/evaluate_markers.bzl +++ b/python/private/pypi/evaluate_markers.bzl @@ -14,16 +14,28 @@ """A simple function that evaluates markers using a python interpreter.""" +load(":deps.bzl", "record_files") load(":pep508_env.bzl", "env") load(":pep508_evaluate.bzl", "evaluate") load(":pep508_platform.bzl", "platform_from_str") load(":pep508_requirement.bzl", "requirement") +load(":pypi_repo_utils.bzl", "pypi_repo_utils") -def evaluate_markers(requirements): +# Used as a default value in a rule to ensure we fetch the dependencies. +SRCS = [ + # When the version, or any of the files in `packaging` package changes, + # this file will change as well. + record_files["pypi__packaging"], + Label("//python/private/pypi/requirements_parser:resolve_target_platforms.py"), + Label("//python/private/pypi/whl_installer:platform.py"), +] + +def evaluate_markers(requirements, python_version = None): """Return the list of supported platforms per requirements line. Args: - requirements: dict[str, list[str]] of the requirement file lines to evaluate. + requirements: {type}`dict[str, list[str]]` of the requirement file lines to evaluate. + python_version: {type}`str | None` the version that can be used when evaluating the markers. Returns: dict of string lists with target platforms @@ -32,7 +44,58 @@ def evaluate_markers(requirements): for req_string, platforms in requirements.items(): req = requirement(req_string) for platform in platforms: - if evaluate(req.marker, env = env(platform_from_str(platform, None))): + if evaluate(req.marker, env = env(platform_from_str(platform, python_version))): ret.setdefault(req_string, []).append(platform) return ret + +def evaluate_markers_py(mrctx, *, requirements, python_interpreter, python_interpreter_target, srcs, logger = None): + """Return the list of supported platforms per requirements line. + + Args: + mrctx: repository_ctx or module_ctx. + requirements: list[str] of the requirement file lines to evaluate. + python_interpreter: str, path to the python_interpreter to use to + evaluate the env markers in the given requirements files. It will + be only called if the requirements files have env markers. This + should be something that is in your PATH or an absolute path. + python_interpreter_target: Label, same as python_interpreter, but in a + label format. + srcs: list[Label], the value of SRCS passed from the `rctx` or `mctx` to this function. + logger: repo_utils.logger or None, a simple struct to log diagnostic + messages. Defaults to None. + + Returns: + dict of string lists with target platforms + """ + if not requirements: + return {} + + in_file = mrctx.path("requirements_with_markers.in.json") + out_file = mrctx.path("requirements_with_markers.out.json") + mrctx.file(in_file, json.encode(requirements)) + + pypi_repo_utils.execute_checked( + mrctx, + op = "ResolveRequirementEnvMarkers({})".format(in_file), + python = pypi_repo_utils.resolve_python_interpreter( + mrctx, + python_interpreter = python_interpreter, + python_interpreter_target = python_interpreter_target, + ), + arguments = [ + "-m", + "python.private.pypi.requirements_parser.resolve_target_platforms", + in_file, + out_file, + ], + srcs = srcs, + environment = { + "PYTHONPATH": [ + Label("@pypi__packaging//:BUILD.bazel"), + Label("//:BUILD.bazel"), + ], + }, + logger = logger, + ) + return json.decode(mrctx.read(out_file)) diff --git a/python/private/pypi/extension.bzl b/python/private/pypi/extension.bzl index 68776e32d0..b79be6e038 100644 --- a/python/private/pypi/extension.bzl +++ b/python/private/pypi/extension.bzl @@ -16,13 +16,16 @@ load("@bazel_features//:features.bzl", "bazel_features") load("@pythons_hub//:interpreters.bzl", "INTERPRETER_LABELS") +load("@pythons_hub//:versions.bzl", "MINOR_MAPPING") +load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") load("//python/private:auth.bzl", "AUTH_ATTRS") +load("//python/private:full_version.bzl", "full_version") load("//python/private:normalize_name.bzl", "normalize_name") load("//python/private:repo_utils.bzl", "repo_utils") -load("//python/private:semver.bzl", "semver") +load("//python/private:version.bzl", "version") load("//python/private:version_label.bzl", "version_label") load(":attrs.bzl", "use_isolated") -load(":evaluate_markers.bzl", "evaluate_markers") +load(":evaluate_markers.bzl", "evaluate_markers_py", EVALUATE_MARKERS_SRCS = "SRCS") load(":hub_repository.bzl", "hub_repository", "whl_config_settings_to_json") load(":parse_requirements.bzl", "parse_requirements") load(":parse_whl_name.bzl", "parse_whl_name") @@ -32,11 +35,10 @@ load(":simpleapi_download.bzl", "simpleapi_download") load(":whl_config_setting.bzl", "whl_config_setting") load(":whl_library.bzl", "whl_library") load(":whl_repo_name.bzl", "pypi_repo_name", "whl_repo_name") -load(":whl_target_platforms.bzl", "whl_target_platforms") -def _major_minor_version(version): - version = semver(version) - return "{}.{}".format(version.major, version.minor) +def _major_minor_version(version_str): + ver = version.parse(version_str) + return "{}.{}".format(ver.release[0], ver.release[1]) def _whl_mods_impl(whl_mods_dict): """Implementation of the pip.whl_mods tag class. @@ -68,21 +70,26 @@ def _create_whl_repos( *, pip_attr, whl_overrides, - evaluate_markers = evaluate_markers, available_interpreters = INTERPRETER_LABELS, - get_index_urls = None): + minor_mapping = MINOR_MAPPING, + evaluate_markers = evaluate_markers_py, + get_index_urls = None, + enable_pipstar = False): """create all of the whl repositories Args: module_ctx: {type}`module_ctx`. pip_attr: {type}`struct` - the struct that comes from the tag class iteration. whl_overrides: {type}`dict[str, struct]` - per-wheel overrides. - evaluate_markers: the function to use to evaluate markers. get_index_urls: A function used to get the index URLs available_interpreters: {type}`dict[str, Label]` The dictionary of available interpreters that have been registered using the `python` bzlmod extension. The keys are in the form `python_{snake_case_version}_host`. This is to be used during the `repository_rule` and must be always compatible with the host. + minor_mapping: {type}`dict[str, str]` The dictionary needed to resolve the full + python version used to parse package METADATA files. + evaluate_markers: the function used to evaluate the markers. + enable_pipstar: enable the pipstar feature. Returns a {type}`struct` with the following attributes: whl_map: {type}`dict[str, list[struct]]` the output is keyed by the @@ -162,20 +169,45 @@ def _create_whl_repos( requirements_osx = pip_attr.requirements_darwin, requirements_windows = pip_attr.requirements_windows, extra_pip_args = pip_attr.extra_pip_args, - python_version = major_minor, + python_version = full_version( + version = pip_attr.python_version, + minor_mapping = minor_mapping, + ), logger = logger, ), extra_pip_args = pip_attr.extra_pip_args, get_index_urls = get_index_urls, - # NOTE @aignas 2025-02-24: we will use the "cp3xx_os_arch" platform labels - # for converting to the PEP508 environment and will evaluate them in starlark - # without involving the interpreter at all. - evaluate_markers = evaluate_markers, + # NOTE @aignas 2024-08-02: , we will execute any interpreter that we find either + # in the PATH or if specified as a label. We will configure the env + # markers when evaluating the requirement lines based on the output + # from the `requirements_files_by_platform` which should have something + # similar to: + # { + # "//:requirements.txt": ["cp311_linux_x86_64", ...] + # } + # + # We know the target python versions that we need to evaluate the + # markers for and thus we don't need to use multiple python interpreter + # instances to perform this manipulation. This function should be executed + # only once by the underlying code to minimize the overhead needed to + # spin up a Python interpreter. + evaluate_markers = lambda module_ctx, requirements: evaluate_markers( + module_ctx, + requirements = requirements, + python_interpreter = pip_attr.python_interpreter, + python_interpreter_target = python_interpreter_target, + srcs = pip_attr._evaluate_markers_srcs, + logger = logger, + ), logger = logger, ) - for whl_name, requirements in requirements_by_platform.items(): - group_name = whl_group_mapping.get(whl_name) + exposed_packages = {} + for whl in requirements_by_platform: + if whl.is_exposed: + exposed_packages[whl.name] = None + + group_name = whl_group_mapping.get(whl.name) group_deps = requirement_cycles.get(group_name, []) # Construct args separately so that the lock file can be smaller and does not include unused @@ -186,12 +218,11 @@ def _create_whl_repos( maybe_args = dict( # The following values are safe to omit if they have false like values add_libdir_to_library_search_path = pip_attr.add_libdir_to_library_search_path, - annotation = whl_modifications.get(whl_name), + annotation = whl_modifications.get(whl.name), download_only = pip_attr.download_only, enable_implicit_namespace_pkgs = pip_attr.enable_implicit_namespace_pkgs, environment = pip_attr.environment, envsubst = pip_attr.envsubst, - experimental_target_platforms = pip_attr.experimental_target_platforms, group_deps = group_deps, group_name = group_name, pip_data_exclude = pip_attr.pip_data_exclude, @@ -199,9 +230,12 @@ def _create_whl_repos( python_interpreter_target = python_interpreter_target, whl_patches = { p: json.encode(args) - for p, args in whl_overrides.get(whl_name, {}).items() + for p, args in whl_overrides.get(whl.name, {}).items() }, ) + if not enable_pipstar: + maybe_args["experimental_target_platforms"] = pip_attr.experimental_target_platforms + whl_library_args.update({k: v for k, v in maybe_args.items() if v}) maybe_args_with_default = dict( # The following values have defaults next to them @@ -215,123 +249,104 @@ def _create_whl_repos( if v != default }) - for requirement in requirements: - for repo_name, (args, config_setting) in _whl_repos( - requirement = requirement, + for src in whl.srcs: + repo = _whl_repo( + src = src, whl_library_args = whl_library_args, download_only = pip_attr.download_only, netrc = pip_attr.netrc, auth_patterns = pip_attr.auth_patterns, python_version = major_minor, - multiple_requirements_for_whl = len(requirements) > 1., - ).items(): - repo_name = "{}_{}".format(pip_name, repo_name) - if repo_name in whl_libraries: - fail("Attempting to creating a duplicate library {} for {}".format( - repo_name, - whl_name, - )) - - whl_libraries[repo_name] = args - whl_map.setdefault(whl_name, {})[config_setting] = repo_name + is_multiple_versions = whl.is_multiple_versions, + enable_pipstar = enable_pipstar, + ) + + repo_name = "{}_{}".format(pip_name, repo.repo_name) + if repo_name in whl_libraries: + fail("Attempting to creating a duplicate library {} for {}".format( + repo_name, + whl.name, + )) + + whl_libraries[repo_name] = repo.args + whl_map.setdefault(whl.name, {})[repo.config_setting] = repo_name return struct( whl_map = whl_map, - exposed_packages = { - whl_name: None - for whl_name, requirements in requirements_by_platform.items() - if len([r for r in requirements if r.is_exposed]) > 0 - }, + exposed_packages = exposed_packages, extra_aliases = extra_aliases, whl_libraries = whl_libraries, ) -def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patterns, multiple_requirements_for_whl = False, python_version): - ret = {} - - dists = requirement.whls - if not download_only and requirement.sdist: - dists = dists + [requirement.sdist] - - for distribution in dists: - args = dict(whl_library_args) - if netrc: - args["netrc"] = netrc - if auth_patterns: - args["auth_patterns"] = auth_patterns - - if not distribution.filename.endswith(".whl"): - # pip is not used to download wheels and the python - # `whl_library` helpers are only extracting things, however - # for sdists, they will be built by `pip`, so we still - # need to pass the extra args there. - args["extra_pip_args"] = requirement.extra_pip_args - - # This is no-op because pip is not used to download the wheel. - args.pop("download_only", None) - - args["requirement"] = requirement.srcs.requirement - args["urls"] = [distribution.url] - args["sha256"] = distribution.sha256 - args["filename"] = distribution.filename - args["experimental_target_platforms"] = requirement.target_platforms - - # Pure python wheels or sdists may need to have a platform here - target_platforms = None - if distribution.filename.endswith(".whl") and not distribution.filename.endswith("-any.whl"): - parsed_whl = parse_whl_name(distribution.filename) - whl_platforms = whl_target_platforms( - platform_tag = parsed_whl.platform_tag, - ) - args["experimental_target_platforms"] = [ - p - for p in requirement.target_platforms - if [None for wp in whl_platforms if p.endswith(wp.target_platform)] - ] - elif multiple_requirements_for_whl: - target_platforms = requirement.target_platforms - - repo_name = whl_repo_name( - distribution.filename, - distribution.sha256, - ) - ret[repo_name] = ( - args, - whl_config_setting( +def _whl_repo(*, src, whl_library_args, is_multiple_versions, download_only, netrc, auth_patterns, python_version, enable_pipstar = False): + args = dict(whl_library_args) + args["requirement"] = src.requirement_line + is_whl = src.filename.endswith(".whl") + + if src.extra_pip_args and not is_whl: + # pip is not used to download wheels and the python + # `whl_library` helpers are only extracting things, however + # for sdists, they will be built by `pip`, so we still + # need to pass the extra args there, so only pop this for whls + args["extra_pip_args"] = src.extra_pip_args + + if not src.url or (not is_whl and download_only): + # Fallback to a pip-installed wheel + target_platforms = src.target_platforms if is_multiple_versions else [] + return struct( + repo_name = pypi_repo_name( + normalize_name(src.distribution), + *target_platforms + ), + args = args, + config_setting = whl_config_setting( version = python_version, - filename = distribution.filename, - target_platforms = target_platforms, + target_platforms = target_platforms or None, ), ) - if ret: - return ret + # This is no-op because pip is not used to download the wheel. + args.pop("download_only", None) + + if netrc: + args["netrc"] = netrc + if auth_patterns: + args["auth_patterns"] = auth_patterns + + args["urls"] = [src.url] + args["sha256"] = src.sha256 + args["filename"] = src.filename + if not enable_pipstar: + args["experimental_target_platforms"] = [ + # Get rid of the version fot the target platforms because we are + # passing the interpreter any way. Ideally we should search of ways + # how to pass the target platforms through the hub repo. + p.partition("_")[2] + for p in src.target_platforms + ] + + # Pure python wheels or sdists may need to have a platform here + target_platforms = None + if is_whl and not src.filename.endswith("-any.whl"): + pass + elif is_multiple_versions: + target_platforms = src.target_platforms - # Fallback to a pip-installed wheel - args = dict(whl_library_args) # make a copy - args["requirement"] = requirement.srcs.requirement_line - if requirement.extra_pip_args: - args["extra_pip_args"] = requirement.extra_pip_args - - if download_only: - args.setdefault("experimental_target_platforms", requirement.target_platforms) - - target_platforms = requirement.target_platforms if multiple_requirements_for_whl else [] - repo_name = pypi_repo_name( - normalize_name(requirement.distribution), - *target_platforms - ) - ret[repo_name] = ( - args, - whl_config_setting( + return struct( + repo_name = whl_repo_name(src.filename, src.sha256), + args = args, + config_setting = whl_config_setting( version = python_version, - target_platforms = target_platforms or None, + filename = src.filename, + target_platforms = target_platforms, ), ) - return ret - -def parse_modules(module_ctx, _fail = fail, simpleapi_download = simpleapi_download, **kwargs): +def parse_modules( + module_ctx, + _fail = fail, + simpleapi_download = simpleapi_download, + **kwargs): """Implementation of parsing the tag classes for the extension and return a struct for registering repositories. Args: @@ -607,7 +622,7 @@ def _pip_impl(module_ctx): module_ctx: module contents """ - mods = parse_modules(module_ctx) + mods = parse_modules(module_ctx, enable_pipstar = rp_config.enable_pipstar) # Build all of the wheel modifications if the tag class is called. _whl_mods_impl(mods.whl_mods) @@ -629,12 +644,9 @@ def _pip_impl(module_ctx): ) if bazel_features.external_deps.extension_metadata_has_reproducible: - # If we are not using the `experimental_index_url feature, the extension is fully - # deterministic and we don't need to create a lock entry for it. - # - # In order to be able to dogfood the `experimental_index_url` feature before it gets - # stabilized, we have created the `_pip_non_reproducible` function, that will result - # in extra entries in the lock file. + # NOTE @aignas 2025-04-15: this is set to be reproducible, because the + # results after calling the PyPI index should be reproducible on each + # machine. return module_ctx.extension_metadata(reproducible = True) else: return None @@ -776,6 +788,13 @@ EXPERIMENTAL: this may be removed without notice. doc = """\ A dict of labels to wheel names that is typically generated by the whl_modifications. The labels are JSON config files describing the modifications. +""", + ), + "_evaluate_markers_srcs": attr.label_list( + default = EVALUATE_MARKERS_SRCS, + doc = """\ +The list of labels to use as SRCS for the marker evaluation code. This ensures that the +code will be re-evaluated when any of files in the default changes. """, ), }, **ATTRS) diff --git a/python/private/pypi/flags.bzl b/python/private/pypi/flags.bzl index a25579a2b8..037383910e 100644 --- a/python/private/pypi/flags.bzl +++ b/python/private/pypi/flags.bzl @@ -20,6 +20,15 @@ unnecessary files when all that are needed are flag definitions. load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo", "string_flag") load("//python/private:enum.bzl", "enum") +load(":env_marker_info.bzl", "EnvMarkerInfo") +load( + ":pep508_env.bzl", + "create_env", + "os_name_select_map", + "platform_machine_select_map", + "platform_system_select_map", + "sys_platform_select_map", +) # Determines if we should use whls for third party # @@ -82,6 +91,10 @@ def define_pypi_internal_flags(name): visibility = ["//visibility:public"], ) + _default_env_marker_config( + name = "_pip_env_marker_default_config", + ) + def _allow_wheels_flag_impl(ctx): input = ctx.attr._setting[BuildSettingInfo].value value = "yes" if input in ["auto", "only"] else "no" @@ -97,3 +110,58 @@ This rule allows us to greatly reduce the number of config setting targets at no if we are duplicating some of the functionality of the `native.config_setting`. """, ) + +def _default_env_marker_config(**kwargs): + _env_marker_config( + os_name = select(os_name_select_map), + sys_platform = select(sys_platform_select_map), + platform_machine = select(platform_machine_select_map), + platform_system = select(platform_system_select_map), + platform_release = select({ + "@platforms//os:osx": "USE_OSX_VERSION_FLAG", + "//conditions:default": "", + }), + **kwargs + ) + +def _env_marker_config_impl(ctx): + env = create_env() + env["os_name"] = ctx.attr.os_name + env["sys_platform"] = ctx.attr.sys_platform + env["platform_machine"] = ctx.attr.platform_machine + + # NOTE: Platform release for Android will be Android version: + # https://peps.python.org/pep-0738/#platform + # Similar for iOS: + # https://peps.python.org/pep-0730/#platform + platform_release = ctx.attr.platform_release + if platform_release == "USE_OSX_VERSION_FLAG": + platform_release = _get_flag(ctx.attr._pip_whl_osx_version_flag) + env["platform_release"] = platform_release + env["platform_system"] = ctx.attr.platform_system + + # NOTE: We intentionally do not call set_missing_env_defaults() here because + # `env_marker_setting()` computes missing values using the toolchain. + return [EnvMarkerInfo(env = env)] + +_env_marker_config = rule( + implementation = _env_marker_config_impl, + attrs = { + "os_name": attr.string(), + "platform_machine": attr.string(), + "platform_release": attr.string(), + "platform_system": attr.string(), + "sys_platform": attr.string(), + "_pip_whl_osx_version_flag": attr.label( + default = "//python/config_settings:pip_whl_osx_version", + providers = [[BuildSettingInfo], [config_common.FeatureFlagInfo]], + ), + }, +) + +def _get_flag(t): + if config_common.FeatureFlagInfo in t: + return t[config_common.FeatureFlagInfo].value + if BuildSettingInfo in t: + return t[BuildSettingInfo].value + fail("Should not occur: {} does not have necessary providers") diff --git a/python/private/pypi/generate_whl_library_build_bazel.bzl b/python/private/pypi/generate_whl_library_build_bazel.bzl index 8050cd22ad..3764e720c0 100644 --- a/python/private/pypi/generate_whl_library_build_bazel.bzl +++ b/python/private/pypi/generate_whl_library_build_bazel.bzl @@ -24,20 +24,24 @@ _RENDER = { "dependencies": render.list, "dependencies_by_platform": lambda x: render.dict(x, value_repr = render.list), "entry_points": render.dict, + "extras": render.list, "group_deps": render.list, + "include": str, + "requires_dist": render.list, "srcs_exclude": render.list, "tags": render.list, + "target_platforms": render.list, } # NOTE @aignas 2024-10-25: We have to keep this so that files in # this repository can be publicly visible without the need for # export_files _TEMPLATE = """\ -load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets") +{loads} package(default_visibility = ["//visibility:public"]) -whl_library_targets( +{fn}( {kwargs} ) """ @@ -45,11 +49,13 @@ whl_library_targets( def generate_whl_library_build_bazel( *, annotation = None, + default_python_version = None, **kwargs): """Generate a BUILD file for an unzipped Wheel Args: annotation: The annotation for the build file. + default_python_version: The python version to use to parse the METADATA. **kwargs: Extra args serialized to be passed to the {obj}`whl_library_targets`. @@ -57,6 +63,45 @@ def generate_whl_library_build_bazel( A complete BUILD file as a string """ + loads = [] + if kwargs.get("tags"): + fn = "whl_library_targets" + + # legacy path + unsupported_args = [ + "requires", + "metadata_name", + "metadata_version", + "include", + ] + else: + fn = "whl_library_targets_from_requires" + unsupported_args = [ + "dependencies", + "dependencies_by_platform", + "target_platforms", + "default_python_version", + ] + dep_template = kwargs.get("dep_template") + loads.append( + """load("{}", "{}")""".format( + dep_template.format( + name = "", + target = "config.bzl", + ), + "whl_map", + ), + ) + kwargs["include"] = "whl_map" + + for arg in unsupported_args: + if kwargs.get(arg): + fail("BUG, unsupported arg: '{}'".format(arg)) + + loads.extend([ + """load("@rules_python//python/private/pypi:whl_library_targets.bzl", "{}")""".format(fn), + ]) + additional_content = [] if annotation: kwargs["data"] = annotation.data @@ -66,10 +111,14 @@ def generate_whl_library_build_bazel( kwargs["srcs_exclude"] = annotation.srcs_exclude_glob if annotation.additive_build_content: additional_content.append(annotation.additive_build_content) + if default_python_version: + kwargs["default_python_version"] = default_python_version contents = "\n".join( [ _TEMPLATE.format( + loads = "\n".join(loads), + fn = fn, kwargs = render.indent("\n".join([ "{} = {},".format(k, _RENDER.get(k, repr)(v)) for k, v in sorted(kwargs.items()) diff --git a/python/private/pypi/hub_repository.bzl b/python/private/pypi/hub_repository.bzl index 48245b4106..0dbc6c29c2 100644 --- a/python/private/pypi/hub_repository.bzl +++ b/python/private/pypi/hub_repository.bzl @@ -45,7 +45,14 @@ def _impl(rctx): macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name) rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS) - rctx.template("requirements.bzl", rctx.attr._template, substitutions = { + rctx.template( + "config.bzl", + rctx.attr._config_template, + substitutions = { + "%%WHL_MAP%%": render.dict(rctx.attr.whl_map, value_repr = lambda x: "None"), + }, + ) + rctx.template("requirements.bzl", rctx.attr._requirements_bzl_template, substitutions = { "%%ALL_DATA_REQUIREMENTS%%": render.list([ macro_tmpl.format(p, "data") for p in bzl_packages @@ -87,7 +94,10 @@ The wheel map where values are json.encoded strings of the whl_map constructed in the pip.parse tag class. """, ), - "_template": attr.label( + "_config_template": attr.label( + default = ":config.bzl.tmpl.bzlmod", + ), + "_requirements_bzl_template": attr.label( default = ":requirements.bzl.tmpl.bzlmod", ), }, diff --git a/python/private/pypi/index_sources.bzl b/python/private/pypi/index_sources.bzl index e3762d2a48..803670c3e4 100644 --- a/python/private/pypi/index_sources.bzl +++ b/python/private/pypi/index_sources.bzl @@ -16,6 +16,23 @@ A file that houses private functions used in the `bzlmod` extension with the same name. """ +# Just list them here and me super conservative +_KNOWN_EXTS = [ + # Note, the following source in pip has more extensions + # https://github.com/pypa/pip/blob/3c5a189141a965f21a473e46c3107e689eb9f79f/src/pip/_vendor/distlib/locators.py#L90 + # + # ".tar.bz2", + # ".tar", + # ".tgz", + # ".tbz", + # + # But we support only the following, used in 'packaging' + # https://github.com/pypa/pip/blob/3c5a189141a965f21a473e46c3107e689eb9f79f/src/pip/_vendor/packaging/utils.py#L137 + ".whl", + ".tar.gz", + ".zip", +] + def index_sources(line): """Get PyPI sources from a requirements.txt line. @@ -58,11 +75,31 @@ def index_sources(line): ).strip() url = "" + filename = "" if "@" in head: - requirement = requirement_line - _, _, url_and_rest = requirement.partition("@") + maybe_requirement, _, url_and_rest = requirement.partition("@") url = url_and_rest.strip().partition(" ")[0].strip() + url, _, sha256 = url.partition("#sha256=") + if sha256: + shas.append(sha256) + _, _, filename = url.rpartition("/") + + # Replace URL encoded characters and luckily there is only one case + filename = filename.replace("%2B", "+") + is_known_ext = False + for ext in _KNOWN_EXTS: + if filename.endswith(ext): + is_known_ext = True + break + + if is_known_ext: + requirement = maybe_requirement.strip() + else: + # could not detect filename from the URL + filename = "" + requirement = requirement_line + return struct( requirement = requirement, requirement_line = requirement_line, @@ -70,4 +107,5 @@ def index_sources(line): shas = sorted(shas), marker = marker, url = url, + filename = filename, ) diff --git a/python/private/pypi/namespace_pkg_tmpl.py b/python/private/pypi/namespace_pkg_tmpl.py new file mode 100644 index 0000000000..a21b846e76 --- /dev/null +++ b/python/private/pypi/namespace_pkg_tmpl.py @@ -0,0 +1,2 @@ +# __path__ manipulation added by bazel-contrib/rules_python to support namespace pkgs. +__path__ = __import__("pkgutil").extend_path(__path__, __name__) diff --git a/python/private/pypi/namespace_pkgs.bzl b/python/private/pypi/namespace_pkgs.bzl new file mode 100644 index 0000000000..bf4689a5ea --- /dev/null +++ b/python/private/pypi/namespace_pkgs.bzl @@ -0,0 +1,83 @@ +"""Utilities to get where we should write namespace pkg paths.""" + +load("@bazel_skylib//rules:copy_file.bzl", "copy_file") + +_ext = struct( + py = ".py", + pyd = ".pyd", + so = ".so", + pyc = ".pyc", +) + +_TEMPLATE = Label("//python/private/pypi:namespace_pkg_tmpl.py") + +def _add_all(dirname, dirs): + dir_path = "." + for dir_name in dirname.split("/"): + dir_path = "{}/{}".format(dir_path, dir_name) + dirs[dir_path[2:]] = None + +def get_files(*, srcs, ignored_dirnames = [], root = None): + """Get the list of filenames to write the namespace pkg files. + + Args: + srcs: {type}`src` a list of files to be passed to {bzl:obj}`py_library` + as `srcs` and `data`. This is usually a result of a {obj}`glob`. + ignored_dirnames: {type}`str` a list of patterns to ignore. + root: {type}`str` the prefix to use as the root. + + Returns: + {type}`src` a list of paths to write the namespace pkg `__init__.py` file. + """ + dirs = {} + ignored = {i: None for i in ignored_dirnames} + + if root: + _add_all(root, ignored) + + for file in srcs: + dirname, _, filename = file.rpartition("/") + + if filename == "__init__.py": + ignored[dirname] = None + dirname, _, _ = dirname.rpartition("/") + elif filename.endswith(_ext.py): + pass + elif filename.endswith(_ext.pyc): + pass + elif filename.endswith(_ext.pyd): + pass + elif filename.endswith(_ext.so): + pass + else: + continue + + if dirname in dirs or not dirname: + continue + + _add_all(dirname, dirs) + + return sorted([d for d in dirs if d not in ignored]) + +def create_inits(**kwargs): + """Create init files and return the list to be included `py_library` srcs. + + Args: + **kwargs: passed to {obj}`get_files`. + + Returns: + {type}`list[str]` to be included as part of `py_library`. + """ + srcs = [] + for out in get_files(**kwargs): + src = "{}/__init__.py".format(out) + srcs.append(srcs) + + copy_file( + name = "_cp_{}_namespace".format(out), + src = _TEMPLATE, + out = src, + **kwargs + ) + + return srcs diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index 1cbf094f5c..e4a8b90acb 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl @@ -40,6 +40,7 @@ def parse_requirements( extra_pip_args = [], get_index_urls = None, evaluate_markers = None, + extract_url_srcs = True, logger = None): """Get the requirements with platforms that the requirements apply to. @@ -58,6 +59,8 @@ def parse_requirements( the platforms stored as values in the input dict. Returns the same dict, but with values being platforms that are compatible with the requirements line. + extract_url_srcs: A boolean to enable extracting URLs from requirement + lines to enable using bazel downloader. logger: repo_utils.logger or None, a simple struct to log diagnostic messages. Returns: @@ -80,7 +83,7 @@ def parse_requirements( The second element is extra_pip_args should be passed to `whl_library`. """ - evaluate_markers = evaluate_markers or (lambda _: {}) + evaluate_markers = evaluate_markers or (lambda _ctx, _requirements: {}) options = {} requirements = {} for file, plats in requirements_by_platform.items(): @@ -156,7 +159,7 @@ def parse_requirements( # to do, we could use Python to parse the requirement lines and infer the # URL of the files to download things from. This should be important for # VCS package references. - env_marker_target_platforms = evaluate_markers(reqs_with_env_markers) + env_marker_target_platforms = evaluate_markers(ctx, reqs_with_env_markers) if logger: logger.debug(lambda: "Evaluated env markers from:\n{}\n\nTo:\n{}".format( reqs_with_env_markers, @@ -176,49 +179,101 @@ def parse_requirements( }), ) - ret = {} - for whl_name, reqs in sorted(requirements_by_platform.items()): + ret = [] + for name, reqs in sorted(requirements_by_platform.items()): requirement_target_platforms = {} for r in reqs.values(): target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms) for p in target_platforms: requirement_target_platforms[p] = None - is_exposed = len(requirement_target_platforms) == len(requirements) - if not is_exposed and logger: + item = struct( + # Return normalized names + name = normalize_name(name), + is_exposed = len(requirement_target_platforms) == len(requirements), + is_multiple_versions = len(reqs.values()) > 1, + srcs = _package_srcs( + name = name, + reqs = reqs, + index_urls = index_urls, + env_marker_target_platforms = env_marker_target_platforms, + extract_url_srcs = extract_url_srcs, + logger = logger, + ), + ) + ret.append(item) + if not item.is_exposed and logger: logger.debug(lambda: "Package '{}' will not be exposed because it is only present on a subset of platforms: {} out of {}".format( - whl_name, + name, sorted(requirement_target_platforms), sorted(requirements), )) - # Return normalized names - ret_requirements = ret.setdefault(normalize_name(whl_name), []) + if logger: + logger.debug(lambda: "Will configure whl repos: {}".format([w.name for w in ret])) - for r in sorted(reqs.values(), key = lambda r: r.requirement_line): - whls, sdist = _add_dists( - requirement = r, - index_urls = index_urls.get(whl_name), - logger = logger, - ) + return ret - target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms) - ret_requirements.append( +def _package_srcs( + *, + name, + reqs, + index_urls, + logger, + env_marker_target_platforms, + extract_url_srcs): + """A function to return sources for a particular package.""" + srcs = {} + for r in sorted(reqs.values(), key = lambda r: r.requirement_line): + whls, sdist = _add_dists( + requirement = r, + index_urls = index_urls.get(name), + logger = logger, + ) + + target_platforms = env_marker_target_platforms.get(r.requirement_line, r.target_platforms) + target_platforms = sorted(target_platforms) + + all_dists = [] + whls + if sdist: + all_dists.append(sdist) + + if extract_url_srcs and all_dists: + req_line = r.srcs.requirement + else: + all_dists = [struct( + url = "", + filename = "", + sha256 = "", + yanked = False, + )] + req_line = r.srcs.requirement_line + + extra_pip_args = tuple(r.extra_pip_args) + for dist in all_dists: + key = ( + dist.filename, + req_line, + extra_pip_args, + ) + entry = srcs.setdefault( + key, struct( - distribution = r.distribution, - srcs = r.srcs, - target_platforms = sorted(target_platforms), + distribution = name, extra_pip_args = r.extra_pip_args, - whls = whls, - sdist = sdist, - is_exposed = is_exposed, + requirement_line = req_line, + target_platforms = [], + filename = dist.filename, + sha256 = dist.sha256, + url = dist.url, + yanked = dist.yanked, ), ) + for p in target_platforms: + if p not in entry.target_platforms: + entry.target_platforms.append(p) - if logger: - logger.debug(lambda: "Will configure whl repos: {}".format(ret.keys())) - - return ret + return srcs.values() def select_requirement(requirements, *, platform): """A simple function to get a requirement for a particular platform. @@ -281,27 +336,26 @@ def _add_dists(*, requirement, index_urls, logger = None): logger: A logger for printing diagnostic info. """ - # Handle direct URLs in requirements if requirement.srcs.url: - url = requirement.srcs.url - _, _, filename = url.rpartition("/") - if "." not in filename: - # detected filename has no extension, it might be an sdist ref - # TODO @aignas 2025-04-03: should be handled if the following is fixed: - # https://github.com/bazel-contrib/rules_python/issues/2363 + if not requirement.srcs.filename: + if logger: + logger.debug(lambda: "Could not detect the filename from the URL, falling back to pip: {}".format( + requirement.srcs.url, + )) return [], None - direct_url_dist = struct( - url = url, - filename = filename, + # Handle direct URLs in requirements + dist = struct( + url = requirement.srcs.url, + filename = requirement.srcs.filename, sha256 = requirement.srcs.shas[0] if requirement.srcs.shas else "", yanked = False, ) - if filename.endswith(".whl"): - return [direct_url_dist], None + if dist.filename.endswith(".whl"): + return [dist], None else: - return [], direct_url_dist + return [], dist if not index_urls: return [], None diff --git a/python/private/pypi/parse_simpleapi_html.bzl b/python/private/pypi/parse_simpleapi_html.bzl index 8c6f739fe3..a41f0750c4 100644 --- a/python/private/pypi/parse_simpleapi_html.bzl +++ b/python/private/pypi/parse_simpleapi_html.bzl @@ -52,7 +52,7 @@ def parse_simpleapi_html(*, url, content): # Each line follows the following pattern # filename
- sha256_by_version = {} + sha256s_by_version = {} for line in lines[1:]: dist_url, _, tail = line.partition("#sha256=") dist_url = _absolute_url(url, dist_url) @@ -65,7 +65,7 @@ def parse_simpleapi_html(*, url, content): head, _, _ = tail.rpartition("") maybe_metadata, _, filename = head.rpartition(">") version = _version(filename) - sha256_by_version.setdefault(version, []).append(sha256) + sha256s_by_version.setdefault(version, []).append(sha256) metadata_sha256 = "" metadata_url = "" @@ -102,7 +102,7 @@ def parse_simpleapi_html(*, url, content): return struct( sdists = sdists, whls = whls, - sha256_by_version = sha256_by_version, + sha256s_by_version = sha256s_by_version, ) _SDIST_EXTS = [ diff --git a/python/private/pypi/pep508.bzl b/python/private/pypi/pep508.bzl deleted file mode 100644 index e74352def2..0000000000 --- a/python/private/pypi/pep508.bzl +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2025 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This module is for implementing PEP508 in starlark as FeatureFlagInfo -""" - -load(":pep508_env.bzl", _env = "env") -load(":pep508_evaluate.bzl", _evaluate = "evaluate", _to_string = "to_string") - -to_string = _to_string -evaluate = _evaluate -env = _env diff --git a/python/private/pypi/pep508_deps.bzl b/python/private/pypi/pep508_deps.bzl index af0a75362b..e73f747bed 100644 --- a/python/private/pypi/pep508_deps.bzl +++ b/python/private/pypi/pep508_deps.bzl @@ -16,35 +16,26 @@ """ load("//python/private:normalize_name.bzl", "normalize_name") -load(":pep508_env.bzl", "env") load(":pep508_evaluate.bzl", "evaluate") -load(":pep508_platform.bzl", "platform", "platform_from_str") load(":pep508_requirement.bzl", "requirement") -_ALL_OS_VALUES = [ - "windows", - "osx", - "linux", -] -_ALL_ARCH_VALUES = [ - "aarch64", - "ppc64", - "ppc64le", - "s390x", - "x86_32", - "x86_64", -] - -def deps(name, *, requires_dist, platforms = [], extras = [], host_python_version = None): +def deps( + name, + *, + requires_dist, + extras = [], + excludes = [], + include = []): """Parse the RequiresDist from wheel METADATA Args: name: {type}`str` the name of the wheel. requires_dist: {type}`list[str]` the list of RequiresDist lines from the METADATA file. + excludes: {type}`list[str]` what packages should we exclude. + include: {type}`list[str]` what packages should we exclude. If it is not + specified, then we will include all deps from `requires_dist`. extras: {type}`list[str]` the requested extras to generate targets for. - platforms: {type}`list[str]` the list of target platform strings. - host_python_version: {type}`str` the host python version. Returns: A struct with attributes: @@ -60,165 +51,48 @@ def deps(name, *, requires_dist, platforms = [], extras = [], host_python_versio deps_select = {} name = normalize_name(name) want_extras = _resolve_extras(name, reqs, extras) + include = [normalize_name(n) for n in include] # drop self edges - reqs = [r for r in reqs if r.name != name] - - platforms = [ - platform_from_str(p, python_version = host_python_version) - for p in platforms - ] or [ - platform_from_str("", python_version = host_python_version), - ] + excludes = [name] + [normalize_name(x) for x in excludes] - abis = sorted({p.abi: True for p in platforms if p.abi}) - if host_python_version and len(abis) > 1: - _, _, minor_version = host_python_version.partition(".") - minor_version, _, _ = minor_version.partition(".") - default_abi = "cp3" + minor_version - elif len(abis) > 1: - fail( - "all python versions need to be specified explicitly, got: {}".format(platforms), - ) - else: - default_abi = None + reqs_by_name = {} for req in reqs: - _add_req( + if req.name_ in excludes: + continue + + if include and req.name_ not in include: + continue + + reqs_by_name.setdefault(req.name, []).append(req) + + for name, reqs in reqs_by_name.items(): + _add_reqs( deps, deps_select, - req, + normalize_name(name), + reqs, extras = want_extras, - platforms = platforms, - default_abi = default_abi, ) return struct( deps = sorted(deps), deps_select = { - _platform_str(p): sorted(deps) - for p, deps in deps_select.items() + d: markers + for d, markers in sorted(deps_select.items()) }, ) -def _platform_str(self): - if self.abi == None: - if not self.os and not self.arch: - return "//conditions:default" - elif not self.arch: - return "@platforms//os:{}".format(self.os) - else: - return "{}_{}".format(self.os, self.arch) - - minor_version = self.abi[3:] - if self.arch == None and self.os == None: - return str(Label("//python/config_settings:is_python_3.{}".format(minor_version))) - - return "cp3{}_{}_{}".format( - minor_version, - self.os or "anyos", - self.arch or "anyarch", - ) - -def _platform_specializations(self, cpu_values = _ALL_ARCH_VALUES, os_values = _ALL_OS_VALUES): - """Return the platform itself and all its unambiguous specializations. - - For more info about specializations see - https://bazel.build/docs/configurable-attributes - """ - specializations = [] - specializations.append(self) - if self.arch == None: - specializations.extend([ - platform(os = self.os, arch = arch, abi = self.abi) - for arch in cpu_values - ]) - if self.os == None: - specializations.extend([ - platform(os = os, arch = self.arch, abi = self.abi) - for os in os_values - ]) - if self.os == None and self.arch == None: - specializations.extend([ - platform(os = os, arch = arch, abi = self.abi) - for os in os_values - for arch in cpu_values - ]) - return specializations - -def _add(deps, deps_select, dep, platform): +def _add(deps, deps_select, dep, markers = None): dep = normalize_name(dep) - if platform == None: + if not markers: deps[dep] = True - - # If the dep is in the platform-specific list, remove it from the select. - pop_keys = [] - for p, _deps in deps_select.items(): - if dep not in _deps: - continue - - _deps.pop(dep) - if not _deps: - pop_keys.append(p) - - for p in pop_keys: - deps_select.pop(p) - return - - if dep in deps: - # If the dep is already in the main dependency list, no need to add it in the - # platform-specific dependency list. - return - - # Add the platform-specific branch - deps_select.setdefault(platform, {}) - - # Add the dep to specializations of the given platform if they - # exist in the select statement. - for p in _platform_specializations(platform): - if p not in deps_select: - continue - - deps_select[p][dep] = True - - if len(deps_select[platform]) == 1: - # We are adding a new item to the select and we need to ensure that - # existing dependencies from less specialized platforms are propagated - # to the newly added dependency set. - for p, _deps in deps_select.items(): - # Check if the existing platform overlaps with the given platform - if p == platform or platform not in _platform_specializations(p): - continue - - deps_select[platform].update(_deps) - -def _maybe_add_common_dep(deps, deps_select, platforms, dep): - abis = sorted({p.abi: True for p in platforms if p.abi}) - if len(abis) < 2: - return - - platforms = [platform()] + [ - platform(abi = abi) - for abi in abis - ] - - # If the dep is targeting all target python versions, lets add it to - # the common dependency list to simplify the select statements. - for p in platforms: - if p not in deps_select: - return - - if dep not in deps_select[p]: - return - - # All of the python version-specific branches have the dep, so lets add - # it to the common deps. - deps[dep] = True - for p in platforms: - deps_select[p].pop(dep) - if not deps_select[p]: - deps_select.pop(p) + elif len(markers) == 1: + deps_select[dep] = markers[0] + else: + deps_select[dep] = "({})".format(") or (".join(sorted(markers))) def _resolve_extras(self_name, reqs, extras): """Resolve extras which are due to depending on self[some_other_extra]. @@ -275,77 +149,24 @@ def _resolve_extras(self_name, reqs, extras): # Poor mans set return sorted({x: None for x in extras}) -def _add_req(deps, deps_select, req, *, extras, platforms, default_abi = None): - if not req.marker: - _add(deps, deps_select, req.name, None) - return - - # NOTE @aignas 2023-12-08: in order to have reasonable select statements - # we do have to have some parsing of the markers, so it begs the question - # if packaging should be reimplemented in Starlark to have the best solution - # for now we will implement it in Python and see what the best parsing result - # can be before making this decision. - match_os = len([ - tag - for tag in [ - "os_name", - "sys_platform", - "platform_system", - ] - if tag in req.marker - ]) > 0 - match_arch = "platform_machine" in req.marker - match_version = "version" in req.marker - - if not (match_os or match_arch or match_version): - if [ - True - for extra in extras - for p in platforms - if evaluate( - req.marker, - env = env( - target_platform = p, - extra = extra, - ), - ) - ]: - _add(deps, deps_select, req.name, None) - return - - for plat in platforms: - if not [ - True - for extra in extras - if evaluate( - req.marker, - env = env( - target_platform = plat, - extra = extra, - ), - ) - ]: - continue +def _add_reqs(deps, deps_select, dep, reqs, *, extras): + for req in reqs: + if not req.marker: + _add(deps, deps_select, dep) + return - if match_arch and default_abi: - _add(deps, deps_select, req.name, plat) - if plat.abi == default_abi: - _add(deps, deps_select, req.name, platform(os = plat.os, arch = plat.arch)) - elif match_arch: - _add(deps, deps_select, req.name, platform(os = plat.os, arch = plat.arch)) - elif match_os and default_abi: - _add(deps, deps_select, req.name, platform(os = plat.os, abi = plat.abi)) - if plat.abi == default_abi: - _add(deps, deps_select, req.name, platform(os = plat.os)) - elif match_os: - _add(deps, deps_select, req.name, platform(os = plat.os)) - elif match_version and default_abi: - _add(deps, deps_select, req.name, platform(abi = plat.abi)) - if plat.abi == default_abi: - _add(deps, deps_select, req.name, platform()) - elif match_version: - _add(deps, deps_select, req.name, None) - else: - fail("BUG: {} support is not implemented".format(req.marker)) + markers = {} + for req in reqs: + for x in extras: + m = evaluate(req.marker, env = {"extra": x}, strict = False) + if m == False: + continue + elif m == True: + _add(deps, deps_select, dep) + break + else: + markers[m] = None + continue - _maybe_add_common_dep(deps, deps_select, platforms, req.name) + if markers: + _add(deps, deps_select, dep, sorted(markers)) diff --git a/python/private/pypi/pep508_env.bzl b/python/private/pypi/pep508_env.bzl index 265a8e9b99..a6efb3c50c 100644 --- a/python/private/pypi/pep508_env.bzl +++ b/python/private/pypi/pep508_env.bzl @@ -18,7 +18,7 @@ load(":pep508_platform.bzl", "platform_from_str") # See https://stackoverflow.com/a/45125525 -_platform_machine_aliases = { +platform_machine_aliases = { # These pairs mean the same hardware, but different values may be used # on different host platforms. "amd64": "x86_64", @@ -27,13 +27,67 @@ _platform_machine_aliases = { "i686": "x86_32", } +# NOTE: There are many cpus, and unfortunately, the value isn't directly +# accessible to Starlark. Using CcToolchain.cpu might work, though. +# Some targets are aliases and are omitted below as their value is implied +# by the target they resolve to. +platform_machine_select_map = { + "@platforms//cpu:aarch32": "aarch32", + "@platforms//cpu:aarch64": "aarch64", + # @platforms//cpu:arm is an alias for @platforms//cpu:aarch32 + # @platforms//cpu:arm64 is an alias for @platforms//cpu:aarch64 + "@platforms//cpu:arm64_32": "arm64_32", + "@platforms//cpu:arm64e": "arm64e", + "@platforms//cpu:armv6-m": "armv6-m", + "@platforms//cpu:armv7": "armv7", + "@platforms//cpu:armv7-m": "armv7-m", + "@platforms//cpu:armv7e-m": "armv7e-m", + "@platforms//cpu:armv7e-mf": "armv7e-mf", + "@platforms//cpu:armv7k": "armv7k", + "@platforms//cpu:armv8-m": "armv8-m", + "@platforms//cpu:cortex-r52": "cortex-r52", + "@platforms//cpu:cortex-r82": "cortex-r82", + "@platforms//cpu:i386": "i386", + "@platforms//cpu:mips64": "mips64", + "@platforms//cpu:ppc": "ppc", + "@platforms//cpu:ppc32": "ppc32", + "@platforms//cpu:ppc64le": "ppc64le", + "@platforms//cpu:riscv32": "riscv32", + "@platforms//cpu:riscv64": "riscv64", + "@platforms//cpu:s390x": "s390x", + "@platforms//cpu:wasm32": "wasm32", + "@platforms//cpu:wasm64": "wasm64", + "@platforms//cpu:x86_32": "x86_32", + "@platforms//cpu:x86_64": "x86_64", + # The value is empty string if it cannot be determined: + # https://docs.python.org/3/library/platform.html#platform.machine + "//conditions:default": "", +} + # Platform system returns results from the `uname` call. _platform_system_values = { + # See https://peps.python.org/pep-0738/#platform + "android": "Android", + "freebsd": "FreeBSD", + # See https://peps.python.org/pep-0730/#platform + # NOTE: Per Pep 730, "iPadOS" is also an acceptable value + "ios": "iOS", "linux": "Linux", + "netbsd": "NetBSD", + "openbsd": "OpenBSD", "osx": "Darwin", "windows": "Windows", } +platform_system_select_map = { + "@platforms//os:{}".format(bazel_os): py_system + for bazel_os, py_system in _platform_system_values.items() +} | { + # The value is empty string if it cannot be determined: + # https://docs.python.org/3/library/platform.html#platform.machine + "//conditions:default": "", +} + # The copy of SO [answer](https://stackoverflow.com/a/13874620) containing # all of the platforms: # ┍━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━┑ @@ -60,19 +114,55 @@ _platform_system_values = { # # We are using only the subset that we actually support. _sys_platform_values = { + # These values are decided by the sys.platform docs. + "android": "android", + "emscripten": "emscripten", + # NOTE: The below values are approximations. The sys.platform() docs + # don't have documented values for these OSes. Per docs, the + # sys.platform() value reflects the OS at the time Python was *built* + # instead of the runtime (target) OS value. + "freebsd": "freebsd", + "ios": "ios", "linux": "linux", + "openbsd": "openbsd", "osx": "darwin", + "wasi": "wasi", "windows": "win32", } + +sys_platform_select_map = { + "@platforms//os:{}".format(bazel_os): py_platform + for bazel_os, py_platform in _sys_platform_values.items() +} | { + # For lack of a better option, use empty string. No standard doc/spec + # about sys_platform value. + "//conditions:default": "", +} + +# The "java" value is documented, but with Jython defunct, +# shouldn't occur in practice. +# The os.name value is technically a property of the runtime, not the +# targetted runtime OS, but the distinction shouldn't matter if +# things are properly configured. _os_name_values = { "linux": "posix", "osx": "posix", "windows": "nt", } +os_name_select_map = { + "@platforms//os:{}".format(bazel_os): py_os + for bazel_os, py_os in _os_name_values.items() +} | { + "//conditions:default": "posix", +} + def env(target_platform, *, extra = None): """Return an env target platform + NOTE: This is for use during the loading phase. For the analysis phase, + `env_marker_setting()` constructs the env dict. + Args: target_platform: {type}`str` the target platform identifier, e.g. `cp33_linux_aarch64` @@ -81,16 +171,9 @@ def env(target_platform, *, extra = None): Returns: A dict that can be used as `env` in the marker evaluation. """ - - # TODO @aignas 2025-02-13: consider moving this into config settings. - - env = {"extra": extra} if extra != None else {} - env = env | { - "implementation_name": "cpython", - "platform_python_implementation": "CPython", - "platform_release": "", - "platform_version": "", - } + env = create_env() + if extra != None: + env["extra"] = extra if type(target_platform) == type(""): target_platform = platform_from_str(target_platform, python_version = "") @@ -111,10 +194,42 @@ def env(target_platform, *, extra = None): "platform_system": _platform_system_values.get(os, ""), "sys_platform": _sys_platform_values.get(os, ""), } + set_missing_env_defaults(env) + + return env - # This is split by topic - return env | { +def create_env(): + return { + # This is split by topic "_aliases": { - "platform_machine": _platform_machine_aliases, + "platform_machine": platform_machine_aliases, }, } + +def set_missing_env_defaults(env): + """Sets defaults based on existing values. + + Args: + env: dict; NOTE: modified in-place + """ + if "implementation_name" not in env: + # Use cpython as the default because it's likely the correct value. + env["implementation_name"] = "cpython" + if "platform_python_implementation" not in env: + # The `platform_python_implementation` marker value is supposed to come + # from `platform.python_implementation()`, however, PEP 421 introduced + # `sys.implementation.name` and the `implementation_name` env marker to + # replace it. Per the platform.python_implementation docs, there's now + # essentially just two possible "registered" values: CPython or PyPy. + # Rather than add a field to the toolchain, we just special case the value + # from `sys.implementation.name` to handle the two documented values. + platform_python_impl = env["implementation_name"] + if platform_python_impl == "cpython": + platform_python_impl = "CPython" + elif platform_python_impl == "pypy": + platform_python_impl = "PyPy" + env["platform_python_implementation"] = platform_python_impl + if "platform_release" not in env: + env["platform_release"] = "" + if "platform_version" not in env: + env["platform_version"] = "0" diff --git a/python/private/pypi/pep508_evaluate.bzl b/python/private/pypi/pep508_evaluate.bzl index f8ef553034..d4492a75bb 100644 --- a/python/private/pypi/pep508_evaluate.bzl +++ b/python/private/pypi/pep508_evaluate.bzl @@ -16,23 +16,11 @@ """ load("//python/private:enum.bzl", "enum") -load("//python/private:semver.bzl", "semver") +load("//python/private:version.bzl", "version") # The expression parsing and resolution for the PEP508 is below # -# Taken from -# https://peps.python.org/pep-0508/#grammar -# -# version_cmp = wsp* '<' | '<=' | '!=' | '==' | '>=' | '>' | '~=' | '===' -_VERSION_CMP = sorted( - [ - i.strip(" '") - for i in "'<' | '<=' | '!=' | '==' | '>=' | '>' | '~=' | '==='".split(" | ") - ], - key = lambda x: (-len(x), x), -) - _STATE = enum( STRING = "string", VAR = "var", @@ -134,7 +122,9 @@ def evaluate(marker, *, env, strict = True, **kwargs): **kwargs: Extra kwargs to be passed to the expression evaluator. Returns: - The {type}`bool` If the marker is compatible with the given env. + The {type}`bool | str` If the marker is compatible with the given env. If strict is + `False`, then the output type is `str` which will represent the remaining + expression that has not been evaluated. """ tokens = tokenize(marker) @@ -344,37 +334,43 @@ def _env_expr(left, op, right): return left in right elif op == "not in": return left not in right + elif op == "<": + return left < right + elif op == "<=": + return left <= right + elif op == ">": + return left > right + elif op == ">=": + return left >= right else: - return fail("TODO: op unsupported: '{}'".format(op)) + return fail("unsupported op: '{}' {} '{}'".format(left, op, right)) def _version_expr(left, op, right): """Evaluate a version comparison expression""" - left = semver(left) - right = semver(right) - _left = left.key() - _right = right.key() - if op == "<": - return _left < _right + _left = version.parse(left) + _right = version.parse(right) + if _left == None or _right == None: + # Per spec, if either can't be normalized to a version, then + # fallback to simple string comparison. Usually this is `platform_version` + # or `platform_release`, which vary depending on platform. + return _env_expr(left, op, right) + + if op == "===": + return version.is_eeq(_left, _right) + elif op == "!=": + return version.is_ne(_left, _right) + elif op == "==": + return version.is_eq(_left, _right) + elif op == "<": + return version.is_lt(_left, _right) elif op == ">": - return _left > _right + return version.is_gt(_left, _right) elif op == "<=": - return _left <= _right + return version.is_le(_left, _right) elif op == ">=": - return _left >= _right - elif op == "!=": - return _left != _right - elif op == "==": - # Matching of major, minor, patch only - return _left[:3] == _right[:3] + return version.is_ge(_left, _right) elif op == "~=": - right_plus = right.upper() - _right_plus = right_plus.key() - return _left >= _right and _left < _right_plus - elif op == "===": - # Strict matching - return _left == _right - elif op in _VERSION_CMP: - fail("TODO: op unsupported: '{}'".format(op)) + return version.is_compatible(_left, _right) else: return False # Let's just ignore the invalid ops diff --git a/python/private/pypi/pep508_requirement.bzl b/python/private/pypi/pep508_requirement.bzl index ee7b5dfc35..b5be17f890 100644 --- a/python/private/pypi/pep508_requirement.bzl +++ b/python/private/pypi/pep508_requirement.bzl @@ -47,9 +47,11 @@ def requirement(spec): requires, _, _ = requires.partition(char) extras = extras_unparsed.replace(" ", "").split(",") name = requires.strip(" ") + name = normalize_name(name) return struct( - name = normalize_name(name).replace("_", "-"), + name = name.replace("_", "-"), + name_ = name, marker = marker.strip(" "), extras = extras, version = version, diff --git a/python/private/pypi/pip_compile.bzl b/python/private/pypi/pip_compile.bzl index 8e46947b99..78b681b4ad 100644 --- a/python/private/pypi/pip_compile.bzl +++ b/python/private/pypi/pip_compile.bzl @@ -38,16 +38,16 @@ def pip_compile( requirements_windows = None, visibility = ["//visibility:private"], tags = None, + constraints = [], **kwargs): """Generates targets for managing pip dependencies with pip-compile. By default this rules generates a filegroup named "[name]" which can be included in the data of some other compile_pip_requirements rule that references these requirements (e.g. with `-r ../other/requirements.txt`). - It also generates two targets for running pip-compile: - - validate with `bazel test [name]_test` + - validate with `bazel test [name].test` - update with `bazel run [name].update` If you are using a version control system, the requirements.txt generated by this rule should @@ -77,6 +77,7 @@ def pip_compile( requirements_windows: File of windows specific resolve output to check validate if requirement.in has changes. tags: tagging attribute common to all build rules, passed to both the _test and .update rules. visibility: passed to both the _test and .update rules. + constraints: a list of files containing constraints to pass to pip-compile with `--constraint`. **kwargs: other bazel attributes passed to the "_test" rule. """ if len([x for x in [srcs, src, requirements_in] if x != None]) > 1: @@ -100,7 +101,7 @@ def pip_compile( visibility = visibility, ) - data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints # Use the Label constructor so this is expanded in the context of the file # where it appears, which is to say, in @rules_python @@ -110,7 +111,7 @@ def pip_compile( args = ["--src=%s" % loc.format(src) for src in srcs] + [ loc.format(requirements_txt), - "//%s:%s.update" % (native.package_name(), name), + "//%s:%s" % (native.package_name(), name), "--resolver=backtracking", "--allow-unsafe", ] @@ -122,6 +123,8 @@ def pip_compile( args.append("--requirements-darwin={}".format(loc.format(requirements_darwin))) if requirements_windows: args.append("--requirements-windows={}".format(loc.format(requirements_windows))) + for constraint in constraints: + args.append("--constraint=$(location {})".format(constraint)) args.extend(extra_args) deps = [ @@ -156,17 +159,24 @@ def pip_compile( } env = kwargs.pop("env", {}) + env_inherit = kwargs.pop("env_inherit", []) + proxy_variables = ["https_proxy", "http_proxy", "no_proxy", "HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY"] + + for var in proxy_variables: + if var not in env_inherit: + env_inherit.append(var) py_binary( name = name + ".update", env = env, + python_version = kwargs.get("python_version", None), **attrs ) timeout = kwargs.pop("timeout", "short") py_test( - name = name + "_test", + name = name + ".test", timeout = timeout, # setuptools (the default python build tool) attempts to find user # configuration in the user's home direcotory. This seems to work fine on @@ -177,6 +187,13 @@ def pip_compile( "@@platforms//os:windows": {"USERPROFILE": "Z:\\FakeSetuptoolsHomeDirectoryHack"}, "//conditions:default": {}, }) | env, + env_inherit = env_inherit, # kwargs could contain test-specific attributes like size **dict(attrs, **kwargs) ) + + native.alias( + name = "{}_test".format(name), + actual = ":{}.test".format(name), + deprecation = "Use '{}.test' instead. The '*_test' target will be removed in the next major release.".format(name), + ) diff --git a/python/private/pypi/pip_repository.bzl b/python/private/pypi/pip_repository.bzl index 01a541cf2f..724fb6ddba 100644 --- a/python/private/pypi/pip_repository.bzl +++ b/python/private/pypi/pip_repository.bzl @@ -18,7 +18,7 @@ load("@bazel_skylib//lib:sets.bzl", "sets") load("//python/private:normalize_name.bzl", "normalize_name") load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR") load("//python/private:text_util.bzl", "render") -load(":evaluate_markers.bzl", "evaluate_markers") +load(":evaluate_markers.bzl", "evaluate_markers_py", EVALUATE_MARKERS_SRCS = "SRCS") load(":parse_requirements.bzl", "host_platform", "parse_requirements", "select_requirement") load(":pip_repository_attrs.bzl", "ATTRS") load(":render_pkg_aliases.bzl", "render_pkg_aliases") @@ -82,20 +82,27 @@ def _pip_repository_impl(rctx): extra_pip_args = rctx.attr.extra_pip_args, ), extra_pip_args = rctx.attr.extra_pip_args, - evaluate_markers = evaluate_markers, + evaluate_markers = lambda rctx, requirements: evaluate_markers_py( + rctx, + requirements = requirements, + python_interpreter = rctx.attr.python_interpreter, + python_interpreter_target = rctx.attr.python_interpreter_target, + srcs = rctx.attr._evaluate_markers_srcs, + ), + extract_url_srcs = False, ) selected_requirements = {} options = None repository_platform = host_platform(rctx) - for name, requirements in requirements_by_platform.items(): - r = select_requirement( - requirements, + for whl in requirements_by_platform: + requirement = select_requirement( + whl.srcs, platform = None if rctx.attr.download_only else repository_platform, ) - if not r: + if not requirement: continue - options = options or r.extra_pip_args - selected_requirements[name] = r.srcs.requirement_line + options = options or requirement.extra_pip_args + selected_requirements[whl.name] = requirement.requirement_line bzl_packages = sorted(selected_requirements.keys()) @@ -228,6 +235,13 @@ file](https://github.com/bazel-contrib/rules_python/blob/main/examples/pip_repos _template = attr.label( default = ":requirements.bzl.tmpl.workspace", ), + _evaluate_markers_srcs = attr.label_list( + default = EVALUATE_MARKERS_SRCS, + doc = """\ +The list of labels to use as SRCS for the marker evaluation code. This ensures that the +code will be re-evaluated when any of files in the default changes. +""", + ), **ATTRS ), doc = """Accepts a locked/compiled requirements file and installs the dependencies listed within. diff --git a/python/private/pypi/pkg_aliases.bzl b/python/private/pypi/pkg_aliases.bzl index a9eee7be88..d71c37cb4b 100644 --- a/python/private/pypi/pkg_aliases.bzl +++ b/python/private/pypi/pkg_aliases.bzl @@ -237,9 +237,10 @@ def multiplatform_whl_aliases( Exposed only for unit tests. Args: - aliases: {type}`str | dict[whl_config_setting | str, str]`: The aliases + aliases: {type}`str | dict[struct | str, str]`: The aliases to process. Any aliases that have the filename set will be - converted to a dict of config settings to repo names. + converted to a dict of config settings to repo names. The + struct is created by {func}`whl_config_setting`. glibc_versions: {type}`list[tuple[int, int]]` list of versions that can be used in this hub repo. muslc_versions: {type}`list[tuple[int, int]]` list of versions that can be @@ -371,6 +372,9 @@ def get_filename_config_settings( abi = parsed.abi_tag + # TODO @aignas 2025-04-20: test + abi, _, _ = abi.partition(".") + if parsed.platform_tag == "any": prefixes = ["{}{}_any".format(py, abi)] else: diff --git a/python/private/pypi/render_pkg_aliases.bzl b/python/private/pypi/render_pkg_aliases.bzl index 863d25095c..28f32edc78 100644 --- a/python/private/pypi/render_pkg_aliases.bzl +++ b/python/private/pypi/render_pkg_aliases.bzl @@ -143,6 +143,18 @@ def render_pkg_aliases(*, aliases, requirement_cycles = None, extra_hub_aliases files["_groups/BUILD.bazel"] = generate_group_library_build_bazel("", requirement_cycles) return files +def _major_minor(python_version): + major, _, tail = python_version.partition(".") + minor, _, _ = tail.partition(".") + return "{}.{}".format(major, minor) + +def _major_minor_versions(python_versions): + if not python_versions: + return [] + + # Use a dict as a simple set + return sorted({_major_minor(v): None for v in python_versions}) + def render_multiplatform_pkg_aliases(*, aliases, **kwargs): """Render the multi-platform pkg aliases. @@ -174,7 +186,7 @@ def render_multiplatform_pkg_aliases(*, aliases, **kwargs): glibc_versions = flag_versions.get("glibc_versions", []), muslc_versions = flag_versions.get("muslc_versions", []), osx_versions = flag_versions.get("osx_versions", []), - python_versions = flag_versions.get("python_versions", []), + python_versions = _major_minor_versions(flag_versions.get("python_versions", [])), target_platforms = flag_versions.get("target_platforms", []), visibility = ["//:__subpackages__"], ) diff --git a/python/private/pypi/requirements_files_by_platform.bzl b/python/private/pypi/requirements_files_by_platform.bzl index e3aafc083f..9165c05bed 100644 --- a/python/private/pypi/requirements_files_by_platform.bzl +++ b/python/private/pypi/requirements_files_by_platform.bzl @@ -91,13 +91,12 @@ def _platforms_from_args(extra_pip_args): return list(platforms.keys()) def _platform(platform_string, python_version = None): - if not python_version or platform_string.startswith("cp3"): + if not python_version or platform_string.startswith("cp"): return platform_string - _, _, tail = python_version.partition(".") - minor, _, _ = tail.partition(".") + major, _, tail = python_version.partition(".") - return "cp3{}_{}".format(minor, platform_string) + return "cp{}{}_{}".format(major, tail, platform_string) def requirements_files_by_platform( *, diff --git a/python/private/pypi/requirements_parser/BUILD.bazel b/python/private/pypi/requirements_parser/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/private/pypi/requirements_parser/resolve_target_platforms.py b/python/private/pypi/requirements_parser/resolve_target_platforms.py new file mode 100755 index 0000000000..c899a943cc --- /dev/null +++ b/python/private/pypi/requirements_parser/resolve_target_platforms.py @@ -0,0 +1,63 @@ +"""A CLI to evaluate env markers for requirements files. + +A simple script to evaluate the `requirements.txt` files. Currently it is only +handling environment markers in the requirements files, but in the future it +may handle more things. We require a `python` interpreter that can run on the +host platform and then we depend on the [packaging] PyPI wheel. + +In order to be able to resolve requirements files for any platform, we are +re-using the same code that is used in the `whl_library` installer. See +[here](../whl_installer/wheel.py). + +Requirements for the code are: +- Depends only on `packaging` and core Python. +- Produces the same result irrespective of the Python interpreter platform or version. + +[packaging]: https://packaging.pypa.io/en/stable/ +""" + +import argparse +import json +import pathlib + +from packaging.requirements import Requirement + +from python.private.pypi.whl_installer.platform import Platform + +INPUT_HELP = """\ +Input path to read the requirements as a json file, the keys in the dictionary +are the requirements lines and the values are strings of target platforms. +""" +OUTPUT_HELP = """\ +Output to write the requirements as a json filepath, the keys in the dictionary +are the requirements lines and the values are strings of target platforms, which +got changed based on the evaluated markers. +""" + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("input_path", type=pathlib.Path, help=INPUT_HELP.strip()) + parser.add_argument("output_path", type=pathlib.Path, help=OUTPUT_HELP.strip()) + args = parser.parse_args() + + with args.input_path.open() as f: + reqs = json.load(f) + + response = {} + for requirement_line, target_platforms in reqs.items(): + entry, prefix, hashes = requirement_line.partition("--hash") + hashes = prefix + hashes + + req = Requirement(entry) + for p in target_platforms: + (platform,) = Platform.from_string(p) + if not req.marker or req.marker.evaluate(platform.env_markers("")): + response.setdefault(requirement_line, []).append(p) + + with args.output_path.open("w") as f: + json.dump(response, f) + + +if __name__ == "__main__": + main() diff --git a/python/private/pypi/simpleapi_download.bzl b/python/private/pypi/simpleapi_download.bzl index e8d7d0941a..164d4e8dbd 100644 --- a/python/private/pypi/simpleapi_download.bzl +++ b/python/private/pypi/simpleapi_download.bzl @@ -83,6 +83,7 @@ def simpleapi_download( found_on_index = {} warn_overrides = False + ctx.report_progress("Fetch package lists from PyPI index") for i, index_url in enumerate(index_urls): if i != 0: # Warn the user about a potential fix for the overrides diff --git a/python/private/pypi/whl_config_setting.bzl b/python/private/pypi/whl_config_setting.bzl index d966206372..3b81e4694f 100644 --- a/python/private/pypi/whl_config_setting.bzl +++ b/python/private/pypi/whl_config_setting.bzl @@ -21,24 +21,34 @@ def whl_config_setting(*, version = None, config_setting = None, filename = None aliases in a hub repository. Args: - version: optional(str), the version of the python toolchain that this + version: {type}`str | None`the version of the python toolchain that this whl alias is for. If not set, then non-version aware aliases will be constructed. This is mainly used for better error messages when there is no match found during a select. - config_setting: optional(Label or str), the config setting that we should use. Defaults + config_setting: {type}`str | Label | None` the config setting that we should use. Defaults to "//_config:is_python_{version}". - filename: optional(str), the distribution filename to derive the config_setting. - target_platforms: optional(list[str]), the list of target_platforms for this + filename: {type}`str | None` the distribution filename to derive the config_setting. + target_platforms: {type}`list[str] | None` the list of target_platforms for this distribution. Returns: a struct with the validated and parsed values. """ if target_platforms: - for p in target_platforms: + target_platforms_input = target_platforms + target_platforms = [] + for p in target_platforms_input: if not p.startswith("cp"): fail("target_platform should start with 'cp' denoting the python version, got: " + p) + abi, _, tail = p.partition("_") + + # drop the micro version here, currently there is no usecase to use + # multiple python interpreters with the same minor version but + # different micro version. + abi, _, _ = abi.partition(".") + target_platforms.append("{}_{}".format(abi, tail)) + return struct( config_setting = config_setting, filename = filename, diff --git a/python/private/pypi/whl_installer/BUILD.bazel b/python/private/pypi/whl_installer/BUILD.bazel index 49f1a119c1..5fb617004d 100644 --- a/python/private/pypi/whl_installer/BUILD.bazel +++ b/python/private/pypi/whl_installer/BUILD.bazel @@ -6,6 +6,7 @@ py_library( srcs = [ "arguments.py", "namespace_pkgs.py", + "platform.py", "wheel.py", "wheel_installer.py", ], diff --git a/python/private/pypi/whl_installer/arguments.py b/python/private/pypi/whl_installer/arguments.py index bb841ea9ab..57dae45ae9 100644 --- a/python/private/pypi/whl_installer/arguments.py +++ b/python/private/pypi/whl_installer/arguments.py @@ -17,6 +17,8 @@ import pathlib from typing import Any, Dict, Set +from python.private.pypi.whl_installer.platform import Platform + def parser(**kwargs: Any) -> argparse.ArgumentParser: """Create a parser for the wheel_installer tool.""" @@ -40,14 +42,20 @@ def parser(**kwargs: Any) -> argparse.ArgumentParser: help="Extra arguments to pass down to pip.", ) parser.add_argument( - "--pip_data_exclude", - action="store", - help="Additional data exclusion parameters to add to the pip packages BUILD file.", + "--platform", + action="extend", + type=Platform.from_string, + help="Platforms to target dependencies. Can be used multiple times.", ) parser.add_argument( - "--enable_implicit_namespace_pkgs", + "--enable-pipstar", action="store_true", - help="Disables conversion of implicit namespace packages into pkg-util style packages.", + help="Disable certain code paths if we expect to process the whl in Starlark.", + ) + parser.add_argument( + "--pip_data_exclude", + action="store", + help="Additional data exclusion parameters to add to the pip packages BUILD file.", ) parser.add_argument( "--environment", diff --git a/python/private/pypi/whl_installer/platform.py b/python/private/pypi/whl_installer/platform.py new file mode 100644 index 0000000000..ff267fe4aa --- /dev/null +++ b/python/private/pypi/whl_installer/platform.py @@ -0,0 +1,300 @@ +# Copyright 2024 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Utility class to inspect an extracted wheel directory""" + +import platform +import sys +from dataclasses import dataclass +from enum import Enum +from typing import Any, Dict, Iterator, List, Optional, Tuple, Union + + +class OS(Enum): + linux = 1 + osx = 2 + windows = 3 + darwin = osx + win32 = windows + + @classmethod + def interpreter(cls) -> "OS": + "Return the interpreter operating system." + return cls[sys.platform.lower()] + + def __str__(self) -> str: + return self.name.lower() + + +class Arch(Enum): + x86_64 = 1 + x86_32 = 2 + aarch64 = 3 + ppc = 4 + ppc64le = 5 + s390x = 6 + arm = 7 + amd64 = x86_64 + arm64 = aarch64 + i386 = x86_32 + i686 = x86_32 + x86 = x86_32 + + @classmethod + def interpreter(cls) -> "Arch": + "Return the currently running interpreter architecture." + # FIXME @aignas 2023-12-13: Hermetic toolchain on Windows 3.11.6 + # is returning an empty string here, so lets default to x86_64 + return cls[platform.machine().lower() or "x86_64"] + + def __str__(self) -> str: + return self.name.lower() + + +def _as_int(value: Optional[Union[OS, Arch]]) -> int: + """Convert one of the enums above to an int for easier sorting algorithms. + + Args: + value: The value of an enum or None. + + Returns: + -1 if we get None, otherwise, the numeric value of the given enum. + """ + if value is None: + return -1 + + return int(value.value) + + +def host_interpreter_version() -> Tuple[int, int]: + return (sys.version_info.minor, sys.version_info.micro) + + +@dataclass(frozen=True) +class Platform: + os: Optional[OS] = None + arch: Optional[Arch] = None + minor_version: Optional[int] = None + micro_version: Optional[int] = None + + @classmethod + def all( + cls, + want_os: Optional[OS] = None, + minor_version: Optional[int] = None, + micro_version: Optional[int] = None, + ) -> List["Platform"]: + return sorted( + [ + cls( + os=os, + arch=arch, + minor_version=minor_version, + micro_version=micro_version, + ) + for os in OS + for arch in Arch + if not want_os or want_os == os + ] + ) + + @classmethod + def host(cls) -> List["Platform"]: + """Use the Python interpreter to detect the platform. + + We extract `os` from sys.platform and `arch` from platform.machine + + Returns: + A list of parsed values which makes the signature the same as + `Platform.all` and `Platform.from_string`. + """ + minor, micro = host_interpreter_version() + return [ + Platform( + os=OS.interpreter(), + arch=Arch.interpreter(), + minor_version=minor, + micro_version=micro, + ) + ] + + def __lt__(self, other: Any) -> bool: + """Add a comparison method, so that `sorted` returns the most specialized platforms first.""" + if not isinstance(other, Platform) or other is None: + raise ValueError(f"cannot compare {other} with Platform") + + self_arch, self_os = _as_int(self.arch), _as_int(self.os) + other_arch, other_os = _as_int(other.arch), _as_int(other.os) + + if self_os == other_os: + return self_arch < other_arch + else: + return self_os < other_os + + def __str__(self) -> str: + if self.minor_version is None: + return f"{self.os}_{self.arch}" + + minor_version = self.minor_version + micro_version = self.micro_version + + if micro_version is None: + return f"cp3{minor_version}_{self.os}_{self.arch}" + else: + return f"cp3{minor_version}.{micro_version}_{self.os}_{self.arch}" + + @classmethod + def from_string(cls, platform: Union[str, List[str]]) -> List["Platform"]: + """Parse a string and return a list of platforms""" + platform = [platform] if isinstance(platform, str) else list(platform) + ret = set() + for p in platform: + if p == "host": + ret.update(cls.host()) + continue + + abi, _, tail = p.partition("_") + if not abi.startswith("cp"): + # The first item is not an abi + tail = p + abi = "" + os, _, arch = tail.partition("_") + arch = arch or "*" + + if abi: + tail = abi[len("cp3") :] + minor_version, _, micro_version = tail.partition(".") + minor_version = int(minor_version) + if micro_version == "": + micro_version = None + else: + micro_version = int(micro_version) + else: + minor_version = None + micro_version = None + + if arch != "*": + ret.add( + cls( + os=OS[os] if os != "*" else None, + arch=Arch[arch], + minor_version=minor_version, + micro_version=micro_version, + ) + ) + + else: + ret.update( + cls.all( + want_os=OS[os] if os != "*" else None, + minor_version=minor_version, + micro_version=micro_version, + ) + ) + + return sorted(ret) + + # NOTE @aignas 2023-12-05: below is the minimum number of accessors that are defined in + # https://peps.python.org/pep-0496/ to make rules_python generate dependencies. + # + # WARNING: It may not work in cases where the python implementation is different between + # different platforms. + + # derived from OS + @property + def os_name(self) -> str: + if self.os == OS.linux or self.os == OS.osx: + return "posix" + elif self.os == OS.windows: + return "nt" + else: + return "" + + @property + def sys_platform(self) -> str: + if self.os == OS.linux: + return "linux" + elif self.os == OS.osx: + return "darwin" + elif self.os == OS.windows: + return "win32" + else: + return "" + + @property + def platform_system(self) -> str: + if self.os == OS.linux: + return "Linux" + elif self.os == OS.osx: + return "Darwin" + elif self.os == OS.windows: + return "Windows" + else: + return "" + + # derived from OS and Arch + @property + def platform_machine(self) -> str: + """Guess the target 'platform_machine' marker. + + NOTE @aignas 2023-12-05: this may not work on really new systems, like + Windows if they define the platform markers in a different way. + """ + if self.arch == Arch.x86_64: + return "x86_64" + elif self.arch == Arch.x86_32 and self.os != OS.osx: + return "i386" + elif self.arch == Arch.x86_32: + return "" + elif self.arch == Arch.aarch64 and self.os == OS.linux: + return "aarch64" + elif self.arch == Arch.aarch64: + # Assuming that OSX and Windows use this one since the precedent is set here: + # https://github.com/cgohlke/win_arm64-wheels + return "arm64" + elif self.os != OS.linux: + return "" + elif self.arch == Arch.ppc: + return "ppc" + elif self.arch == Arch.ppc64le: + return "ppc64le" + elif self.arch == Arch.s390x: + return "s390x" + else: + return "" + + def env_markers(self, extra: str) -> Dict[str, str]: + # If it is None, use the host version + if self.minor_version is None: + minor, micro = host_interpreter_version() + else: + minor, micro = self.minor_version, self.micro_version + + micro = micro or 0 + + return { + "extra": extra, + "os_name": self.os_name, + "sys_platform": self.sys_platform, + "platform_machine": self.platform_machine, + "platform_system": self.platform_system, + "platform_release": "", # unset + "platform_version": "", # unset + "python_version": f"3.{minor}", + "implementation_version": f"3.{minor}.{micro}", + "python_full_version": f"3.{minor}.{micro}", + # we assume that the following are the same as the interpreter used to setup the deps: + # "implementation_name": "cpython" + # "platform_python_implementation: "CPython", + } diff --git a/python/private/pypi/whl_installer/wheel.py b/python/private/pypi/whl_installer/wheel.py index da81b5ea9f..25003e6280 100644 --- a/python/private/pypi/whl_installer/wheel.py +++ b/python/private/pypi/whl_installer/wheel.py @@ -25,6 +25,224 @@ from packaging.requirements import Requirement from pip._vendor.packaging.utils import canonicalize_name +from python.private.pypi.whl_installer.platform import ( + Platform, + host_interpreter_version, +) + + +@dataclass(frozen=True) +class FrozenDeps: + deps: List[str] + deps_select: Dict[str, List[str]] + + +class Deps: + """Deps is a dependency builder that has a build() method to return FrozenDeps.""" + + def __init__( + self, + name: str, + requires_dist: List[str], + *, + extras: Optional[Set[str]] = None, + platforms: Optional[Set[Platform]] = None, + ): + """Create a new instance and parse the requires_dist + + Args: + name (str): The name of the whl distribution + requires_dist (list[Str]): The Requires-Dist from the METADATA of the whl + distribution. + extras (set[str], optional): The list of requested extras, defaults to None. + platforms (set[Platform], optional): The list of target platforms, defaults to + None. If the list of platforms has multiple `minor_version` values, it + will change the code to generate the select statements using + `@rules_python//python/config_settings:is_python_3.y` conditions. + """ + self.name: str = Deps._normalize(name) + self._platforms: Set[Platform] = platforms or set() + self._target_versions = { + (p.minor_version, p.micro_version) for p in platforms or {} + } + if platforms and len(self._target_versions) > 1: + # TODO @aignas 2024-06-23: enable this to be set via a CLI arg + # for being more explicit. + self._default_minor_version, _ = host_interpreter_version() + else: + self._default_minor_version = None + + if None in self._target_versions and len(self._target_versions) > 2: + raise ValueError( + f"all python versions need to be specified explicitly, got: {platforms}" + ) + + # Sort so that the dictionary order in the FrozenDeps is deterministic + # without the final sort because Python retains insertion order. That way + # the sorting by platform is limited within the Platform class itself and + # the unit-tests for the Deps can be simpler. + reqs = sorted( + (Requirement(wheel_req) for wheel_req in requires_dist), + key=lambda x: f"{x.name}:{sorted(x.extras)}", + ) + + want_extras = self._resolve_extras(reqs, extras) + + # Then add all of the requirements in order + self._deps: Set[str] = set() + self._select: Dict[Platform, Set[str]] = defaultdict(set) + + reqs_by_name = {} + for req in reqs: + reqs_by_name.setdefault(req.name, []).append(req) + + for req_name, reqs in reqs_by_name.items(): + self._add_req(req_name, reqs, want_extras) + + def _add(self, dep: str, platform: Optional[Platform]): + dep = Deps._normalize(dep) + + # Self-edges are processed in _resolve_extras + if dep == self.name: + return + + if not platform: + self._deps.add(dep) + + # If the dep is in the platform-specific list, remove it from the select. + pop_keys = [] + for p, deps in self._select.items(): + if dep not in deps: + continue + + deps.remove(dep) + if not deps: + pop_keys.append(p) + + for p in pop_keys: + self._select.pop(p) + return + + if dep in self._deps: + # If the dep is already in the main dependency list, no need to add it in the + # platform-specific dependency list. + return + + # Add the platform-specific dep + self._select[platform].add(dep) + + @staticmethod + def _normalize(name: str) -> str: + return re.sub(r"[-_.]+", "_", name).lower() + + def _resolve_extras( + self, reqs: List[Requirement], want_extras: Optional[Set[str]] + ) -> Set[str]: + """Resolve extras which are due to depending on self[some_other_extra]. + + Some packages may have cyclic dependencies resulting from extras being used, one example is + `etils`, where we have one set of extras as aliases for other extras + and we have an extra called 'all' that includes all other extras. + + Example: github.com/google/etils/blob/a0b71032095db14acf6b33516bca6d885fe09e35/pyproject.toml#L32. + + When the `requirements.txt` is generated by `pip-tools`, then it is likely that + this step is not needed, but for other `requirements.txt` files this may be useful. + + NOTE @aignas 2023-12-08: the extra resolution is not platform dependent, + but in order for it to become platform dependent we would have to have + separate targets for each extra in extras. + """ + + # Resolve any extra extras due to self-edges, empty string means no + # extras The empty string in the set is just a way to make the handling + # of no extras and a single extra easier and having a set of {"", "foo"} + # is equivalent to having {"foo"}. + extras: Set[str] = want_extras or {""} + + self_reqs = [] + for req in reqs: + if Deps._normalize(req.name) != self.name: + continue + + if req.marker is None: + # I am pretty sure we cannot reach this code as it does not + # make sense to specify packages in this way, but since it is + # easy to handle, lets do it. + # + # TODO @aignas 2023-12-08: add a test + extras = extras | req.extras + else: + # process these in a separate loop + self_reqs.append(req) + + # A double loop is not strictly optimal, but always correct without recursion + for req in self_reqs: + if any(req.marker.evaluate({"extra": extra}) for extra in extras): + extras = extras | req.extras + else: + continue + + # Iterate through all packages to ensure that we include all of the extras from previously + # visited packages. + for req_ in self_reqs: + if any(req_.marker.evaluate({"extra": extra}) for extra in extras): + extras = extras | req_.extras + + return extras + + def _add_req(self, req_name, reqs: List[Requirement], extras: Set[str]) -> None: + platforms_to_add = set() + for req in reqs: + if req.marker is None: + self._add(req.name, None) + return + + if not self._platforms: + if any(req.marker.evaluate({"extra": extra}) for extra in extras): + self._add(req.name, None) + return + + for plat in self._platforms: + if plat in platforms_to_add: + # marker evaluation is more expensive than this check + continue + + added = False + for extra in extras: + if added: + break + + if req.marker.evaluate(plat.env_markers(extra)): + platforms_to_add.add(plat) + added = True + break + + if not self._platforms: + return + + if len(platforms_to_add) == len(self._platforms): + # the dep is in all target platforms, let's just add it to the regular + # list + self._add(req_name, None) + return + + for plat in platforms_to_add: + if self._default_minor_version is not None: + self._add(req_name, plat) + + if ( + self._default_minor_version is None + or plat.minor_version == self._default_minor_version + ): + self._add(req_name, Platform(os=plat.os, arch=plat.arch)) + + def build(self) -> FrozenDeps: + return FrozenDeps( + deps=sorted(self._deps), + deps_select={str(p): sorted(deps) for p, deps in self._select.items()}, + ) + class Wheel: """Representation of the compressed .whl file""" @@ -75,6 +293,18 @@ def entry_points(self) -> Dict[str, Tuple[str, str]]: return entry_points_mapping + def dependencies( + self, + extras_requested: Set[str] = None, + platforms: Optional[Set[Platform]] = None, + ) -> FrozenDeps: + return Deps( + self.name, + extras=extras_requested, + platforms=platforms, + requires_dist=self.metadata.get_all("Requires-Dist", []), + ).build() + def unzip(self, directory: str) -> None: installation_schemes = { "purelib": "/site-packages", diff --git a/python/private/pypi/whl_installer/wheel_installer.py b/python/private/pypi/whl_installer/wheel_installer.py index c7695d92e8..a6a9dd0429 100644 --- a/python/private/pypi/whl_installer/wheel_installer.py +++ b/python/private/pypi/whl_installer/wheel_installer.py @@ -23,11 +23,11 @@ import sys from pathlib import Path from tempfile import NamedTemporaryFile -from typing import Dict, Optional, Set, Tuple +from typing import Dict, List, Optional, Set, Tuple from pip._vendor.packaging.utils import canonicalize_name -from python.private.pypi.whl_installer import arguments, namespace_pkgs, wheel +from python.private.pypi.whl_installer import arguments, wheel def _configure_reproducible_wheels() -> None: @@ -77,33 +77,11 @@ def _parse_requirement_for_extra( return None, None -def _setup_namespace_pkg_compatibility(wheel_dir: str) -> None: - """Converts native namespace packages to pkgutil-style packages - - Namespace packages can be created in one of three ways. They are detailed here: - https://packaging.python.org/guides/packaging-namespace-packages/#creating-a-namespace-package - - 'pkgutil-style namespace packages' (2) and 'pkg_resources-style namespace packages' (3) works in Bazel, but - 'native namespace packages' (1) do not. - - We ensure compatibility with Bazel of method 1 by converting them into method 2. - - Args: - wheel_dir: the directory of the wheel to convert - """ - - namespace_pkg_dirs = namespace_pkgs.implicit_namespace_packages( - wheel_dir, - ignored_dirnames=["%s/bin" % wheel_dir], - ) - - for ns_pkg_dir in namespace_pkg_dirs: - namespace_pkgs.add_pkgutil_style_namespace_pkg_init(ns_pkg_dir) - - def _extract_wheel( wheel_file: str, - enable_implicit_namespace_pkgs: bool, + extras: Dict[str, Set[str]], + enable_pipstar: bool, + platforms: List[wheel.Platform], installation_dir: Path = Path("."), ) -> None: """Extracts wheel into given directory and creates py_library and filegroup targets. @@ -111,17 +89,14 @@ def _extract_wheel( Args: wheel_file: the filepath of the .whl installation_dir: the destination directory for installation of the wheel. - enable_implicit_namespace_pkgs: if true, disables conversion of implicit namespace packages and will unzip as-is + extras: a list of extras to add as dependencies for the installed wheel + enable_pipstar: if true, turns off certain operations. """ whl = wheel.Wheel(wheel_file) whl.unzip(installation_dir) - if not enable_implicit_namespace_pkgs: - _setup_namespace_pkg_compatibility(installation_dir) - metadata = { - "python_version": sys.version.partition(" ")[0], "entry_points": [ { "name": name, @@ -131,6 +106,18 @@ def _extract_wheel( for name, (module, attribute) in sorted(whl.entry_points().items()) ], } + if not enable_pipstar: + extras_requested = extras[whl.name] if whl.name in extras else set() + dependencies = whl.dependencies(extras_requested, platforms) + + metadata.update( + { + "name": whl.name, + "version": whl.version, + "deps": dependencies.deps, + "deps_by_platform": dependencies.deps_select, + } + ) with open(os.path.join(installation_dir, "metadata.json"), "w") as f: json.dump(metadata, f) @@ -146,9 +133,13 @@ def main() -> None: if args.whl_file: whl = Path(args.whl_file) + name, extras_for_pkg = _parse_requirement_for_extra(args.requirement) + extras = {name: extras_for_pkg} if extras_for_pkg and name else dict() _extract_wheel( wheel_file=whl, - enable_implicit_namespace_pkgs=args.enable_implicit_namespace_pkgs, + extras=extras, + enable_pipstar=args.enable_pipstar, + platforms=arguments.get_platforms(args), ) return diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl index 0a580011ab..c271449b3d 100644 --- a/python/private/pypi/whl_library.bzl +++ b/python/private/pypi/whl_library.bzl @@ -14,6 +14,7 @@ "" +load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth") load("//python/private:envsubst.bzl", "envsubst") load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter") @@ -21,11 +22,8 @@ load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils") load(":attrs.bzl", "ATTRS", "use_isolated") load(":deps.bzl", "all_repo_names", "record_files") load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel") -load(":parse_requirements.bzl", "host_platform") load(":parse_whl_name.bzl", "parse_whl_name") load(":patch_whl.bzl", "patch_whl") -load(":pep508_deps.bzl", "deps") -load(":pep508_requirement.bzl", "requirement") load(":pypi_repo_utils.bzl", "pypi_repo_utils") load(":whl_metadata.bzl", "whl_metadata") load(":whl_target_platforms.bzl", "whl_target_platforms") @@ -175,9 +173,6 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None): json.encode(struct(arg = rctx.attr.pip_data_exclude)), ] - if rctx.attr.enable_implicit_namespace_pkgs: - args.append("--enable_implicit_namespace_pkgs") - env = {} if rctx.attr.environment != None: for key, value in rctx.attr.environment.items(): @@ -259,19 +254,9 @@ def _whl_library_impl(rctx): # Simulate the behaviour where the whl is present in the current directory. rctx.symlink(whl_path, whl_path.basename) whl_path = rctx.path(whl_path.basename) - elif rctx.attr.urls: + elif rctx.attr.urls and rctx.attr.filename: filename = rctx.attr.filename urls = rctx.attr.urls - if not filename: - _, _, filename = urls[0].rpartition("/") - - if not (filename.endswith(".whl") or filename.endswith("tar.gz") or filename.endswith(".zip")): - if rctx.attr.filename: - msg = "got '{}'".format(filename) - else: - msg = "detected '{}' from url:\n{}".format(filename, urls[0]) - fail("Only '.whl', '.tar.gz' or '.zip' files are supported, {}".format(msg)) - result = rctx.download( url = urls, output = filename, @@ -289,7 +274,7 @@ def _whl_library_impl(rctx): fail("could not download the '{}' from {}:\n{}".format(filename, urls, result)) if filename.endswith(".whl"): - whl_path = rctx.path(rctx.attr.filename) + whl_path = rctx.path(filename) else: # It is an sdist and we need to tell PyPI to use a file in this directory # and, allow getting build dependencies from PYTHONPATH, which we @@ -344,120 +329,148 @@ def _whl_library_impl(rctx): timeout = rctx.attr.timeout, ) - target_platforms = rctx.attr.experimental_target_platforms - if target_platforms: - parsed_whl = parse_whl_name(whl_path.basename) - if parsed_whl.platform_tag != "any": - # NOTE @aignas 2023-12-04: if the wheel is a platform specific - # wheel, we only include deps for that target platform - target_platforms = [ - p.target_platform - for p in whl_target_platforms( - platform_tag = parsed_whl.platform_tag, - abi_tag = parsed_whl.abi_tag.strip("tm"), - ) - ] - - pypi_repo_utils.execute_checked( - rctx, - op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path), - python = python_interpreter, - arguments = args + [ - "--whl-file", - whl_path, - ], - srcs = rctx.attr._python_srcs, - environment = environment, - quiet = rctx.attr.quiet, - timeout = rctx.attr.timeout, - logger = logger, - ) + if rp_config.enable_pipstar: + pypi_repo_utils.execute_checked( + rctx, + op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path), + python = python_interpreter, + arguments = args + [ + "--whl-file", + whl_path, + "--enable-pipstar", + ], + srcs = rctx.attr._python_srcs, + environment = environment, + quiet = rctx.attr.quiet, + timeout = rctx.attr.timeout, + logger = logger, + ) - metadata = json.decode(rctx.read("metadata.json")) - rctx.delete("metadata.json") + metadata = json.decode(rctx.read("metadata.json")) + rctx.delete("metadata.json") - # NOTE @aignas 2024-06-22: this has to live on until we stop supporting - # passing `twine` as a `:pkg` library via the `WORKSPACE` builds. - # - # See ../../packaging.bzl line 190 - entry_points = {} - for item in metadata["entry_points"]: - name = item["name"] - module = item["module"] - attribute = item["attribute"] - - # There is an extreme edge-case with entry_points that end with `.py` - # See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174 - entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name - entry_point_target_name = ( - _WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py + # NOTE @aignas 2024-06-22: this has to live on until we stop supporting + # passing `twine` as a `:pkg` library via the `WORKSPACE` builds. + # + # See ../../packaging.bzl line 190 + entry_points = {} + for item in metadata["entry_points"]: + name = item["name"] + module = item["module"] + attribute = item["attribute"] + + # There is an extreme edge-case with entry_points that end with `.py` + # See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174 + entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name + entry_point_target_name = ( + _WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py + ) + entry_point_script_name = entry_point_target_name + ".py" + + rctx.file( + entry_point_script_name, + _generate_entry_point_contents(module, attribute), + ) + entry_points[entry_point_without_py] = entry_point_script_name + + metadata = whl_metadata( + install_dir = whl_path.dirname.get_child("site-packages"), + read_fn = rctx.read, + logger = logger, + ) + + build_file_contents = generate_whl_library_build_bazel( + name = whl_path.basename, + dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix), + entry_points = entry_points, + metadata_name = metadata.name, + metadata_version = metadata.version, + requires_dist = metadata.requires_dist, + # TODO @aignas 2025-05-17: maybe have a build flag for this instead + enable_implicit_namespace_pkgs = rctx.attr.enable_implicit_namespace_pkgs, + # TODO @aignas 2025-04-14: load through the hub: + annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))), + data_exclude = rctx.attr.pip_data_exclude, + group_deps = rctx.attr.group_deps, + group_name = rctx.attr.group_name, ) - entry_point_script_name = entry_point_target_name + ".py" + else: + target_platforms = rctx.attr.experimental_target_platforms or [] + if target_platforms: + parsed_whl = parse_whl_name(whl_path.basename) + + # NOTE @aignas 2023-12-04: if the wheel is a platform specific wheel, we + # only include deps for that target platform + if parsed_whl.platform_tag != "any": + target_platforms = [ + p.target_platform + for p in whl_target_platforms( + platform_tag = parsed_whl.platform_tag, + abi_tag = parsed_whl.abi_tag.strip("tm"), + ) + ] - rctx.file( - entry_point_script_name, - _generate_entry_point_contents(module, attribute), + pypi_repo_utils.execute_checked( + rctx, + op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path), + python = python_interpreter, + arguments = args + [ + "--whl-file", + whl_path, + ] + ["--platform={}".format(p) for p in target_platforms], + srcs = rctx.attr._python_srcs, + environment = environment, + quiet = rctx.attr.quiet, + timeout = rctx.attr.timeout, + logger = logger, ) - entry_points[entry_point_without_py] = entry_point_script_name - # TODO @aignas 2025-04-04: move this to whl_library_targets.bzl to have - # this in the analysis phase. - # - # This means that whl_library_targets will have to accept the following args: - # * name - the name of the package in the METADATA. - # * requires_dist - the list of METADATA Requires-Dist. - # * platforms - the list of target platforms. The target_platforms - # should come from the hub repo via a 'load' statement so that they don't - # need to be passed as an argument to `whl_library`. - # * extras - the list of required extras. This comes from the - # `rctx.attr.requirement` for now. In the future the required extras could - # stay in the hub repo, where we calculate the extra aliases that we need - # to create automatically and this way expose the targets for the specific - # extras. The first step will be to generate a target per extra for the - # `py_library` and `filegroup`. Maybe we need to have a special provider - # or an output group so that we can return the `whl` file from the - # `py_library` target? filegroup can use output groups to expose files. - # * host_python_version/versons - the list of python versions to support - # should come from the hub, similar to how the target platforms are specified. - # - # Extra things that we should move at the same time: - # * group_name, group_deps - this info can stay in the hub repository so that - # it is piped at the analysis time and changing the requirement groups does - # cause to re-fetch the deps. - python_version = metadata["python_version"] - metadata = whl_metadata( - install_dir = rctx.path("site-packages"), - read_fn = rctx.read, - logger = logger, - ) + metadata = json.decode(rctx.read("metadata.json")) + rctx.delete("metadata.json") - # TODO @aignas 2025-04-09: this will later be removed when loaded through the hub - major_minor, _, _ = python_version.rpartition(".") - package_deps = deps( - name = metadata.name, - requires_dist = metadata.requires_dist, - platforms = target_platforms or [ - "cp{}_{}".format(major_minor.replace(".", ""), host_platform(rctx)), - ], - extras = requirement(rctx.attr.requirement).extras, - host_python_version = python_version, - ) + # NOTE @aignas 2024-06-22: this has to live on until we stop supporting + # passing `twine` as a `:pkg` library via the `WORKSPACE` builds. + # + # See ../../packaging.bzl line 190 + entry_points = {} + for item in metadata["entry_points"]: + name = item["name"] + module = item["module"] + attribute = item["attribute"] + + # There is an extreme edge-case with entry_points that end with `.py` + # See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174 + entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name + entry_point_target_name = ( + _WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py + ) + entry_point_script_name = entry_point_target_name + ".py" + + rctx.file( + entry_point_script_name, + _generate_entry_point_contents(module, attribute), + ) + entry_points[entry_point_without_py] = entry_point_script_name + + build_file_contents = generate_whl_library_build_bazel( + name = whl_path.basename, + dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix), + entry_points = entry_points, + # TODO @aignas 2025-05-17: maybe have a build flag for this instead + enable_implicit_namespace_pkgs = rctx.attr.enable_implicit_namespace_pkgs, + # TODO @aignas 2025-04-14: load through the hub: + dependencies = metadata["deps"], + dependencies_by_platform = metadata["deps_by_platform"], + annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))), + data_exclude = rctx.attr.pip_data_exclude, + group_deps = rctx.attr.group_deps, + group_name = rctx.attr.group_name, + tags = [ + "pypi_name={}".format(metadata["name"]), + "pypi_version={}".format(metadata["version"]), + ], + ) - build_file_contents = generate_whl_library_build_bazel( - name = whl_path.basename, - dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix), - dependencies = package_deps.deps, - dependencies_by_platform = package_deps.deps_select, - group_name = rctx.attr.group_name, - group_deps = rctx.attr.group_deps, - data_exclude = rctx.attr.pip_data_exclude, - tags = [ - "pypi_name=" + metadata.name, - "pypi_version=" + metadata.version, - ], - entry_points = entry_points, - annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))), - ) rctx.file("BUILD.bazel", build_file_contents) return @@ -517,10 +530,7 @@ and the target that we need respectively. doc = "Name of the group, if any.", ), "repo": attr.string( - doc = """\ -Pointer to parent repo name. Used to make these rules rerun if the parent repo changes. -Only used in WORKSPACE when the {attr}`dep_template` is not set. -""", + doc = "Pointer to parent repo name. Used to make these rules rerun if the parent repo changes.", ), "repo_prefix": attr.string( doc = """ @@ -571,7 +581,6 @@ attr makes `extra_pip_args` and `download_only` ignored.""", Label("//python/private/pypi/whl_installer:wheel.py"), Label("//python/private/pypi/whl_installer:wheel_installer.py"), Label("//python/private/pypi/whl_installer:arguments.py"), - Label("//python/private/pypi/whl_installer:namespace_pkgs.py"), ] + record_files.values(), ), "_rule_name": attr.string(default = "whl_library"), diff --git a/python/private/pypi/whl_library_targets.bzl b/python/private/pypi/whl_library_targets.bzl index d32746b604..3529566c49 100644 --- a/python/private/pypi/whl_library_targets.bzl +++ b/python/private/pypi/whl_library_targets.bzl @@ -19,6 +19,7 @@ load("//python:py_binary.bzl", "py_binary") load("//python:py_library.bzl", "py_library") load("//python/private:glob_excludes.bzl", "glob_excludes") load("//python/private:normalize_name.bzl", "normalize_name") +load(":env_marker_setting.bzl", "env_marker_setting") load( ":labels.bzl", "DATA_LABEL", @@ -29,6 +30,68 @@ load( "WHEEL_FILE_IMPL_LABEL", "WHEEL_FILE_PUBLIC_LABEL", ) +load(":namespace_pkgs.bzl", "create_inits") +load(":pep508_deps.bzl", "deps") + +def whl_library_targets_from_requires( + *, + name, + metadata_name = "", + metadata_version = "", + requires_dist = [], + extras = [], + include = [], + group_deps = [], + **kwargs): + """The macro to create whl targets from the METADATA. + + Args: + name: {type}`str` The wheel filename + metadata_name: {type}`str` The package name as written in wheel `METADATA`. + metadata_version: {type}`str` The package version as written in wheel `METADATA`. + group_deps: {type}`list[str]` names of fellow members of the group (if + any). These will be excluded from generated deps lists so as to avoid + direct cycles. These dependencies will be provided at runtime by the + group rules which wrap this library and its fellows together. + requires_dist: {type}`list[str]` The list of `Requires-Dist` values from + the whl `METADATA`. + extras: {type}`list[str]` The list of requested extras. This essentially includes extra transitive dependencies in the final targets depending on the wheel `METADATA`. + include: {type}`list[str]` The list of packages to include. + **kwargs: Extra args passed to the {obj}`whl_library_targets` + """ + package_deps = _parse_requires_dist( + name = metadata_name, + requires_dist = requires_dist, + excludes = group_deps, + extras = extras, + include = include, + ) + + whl_library_targets( + name = name, + dependencies = package_deps.deps, + dependencies_with_markers = package_deps.deps_select, + tags = [ + "pypi_name={}".format(metadata_name), + "pypi_version={}".format(metadata_version), + ], + **kwargs + ) + +def _parse_requires_dist( + *, + name, + requires_dist, + excludes, + include, + extras): + return deps( + name = normalize_name(name), + requires_dist = requires_dist, + excludes = excludes, + include = include, + extras = extras, + ) def whl_library_targets( *, @@ -43,6 +106,7 @@ def whl_library_targets( }, dependencies = [], dependencies_by_platform = {}, + dependencies_with_markers = {}, group_deps = [], group_name = "", data = [], @@ -50,10 +114,12 @@ def whl_library_targets( copy_executables = {}, entry_points = {}, native = native, + enable_implicit_namespace_pkgs = False, rules = struct( copy_file = copy_file, py_binary = py_binary, py_library = py_library, + env_marker_setting = env_marker_setting, )): """Create all of the whl_library targets. @@ -66,6 +132,8 @@ def whl_library_targets( dependencies: {type}`list[str]` A list of dependencies. dependencies_by_platform: {type}`dict[str, list[str]]` A list of dependencies by platform key. + dependencies_with_markers: {type}`dict[str, str]` A marker to evaluate + in order for the dep to be included. filegroups: {type}`dict[str, list[str]]` A dictionary of the target names and the glob matches. group_name: {type}`str` name of the dependency group (if any) which @@ -87,6 +155,8 @@ def whl_library_targets( data: {type}`list[str]` A list of labels to include as part of the `data` attribute in `py_library`. entry_points: {type}`dict[str, str]` The mapping between the script name and the python file to use. DEPRECATED. + enable_implicit_namespace_pkgs: {type}`boolean` generate __init__.py + files for namespace pkgs. native: {type}`native` The native struct for overriding in tests. rules: {type}`struct` A struct with references to rules for creating targets. """ @@ -124,10 +194,16 @@ def whl_library_targets( data.append(dest) _config_settings( - dependencies_by_platform.keys(), + dependencies_by_platform = dependencies_by_platform.keys(), + dependencies_with_markers = dependencies_with_markers, native = native, + rules = rules, visibility = ["//visibility:private"], ) + deps_conditional = { + d: "is_include_{}_true".format(d) + for d in dependencies_with_markers + } # TODO @aignas 2024-10-25: remove the entry_point generation once # `py_console_script_binary` is the only way to use entry points. @@ -207,6 +283,7 @@ def whl_library_targets( data = _deps( deps = dependencies, deps_by_platform = dependencies_by_platform, + deps_conditional = deps_conditional, tmpl = dep_template.format(name = "{}", target = WHEEL_FILE_PUBLIC_LABEL), # NOTE @aignas 2024-10-28: Actually, `select` is not part of # `native`, but in order to support bazel 6.4 in unit tests, I @@ -220,6 +297,14 @@ def whl_library_targets( ) if hasattr(rules, "py_library"): + srcs = native.glob( + ["site-packages/**/*.py"], + exclude = srcs_exclude, + # Empty sources are allowed to support wheels that don't have any + # pure-Python code, e.g. pymssql, which is written in Cython. + allow_empty = True, + ) + # NOTE: pyi files should probably be excluded because they're carried # by the pyi_srcs attribute. However, historical behavior included # them in data and some tools currently rely on that. @@ -236,29 +321,38 @@ def whl_library_targets( if item not in _data_exclude: _data_exclude.append(item) + data = data + native.glob( + ["site-packages/**/*"], + exclude = _data_exclude, + ) + + pyi_srcs = native.glob( + ["site-packages/**/*.pyi"], + allow_empty = True, + ) + + if enable_implicit_namespace_pkgs: + srcs = srcs + getattr(native, "select", select)({ + Label("//python/config_settings:is_venvs_site_packages"): [], + "//conditions:default": create_inits( + srcs = srcs + data + pyi_srcs, + ignore_dirnames = [], # If you need to ignore certain folders, you can patch rules_python here to do so. + root = "site-packages", + ), + }) + rules.py_library( name = py_library_label, - srcs = native.glob( - ["site-packages/**/*.py"], - exclude = srcs_exclude, - # Empty sources are allowed to support wheels that don't have any - # pure-Python code, e.g. pymssql, which is written in Cython. - allow_empty = True, - ), - pyi_srcs = native.glob( - ["site-packages/**/*.pyi"], - allow_empty = True, - ), - data = data + native.glob( - ["site-packages/**/*"], - exclude = _data_exclude, - ), + srcs = srcs, + pyi_srcs = pyi_srcs, + data = data, # This makes this directory a top-level in the python import # search path for anything that depends on this. imports = ["site-packages"], deps = _deps( deps = dependencies, deps_by_platform = dependencies_by_platform, + deps_conditional = deps_conditional, tmpl = dep_template.format(name = "{}", target = PY_LIBRARY_PUBLIC_LABEL), select = getattr(native, "select", select), ), @@ -267,7 +361,7 @@ def whl_library_targets( experimental_venvs_site_packages = Label("@rules_python//python/config_settings:venvs_site_packages"), ) -def _config_settings(dependencies_by_platform, native = native, **kwargs): +def _config_settings(dependencies_by_platform, dependencies_with_markers, rules, native = native, **kwargs): """Generate config settings for the targets. Args: @@ -279,33 +373,39 @@ def _config_settings(dependencies_by_platform, native = native, **kwargs): * `@//python/config_settings:is_python_3.{minor_version}` * `{os}_{cpu}` * `cp3{minor_version}_{os}_{cpu}` + dependencies_with_markers: {type}`dict[str, str]` The markers to evaluate by + each dep. + rules: used for testing native: {type}`native` The native struct for overriding in tests. **kwargs: Extra kwargs to pass to the rule. """ + for dep, expression in dependencies_with_markers.items(): + rules.env_marker_setting( + name = "include_{}".format(dep), + expression = expression, + **kwargs + ) + for p in dependencies_by_platform: if p.startswith("@") or p.endswith("default"): continue + # TODO @aignas 2025-04-20: add tests here abi, _, tail = p.partition("_") if not abi.startswith("cp"): tail = p abi = "" - os, _, arch = tail.partition("_") - os = "" if os == "anyos" else os - arch = "" if arch == "anyarch" else arch _kwargs = dict(kwargs) - if arch: - _kwargs.setdefault("constraint_values", []).append("@platforms//cpu:{}".format(arch)) - if os: - _kwargs.setdefault("constraint_values", []).append("@platforms//os:{}".format(os)) + _kwargs["constraint_values"] = [ + "@platforms//cpu:{}".format(arch), + "@platforms//os:{}".format(os), + ] if abi: _kwargs["flag_values"] = { - "@rules_python//python/config_settings:python_version_major_minor": "3.{minor_version}".format( - minor_version = abi[len("cp3"):], - ), + Label("//python/config_settings:python_version"): "3.{}".format(abi[len("cp3"):]), } native.config_setting( @@ -325,9 +425,15 @@ def _plat_label(plat): else: return ":is_" + plat.replace("cp3", "python_3.") -def _deps(deps, deps_by_platform, tmpl, select = select): +def _deps(deps, deps_by_platform, deps_conditional, tmpl, select = select): deps = [tmpl.format(d) for d in sorted(deps)] + for dep, setting in deps_conditional.items(): + deps = deps + select({ + ":{}".format(setting): [tmpl.format(dep)], + "//conditions:default": [], + }) + if not deps_by_platform: return deps diff --git a/python/private/pypi/whl_metadata.bzl b/python/private/pypi/whl_metadata.bzl index 8a86ffbff1..cf2d51afda 100644 --- a/python/private/pypi/whl_metadata.bzl +++ b/python/private/pypi/whl_metadata.bzl @@ -52,7 +52,7 @@ def parse_whl_metadata(contents): "version": "", } for line in contents.strip().split("\n"): - if not line.strip(): + if not line: # Stop parsing on first empty line, which marks the end of the # headers containing the metadata. break diff --git a/python/private/pypi/whl_repo_name.bzl b/python/private/pypi/whl_repo_name.bzl index 02a7c8142c..2b3b5418aa 100644 --- a/python/private/pypi/whl_repo_name.bzl +++ b/python/private/pypi/whl_repo_name.bzl @@ -44,7 +44,7 @@ def whl_repo_name(filename, sha256): else: parsed = parse_whl_name(filename) name = normalize_name(parsed.distribution) - version = parsed.version.replace(".", "_").replace("!", "_") + version = parsed.version.replace(".", "_").replace("!", "_").replace("+", "_").replace("%", "_") python_tag, _, _ = parsed.python_tag.partition(".") abi_tag, _, _ = parsed.abi_tag.partition(".") platform_tag, _, _ = parsed.platform_tag.partition(".") diff --git a/python/private/pypi/whl_target_platforms.bzl b/python/private/pypi/whl_target_platforms.bzl index 9f47e625b3..6ea3f120c3 100644 --- a/python/private/pypi/whl_target_platforms.bzl +++ b/python/private/pypi/whl_target_platforms.bzl @@ -75,8 +75,11 @@ def select_whls(*, whls, want_platforms = [], logger = None): fail("expected all platforms to start with ABI, but got: {}".format(p)) abi, _, os_cpu = p.partition("_") + abi, _, _ = abi.partition(".") _want_platforms[os_cpu] = None - _want_platforms[p] = None + + # TODO @aignas 2025-04-20: add a test + _want_platforms["{}_{}".format(abi, os_cpu)] = None version_limit_candidate = int(abi[3:]) if not version_limit: diff --git a/python/private/python.bzl b/python/private/python.bzl index f49fb26d52..8e23668879 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -18,34 +18,44 @@ load("@bazel_features//:features.bzl", "bazel_features") load("//python:versions.bzl", "DEFAULT_RELEASE_BASE_URL", "PLATFORMS", "TOOL_VERSIONS") load(":auth.bzl", "AUTH_ATTRS") load(":full_version.bzl", "full_version") +load(":platform_info.bzl", "platform_info") load(":python_register_toolchains.bzl", "python_register_toolchains") load(":pythons_hub.bzl", "hub_repo") load(":repo_utils.bzl", "repo_utils") -load(":semver.bzl", "semver") -load(":text_util.bzl", "render") -load(":toolchains_repo.bzl", "multi_toolchain_aliases") +load( + ":toolchains_repo.bzl", + "host_compatible_python_repo", + "multi_toolchain_aliases", + "sorted_host_platform_names", + "sorted_host_platforms", +) load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER") +load(":version.bzl", "version") -# This limit can be increased essentially arbitrarily, but doing so will cause a rebuild of all -# targets using any of these toolchains due to the changed repository name. -_MAX_NUM_TOOLCHAINS = 9999 -_TOOLCHAIN_INDEX_PAD_LENGTH = len(str(_MAX_NUM_TOOLCHAINS)) - -def parse_modules(*, module_ctx, _fail = fail): +def parse_modules(*, module_ctx, logger, _fail = fail): """Parse the modules and return a struct for registrations. Args: module_ctx: {type}`module_ctx` module context. + logger: {type}`repo_utils.logger` A logger to use. _fail: {type}`function` the failure function, mainly for testing. Returns: A struct with the following attributes: - * `toolchains`: The list of toolchains to register. The last - element is special and is treated as the default toolchain. - * `defaults`: The default `kwargs` passed to - {bzl:obj}`python_register_toolchains`. - * `debug_info`: {type}`None | dict` extra information to be passed - to the debug repo. + * `toolchains`: {type}`list[ToolchainConfig]` The list of toolchains to + register. The last element is special and is treated as the default + toolchain. + * `config`: Various toolchain config, see `_get_toolchain_config`. + * `debug_info`: {type}`None | dict` extra information to be passed + to the debug repo. + * `platforms`: {type}`dict[str, platform_info]` of the base set of + platforms toolchains should be created for, if possible. + + ToolchainConfig struct: + * python_version: str, full python version string + * name: str, the base toolchain name, e.g., "python_3_10", no + platform suffix. + * register_coverage_tool: bool """ if module_ctx.os.environ.get("RULES_PYTHON_BZLMOD_DEBUG", "0") == "1": debug_info = { @@ -69,8 +79,6 @@ def parse_modules(*, module_ctx, _fail = fail): ignore_root_user_error = None - logger = repo_utils.logger(module_ctx, "python") - # if the root module does not register any toolchain then the # ignore_root_user_error takes its default value: True if not module_ctx.modules[0].tags.toolchain: @@ -228,7 +236,7 @@ def parse_modules(*, module_ctx, _fail = fail): # A default toolchain is required so that the non-version-specific rules # are able to match a toolchain. if default_toolchain == None: - fail("No default Python toolchain configured. Is rules_python missing `is_default=True`?") + fail("No default Python toolchain configured. Is rules_python missing `python.defaults()`?") elif default_toolchain.python_version not in global_toolchain_versions: fail('Default version "{python_version}" selected by module ' + '"{module_name}", but no toolchain with that version registered'.format( @@ -240,9 +248,6 @@ def parse_modules(*, module_ctx, _fail = fail): # toolchain. We need the default last. toolchains.append(default_toolchain) - if len(toolchains) > _MAX_NUM_TOOLCHAINS: - fail("more than {} python versions are not supported".format(_MAX_NUM_TOOLCHAINS)) - # sort the toolchains so that the toolchain versions that are in the # `minor_mapping` are coming first. This ensures that `python_version = # "3.X"` transitions work as expected. @@ -273,10 +278,38 @@ def parse_modules(*, module_ctx, _fail = fail): ) def _python_impl(module_ctx): - py = parse_modules(module_ctx = module_ctx) + logger = repo_utils.logger(module_ctx, "python") + py = parse_modules(module_ctx = module_ctx, logger = logger) + + # Host compatible runtime repos + # dict[str version, struct] where struct has: + # * full_python_version: str + # * platform: platform_info struct + # * platform_name: str platform name + # * impl_repo_name: str repo name of the runtime's python_repository() repo + all_host_compatible_impls = {} + + # Host compatible repos that still need to be created because, when + # creating the actual runtime repo, there wasn't a host-compatible + # variant defined for it. + # dict[str reponame, struct] where struct has: + # * compatible_version: str, e.g. 3.10 or 3.10.1. The version the host + # repo should be compatible with + # * full_python_version: str, e.g. 3.10.1, the full python version of + # the toolchain that still needs a host repo created. + needed_host_repos = {} + + # list of structs; see inline struct call within the loop below. + toolchain_impls = [] + + # list[str] of the repo names for host compatible repos + all_host_compatible_repo_names = [] + + # Create the underlying python_repository repos that contain the + # python runtimes and their toolchain implementation definitions. + for i, toolchain_info in enumerate(py.toolchains): + is_last = (i + 1) == len(py.toolchains) - loaded_platforms = {} - for toolchain_info in py.toolchains: # Ensure that we pass the full version here. full_python_version = full_version( version = toolchain_info.python_version, @@ -291,36 +324,172 @@ def _python_impl(module_ctx): kwargs.update(py.config.kwargs.get(toolchain_info.python_version, {})) kwargs.update(py.config.kwargs.get(full_python_version, {})) kwargs.update(py.config.default) - loaded_platforms[full_python_version] = python_register_toolchains( + register_result = python_register_toolchains( name = toolchain_info.name, _internal_bzlmod_toolchain_call = True, **kwargs ) + if not register_result.impl_repos: + continue + + host_platforms = {} + for repo_name, (platform_name, platform_info) in register_result.impl_repos.items(): + toolchain_impls.append(struct( + # str: The base name to use for the toolchain() target + name = repo_name, + # str: The repo name the toolchain() target points to. + impl_repo_name = repo_name, + # str: platform key in the passed-in platforms dict + platform_name = platform_name, + # struct: platform_info() struct + platform = platform_info, + # str: Major.Minor.Micro python version + full_python_version = full_python_version, + # bool: whether to implicitly add the python version constraint + # to the toolchain's target_settings. + # The last toolchain is the default; it can't have version constraints + set_python_version_constraint = is_last, + )) + if _is_compatible_with_host(module_ctx, platform_info): + host_compat_entry = struct( + full_python_version = full_python_version, + platform = platform_info, + platform_name = platform_name, + impl_repo_name = repo_name, + ) + host_platforms[platform_name] = host_compat_entry + all_host_compatible_impls.setdefault(full_python_version, []).append( + host_compat_entry, + ) + parsed_version = version.parse(full_python_version) + all_host_compatible_impls.setdefault( + "{}.{}".format(*parsed_version.release[0:2]), + [], + ).append(host_compat_entry) + + host_repo_name = toolchain_info.name + "_host" + if host_platforms: + all_host_compatible_repo_names.append(host_repo_name) + host_platforms = sorted_host_platforms(host_platforms) + entries = host_platforms.values() + host_compatible_python_repo( + name = host_repo_name, + base_name = host_repo_name, + # NOTE: Order matters. The first found to be compatible is + # (usually) used. + platforms = host_platforms.keys(), + os_names = {str(i): e.platform.os_name for i, e in enumerate(entries)}, + arch_names = {str(i): e.platform.arch for i, e in enumerate(entries)}, + python_versions = {str(i): e.full_python_version for i, e in enumerate(entries)}, + impl_repo_names = {str(i): e.impl_repo_name for i, e in enumerate(entries)}, + ) + else: + needed_host_repos[host_repo_name] = struct( + compatible_version = toolchain_info.python_version, + full_python_version = full_python_version, + ) + + if needed_host_repos: + for key, entries in all_host_compatible_impls.items(): + all_host_compatible_impls[key] = sorted( + entries, + reverse = True, + key = lambda e: version.key(version.parse(e.full_python_version)), + ) + + for host_repo_name, info in needed_host_repos.items(): + choices = [] + if info.compatible_version not in all_host_compatible_impls: + logger.warn("No host compatible runtime found compatible with version {}".format(info.compatible_version)) + continue + + choices = all_host_compatible_impls[info.compatible_version] + platform_keys = [ + # We have to prepend the offset because the same platform + # name might occur across different versions + "{}_{}".format(i, entry.platform_name) + for i, entry in enumerate(choices) + ] + platform_keys = sorted_host_platform_names(platform_keys) + + all_host_compatible_repo_names.append(host_repo_name) + host_compatible_python_repo( + name = host_repo_name, + base_name = host_repo_name, + platforms = platform_keys, + impl_repo_names = { + str(i): entry.impl_repo_name + for i, entry in enumerate(choices) + }, + os_names = {str(i): entry.platform.os_name for i, entry in enumerate(choices)}, + arch_names = {str(i): entry.platform.arch for i, entry in enumerate(choices)}, + python_versions = {str(i): entry.full_python_version for i, entry in enumerate(choices)}, + ) + + # list[str] The infix to use for the resulting toolchain() `name` arg. + toolchain_names = [] + + # dict[str i, str repo]; where repo is the full repo name + # ("python_3_10_unknown-linux-x86_64") for the toolchain + # i corresponds to index `i` in toolchain_names + toolchain_repo_names = {} + + # dict[str i, list[str] constraints]; where constraints is a list + # of labels for target_compatible_with + # i corresponds to index `i` in toolchain_names + toolchain_tcw_map = {} + + # dict[str i, list[str] settings]; where settings is a list + # of labels for target_settings + # i corresponds to index `i` in toolchain_names + toolchain_ts_map = {} + + # dict[str i, str set_constraint]; where set_constraint is the string + # "True" or "False". + # i corresponds to index `i` in toolchain_names + toolchain_set_python_version_constraints = {} + + # dict[str i, str python_version]; where python_version is the full + # python version ("3.4.5"). + toolchain_python_versions = {} + + # dict[str i, str platform_key]; where platform_key is the key within + # the PLATFORMS global for this toolchain + toolchain_platform_keys = {} + + # Split the toolchain info into separate objects so they can be passed onto + # the repository rule. + for entry in toolchain_impls: + key = str(len(toolchain_names)) + + toolchain_names.append(entry.name) + toolchain_repo_names[key] = entry.impl_repo_name + toolchain_tcw_map[key] = entry.platform.compatible_with + + # The target_settings attribute may not be present for users + # patching python/versions.bzl. + toolchain_ts_map[key] = getattr(entry.platform, "target_settings", []) + toolchain_platform_keys[key] = entry.platform_name + toolchain_python_versions[key] = entry.full_python_version + + # Repo rules can't accept dict[str, bool], so encode them as a string value. + toolchain_set_python_version_constraints[key] = ( + "True" if entry.set_python_version_constraint else "False" + ) - # Create the pythons_hub repo for the interpreter meta data and the - # the various toolchains. hub_repo( name = "pythons_hub", - # Last toolchain is default + toolchain_names = toolchain_names, + toolchain_repo_names = toolchain_repo_names, + toolchain_target_compatible_with_map = toolchain_tcw_map, + toolchain_target_settings_map = toolchain_ts_map, + toolchain_platform_keys = toolchain_platform_keys, + toolchain_python_versions = toolchain_python_versions, + toolchain_set_python_version_constraints = toolchain_set_python_version_constraints, + host_compatible_repo_names = sorted(all_host_compatible_repo_names), default_python_version = py.default_python_version, minor_mapping = py.config.minor_mapping, python_versions = list(py.config.default["tool_versions"].keys()), - toolchain_prefixes = [ - render.toolchain_prefix(index, toolchain.name, _TOOLCHAIN_INDEX_PAD_LENGTH) - for index, toolchain in enumerate(py.toolchains) - ], - toolchain_python_versions = [ - full_version(version = t.python_version, minor_mapping = py.config.minor_mapping) - for t in py.toolchains - ], - # The last toolchain is the default; it can't have version constraints - # Despite the implication of the arg name, the values are strs, not bools - toolchain_set_python_version_constraints = [ - "True" if i != len(py.toolchains) - 1 else "False" - for i in range(len(py.toolchains)) - ], - toolchain_user_repository_names = [t.name for t in py.toolchains], - loaded_platforms = loaded_platforms, ) # This is require in order to support multiple version py_test @@ -344,6 +513,11 @@ def _python_impl(module_ctx): else: return None +def _is_compatible_with_host(mctx, platform_info): + os_name = repo_utils.get_platforms_os_name(mctx) + cpu_name = repo_utils.get_platforms_cpu_name(mctx) + return platform_info.os_name == os_name and platform_info.arch == cpu_name + def _one_or_the_same(first, second, *, onerror = None): if not first: return second @@ -412,16 +586,20 @@ def _fail_multiple_default_toolchains(first, second): second = second, )) -def _validate_version(*, version, _fail = fail): - parsed = semver(version) - if parsed.patch == None or parsed.build or parsed.pre_release: - _fail("The 'python_version' attribute needs to specify an 'X.Y.Z' semver-compatible version, got: '{}'".format(version)) +def _validate_version(version_str, *, _fail = fail): + v = version.parse(version_str, strict = True, _fail = _fail) + if v == None: + # Only reachable in tests + return False + + if len(v.release) < 3: + _fail("The 'python_version' attribute needs to specify the full version in at least 'X.Y.Z' format, got: '{}'".format(v.string)) return False return True def _process_single_version_overrides(*, tag, _fail = fail, default): - if not _validate_version(version = tag.python_version, _fail = _fail): + if not _validate_version(tag.python_version, _fail = _fail): return available_versions = default["tool_versions"] @@ -433,9 +611,9 @@ def _process_single_version_overrides(*, tag, _fail = fail, default): return for platform in (tag.sha256 or []): - if platform not in PLATFORMS: + if platform not in default["platforms"]: _fail("The platform must be one of {allowed} but got '{got}'".format( - allowed = sorted(PLATFORMS), + allowed = sorted(default["platforms"]), got = platform, )) return @@ -471,7 +649,7 @@ def _process_single_version_overrides(*, tag, _fail = fail, default): kwargs.setdefault(tag.python_version, {})["distutils"] = tag.distutils def _process_single_version_platform_overrides(*, tag, _fail = fail, default): - if not _validate_version(version = tag.python_version, _fail = _fail): + if not _validate_version(tag.python_version, _fail = _fail): return available_versions = default["tool_versions"] @@ -492,9 +670,56 @@ def _process_single_version_platform_overrides(*, tag, _fail = fail, default): available_versions[tag.python_version].setdefault("sha256", {})[tag.platform] = tag.sha256 if tag.strip_prefix: available_versions[tag.python_version].setdefault("strip_prefix", {})[tag.platform] = tag.strip_prefix + if tag.urls: available_versions[tag.python_version].setdefault("url", {})[tag.platform] = tag.urls + # If platform is customized, or doesn't exist, (re)define one. + if ((tag.target_compatible_with or tag.target_settings or tag.os_name or tag.arch) or + tag.platform not in default["platforms"]): + os_name = tag.os_name + arch = tag.arch + + if not tag.target_compatible_with: + target_compatible_with = [] + if os_name: + target_compatible_with.append("@platforms//os:{}".format( + repo_utils.get_platforms_os_name(os_name), + )) + if arch: + target_compatible_with.append("@platforms//cpu:{}".format( + repo_utils.get_platforms_cpu_name(arch), + )) + else: + target_compatible_with = tag.target_compatible_with + + # For lack of a better option, give a bogus value. It only affects + # if the runtime is considered host-compatible. + if not os_name: + os_name = "UNKNOWN_CUSTOM_OS" + if not arch: + arch = "UNKNOWN_CUSTOM_ARCH" + + # Move the override earlier in the ordering -- the platform key ordering + # becomes the toolchain ordering within the version. This allows the + # override to have a superset of constraints from a regular runtimes + # (e.g. same platform, but with a custom flag required). + override_first = { + tag.platform: platform_info( + compatible_with = target_compatible_with, + target_settings = tag.target_settings, + os_name = os_name, + arch = arch, + ), + } + for key, value in default["platforms"].items(): + # Don't replace our override with the old value + if key in override_first: + continue + override_first[key] = value + + default["platforms"] = override_first + def _process_global_overrides(*, tag, default, _fail = fail): if tag.available_python_versions: available_versions = default["tool_versions"] @@ -512,12 +737,12 @@ def _process_global_overrides(*, tag, default, _fail = fail): if tag.minor_mapping: for minor_version, full_version in tag.minor_mapping.items(): - parsed = semver(minor_version) - if parsed.patch != None or parsed.build or parsed.pre_release: - fail("Expected the key to be of `X.Y` format but got `{}`".format(minor_version)) - parsed = semver(full_version) - if parsed.patch == None: - fail("Expected the value to at least be of `X.Y.Z` format but got `{}`".format(minor_version)) + parsed = version.parse(minor_version, strict = True, _fail = _fail) + if len(parsed.release) > 2 or parsed.pre or parsed.post or parsed.dev or parsed.local: + fail("Expected the key to be of `X.Y` format but got `{}`".format(parsed.string)) + + # Ensure that the version is valid + version.parse(full_version, strict = True, _fail = _fail) default["minor_mapping"] = tag.minor_mapping @@ -552,25 +777,53 @@ def _override_defaults(*overrides, modules, _fail = fail, default): override.fn(tag = tag, _fail = _fail, default = default) def _get_toolchain_config(*, modules, _fail = fail): + """Computes the configs for toolchains. + + Args: + modules: The modules from module_ctx + _fail: Function to call for failing; only used for testing. + + Returns: + A struct with the following: + * `kwargs`: {type}`dict[str, dict[str, object]` custom kwargs to pass to + `python_register_toolchains`, keyed by python version. + The first key is either a Major.Minor or Major.Minor.Patch + string. + * `minor_mapping`: {type}`dict[str, str]` the mapping of Major.Minor + to Major.Minor.Patch. + * `default`: {type}`dict[str, object]` of kwargs passed along to + `python_register_toolchains`. These keys take final precedence. + * `register_all_versions`: {type}`bool` whether all known versions + should be registered. + """ + # Items that can be overridden - available_versions = { - version: { - # Use a dicts straight away so that we could do URL overrides for a - # single version. - "sha256": dict(item["sha256"]), - "strip_prefix": { - platform: item["strip_prefix"] - for platform in item["sha256"] - } if type(item["strip_prefix"]) == type("") else item["strip_prefix"], - "url": { - platform: [item["url"]] - for platform in item["sha256"] - } if type(item["url"]) == type("") else item["url"], - } - for version, item in TOOL_VERSIONS.items() - } + available_versions = {} + for py_version, item in TOOL_VERSIONS.items(): + available_versions[py_version] = {} + available_versions[py_version]["sha256"] = dict(item["sha256"]) + platforms = item["sha256"].keys() + + strip_prefix = item["strip_prefix"] + if type(strip_prefix) == type(""): + available_versions[py_version]["strip_prefix"] = { + platform: strip_prefix + for platform in platforms + } + else: + available_versions[py_version]["strip_prefix"] = dict(strip_prefix) + url = item["url"] + if type(url) == type(""): + available_versions[py_version]["url"] = { + platform: url + for platform in platforms + } + else: + available_versions[py_version]["url"] = dict(url) + default = { "base_url": DEFAULT_RELEASE_BASE_URL, + "platforms": dict(PLATFORMS), # Copy so it's mutable. "tool_versions": available_versions, } @@ -605,8 +858,11 @@ def _get_toolchain_config(*, modules, _fail = fail): versions = {} for version_string in available_versions: - v = semver(version_string) - versions.setdefault("{}.{}".format(v.major, v.minor), []).append((int(v.patch), version_string)) + v = version.parse(version_string, strict = True) + versions.setdefault( + "{}.{}".format(v.release[0], v.release[1]), + [], + ).append((version.key(v), v.string)) minor_mapping = { major_minor: max(subset)[1] @@ -776,10 +1032,8 @@ In order to use a different name than the above, you can use the following `MODU syntax: ```starlark python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain( - is_default = True, - python_version = "3.11", -) +python.defaults(python_version = "3.11") +python.toolchain(python_version = "3.11") use_repo(python, my_python_name = "python_3_11") ``` @@ -815,7 +1069,7 @@ Whether the toolchain is the default version. :::{versionchanged} 1.4.0 This setting is ignored if the default version is set using the `defaults` -tag class. +tag class (encouraged). ::: """, ), @@ -971,10 +1225,48 @@ configuration, please use {obj}`single_version_override`. ::: """, attrs = { + "arch": attr.string( + doc = """ +The arch (cpu) the runtime is compatible with. + +If not set, then the runtime cannot be used as a `python_X_Y_host` runtime. + +If set, the `os_name`, `target_compatible_with` and `target_settings` attributes +should also be set. + +The values should be one of the values in `@platforms//cpu` + +:::{seealso} +Docs for [Registering custom runtimes] +::: + +:::{{versionadded}} VERSION_NEXT_FEATURE +::: +""", + ), "coverage_tool": attr.label( doc = """\ The coverage tool to be used for a particular Python interpreter. This can override `rules_python` defaults. +""", + ), + "os_name": attr.string( + doc = """ +The host OS the runtime is compatible with. + +If not set, then the runtime cannot be used as a `python_X_Y_host` runtime. + +If set, the `os_name`, `target_compatible_with` and `target_settings` attributes +should also be set. + +The values should be one of the values in `@platforms//os` + +:::{seealso} +Docs for [Registering custom runtimes] +::: + +:::{{versionadded}} VERSION_NEXT_FEATURE +::: """, ), "patch_strip": attr.int( @@ -988,8 +1280,20 @@ The coverage tool to be used for a particular Python interpreter. This can overr ), "platform": attr.string( mandatory = True, - values = PLATFORMS.keys(), - doc = "The platform to override the values for, must be one of:\n{}.".format("\n".join(sorted(["* `{}`".format(p) for p in PLATFORMS]))), + doc = """ +The platform to override the values for, typically one of:\n +{platforms} + +Other values are allowed, in which case, `target_compatible_with`, +`target_settings`, `os_name`, and `arch` should be specified so the toolchain is +only used when appropriate. + +:::{{versionchanged}} VERSION_NEXT_FEATURE +Arbitrary platform strings allowed. +::: +""".format( + platforms = "\n".join(sorted(["* `{}`".format(p) for p in PLATFORMS])), + ), ), "python_version": attr.string( mandatory = True, @@ -1004,6 +1308,36 @@ The coverage tool to be used for a particular Python interpreter. This can overr doc = "The 'strip_prefix' for the archive, defaults to 'python'.", default = "python", ), + "target_compatible_with": attr.string_list( + doc = """ +The `target_compatible_with` values to use for the toolchain definition. + +If not set, then `os_name` and `arch` will be used to populate it. + +If set, `target_settings`, `os_name`, and `arch` should also be set. + +:::{seealso} +Docs for [Registering custom runtimes] +::: + +:::{{versionadded}} VERSION_NEXT_FEATURE +::: +""", + ), + "target_settings": attr.string_list( + doc = """ +The `target_setings` values to use for the toolchain definition. + +If set, `target_compatible_with`, `os_name`, and `arch` should also be set. + +:::{seealso} +Docs for [Registering custom runtimes] +::: + +:::{{versionadded}} VERSION_NEXT_FEATURE +::: +""", + ), "urls": attr.string_list( mandatory = False, doc = "The URL template to fetch releases for this Python version. If the URL template results in a relative fragment, default base URL is going to be used. Occurrences of `{python_version}`, `{platform}` and `{build}` will be interpolated based on the contents in the override and the known {attr}`platform` values.", diff --git a/python/private/python_bootstrap_template.txt b/python/private/python_bootstrap_template.txt index eb5595f4a1..a979fd4422 100644 --- a/python/private/python_bootstrap_template.txt +++ b/python/private/python_bootstrap_template.txt @@ -46,7 +46,15 @@ def GetWindowsPathWithUNCPrefix(path): # removed from common Win32 file and directory functions. # Related doc: https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd#enable-long-paths-in-windows-10-version-1607-and-later import platform - if platform.win32_ver()[1] >= '10.0.14393': + win32_version = None + # Windows 2022 with Python 3.12.8 gives flakey errors, so try a couple times. + for _ in range(3): + try: + win32_version = platform.win32_ver()[1] + break + except (ValueError, KeyError): + pass + if win32_version and win32_version >= '10.0.14393': return path # import sysconfig only now to maintain python 2.6 compatibility @@ -491,8 +499,12 @@ def Main(): # The magic string percent-main-percent is replaced with the runfiles-relative # filename of the main file of the Python binary in BazelPythonSemantics.java. main_rel_path = '%main%' - if IsWindows(): - main_rel_path = main_rel_path.replace('/', os.sep) + # NOTE: We call normpath for two reasons: + # 1. Transform Bazel `foo/bar` to Windows `foo\bar` + # 2. Transform `_main/../foo/main.py` to simply `foo/main.py`, which + # matters if `_main` doesn't exist (which can occur if a binary + # is packaged and needs no artifacts from the main repo) + main_rel_path = os.path.normpath(main_rel_path) if IsRunningFromZip(): module_space = CreateModuleSpace() diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl index cd3e9cbed7..2e0748deb0 100644 --- a/python/private/python_register_toolchains.bzl +++ b/python/private/python_register_toolchains.bzl @@ -28,7 +28,7 @@ load(":full_version.bzl", "full_version") load(":python_repository.bzl", "python_repository") load( ":toolchains_repo.bzl", - "host_toolchain", + "host_compatible_python_repo", "toolchain_aliases", "toolchains_repo", ) @@ -41,13 +41,14 @@ def python_register_toolchains( register_coverage_tool = False, set_python_version_constraint = False, tool_versions = None, + platforms = PLATFORMS, minor_mapping = None, **kwargs): """Convenience macro for users which does typical setup. With `bzlmod` enabled, this function is not needed since `rules_python` is handling everything. In order to override the default behaviour from the - root module one can see the docs for the {rule}`python` extension. + root module one can see the docs for the {obj}`python` extension. - 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. @@ -70,12 +71,19 @@ def python_register_toolchains( tool_versions: {type}`dict` contains a mapping of version with SHASUM and platform info. If not supplied, the defaults in python/versions.bzl will be used. + platforms: {type}`dict[str, struct]` 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: {type}`dict[str, str]` contains a mapping from `X.Y` to `X.Y.Z` version. **kwargs: passed to each {obj}`python_repository` call. Returns: - On bzlmod this returns the loaded platform labels. Otherwise None. + On workspace, returns None. + + On bzlmod, returns a `dict[str, platform_info]`, which is the + subset of `platforms` that it created repositories for. """ bzlmod_toolchain_call = kwargs.pop("_internal_bzlmod_toolchain_call", False) if bzlmod_toolchain_call: @@ -104,8 +112,12 @@ def python_register_toolchains( )) register_coverage_tool = False + # list[str] of the platform names that were used loaded_platforms = [] - for platform in PLATFORMS.keys(): + + # dict[str repo name, tuple[str, platform_info]] + impl_repos = {} + for platform, platform_info in platforms.items(): sha256 = tool_versions[python_version]["sha256"].get(platform, None) if not sha256: continue @@ -130,11 +142,10 @@ def python_register_toolchains( )], ) + impl_repo_name = "{}_{}".format(name, platform) + impl_repos[impl_repo_name] = (platform, platform_info) python_repository( - name = "{name}_{platform}".format( - name = name, - platform = platform, - ), + name = impl_repo_name, sha256 = sha256, patches = patches, patch_strip = patch_strip, @@ -160,12 +171,6 @@ def python_register_toolchains( platform = platform, )) - host_toolchain( - name = name + "_host", - platforms = loaded_platforms, - python_version = python_version, - ) - toolchain_aliases( name = name, python_version = python_version, @@ -173,9 +178,18 @@ def python_register_toolchains( platforms = loaded_platforms, ) - # in bzlmod we write out our own toolchain repos + # in bzlmod we write out our own toolchain repos and host repos if bzlmod_toolchain_call: - return loaded_platforms + return struct( + # dict[str name, tuple[str platform_name, platform_info]] + impl_repos = impl_repos, + ) + + host_compatible_python_repo( + name = name + "_host", + platforms = loaded_platforms, + python_version = python_version, + ) toolchains_repo( name = toolchain_repo_name, diff --git a/python/private/python_repository.bzl b/python/private/python_repository.bzl index fd86b415cc..cb0731e6eb 100644 --- a/python/private/python_repository.bzl +++ b/python/private/python_repository.bzl @@ -15,7 +15,7 @@ """This file contains repository rules and macros to support toolchain registration. """ -load("//python:versions.bzl", "FREETHREADED", "INSTALL_ONLY", "PLATFORMS") +load("//python:versions.bzl", "FREETHREADED", "INSTALL_ONLY") load(":auth.bzl", "get_auth") load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils") load(":text_util.bzl", "render") @@ -327,7 +327,6 @@ function defaults (e.g. `single_version_override` for `MODULE.bazel` files. "platform": attr.string( doc = "The platform name for the Python interpreter tarball.", mandatory = True, - values = PLATFORMS.keys(), ), "python_version": attr.string( doc = "The Python version.", diff --git a/python/private/pythons_hub.bzl b/python/private/pythons_hub.bzl index b448d53097..cc25b4ba1d 100644 --- a/python/private/pythons_hub.bzl +++ b/python/private/pythons_hub.bzl @@ -16,7 +16,7 @@ load("//python:versions.bzl", "PLATFORMS") load(":text_util.bzl", "render") -load(":toolchains_repo.bzl", "python_toolchain_build_file_content") +load(":toolchains_repo.bzl", "toolchain_suite_content") def _have_same_length(*lists): if not lists: @@ -24,8 +24,10 @@ def _have_same_length(*lists): return len({len(length): None for length in lists}) == 1 _HUB_BUILD_FILE_TEMPLATE = """\ -load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +# Generated by @rules_python//python/private:pythons_hub.bzl + load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite") +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") bzl_library( name = "interpreters_bzl", @@ -42,54 +44,47 @@ bzl_library( {toolchains} """ -def _hub_build_file_content( - prefixes, - python_versions, - set_python_version_constraints, - user_repository_names, - workspace_location, - loaded_platforms): - """This macro iterates over each of the lists and returns the toolchain content. - - python_toolchain_build_file_content is called to generate each of the toolchain - definitions. - """ - - if not _have_same_length(python_versions, set_python_version_constraints, user_repository_names): +def _hub_build_file_content(rctx): + # Verify a precondition. If these don't match, then something went wrong. + if not _have_same_length( + rctx.attr.toolchain_names, + rctx.attr.toolchain_platform_keys, + rctx.attr.toolchain_repo_names, + rctx.attr.toolchain_target_compatible_with_map, + rctx.attr.toolchain_target_settings_map, + rctx.attr.toolchain_set_python_version_constraints, + rctx.attr.toolchain_python_versions, + ): fail("all lists must have the same length") - # Iterate over the length of python_versions and call - # build the toolchain content by calling python_toolchain_build_file_content - toolchains = "\n".join( - [ - python_toolchain_build_file_content( - prefix = prefixes[i], - python_version = python_versions[i], - set_python_version_constraint = set_python_version_constraints[i], - user_repository_name = user_repository_names[i], - loaded_platforms = { - k: v - for k, v in PLATFORMS.items() - if k in loaded_platforms[python_versions[i]] - }, - ) - for i in range(len(python_versions)) - ], - ) + #pad_length = len(str(len(rctx.attr.toolchain_names))) + 1 + pad_length = 4 + toolchains = [] + for i, base_name in enumerate(rctx.attr.toolchain_names): + key = str(i) + platform = rctx.attr.toolchain_platform_keys[key] + if platform in PLATFORMS: + flag_values = PLATFORMS[platform].flag_values + else: + flag_values = {} + + toolchains.append(toolchain_suite_content( + prefix = "_{}_{}".format(render.left_pad_zero(i, pad_length), base_name), + user_repository_name = rctx.attr.toolchain_repo_names[key], + target_compatible_with = rctx.attr.toolchain_target_compatible_with_map[key], + flag_values = flag_values, + target_settings = rctx.attr.toolchain_target_settings_map[key], + set_python_version_constraint = rctx.attr.toolchain_set_python_version_constraints[key], + python_version = rctx.attr.toolchain_python_versions[key], + )) return _HUB_BUILD_FILE_TEMPLATE.format( - toolchains = toolchains, - rules_python = workspace_location.repo_name, + toolchains = "\n".join(toolchains), + rules_python = rctx.attr._rules_python_workspace.repo_name, ) _interpreters_bzl_template = """ -INTERPRETER_LABELS = {{ -{interpreter_labels} -}} -""" - -_line_for_hub_template = """\ - "{name}_host": Label("@{name}_host//:python"), +INTERPRETER_LABELS = {labels} """ _versions_bzl_template = """ @@ -103,28 +98,22 @@ def _hub_repo_impl(rctx): # write them to the BUILD file. rctx.file( "BUILD.bazel", - _hub_build_file_content( - rctx.attr.toolchain_prefixes, - rctx.attr.toolchain_python_versions, - rctx.attr.toolchain_set_python_version_constraints, - rctx.attr.toolchain_user_repository_names, - rctx.attr._rules_python_workspace, - rctx.attr.loaded_platforms, - ), + _hub_build_file_content(rctx), executable = False, ) # Create a dict that is later used to create # a symlink to a interpreter. - interpreter_labels = "".join([ - _line_for_hub_template.format(name = name) - for name in rctx.attr.toolchain_user_repository_names - ]) - rctx.file( "interpreters.bzl", _interpreters_bzl_template.format( - interpreter_labels = interpreter_labels, + labels = render.dict( + { + name: 'Label("@{}//:python")'.format(name) + for name in rctx.attr.host_compatible_repo_names + }, + value_repr = str, + ), ), executable = False, ) @@ -154,8 +143,9 @@ This rule also writes out the various toolchains for the different Python versio doc = "Default Python version for the build in `X.Y` or `X.Y.Z` format.", mandatory = True, ), - "loaded_platforms": attr.string_list_dict( - doc = "The list of loaded platforms keyed by the toolchain full python version", + "host_compatible_repo_names": attr.string_list( + doc = "Names of `host_compatible_python_repo` repos.", + mandatory = True, ), "minor_mapping": attr.string_dict( doc = "The minor mapping of the `X.Y` to `X.Y.Z` format that is used in config settings.", @@ -165,20 +155,32 @@ This rule also writes out the various toolchains for the different Python versio doc = "The list of python versions to include in the `interpreters.bzl` if the toolchains are not specified. Used in `WORKSPACE` builds.", mandatory = False, ), - "toolchain_prefixes": attr.string_list( - doc = "List prefixed for the toolchains", + "toolchain_names": attr.string_list( + doc = "Names of toolchains", + mandatory = True, + ), + "toolchain_platform_keys": attr.string_dict( + doc = "The platform key in PLATFORMS for toolchains.", mandatory = True, ), - "toolchain_python_versions": attr.string_list( + "toolchain_python_versions": attr.string_dict( doc = "List of Python versions for the toolchains. In `X.Y.Z` format.", mandatory = True, ), - "toolchain_set_python_version_constraints": attr.string_list( + "toolchain_repo_names": attr.string_dict( + doc = "The repo names containing toolchain implementations.", + mandatory = True, + ), + "toolchain_set_python_version_constraints": attr.string_dict( doc = "List of version contraints for the toolchains", mandatory = True, ), - "toolchain_user_repository_names": attr.string_list( - doc = "List of the user repo names for the toolchains", + "toolchain_target_compatible_with_map": attr.string_list_dict( + doc = "The target_compatible_with settings for toolchains.", + mandatory = True, + ), + "toolchain_target_settings_map": attr.string_list_dict( + doc = "The target_settings for toolchains", mandatory = True, ), "_rules_python_workspace": attr.label(default = Label("//:does_not_matter_what_this_name_is")), diff --git a/python/private/repl.bzl b/python/private/repl.bzl new file mode 100644 index 0000000000..838166a187 --- /dev/null +++ b/python/private/repl.bzl @@ -0,0 +1,84 @@ +"""Implementation of the rules to expose a REPL.""" + +load("//python:py_binary.bzl", _py_binary = "py_binary") + +def _generate_repl_main_impl(ctx): + stub_repo = ctx.attr.stub.label.repo_name or ctx.workspace_name + stub_path = "/".join([stub_repo, ctx.file.stub.short_path]) + + out = ctx.actions.declare_file(ctx.label.name + ".py") + + # Point the generated main file at the stub. + ctx.actions.expand_template( + template = ctx.file._template, + output = out, + substitutions = { + "%stub_path%": stub_path, + }, + ) + + return [DefaultInfo(files = depset([out]))] + +_generate_repl_main = rule( + implementation = _generate_repl_main_impl, + attrs = { + "stub": attr.label( + mandatory = True, + allow_single_file = True, + doc = ("The stub responsible for actually invoking the final shell. " + + "See the \"Customizing the REPL\" docs for details."), + ), + "_template": attr.label( + default = "//python/private:repl_template.py", + allow_single_file = True, + doc = "The template to use for generating `out`.", + ), + }, + doc = """\ +Generates a "main" script for a py_binary target that starts a Python REPL. + +The template is designed to take care of the majority of the logic. The user +customizes the exact shell that will be started via the stub. The stub is a +simple shell script that imports the desired shell and then executes it. + +The target's name is used for the output filename (with a .py extension). +""", +) + +def py_repl_binary(name, stub, deps = [], data = [], **kwargs): + """A py_binary target that executes a REPL when run. + + The stub is the script that ultimately decides which shell the REPL will run. + It can be as simple as this: + + import code + code.interact() + + Or it can load something like IPython instead. + + Args: + name: Name of the generated py_binary target. + stub: The script that invokes the shell. + deps: The dependencies of the py_binary. + data: The runtime dependencies of the py_binary. + **kwargs: Forwarded to the py_binary. + """ + _generate_repl_main( + name = "%s_py" % name, + stub = stub, + ) + + _py_binary( + name = name, + srcs = [ + ":%s_py" % name, + ], + main = "%s_py.py" % name, + data = data + [ + stub, + ], + deps = deps + [ + "//python/runfiles", + ], + **kwargs + ) diff --git a/python/private/repl_template.py b/python/private/repl_template.py new file mode 100644 index 0000000000..37f4529fbe --- /dev/null +++ b/python/private/repl_template.py @@ -0,0 +1,45 @@ +import os +import runpy +import sys +from pathlib import Path + +from python.runfiles import runfiles + +STUB_PATH = "%stub_path%" + + +def start_repl(): + if sys.stdin.isatty(): + # Print the banner similar to how python does it on startup when running interactively. + cprt = 'Type "help", "copyright", "credits" or "license" for more information.' + sys.stderr.write("Python %s on %s\n%s\n" % (sys.version, sys.platform, cprt)) + + # If there's a PYTHONSTARTUP script, we need to capture the new variables + # that it defines. + new_globals = {} + + # Simulate Python's behavior when a valid startup script is defined by the + # PYTHONSTARTUP variable. If this file path fails to load, print the error + # and revert to the default behavior. + # + # See upstream for more information: + # https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP + if startup_file := os.getenv("PYTHONSTARTUP"): + try: + source_code = Path(startup_file).read_text() + except Exception as error: + print(f"{type(error).__name__}: {error}") + else: + compiled_code = compile(source_code, filename=startup_file, mode="exec") + eval(compiled_code, new_globals) + + bazel_runfiles = runfiles.Create() + runpy.run_path( + bazel_runfiles.Rlocation(STUB_PATH), + init_globals=new_globals, + run_name="__main__", + ) + + +if __name__ == "__main__": + start_repl() diff --git a/python/private/repo_utils.bzl b/python/private/repo_utils.bzl index 73883a9244..32a5b70e15 100644 --- a/python/private/repo_utils.bzl +++ b/python/private/repo_utils.bzl @@ -31,13 +31,15 @@ def _is_repo_debug_enabled(mrctx): """ return _getenv(mrctx, REPO_DEBUG_ENV_VAR) == "1" -def _logger(mrctx, name = None): +def _logger(mrctx = None, name = None, verbosity_level = None): """Creates a logger instance for printing messages. Args: mrctx: repository_ctx or module_ctx object. If the attribute `_rule_name` is present, it will be included in log messages. name: name for the logger. Optional for repository_ctx usage. + verbosity_level: {type}`int | None` verbosity level. If not set, + taken from `mrctx` Returns: A struct with attributes logging: trace, debug, info, warn, fail. @@ -46,13 +48,14 @@ def _logger(mrctx, name = None): the logger injected into the function work as expected by terminating on the given line. """ - if _is_repo_debug_enabled(mrctx): - verbosity_level = "DEBUG" - else: - verbosity_level = "WARN" + if verbosity_level == None: + if _is_repo_debug_enabled(mrctx): + verbosity_level = "DEBUG" + else: + verbosity_level = "WARN" - env_var_verbosity = _getenv(mrctx, REPO_VERBOSITY_ENV_VAR) - verbosity_level = env_var_verbosity or verbosity_level + env_var_verbosity = _getenv(mrctx, REPO_VERBOSITY_ENV_VAR) + verbosity_level = env_var_verbosity or verbosity_level verbosity = { "DEBUG": 2, @@ -98,6 +101,8 @@ def _execute_internal( arguments, environment = {}, logger = None, + log_stdout = True, + log_stderr = True, **kwargs): """Execute a subprocess with debugging instrumentation. @@ -116,6 +121,10 @@ def _execute_internal( logger: optional `Logger` to use for logging execution details. Must be specified when using module_ctx. If not specified, a default will be created. + log_stdout: If True (the default), write stdout to the logged message. Setting + to False can be useful for large stdout messages or for secrets. + log_stderr: If True (the default), write stderr to the logged message. Setting + to False can be useful for large stderr messages or for secrets. **kwargs: additional kwargs to pass onto rctx.execute Returns: @@ -160,7 +169,7 @@ def _execute_internal( cwd = _cwd_to_str(mrctx, kwargs), timeout = _timeout_to_str(kwargs), env_str = _env_to_str(environment), - output = _outputs_to_str(result), + output = _outputs_to_str(result, log_stdout = log_stdout, log_stderr = log_stderr), )) elif _is_repo_debug_enabled(mrctx): logger.debug(( @@ -171,7 +180,7 @@ def _execute_internal( op = op, status = "success" if result.return_code == 0 else "failure", return_code = result.return_code, - output = _outputs_to_str(result), + output = _outputs_to_str(result, log_stdout = log_stdout, log_stderr = log_stderr), )) result_kwargs = {k: getattr(result, k) for k in dir(result)} @@ -183,6 +192,8 @@ def _execute_internal( mrctx = mrctx, kwargs = kwargs, environment = environment, + log_stdout = log_stdout, + log_stderr = log_stderr, ), **result_kwargs ) @@ -220,7 +231,16 @@ def _execute_checked_stdout(*args, **kwargs): """Calls execute_checked, but only returns the stdout value.""" return _execute_checked(*args, **kwargs).stdout -def _execute_describe_failure(*, op, arguments, result, mrctx, kwargs, environment): +def _execute_describe_failure( + *, + op, + arguments, + result, + mrctx, + kwargs, + environment, + log_stdout = True, + log_stderr = True): return ( "repo.execute: {op}: failure:\n" + " command: {cmd}\n" + @@ -236,7 +256,7 @@ def _execute_describe_failure(*, op, arguments, result, mrctx, kwargs, environme cwd = _cwd_to_str(mrctx, kwargs), timeout = _timeout_to_str(kwargs), env_str = _env_to_str(environment), - output = _outputs_to_str(result), + output = _outputs_to_str(result, log_stdout = log_stdout, log_stderr = log_stderr), ) def _which_checked(mrctx, binary_name): @@ -331,11 +351,11 @@ def _env_to_str(environment): def _timeout_to_str(kwargs): return kwargs.get("timeout", "") -def _outputs_to_str(result): +def _outputs_to_str(result, log_stdout = True, log_stderr = True): lines = [] items = [ - ("stdout", result.stdout), - ("stderr", result.stderr), + ("stdout", result.stdout if log_stdout else ""), + ("stderr", result.stderr if log_stderr else ""), ] for name, content in items: if content: @@ -359,7 +379,7 @@ def _get_platforms_os_name(mrctx): """Return the name in @platforms//os for the host os. Args: - mrctx: module_ctx or repository_ctx. + mrctx: {type}`module_ctx | repository_ctx` Returns: `str`. The target name. @@ -388,6 +408,7 @@ def _get_platforms_cpu_name(mrctx): `str`. The target name. """ arch = mrctx.os.arch.lower() + if arch in ["i386", "i486", "i586", "i686", "i786", "x86"]: return "x86_32" if arch in ["amd64", "x86_64", "x64"]: diff --git a/python/private/rule_builders.bzl b/python/private/rule_builders.bzl index 9b7c03136c..360503b21b 100644 --- a/python/private/rule_builders.bzl +++ b/python/private/rule_builders.bzl @@ -192,7 +192,7 @@ ExecGroup = struct( ) def _ToolchainType_typedef(): - """Builder for {obj}`config_common.toolchain_type()` + """Builder for {obj}`config_common.toolchain_type` :::{include} /_includes/field_kwargs_doc.md ::: @@ -253,7 +253,7 @@ def _ToolchainType_build(self): self: implicitly added Returns: - {type}`config_common.toolchain_type` + {type}`toolchain_type` """ kwargs = dict(self.kwargs) name = kwargs.pop("name") # Name must be positional @@ -393,7 +393,7 @@ def _RuleCfg_update_inputs(self, *others): Args: self: implicitly added - *others: {type}`collection[Label]` collection of labels to add to + *others: {type}`list[Label]` collection of labels to add to inputs. Only values not already present are added. Note that a `Label`, not `str`, should be passed to ensure different apparent labels can be properly de-duplicated. @@ -405,7 +405,7 @@ def _RuleCfg_update_outputs(self, *others): Args: self: implicitly added - *others: {type}`collection[Label]` collection of labels to add to + *others: {type}`list[Label]` collection of labels to add to outputs. Only values not already present are added. Note that a `Label`, not `str`, should be passed to ensure different apparent labels can be properly de-duplicated. @@ -673,13 +673,25 @@ def _AttrsDict_build(self): """Build an attribute dict for passing to `rule()`. Returns: - {type}`dict[str, attribute]` where the values are `attr.XXX` objects + {type}`dict[str, Attribute]` where the values are `attr.XXX` objects """ attrs = {} for k, v in self.map.items(): attrs[k] = v.build() if _is_builder(v) else v return attrs +def _AttributeBuilder_typedef(): + """An abstract base typedef for builder for a Bazel {obj}`Attribute` + + Instances of this are a builder for a particular `Attribute` type, + e.g. `attr.label`, `attr.string`, etc. + """ + +# buildifier: disable=name-conventions +AttributeBuilder = struct( + TYPEDEF = _AttributeBuilder_typedef, +) + # buildifier: disable=name-conventions AttrsDict = struct( TYPEDEF = _AttrsDict_typedef, diff --git a/python/private/runtime_env_repo.bzl b/python/private/runtime_env_repo.bzl new file mode 100644 index 0000000000..cade1968bb --- /dev/null +++ b/python/private/runtime_env_repo.bzl @@ -0,0 +1,41 @@ +"""Internal setup to help the runtime_env toolchain.""" + +load("//python/private:repo_utils.bzl", "repo_utils") + +def _runtime_env_repo_impl(rctx): + pyenv = repo_utils.which_unchecked(rctx, "pyenv").binary + if pyenv != None: + pyenv_version_file = repo_utils.execute_checked( + rctx, + op = "GetPyenvVersionFile", + arguments = [pyenv, "version-file"], + ).stdout.strip() + + # When pyenv is used, the version file is what decided the + # version used. Watch it so we compute the correct value if the + # user changes it. + rctx.watch(pyenv_version_file) + + version = repo_utils.execute_checked( + rctx, + op = "GetPythonVersion", + arguments = [ + "python3", + "-I", + "-c", + """import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")""", + ], + environment = { + # Prevent the user's current shell from influencing the result. + # This envvar won't be present when a test is run. + # NOTE: This should be None, but Bazel 7 doesn't support None + # values. Thankfully, pyenv treats empty string the same as missing. + "PYENV_VERSION": "", + }, + ).stdout.strip() + rctx.file("info.bzl", "PYTHON_VERSION = '{}'\n".format(version)) + rctx.file("BUILD.bazel", "") + +runtime_env_repo = repository_rule( + implementation = _runtime_env_repo_impl, +) diff --git a/python/private/runtime_env_toolchain.bzl b/python/private/runtime_env_toolchain.bzl index 2116012c03..1956ad5e95 100644 --- a/python/private/runtime_env_toolchain.bzl +++ b/python/private/runtime_env_toolchain.bzl @@ -17,6 +17,7 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library") load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_pair.bzl", "py_runtime_pair") load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain") +load("//python/private:config_settings.bzl", "is_python_version_at_least") load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain") load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE") @@ -38,6 +39,11 @@ def define_runtime_env_toolchain(name): """ base_name = name.replace("_toolchain", "") + supports_build_time_venv = select({ + ":_is_at_least_py3.11": True, + "//conditions:default": False, + }) + py_runtime( name = "_runtime_env_py3_runtime", interpreter = "//python/private:runtime_env_toolchain_interpreter.sh", @@ -45,6 +51,7 @@ def define_runtime_env_toolchain(name): stub_shebang = "#!/usr/bin/env python3", visibility = ["//visibility:private"], tags = ["manual"], + supports_build_time_venv = supports_build_time_venv, ) # This is a dummy runtime whose interpreter_path triggers the native rule @@ -56,6 +63,7 @@ def define_runtime_env_toolchain(name): python_version = "PY3", visibility = ["//visibility:private"], tags = ["manual"], + supports_build_time_venv = supports_build_time_venv, ) py_runtime_pair( @@ -110,3 +118,7 @@ def define_runtime_env_toolchain(name): toolchain_type = PY_CC_TOOLCHAIN_TYPE, visibility = ["//visibility:public"], ) + is_python_version_at_least( + name = "_is_at_least_py3.11", + at_least = "3.11", + ) diff --git a/python/private/runtime_env_toolchain_interpreter.sh b/python/private/runtime_env_toolchain_interpreter.sh index b09bc53e5c..7b3ec598b2 100755 --- a/python/private/runtime_env_toolchain_interpreter.sh +++ b/python/private/runtime_env_toolchain_interpreter.sh @@ -53,5 +53,32 @@ documentation for py_runtime_pair \ (https://github.com/bazel-contrib/rules_python/blob/master/docs/python.md#py_runtime_pair)." fi -exec "$PYTHON_BIN" "$@" +# Because this is a wrapper script that invokes Python, it prevents Python from +# detecting virtualenvs like normal (i.e. using the venv symlink to find the +# real interpreter). To work around this, we have to manually detect the venv, +# then trick the interpreter into understanding we're in a virtual env. +self_dir=$(dirname "$0") +if [ -e "$self_dir/pyvenv.cfg" ] || [ -e "$self_dir/../pyvenv.cfg" ]; then + case "$0" in + /*) + venv_bin="$0" + ;; + *) + venv_bin="$PWD/$0" + ;; + esac + if [ ! -e "$PYTHON_BIN" ]; then + die "ERROR: Python interpreter does not exist: $PYTHON_BIN" + fi + # PYTHONEXECUTABLE is also used because `exec -a` doesn't fully trick the + # pyenv wrappers. + # NOTE: The PYTHONEXECUTABLE envvar only works for non-Mac starting in Python 3.11 + export PYTHONEXECUTABLE="$venv_bin" + # Python looks at argv[0] to determine sys.executable, so use exec -a + # to make it think it's the venv's binary, not the actual one invoked. + # NOTE: exec -a isn't strictly posix-compatible, but very widespread + exec -a "$venv_bin" "$PYTHON_BIN" "$@" +else + exec "$PYTHON_BIN" "$@" +fi diff --git a/python/private/semver.bzl b/python/private/semver.bzl deleted file mode 100644 index cc9ae6ecb6..0000000000 --- a/python/private/semver.bzl +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2024 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"A semver version parser" - -def _key(version): - return ( - version.major, - version.minor or 0, - version.patch or 0, - # non pre-release versions are higher - version.pre_release == "", - # then we compare each element of the pre_release tag separately - tuple([ - ( - i if not i.isdigit() else "", - # digit values take precedence - int(i) if i.isdigit() else 0, - ) - for i in version.pre_release.split(".") - ]) if version.pre_release else None, - # And build info is just alphabetic - version.build, - ) - -def _to_dict(self): - return { - "build": self.build, - "major": self.major, - "minor": self.minor, - "patch": self.patch, - "pre_release": self.pre_release, - } - -def _upper(self): - major = self.major - minor = self.minor - patch = self.patch - build = "" - pre_release = "" - version = self.str() - - if patch != None: - minor = minor + 1 - patch = 0 - elif minor != None: - major = major + 1 - minor = 0 - elif minor == None: - major = major + 1 - - return _new( - major = major, - minor = minor, - patch = patch, - build = build, - pre_release = pre_release, - version = "~" + version, - ) - -def _new(*, major, minor, patch, pre_release, build, version = None): - # buildifier: disable=uninitialized - self = struct( - major = int(major), - minor = None if minor == None else int(minor), - # NOTE: this is called `micro` in the Python interpreter versioning scheme - patch = None if patch == None else int(patch), - pre_release = pre_release, - build = build, - # buildifier: disable=uninitialized - key = lambda: _key(self), - str = lambda: version, - to_dict = lambda: _to_dict(self), - upper = lambda: _upper(self), - ) - return self - -def semver(version): - """Parse the semver version and return the values as a struct. - - Args: - version: {type}`str` the version string. - - Returns: - A {type}`struct` with `major`, `minor`, `patch` and `build` attributes. - """ - - # Implement the https://semver.org/ spec - major, _, tail = version.partition(".") - minor, _, tail = tail.partition(".") - patch, _, build = tail.partition("+") - patch, _, pre_release = patch.partition("-") - - return _new( - major = int(major), - minor = int(minor) if minor.isdigit() else None, - patch = int(patch) if patch.isdigit() else None, - build = build, - pre_release = pre_release, - version = version, - ) diff --git a/python/private/site_init_template.py b/python/private/site_init_template.py index 40fb4e4139..a87a0d2a8f 100644 --- a/python/private/site_init_template.py +++ b/python/private/site_init_template.py @@ -125,6 +125,14 @@ def _search_path(name): def _setup_sys_path(): + """Perform Bazel/binary specific sys.path setup. + + NOTE: We do not add _RUNFILES_ROOT to sys.path for two reasons: + 1. Under workspace, it makes every external repository importable. If a Bazel + repository matches a Python import name, they conflict. + 2. Under bzlmod, the repo names in the runfiles directory aren't importable + Python names, so there's no point in adding the runfiles root to sys.path. + """ seen = set(sys.path) python_path_entries = [] @@ -195,5 +203,27 @@ def _maybe_add_path(path): return coverage_setup +def _fixup_sys_base_executable(): + """Fixup sys._base_executable to account for Bazel-specific pyvenv.cfg + + The pyvenv.cfg created for py_binary leaves the `home` key unset. A + side-effect of this is `sys._base_executable` points to the venv executable, + not the actual executable. This mostly doesn't matter, but does affect + using the venv module to create venvs (they point to the venv executable, not + the actual executable). + """ + # Must have been set correctly? + if sys.executable != sys._base_executable: + return + # Not in a venv, so don't touch anything. + if sys.prefix == sys.base_prefix: + return + exe = os.path.realpath(sys.executable) + _print_verbose("setting sys._base_executable:", exe) + sys._base_executable = exe + + +_fixup_sys_base_executable() + COVERAGE_SETUP = _setup_sys_path() _print_verbose("DONE") diff --git a/python/private/stage1_bootstrap_template.sh b/python/private/stage1_bootstrap_template.sh index c487624934..d992b55cae 100644 --- a/python/private/stage1_bootstrap_template.sh +++ b/python/private/stage1_bootstrap_template.sh @@ -9,7 +9,8 @@ fi # runfiles-relative path STAGE2_BOOTSTRAP="%stage2_bootstrap%" -# runfiles-relative path to python interpreter to use +# runfiles-relative path to python interpreter to use. +# This is the `bin/python3` path in the binary's venv. PYTHON_BINARY='%python_binary%' # The path that PYTHON_BINARY should symlink to. # runfiles-relative path, absolute path, or single word. @@ -18,8 +19,17 @@ PYTHON_BINARY_ACTUAL="%python_binary_actual%" # 0 or 1 IS_ZIPFILE="%is_zipfile%" -# 0 or 1 +# 0 or 1. +# If 1, then a venv will be created at runtime that replicates what would have +# been the build-time structure. RECREATE_VENV_AT_RUNTIME="%recreate_venv_at_runtime%" +# 0 or 1 +# If 1, then the path to python will be resolved by running +# PYTHON_BINARY_ACTUAL to determine the actual underlying interpreter. +RESOLVE_PYTHON_BINARY_AT_RUNTIME="%resolve_python_binary_at_runtime%" +# venv-relative path to the site-packages +# e.g. lib/python3.12t/site-packages +VENV_REL_SITE_PACKAGES="%venv_rel_site_packages%" # array of strings declare -a INTERPRETER_ARGS_FROM_TARGET=( @@ -152,34 +162,72 @@ elif [[ "$RECREATE_VENV_AT_RUNTIME" == "1" ]]; then fi fi - if [[ "$PYTHON_BINARY_ACTUAL" == /* ]]; then - # An absolute path, i.e. platform runtime, e.g. /usr/bin/python3 - symlink_to=$PYTHON_BINARY_ACTUAL - elif [[ "$PYTHON_BINARY_ACTUAL" == */* ]]; then - # A runfiles-relative path - symlink_to="$RUNFILES_DIR/$PYTHON_BINARY_ACTUAL" - else - # A plain word, e.g. "python3". Symlink to where PATH leads - symlink_to=$(which $PYTHON_BINARY_ACTUAL) - # Guard against trying to symlink to an empty value - if [[ $? -ne 0 ]]; then - echo >&2 "ERROR: Python to use not found on PATH: $PYTHON_BINARY_ACTUAL" - exit 1 - fi - fi - mkdir -p "$venv/bin" # Match the basename; some tools, e.g. pyvenv key off the executable name python_exe="$venv/bin/$(basename $PYTHON_BINARY_ACTUAL)" + if [[ ! -e "$python_exe" ]]; then - ln -s "$symlink_to" "$python_exe" + if [[ "$PYTHON_BINARY_ACTUAL" == /* ]]; then + # An absolute path, i.e. platform runtime, e.g. /usr/bin/python3 + python_exe_actual=$PYTHON_BINARY_ACTUAL + elif [[ "$PYTHON_BINARY_ACTUAL" == */* ]]; then + # A runfiles-relative path + python_exe_actual="$RUNFILES_DIR/$PYTHON_BINARY_ACTUAL" + else + # A plain word, e.g. "python3". Symlink to where PATH leads + python_exe_actual=$(which $PYTHON_BINARY_ACTUAL) + # Guard against trying to symlink to an empty value + if [[ $? -ne 0 ]]; then + echo >&2 "ERROR: Python to use not found on PATH: $PYTHON_BINARY_ACTUAL" + exit 1 + fi + fi + + runfiles_venv="$RUNFILES_DIR/$(dirname $(dirname $PYTHON_BINARY))" + # When RESOLVE_PYTHON_BINARY_AT_RUNTIME is true, it means the toolchain + # has thrown two complications at us: + # 1. The build-time assumption of the Python version may not match the + # runtime Python version. The site-packages directory path includes the + # Python version, so when the versions don't match, the runtime won't + # find it. + # 2. The interpreter might be a wrapper script, which interferes with Python's + # ability to detect when it's within a venv. Starting in Python 3.11, + # the PYTHONEXECUTABLE environment variable can fix this, but due to (1), + # we don't know if that is supported without running Python. + # To fix (1), we symlink the desired site-packages path to the build-time + # directory. Hopefully the version mismatch is OK :D. + # To fix (2), we determine the actual underlying interpreter and symlink + # to that. + if [[ "$RESOLVE_PYTHON_BINARY_AT_RUNTIME" == "1" ]]; then + { + read -r resolved_py_exe + read -r resolved_site_packages + } < <("$python_exe_actual" -I <= "10.0.14393": + win32_version = None + # Windows 2022 with Python 3.12.8 gives flakey errors, so try a couple times. + for _ in range(3): + try: + win32_version = platform.win32_ver()[1] + break + except (ValueError, KeyError): + pass + if win32_version and win32_version >= '10.0.14393': return path # import sysconfig only now to maintain python 2.6 compatibility @@ -365,6 +379,22 @@ def main(): print_verbose("initial environ:", mapping=os.environ) print_verbose("initial sys.path:", values=sys.path) + if VENV_SITE_PACKAGES: + site_packages = os.path.join(sys.prefix, VENV_SITE_PACKAGES) + if site_packages not in sys.path and os.path.exists(site_packages): + # NOTE: if this happens, it likely means we're running with a different + # Python version than was built with. Things may or may not work. + # Such a situation is likely due to the runtime_env toolchain, or some + # toolchain configuration. In any case, this better matches how the + # previous bootstrap=system_python bootstrap worked (using PYTHONPATH, + # which isn't version-specific). + print_verbose( + f"sys.path missing expected site-packages: adding {site_packages}" + ) + import site + + site.addsitedir(site_packages) + main_rel_path = None # todo: things happen to work because find_runfiles_root # ends up using stage2_bootstrap, and ends up computing the proper diff --git a/python/private/text_util.bzl b/python/private/text_util.bzl index a64b5d6243..28979d8981 100644 --- a/python/private/text_util.bzl +++ b/python/private/text_util.bzl @@ -108,6 +108,10 @@ def _render_list(items, *, hanging_indent = ""): def _render_str(value): return repr(value) +def _render_string_list_dict(value): + """Render an attr.string_list_dict value (`dict[str, list[str]`)""" + return _render_dict(value, value_repr = _render_list) + def _render_tuple(items, *, value_repr = repr): if not items: return "tuple()" @@ -166,4 +170,5 @@ render = struct( str = _render_str, toolchain_prefix = _toolchain_prefix, tuple = _render_tuple, + string_list_dict = _render_string_list_dict, ) diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl index 23c4643c0a..93bbb52108 100644 --- a/python/private/toolchains_repo.bzl +++ b/python/private/toolchains_repo.bzl @@ -25,12 +25,164 @@ platform-specific repositories. load( "//python:versions.bzl", + "FREETHREADED", + "MUSL", "PLATFORMS", "WINDOWS_NAME", ) load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils") load(":text_util.bzl", "render") +_SUITE_TEMPLATE = """ +py_toolchain_suite( + flag_values = {flag_values}, + target_settings = {target_settings}, + prefix = {prefix}, + python_version = {python_version}, + set_python_version_constraint = {set_python_version_constraint}, + target_compatible_with = {target_compatible_with}, + user_repository_name = {user_repository_name}, +) +""".lstrip() + +_WORKSPACE_TOOLCHAINS_BUILD_TEMPLATE = """ +# Generated by python/private/toolchains_repo.bzl +# +# These can be registered in the workspace file or passed to --extra_toolchains +# flag. By default all these toolchains are registered by the +# python_register_toolchains macro so you don't normally need to interact with +# these targets. + +load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite") + +""".lstrip() + +_TOOLCHAIN_ALIASES_BUILD_TEMPLATE = """ +# Generated by python/private/toolchains_repo.bzl +load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases") + +package(default_visibility = ["//visibility:public"]) + +exports_files(["defs.bzl"]) + +PLATFORMS = [ +{loaded_platforms} +] +toolchain_aliases( + name = "{py_repository}", + platforms = PLATFORMS, +) +""".lstrip() + +_TOOLCHAIN_ALIASES_DEFS_TEMPLATE = """ +# Generated by python/private/toolchains_repo.bzl + +load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements") +load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation") +load("@@{rules_python}//python/private:text_util.bzl", "render") +load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary") +load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test") +load( + "@@{rules_python}//python/entry_points:py_console_script_binary.bzl", + _py_console_script_binary = "py_console_script_binary", +) + +def _with_deprecation(kwargs, *, name): + kwargs["python_version"] = "{python_version}" + return with_deprecation.symbol( + kwargs, + symbol_name = name, + old_load = "@{name}//:defs.bzl", + new_load = "@rules_python//python:{{}}.bzl".format(name), + snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}}) + ) + +def py_binary(**kwargs): + return _py_binary(**_with_deprecation(kwargs, name = "py_binary")) + +def py_console_script_binary(**kwargs): + return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary")) + +def py_test(**kwargs): + return _py_test(**_with_deprecation(kwargs, name = "py_test")) + +def compile_pip_requirements(**kwargs): + return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements")) +""".lstrip() + +_HOST_TOOLCHAIN_BUILD_CONTENT = """ +# Generated by python/private/toolchains_repo.bzl + +exports_files(["python"], visibility = ["//visibility:public"]) +""".lstrip() + +_HOST_PYTHON_TESTER_TEMPLATE = """ +from pathlib import Path +import sys + +python = Path(sys.executable) +want_python = str(Path("{python}").resolve()) +got_python = str(Path(sys.executable).resolve()) + +assert want_python == got_python, \ + "Expected to use a different interpreter:\\nwant: '{{}}'\\n got: '{{}}'".format( + want_python, + got_python, + ) +""".lstrip() + +_MULTI_TOOLCHAIN_ALIASES_DEFS_TEMPLATE = """ +# Generated by python/private/toolchains_repo.bzl + +load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements") +load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation") +load("@@{rules_python}//python/private:text_util.bzl", "render") +load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary") +load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test") +load( + "@@{rules_python}//python/entry_points:py_console_script_binary.bzl", + _py_console_script_binary = "py_console_script_binary", +) + +def _with_deprecation(kwargs, *, name): + kwargs["python_version"] = "{python_version}" + return with_deprecation.symbol( + kwargs, + symbol_name = name, + old_load = "@{name}//{python_version}:defs.bzl", + new_load = "@rules_python//python:{{}}.bzl".format(name), + snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}}) + ) + +def py_binary(**kwargs): + return _py_binary(**_with_deprecation(kwargs, name = "py_binary")) + +def py_console_script_binary(**kwargs): + return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary")) + +def py_test(**kwargs): + return _py_test(**_with_deprecation(kwargs, name = "py_test")) + +def compile_pip_requirements(**kwargs): + return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements")) +""".lstrip() + +_MULTI_TOOLCHAIN_ALIASES_PIP_TEMPLATE = """ +# Generated by python/private/toolchains_repo.bzl + +load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse") + +def multi_pip_parse(name, requirements_lock, **kwargs): + return _multi_pip_parse( + name = name, + python_versions = {python_versions}, + requirements_lock = requirements_lock, + minor_mapping = {minor_mapping}, + **kwargs + ) + +""".lstrip() + def python_toolchain_build_file_content( prefix, python_version, @@ -53,42 +205,43 @@ def python_toolchain_build_file_content( build_content: Text containing toolchain definitions """ - return "\n\n".join([ - """\ -py_toolchain_suite( - user_repository_name = "{user_repository_name}_{platform}", - prefix = "{prefix}{platform}", - target_compatible_with = {compatible_with}, - flag_values = {flag_values}, - python_version = "{python_version}", - set_python_version_constraint = "{set_python_version_constraint}", -)""".format( - compatible_with = render.indent(render.list(meta.compatible_with)).lstrip(), - flag_values = render.indent(render.dict( - meta.flag_values, - key_repr = lambda x: repr(str(x)), # this is to correctly display labels - )).lstrip(), - platform = platform, - set_python_version_constraint = set_python_version_constraint, - user_repository_name = user_repository_name, - prefix = prefix, + entries = [] + for platform, meta in loaded_platforms.items(): + entries.append(toolchain_suite_content( + target_compatible_with = meta.compatible_with, + flag_values = meta.flag_values, + prefix = "{}{}".format(prefix, platform), + user_repository_name = "{}_{}".format(user_repository_name, platform), python_version = python_version, - ) - for platform, meta in loaded_platforms.items() - ]) - -def _toolchains_repo_impl(rctx): - build_content = """\ -# Generated by python/private/toolchains_repo.bzl -# -# These can be registered in the workspace file or passed to --extra_toolchains -# flag. By default all these toolchains are registered by the -# python_register_toolchains macro so you don't normally need to interact with -# these targets. + set_python_version_constraint = set_python_version_constraint, + target_settings = [], + )) + return "\n\n".join(entries) -load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite") +def toolchain_suite_content( + *, + flag_values, + prefix, + python_version, + set_python_version_constraint, + target_compatible_with, + target_settings, + user_repository_name): + return _SUITE_TEMPLATE.format( + prefix = render.str(prefix), + user_repository_name = render.str(user_repository_name), + target_compatible_with = render.indent(render.list(target_compatible_with)).lstrip(), + flag_values = render.indent(render.dict( + flag_values, + key_repr = lambda x: repr(str(x)), # this is to correctly display labels + )).lstrip(), + target_settings = render.list(target_settings, hanging_indent = " "), + set_python_version_constraint = render.str(set_python_version_constraint), + python_version = render.str(python_version), + ) -""".format( +def _toolchains_repo_impl(rctx): + build_content = _WORKSPACE_TOOLCHAINS_BUILD_TEMPLATE.format( rules_python = rctx.attr._rules_python_workspace.repo_name, ) @@ -121,22 +274,7 @@ toolchains_repo = repository_rule( def _toolchain_aliases_impl(rctx): # Base BUILD file for this repository. - build_contents = """\ -# Generated by python/private/toolchains_repo.bzl -load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases") - -package(default_visibility = ["//visibility:public"]) - -exports_files(["defs.bzl"]) - -PLATFORMS = [ -{loaded_platforms} -] -toolchain_aliases( - name = "{py_repository}", - platforms = PLATFORMS, -) -""".format( + build_contents = _TOOLCHAIN_ALIASES_BUILD_TEMPLATE.format( py_repository = rctx.attr.user_repository_name, loaded_platforms = "\n".join([" \"{}\",".format(p) for p in rctx.attr.platforms]), ) @@ -144,41 +282,7 @@ toolchain_aliases( # Expose a Starlark file so rules can know what host platform we used and where to find an interpreter # when using repository_ctx.path, which doesn't understand aliases. - rctx.file("defs.bzl", content = """\ -# Generated by python/private/toolchains_repo.bzl - -load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements") -load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation") -load("@@{rules_python}//python/private:text_util.bzl", "render") -load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary") -load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test") -load( - "@@{rules_python}//python/entry_points:py_console_script_binary.bzl", - _py_console_script_binary = "py_console_script_binary", -) - -def _with_deprecation(kwargs, *, name): - kwargs["python_version"] = "{python_version}" - return with_deprecation.symbol( - kwargs, - symbol_name = name, - old_load = "@{name}//:defs.bzl", - new_load = "@rules_python//python:{{}}.bzl".format(name), - snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}}) - ) - -def py_binary(**kwargs): - return _py_binary(**_with_deprecation(kwargs, name = "py_binary")) - -def py_console_script_binary(**kwargs): - return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary")) - -def py_test(**kwargs): - return _py_test(**_with_deprecation(kwargs, name = "py_test")) - -def compile_pip_requirements(**kwargs): - return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements")) -""".format( + rctx.file("defs.bzl", content = _TOOLCHAIN_ALIASES_DEFS_TEMPLATE.format( name = rctx.attr.name, python_version = rctx.attr.python_version, rules_python = rctx.attr._rules_python_workspace.repo_name, @@ -205,15 +309,11 @@ actions.""", environ = [REPO_DEBUG_ENV_VAR], ) -def _host_toolchain_impl(rctx): - rctx.file("BUILD.bazel", """\ -# Generated by python/private/toolchains_repo.bzl - -exports_files(["python"], visibility = ["//visibility:public"]) -""") +def _host_compatible_python_repo_impl(rctx): + rctx.file("BUILD.bazel", _HOST_TOOLCHAIN_BUILD_CONTENT) os_name = repo_utils.get_platforms_os_name(rctx) - host_platform = _get_host_platform( + impl_repo_name = _get_host_impl_repo_name( rctx = rctx, logger = repo_utils.logger(rctx), python_version = rctx.attr.python_version, @@ -221,10 +321,11 @@ exports_files(["python"], visibility = ["//visibility:public"]) cpu_name = repo_utils.get_platforms_cpu_name(rctx), platforms = rctx.attr.platforms, ) - repo = "@@{py_repository}_{host_platform}".format( - py_repository = rctx.attr.name[:-len("_host")], - host_platform = host_platform, - ) + + # Bzlmod quirk: A repository rule can't, in its **implemention function**, + # resolve an apparent repo name referring to a repo created by the same + # bzlmod extension. To work around this, we use a canonical label. + repo = "@@{}".format(impl_repo_name) rctx.report_progress("Symlinking interpreter files to the target platform") host_python_repo = rctx.path(Label("{repo}//:BUILD.bazel".format(repo = repo))) @@ -256,20 +357,10 @@ exports_files(["python"], visibility = ["//visibility:public"]) # Ensure that we can run the interpreter and check that we are not # using the host interpreter. - python_tester_contents = """\ -from pathlib import Path -import sys - -python = Path(sys.executable) -want_python = str(Path("{python}").resolve()) -got_python = str(Path(sys.executable).resolve()) - -assert want_python == got_python, \ - "Expected to use a different interpreter:\\nwant: '{{}}'\\n got: '{{}}'".format( - want_python, - got_python, + python_tester_contents = _HOST_PYTHON_TESTER_TEMPLATE.format( + repo = repo.strip("@"), + python = python_binary, ) -""".format(repo = repo.strip("@"), python = python_binary) python_tester = rctx.path("python_tester.py") rctx.file(python_tester, python_tester_contents) repo_utils.execute_checked( @@ -287,18 +378,80 @@ assert want_python == got_python, \ if not rctx.delete(python_tester): fail("Failed to delete the python tester") -host_toolchain = repository_rule( - _host_toolchain_impl, +# NOTE: The term "toolchain" is a misnomer for this rule. This doesn't define +# a repo with toolchains or toolchain implementations. +host_compatible_python_repo = repository_rule( + implementation = _host_compatible_python_repo_impl, doc = """\ Creates a repository with a shorter name meant to be used in the repository_ctx, which needs to have `symlinks` for the interpreter. This is separate from the toolchain_aliases repo because referencing the `python` interpreter target from this repo causes an eager fetch of the toolchain for the host platform. - """, + +This repo has two ways in which is it called: + +1. Workspace. The `platforms` attribute is set, which are keys into the + PLATFORMS global. It assumes `name` + is a + valid repo name which it can use as the backing repo. + +2. Bzlmod. All platform and backing repo information is passed in via the + arch_names, impl_repo_names, os_names, python_versions attributes. +""", attrs = { - "platforms": attr.string_list(mandatory = True), - "python_version": attr.string(mandatory = True), - "_rule_name": attr.string(default = "host_toolchain"), + "arch_names": attr.string_dict( + doc = """ +Arch (cpu) names. Only set in bzlmod. Keyed by index in `platforms` +""", + ), + "base_name": attr.string( + doc = """ +The name arg, but without bzlmod canonicalization applied. Only set in bzlmod. +""", + ), + "impl_repo_names": attr.string_dict( + doc = """ +The names of backing runtime repos. Only set in bzlmod. The names must be repos +in the same extension as creates the host repo. Keyed by index in `platforms`. +""", + ), + "os_names": attr.string_dict( + doc = """ +If set, overrides the platform metadata. Only set in bzlmod. Keyed by +index in `platforms` +""", + ), + "platforms": attr.string_list( + mandatory = True, + doc = """ +Platform names (workspace) or platform name-like keys (bzlmod) + +NOTE: The order of this list matters. The first platform that is compatible +with the host will be selected; this can be customized by using the +`RULES_PYTHON_REPO_TOOLCHAIN_*` env vars. + +The values passed vary depending on workspace vs bzlmod. + +Workspace: the values are keys into the `PLATFORMS` dict and are the suffix +to append to `name` to point to the backing repo name. + +Bzlmod: The values are arbitrary keys to create the platform map from the +other attributes (os_name, arch_names, et al). +""", + ), + "python_version": attr.string( + doc = """ +Full python version, Major.Minor.Micro. + +Only set in workspace calls. +""", + ), + "python_versions": attr.string_dict( + doc = """ +If set, the Python version for the corresponding selected platform. Values in +Major.Minor.Micro format. Keyed by index in `platforms`. +""", + ), + "_rule_name": attr.string(default = "host_compatible_python_repo"), "_rules_python_workspace": attr.label(default = Label("//:WORKSPACE")), }, ) @@ -308,41 +461,7 @@ def _multi_toolchain_aliases_impl(rctx): for python_version, repository_name in rctx.attr.python_versions.items(): file = "{}/defs.bzl".format(python_version) - rctx.file(file, content = """\ -# Generated by python/private/toolchains_repo.bzl - -load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements") -load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation") -load("@@{rules_python}//python/private:text_util.bzl", "render") -load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary") -load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test") -load( - "@@{rules_python}//python/entry_points:py_console_script_binary.bzl", - _py_console_script_binary = "py_console_script_binary", -) - -def _with_deprecation(kwargs, *, name): - kwargs["python_version"] = "{python_version}" - return with_deprecation.symbol( - kwargs, - symbol_name = name, - old_load = "@{name}//{python_version}:defs.bzl", - new_load = "@rules_python//python:{{}}.bzl".format(name), - snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}}) - ) - -def py_binary(**kwargs): - return _py_binary(**_with_deprecation(kwargs, name = "py_binary")) - -def py_console_script_binary(**kwargs): - return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary")) - -def py_test(**kwargs): - return _py_test(**_with_deprecation(kwargs, name = "py_test")) - -def compile_pip_requirements(**kwargs): - return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements")) -""".format( + rctx.file(file, content = _MULTI_TOOLCHAIN_ALIASES_DEFS_TEMPLATE.format( repository_name = repository_name, name = rctx.attr.name, python_version = python_version, @@ -350,21 +469,7 @@ def compile_pip_requirements(**kwargs): )) rctx.file("{}/BUILD.bazel".format(python_version), "") - pip_bzl = """\ -# Generated by python/private/toolchains_repo.bzl - -load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse") - -def multi_pip_parse(name, requirements_lock, **kwargs): - return _multi_pip_parse( - name = name, - python_versions = {python_versions}, - requirements_lock = requirements_lock, - minor_mapping = {minor_mapping}, - **kwargs - ) - -""".format( + pip_bzl = _MULTI_TOOLCHAIN_ALIASES_PIP_TEMPLATE.format( python_versions = rctx.attr.python_versions.keys(), minor_mapping = render.indent(render.dict(rctx.attr.minor_mapping), indent = " " * 8).lstrip(), rules_python = rules_python, @@ -381,10 +486,57 @@ multi_toolchain_aliases = repository_rule( }, ) -def sanitize_platform_name(platform): - return platform.replace("-", "_") +def sorted_host_platform_names(platform_names): + """Sort platform names to give correct precedence. -def _get_host_platform(*, rctx, logger, python_version, os_name, cpu_name, platforms): + The order of keys in the platform mapping matters for the host toolchain + selection. When multiple runtimes are compatible with the host, we take the + first that is compatible (usually; there's also the + `RULES_PYTHON_REPO_TOOLCHAIN_*` environment variables). The historical + behavior carefully constructed the ordering of platform keys such that + the ordering was: + * Regular platforms + * The "-freethreaded" suffix + * The "-musl" suffix + + Here, we formalize that so it isn't subtly encoded in the ordering of keys + in a dict that autoformatters like to clobber and whose only documentation + is an innocous looking formatter disable directive. + + Args: + platform_names: a list of platform names + + Returns: + list[str] the same values, but in the desired order. + """ + + def platform_keyer(name): + # Ascending sort: lower is higher precedence + return ( + 1 if MUSL in name else 0, + 1 if FREETHREADED in name else 0, + ) + + return sorted(platform_names, key = platform_keyer) + +def sorted_host_platforms(platform_map): + """Sort the keys in the platform map to give correct precedence. + + See sorted_host_platform_names for explanation. + + Args: + platform_map: a mapping of platforms and their metadata. + + Returns: + dict; the same values, but with the keys inserted in the desired + order so that iteration happens in the desired order. + """ + return { + key: platform_map[key] + for key in sorted_host_platform_names(platform_map.keys()) + } + +def _get_host_impl_repo_name(*, rctx, logger, python_version, os_name, cpu_name, platforms): """Gets the host platform. Args: @@ -397,15 +549,42 @@ def _get_host_platform(*, rctx, logger, python_version, os_name, cpu_name, platf Returns: The host platform. """ + if rctx.attr.os_names: + platform_map = {} + base_name = rctx.attr.base_name + if not base_name: + fail("The `base_name` attribute must be set under bzlmod") + for i, platform_name in enumerate(platforms): + key = str(i) + impl_repo_name = rctx.attr.impl_repo_names[key] + impl_repo_name = rctx.name.replace(base_name, impl_repo_name) + platform_map[platform_name] = struct( + os_name = rctx.attr.os_names[key], + arch = rctx.attr.arch_names[key], + python_version = rctx.attr.python_versions[key], + impl_repo_name = impl_repo_name, + ) + else: + base_name = rctx.name.removesuffix("_host") + platform_map = {} + for platform_name, info in sorted_host_platforms(PLATFORMS).items(): + platform_map[platform_name] = struct( + os_name = info.os_name, + arch = info.arch, + python_version = python_version, + impl_repo_name = "{}_{}".format(base_name, platform_name), + ) + candidates = [] for platform in platforms: - meta = PLATFORMS[platform] + meta = platform_map[platform] if meta.os_name == os_name and meta.arch == cpu_name: - candidates.append(platform) + candidates.append((platform, meta)) if len(candidates) == 1: - return candidates[0] + platform_name, meta = candidates[0] + return meta.impl_repo_name if candidates: env_var = "RULES_PYTHON_REPO_TOOLCHAIN_{}_{}_{}".format( @@ -425,7 +604,11 @@ def _get_host_platform(*, rctx, logger, python_version, os_name, cpu_name, platf candidates = [preference] if candidates: - return candidates[0] + platform_name, meta = candidates[0] + suffix = meta.impl_repo_name + if not suffix: + suffix = platform_name + return suffix return logger.fail("Could not find a compatible 'host' python for '{os_name}', '{cpu_name}' from the loaded platforms: {platforms}".format( os_name = os_name, diff --git a/python/private/py_wheel_normalize_pep440.bzl b/python/private/version.bzl similarity index 52% rename from python/private/py_wheel_normalize_pep440.bzl rename to python/private/version.bzl index 9566348987..f98165d391 100644 --- a/python/private/py_wheel_normalize_pep440.bzl +++ b/python/private/version.bzl @@ -59,18 +59,23 @@ def _open_context(self): self.contexts.append(_ctx(_context(self)["start"])) return self.contexts[-1] -def _accept(self): +def _accept(self, key = None): """Close the current ctx successfully and merge the results.""" finished = self.contexts.pop() self.contexts[-1]["norm"] += finished["norm"] + if key: + self.contexts[-1][key] = finished["norm"] + self.contexts[-1]["start"] = finished["start"] return True def _context(self): return self.contexts[-1] -def _discard(self): +def _discard(self, key = None): self.contexts.pop() + if key: + self.contexts[-1][key] = "" return False def _new(input): @@ -313,9 +318,9 @@ def accept_epoch(parser): if accept_digits(parser) and accept(parser, _is("!"), "!"): if ctx["norm"] == "0!": ctx["norm"] = "" - return parser.accept() + return parser.accept("epoch") else: - return parser.discard() + return parser.discard("epoch") def accept_release(parser): """Accept the release segment, numbers separated by dots. @@ -329,10 +334,10 @@ def accept_release(parser): parser.open_context() if not accept_digits(parser): - return parser.discard() + return parser.discard("release") accept_dot_number_sequence(parser) - return parser.accept() + return parser.accept("release") def accept_pre_l(parser): """PEP 440: Pre-release spelling. @@ -374,7 +379,7 @@ def accept_prerelease(parser): accept(parser, _in(["-", "_", "."]), "") if not accept_pre_l(parser): - return parser.discard() + return parser.discard("pre") accept(parser, _in(["-", "_", "."]), "") @@ -382,7 +387,7 @@ def accept_prerelease(parser): # PEP 440: Implicit pre-release number ctx["norm"] += "0" - return parser.accept() + return parser.accept("pre") def accept_implicit_postrelease(parser): """PEP 440: Implicit post releases. @@ -444,9 +449,9 @@ def accept_postrelease(parser): parser.open_context() if accept_implicit_postrelease(parser) or accept_explicit_postrelease(parser): - return parser.accept() + return parser.accept("post") - return parser.discard() + return parser.discard("post") def accept_devrelease(parser): """PEP 440: Developmental releases. @@ -470,9 +475,9 @@ def accept_devrelease(parser): # PEP 440: Implicit development release number ctx["norm"] += "0" - return parser.accept() + return parser.accept("dev") - return parser.discard() + return parser.discard("dev") def accept_local(parser): """PEP 440: Local version identifiers. @@ -487,9 +492,9 @@ def accept_local(parser): if accept(parser, _is("+"), "+") and accept_alnum(parser): accept_separator_alnum_sequence(parser) - return parser.accept() + return parser.accept("local") - return parser.discard() + return parser.discard("local") def normalize_pep440(version): """Escape the version component of a filename. @@ -503,7 +508,32 @@ def normalize_pep440(version): Returns: string containing the normalized version. """ - parser = _new(version.strip()) # PEP 440: Leading and Trailing Whitespace + return _parse(version, strict = True)["norm"] + +def _parse(version_str, strict = True, _fail = fail): + """Escape the version component of a filename. + + See https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode + and https://peps.python.org/pep-0440/ + + Args: + version_str: version string to be normalized according to PEP 440. + strict: fail if the version is invalid, defaults to True. + _fail: Used for tests + + Returns: + string containing the normalized version. + """ + + # https://packaging.python.org/en/latest/specifications/version-specifiers/#leading-and-trailing-whitespace + version = version_str.strip() + is_prefix = False + + if not strict: + is_prefix = version.endswith(".*") + version = version.strip(" .*") # PEP 440: Leading and Trailing Whitespace and ".*" + + parser = _new(version) accept(parser, _is("v"), "") # PEP 440: Preceding v character accept_epoch(parser) accept_release(parser) @@ -511,9 +541,318 @@ def normalize_pep440(version): accept_postrelease(parser) accept_devrelease(parser) accept_local(parser) - if parser.input[parser.context()["start"]:]: - fail( - "Failed to parse PEP 440 version identifier '%s'." % parser.input, - "Parse error at '%s'" % parser.input[parser.context()["start"]:], - ) - return parser.context()["norm"] + + parser_ctx = parser.context() + if parser.input[parser_ctx["start"]:]: + if strict: + _fail( + "Failed to parse PEP 440 version identifier '%s'." % parser.input, + "Parse error at '%s'" % parser.input[parser_ctx["start"]:], + ) + + return None + + parser_ctx["is_prefix"] = is_prefix + return parser_ctx + +def parse(version_str, strict = False, _fail = fail): + """Parse a PEP4408 compliant version. + + This is similar to `normalize_pep440`, but it parses individual components to + comparable types. + + Args: + version_str: version string to be normalized according to PEP 440. + strict: fail if the version is invalid. + _fail: used for tests + + Returns: + a struct with individual components of a version: + * `epoch` {type}`int`, defaults to `0` + * `release` {type}`tuple[int]` an n-tuple of ints + * `pre` {type}`tuple[str, int] | None` a tuple of a string and an int, + e.g. ("a", 1) + * `post` {type}`tuple[str, int] | None` a tuple of a string and an int, + e.g. ("~", 1) + * `dev` {type}`tuple[str, int] | None` a tuple of a string and an int, + e.g. ("", 1) + * `local` {type}`tuple[str, int] | None` a tuple of components in the local + version, e.g. ("abc", 123). + * `is_prefix` {type}`bool` whether the version_str ends with `.*`. + * `string` {type}`str` normalized value of the input. + """ + + parts = _parse(version_str, strict = strict, _fail = _fail) + if not parts: + return None + + if parts["is_prefix"] and (parts["local"] or parts["post"] or parts["dev"] or parts["pre"]): + if strict: + _fail("local version part has been obtained, but only public segments can have prefix matches") + + # https://peps.python.org/pep-0440/#public-version-identifiers + return None + + return struct( + epoch = _parse_epoch(parts["epoch"], _fail), + release = _parse_release(parts["release"]), + pre = _parse_pre(parts["pre"]), + post = _parse_post(parts["post"], _fail), + dev = _parse_dev(parts["dev"], _fail), + local = _parse_local(parts["local"], _fail), + string = parts["norm"], + is_prefix = parts["is_prefix"], + ) + +def _parse_epoch(value, fail): + if not value: + return 0 + + if not value.endswith("!"): + fail("epoch string segment needs to end with '!', got: {}".format(value)) + + return int(value[:-1]) + +def _parse_release(value): + return tuple([int(d) for d in value.split(".")]) + +def _parse_local(value, fail): + if not value: + return None + + if not value.startswith("+"): + fail("local release identifier must start with '+', got: {}".format(value)) + + # If the part is numerical, handle it as a number + return tuple([int(part) if part.isdigit() else part for part in value[1:].split(".")]) + +def _parse_dev(value, fail): + if not value: + return None + + if not value.startswith(".dev"): + fail("dev release identifier must start with '.dev', got: {}".format(value)) + dev = int(value[len(".dev"):]) + + # Empty string goes first when comparing + return ("", dev) + +def _parse_pre(value): + if not value: + return None + + if value.startswith("rc"): + prefix = "rc" + else: + prefix = value[0] + + return (prefix, int(value[len(prefix):])) + +def _parse_post(value, fail): + if not value: + return None + + if not value.startswith(".post"): + fail("post release identifier must start with '.post', got: {}".format(value)) + post = int(value[len(".post"):]) + + # We choose `~` since almost all of the ASCII characters will be before + # it. Use `ord` and `chr` functions to find a good value. + return ("~", post) + +def _pad_zeros(release, n): + padding = n - len(release) + if padding <= 0: + return release + + release = list(release) + [0] * padding + return tuple(release) + +def _prefix_err(left, op, right): + if left.is_prefix or right.is_prefix: + fail("PEP440: only '==' and '!=' operators can use prefix matching: {} {} {}".format( + left.string, + op, + right.string, + )) + +def _version_eeq(left, right): + """=== operator""" + if left.is_prefix or right.is_prefix: + fail(_prefix_err(left, "===", right)) + + # https://peps.python.org/pep-0440/#arbitrary-equality + # > simple string equality operations + return left.string == right.string + +def _version_eq(left, right): + """== operator""" + if left.is_prefix and right.is_prefix: + fail("Invalid comparison: both versions cannot be prefix matching") + if left.is_prefix: + return right.string.startswith("{}.".format(left.string)) + if right.is_prefix: + return left.string.startswith("{}.".format(right.string)) + + if left.epoch != right.epoch: + return False + + release_len = max(len(left.release), len(right.release)) + left_release = _pad_zeros(left.release, release_len) + right_release = _pad_zeros(right.release, release_len) + + if left_release != right_release: + return False + + return ( + left.pre == right.pre and + left.post == right.post and + left.dev == right.dev + # local is ignored for == checks + ) + +def _version_compatible(left, right): + """~= operator""" + if left.is_prefix or right.is_prefix: + fail(_prefix_err(left, "~=", right)) + + # https://peps.python.org/pep-0440/#compatible-release + # Note, the ~= operator can be also expressed as: + # >= V.N, == V.* + + right_star = ".".join([str(d) for d in right.release[:-1]]) + if right.epoch: + right_star = "{}!{}.".format(right.epoch, right_star) + else: + right_star = "{}.".format(right_star) + + return _version_ge(left, right) and left.string.startswith(right_star) + +def _version_ne(left, right): + """!= operator""" + return not _version_eq(left, right) + +def _version_lt(left, right): + """< operator""" + if left.is_prefix or right.is_prefix: + fail(_prefix_err(left, "<", right)) + + if left.epoch > right.epoch: + return False + elif left.epoch < right.epoch: + return True + + release_len = max(len(left.release), len(right.release)) + left_release = _pad_zeros(left.release, release_len) + right_release = _pad_zeros(right.release, release_len) + + if left_release > right_release: + return False + elif left_release < right_release: + return True + + # From PEP440, this is not a simple ordering check and we need to check the version + # semantically: + # * The exclusive ordered comparison operator""" + if left.is_prefix or right.is_prefix: + fail(_prefix_err(left, ">", right)) + + if left.epoch > right.epoch: + return True + elif left.epoch < right.epoch: + return False + + release_len = max(len(left.release), len(right.release)) + left_release = _pad_zeros(left.release, release_len) + right_release = _pad_zeros(right.release, release_len) + + if left_release > right_release: + return True + elif left_release < right_release: + return False + + # From PEP440, this is not a simple ordering check and we need to check the version + # semantically: + # * The exclusive ordered comparison >V MUST NOT allow a post-release of the given version + # unless V itself is a post release. + # + # * The exclusive ordered comparison >V MUST NOT match a local version of the specified + # version. + + if left.post and right.post: + return left.post > right.post + else: + # ignore the left.post if right is not a post if right is a post, then this evaluates to + # False anyway. + return False + +def _version_le(left, right): + """<= operator""" + if left.is_prefix or right.is_prefix: + fail(_prefix_err(left, "<=", right)) + + # PEP440: simple order check + # https://peps.python.org/pep-0440/#inclusive-ordered-comparison + _left = _version_key(left, local = False) + _right = _version_key(right, local = False) + return _left < _right or _version_eq(left, right) + +def _version_ge(left, right): + """>= operator""" + if left.is_prefix or right.is_prefix: + fail(_prefix_err(left, ">=", right)) + + # PEP440: simple order check + # https://peps.python.org/pep-0440/#inclusive-ordered-comparison + _left = _version_key(left, local = False) + _right = _version_key(right, local = False) + return _left > _right or _version_eq(left, right) + +def _version_key(self, *, local = True): + """This function returns a tuple that can be used in 'sorted' calls. + + This implements the PEP440 version sorting. + """ + release_key = ("z",) + local = self.local if local else [] + local = local or [] + + return ( + self.epoch, + self.release, + # PEP440 Within a pre-release, post-release or development release segment with + # a shared prefix, ordering MUST be by the value of the numeric component. + # PEP440 release ordering: .devN, aN, bN, rcN, , .postN + # We choose to first match the pre-release, then post release, then dev and + # then stable + self.pre or self.post or self.dev or release_key, + # PEP440 local versions go before post versions + tuple([(type(item) == "int", item) for item in local]), + # PEP440 - pre-release ordering: .devN, , .postN + self.post or self.dev or release_key, + # PEP440 - post release ordering: .devN, + self.dev or release_key, + ) + +version = struct( + normalize = normalize_pep440, + parse = parse, + # methods, keep sorted + key = _version_key, + is_compatible = _version_compatible, + is_eq = _version_eq, + is_eeq = _version_eeq, + is_ge = _version_ge, + is_gt = _version_gt, + is_le = _version_le, + is_lt = _version_lt, + is_ne = _version_ne, +) diff --git a/python/runfiles/README.md b/python/runfiles/README.md index 2a57c76846..b5315a48f5 100644 --- a/python/runfiles/README.md +++ b/python/runfiles/README.md @@ -59,6 +59,8 @@ with open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f: # ... ``` +Here `my_workspace` is the name you specified via `module(name = "...")` in your `MODULE.bazel` file (with `--enable_bzlmod`, default as of Bazel 7) or `workspace(name = "...")` in `WORKSPACE` (with `--noenable_bzlmod`). + The code above creates a manifest- or directory-based implementation based on the environment variables in `os.environ`. See `Runfiles.Create()` for more info. If you want to explicitly create a manifest- or directory-based @@ -70,9 +72,7 @@ r1 = Runfiles.CreateManifestBased("path/to/foo.runfiles_manifest") r2 = Runfiles.CreateDirectoryBased("path/to/foo.runfiles/") ``` -If you want to start subprocesses, and the subprocess can't automatically -find the correct runfiles directory, you can explicitly set the right -environment variables for them: +If you want to start subprocesses that access runfiles, you have to set the right environment variables for them: ```python import subprocess diff --git a/python/uv/private/BUILD.bazel b/python/uv/private/BUILD.bazel index 587ad9a0f9..a07d8591ad 100644 --- a/python/uv/private/BUILD.bazel +++ b/python/uv/private/BUILD.bazel @@ -62,6 +62,7 @@ bzl_library( ":toolchain_types_bzl", ":uv_repository_bzl", ":uv_toolchains_repo_bzl", + "//python/private:auth_bzl", ], ) @@ -69,6 +70,7 @@ bzl_library( name = "uv_repository_bzl", srcs = ["uv_repository.bzl"], visibility = ["//python/uv:__subpackages__"], + deps = ["//python/private:auth_bzl"], ) bzl_library( diff --git a/python/uv/private/uv.bzl b/python/uv/private/uv.bzl index 55a05be032..2cc2df1b21 100644 --- a/python/uv/private/uv.bzl +++ b/python/uv/private/uv.bzl @@ -18,6 +18,7 @@ EXPERIMENTAL: This is experimental and may be removed without notice A module extension for working with uv. """ +load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth") load(":toolchain_types.bzl", "UV_TOOLCHAIN_TYPE") load(":uv_repository.bzl", "uv_repository") load(":uv_toolchains_repo.bzl", "uv_toolchains_repo") @@ -77,7 +78,7 @@ The version of uv to configure the sources for. If this is not specified it will last version used in the module or the default version set by `rules_python`. """, ), -} +} | AUTH_ATTRS default = tag_class( doc = """\ @@ -122,7 +123,7 @@ uv.configure( "urls": attr.string_list( doc = """\ The urls to download the binary from. If this is used, {attr}`base_url` and -{attr}`manifest_name` are ignored for the given version. +{attr}`manifest_filename` are ignored for the given version. ::::note If the `urls` are specified, they need to be specified for all of the platforms @@ -133,7 +134,7 @@ for a particular version. }, ) -def _configure(config, *, platform, compatible_with, target_settings, urls = [], sha256 = "", override = False, **values): +def _configure(config, *, platform, compatible_with, target_settings, auth_patterns, urls = [], sha256 = "", override = False, **values): """Set the value in the config if the value is provided""" for key, value in values.items(): if not value: @@ -144,6 +145,7 @@ def _configure(config, *, platform, compatible_with, target_settings, urls = [], config[key] = value + config.setdefault("auth_patterns", {}).update(auth_patterns) config.setdefault("platforms", {}) if not platform: if compatible_with or target_settings or urls: @@ -173,7 +175,8 @@ def process_modules( hub_name = "uv", uv_repository = uv_repository, toolchain_type = str(UV_TOOLCHAIN_TYPE), - hub_repo = uv_toolchains_repo): + hub_repo = uv_toolchains_repo, + get_auth = get_auth): """Parse the modules to get the config for 'uv' toolchains. Args: @@ -182,6 +185,7 @@ def process_modules( uv_repository: the rule to create a uv_repository override. toolchain_type: the toolchain type to use here. hub_repo: the hub repo factory function to use. + get_auth: the auth function to use. Returns: the result of the hub_repo. Mainly used for tests. @@ -216,6 +220,8 @@ def process_modules( compatible_with = tag.compatible_with, target_settings = tag.target_settings, override = mod.is_root, + netrc = tag.netrc, + auth_patterns = tag.auth_patterns, ) for key in [ @@ -271,6 +277,8 @@ def process_modules( sha256 = tag.sha256, urls = tag.urls, override = mod.is_root, + netrc = tag.netrc, + auth_patterns = tag.auth_patterns, ) if not versions: @@ -301,6 +309,11 @@ def process_modules( for platform, src in config.get("urls", {}).items() if src.urls } + auth = { + "auth_patterns": config.get("auth_patterns"), + "netrc": config.get("netrc"), + } + auth = {k: v for k, v in auth.items() if v} # Or fallback to fetching them from GH manifest file # Example file: https://github.com/astral-sh/uv/releases/download/0.6.3/dist-manifest.json @@ -313,6 +326,8 @@ def process_modules( ), manifest_filename = config["manifest_filename"], platforms = sorted(platforms), + get_auth = get_auth, + **auth ) for platform_name, platform in platforms.items(): @@ -327,6 +342,7 @@ def process_modules( platform = platform_name, urls = urls[platform_name].urls, sha256 = urls[platform_name].sha256, + **auth ) toolchain_names.append(toolchain_name) @@ -363,7 +379,7 @@ def _overlap(first_collection, second_collection): return False -def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename, platforms): +def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename, platforms, get_auth = get_auth, **auth_attrs): """Download the results about remote tool sources. This relies on the tools using the cargo packaging to infer the actual @@ -431,10 +447,13 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename "aarch64-apple-darwin" ] """ + auth_attr = struct(**auth_attrs) dist_manifest = module_ctx.path(manifest_filename) + urls = [base_url + "/" + manifest_filename] result = module_ctx.download( - base_url + "/" + manifest_filename, + url = urls, output = dist_manifest, + auth = get_auth(module_ctx, urls, ctx_attr = auth_attr), ) if not result.success: fail(result) @@ -454,11 +473,13 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename checksum_fname = checksum["name"] checksum_path = module_ctx.path(checksum_fname) + urls = ["{}/{}".format(base_url, checksum_fname)] downloads[checksum_path] = struct( download = module_ctx.download( - "{}/{}".format(base_url, checksum_fname), + url = urls, output = checksum_path, block = False, + auth = get_auth(module_ctx, urls, ctx_attr = auth_attr), ), archive_fname = fname, platforms = checksum["target_triples"], @@ -473,7 +494,7 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename sha256, _, checksummed_fname = module_ctx.read(checksum_path).partition(" ") checksummed_fname = checksummed_fname.strip(" *\n") - if archive_fname != checksummed_fname: + if checksummed_fname and archive_fname != checksummed_fname: fail("The checksum is for a different file, expected '{}' but got '{}'".format( archive_fname, checksummed_fname, diff --git a/python/uv/private/uv_repository.bzl b/python/uv/private/uv_repository.bzl index ba7d2a766c..fed4f576d3 100644 --- a/python/uv/private/uv_repository.bzl +++ b/python/uv/private/uv_repository.bzl @@ -18,6 +18,8 @@ EXPERIMENTAL: This is experimental and may be removed without notice Create repositories for uv toolchain dependencies """ +load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth") + UV_BUILD_TMPL = """\ # Generated by repositories.bzl load("@rules_python//python/uv:uv_toolchain.bzl", "uv_toolchain") @@ -43,6 +45,7 @@ def _uv_repo_impl(repository_ctx): url = repository_ctx.attr.urls, sha256 = repository_ctx.attr.sha256, stripPrefix = strip_prefix, + auth = get_auth(repository_ctx, repository_ctx.attr.urls), ) binary = "uv.exe" if is_windows else "uv" @@ -70,5 +73,5 @@ uv_repository = repository_rule( "sha256": attr.string(mandatory = False), "urls": attr.string_list(mandatory = True), "version": attr.string(mandatory = True), - }, + } | AUTH_ATTRS, ) diff --git a/python/versions.bzl b/python/versions.bzl index 6343ee49c8..e712a2e126 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -15,11 +15,15 @@ """The Python versions we use for the toolchains. """ +load("//python/private:platform_info.bzl", "platform_info") + # Values present in the @platforms//os package MACOS_NAME = "osx" LINUX_NAME = "linux" WINDOWS_NAME = "windows" -FREETHREADED = "freethreaded" + +FREETHREADED = "-freethreaded" +MUSL = "-musl" INSTALL_ONLY = "install_only" DEFAULT_RELEASE_BASE_URL = "https://github.com/astral-sh/python-build-standalone/releases/download" @@ -683,150 +687,139 @@ MINOR_MAPPING = { } def _generate_platforms(): - libc = Label("//python/config_settings:py_linux_libc") + is_libc_glibc = str(Label("//python/config_settings:_is_py_linux_libc_glibc")) + is_libc_musl = str(Label("//python/config_settings:_is_py_linux_libc_musl")) platforms = { - "aarch64-apple-darwin": struct( + "aarch64-apple-darwin": platform_info( compatible_with = [ "@platforms//os:macos", "@platforms//cpu:aarch64", ], - flag_values = {}, os_name = MACOS_NAME, - # Matches the value in @platforms//cpu package arch = "aarch64", ), - "aarch64-unknown-linux-gnu": struct( + "aarch64-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:aarch64", ], - flag_values = { - libc: "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "aarch64", ), - "armv7-unknown-linux-gnu": struct( + "armv7-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:armv7", ], - flag_values = { - libc: "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "arm", ), - "i386-unknown-linux-gnu": struct( + "i386-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:i386", ], - flag_values = { - libc: "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "x86_32", ), - "ppc64le-unknown-linux-gnu": struct( + "ppc64le-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:ppc", ], - flag_values = { - libc: "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "ppc", ), - "riscv64-unknown-linux-gnu": struct( + "riscv64-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:riscv64", ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "riscv64", ), - "s390x-unknown-linux-gnu": struct( + "s390x-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:s390x", ], - flag_values = { - Label("//python/config_settings:py_linux_libc"): "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "s390x", ), - "x86_64-apple-darwin": struct( + "x86_64-apple-darwin": platform_info( compatible_with = [ "@platforms//os:macos", "@platforms//cpu:x86_64", ], - flag_values = {}, os_name = MACOS_NAME, - # Matches the value in @platforms//cpu package arch = "x86_64", ), - "x86_64-pc-windows-msvc": struct( + "x86_64-pc-windows-msvc": platform_info( compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], - flag_values = {}, os_name = WINDOWS_NAME, - # Matches the value in @platforms//cpu package arch = "x86_64", ), - "x86_64-unknown-linux-gnu": struct( + "x86_64-unknown-linux-gnu": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", ], - flag_values = { - libc: "glibc", - }, + target_settings = [ + is_libc_glibc, + ], os_name = LINUX_NAME, - # Matches the value in @platforms//cpu package arch = "x86_64", ), - "x86_64-unknown-linux-musl": struct( + "x86_64-unknown-linux-musl": platform_info( compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", ], - flag_values = { - libc: "musl", - }, + target_settings = [ + is_libc_musl, + ], os_name = LINUX_NAME, arch = "x86_64", ), } - freethreaded = Label("//python/config_settings:py_freethreaded") + is_freethreaded_yes = str(Label("//python/config_settings:_is_py_freethreaded_yes")) + is_freethreaded_no = str(Label("//python/config_settings:_is_py_freethreaded_no")) return { - p + suffix: struct( + p + suffix: platform_info( compatible_with = v.compatible_with, - flag_values = { - freethreaded: freethreaded_value, - } | v.flag_values, + target_settings = [ + freethreadedness, + ] + v.target_settings, os_name = v.os_name, arch = v.arch, ) for p, v in platforms.items() - for suffix, freethreaded_value in { - "": "no", - "-" + FREETHREADED: "yes", + for suffix, freethreadedness in { + "": is_freethreaded_no, + FREETHREADED: is_freethreaded_yes, }.items() } @@ -860,11 +853,11 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U release_filename = None rendered_urls = [] for u in url: - p, _, _ = platform.partition("-" + FREETHREADED) + p, _, _ = platform.partition(FREETHREADED) - if FREETHREADED in platform: + if FREETHREADED.lstrip("-") in platform: build = "{}+{}-full".format( - FREETHREADED, + FREETHREADED.lstrip("-"), { "aarch64-apple-darwin": "pgo+lto", "aarch64-unknown-linux-gnu": "lto", diff --git a/sphinxdocs/docs/index.md b/sphinxdocs/docs/index.md index bd6448ced9..2ea1146e1b 100644 --- a/sphinxdocs/docs/index.md +++ b/sphinxdocs/docs/index.md @@ -11,6 +11,29 @@ documentation. It comes with: While it is primarily oriented towards docgen for Starlark code, the core of it is agnostic as to what is being documented. +### Optimization + +Normally, Sphinx keeps various cache files to improve incremental building. +Unfortunately, programs performing their own caching don't interact well +with Bazel's model of precisely declaring and strictly enforcing what are +inputs, what are outputs, and what files are available when running a program. +The net effect is programs don't have a prior invocation's cache files +available. + +There are two mechanisms available to make some cache available to Sphinx under +Bazel: + +* Disable sandboxing, which allows some files from prior invocations to be + visible to subsequent invocations. This can be done multiple ways: + * Set `tags = ["no-sandbox"]` on the `sphinx_docs` target + * `--modify_execution_info=SphinxBuildDocs=+no-sandbox` (Bazel flag) + * `--strategy=SphinxBuildDocs=local` (Bazel flag) +* Use persistent workers (enabled by default) by setting + `allow_persistent_workers=True` on the `sphinx_docs` target. Note that other + Bazel flags can disable using workers even if an action supports it. Setting + `--strategy=SphinxBuildDocs=dynamic,worker,local,sandbox` should tell Bazel + to use workers if possible, otherwise fallback to non-worker invocations. + ```{toctree} :hidden: diff --git a/sphinxdocs/inventories/bazel_inventory.txt b/sphinxdocs/inventories/bazel_inventory.txt index 458126a849..e14ea76067 100644 --- a/sphinxdocs/inventories/bazel_inventory.txt +++ b/sphinxdocs/inventories/bazel_inventory.txt @@ -3,8 +3,10 @@ # Version: 7.3.0 # The remainder of this file is compressed using zlib Action bzl:type 1 rules/lib/Action - +Attribute bzl:type 1 rules/lib/builtins/Attribute - CcInfo bzl:provider 1 rules/lib/providers/CcInfo - CcInfo.linking_context bzl:provider-field 1 rules/lib/providers/CcInfo#linking_context - +DefaultInfo bzl:type 1 rules/lib/providers/DefaultInfo - ExecutionInfo bzl:type 1 rules/lib/providers/ExecutionInfo - File bzl:type 1 rules/lib/File - Label bzl:type 1 rules/lib/Label - @@ -13,6 +15,7 @@ RBE bzl:obj 1 remote/rbe - RunEnvironmentInfo bzl:type 1 rules/lib/providers/RunEnvironmentInfo - Target bzl:type 1 rules/lib/builtins/Target - ToolchainInfo bzl:type 1 rules/lib/providers/ToolchainInfo.html - +alias bzl:rule 1 reference/be/general#alias - attr.bool bzl:type 1 rules/lib/toplevel/attr#bool - attr.int bzl:type 1 rules/lib/toplevel/attr#int - attr.int_list bzl:type 1 rules/lib/toplevel/attr#int_list - @@ -38,6 +41,8 @@ config.string_list bzl:function 1 rules/lib/toplevel/config#string_list - config.target bzl:function 1 rules/lib/toplevel/config#target - config_common.FeatureFlagInfo bzl:type 1 rules/lib/toplevel/config_common#FeatureFlagInfo - config_common.toolchain_type bzl:function 1 rules/lib/toplevel/config_common#toolchain_type - +config_setting bzl:rule 1 reference/be/general#config_setting - +ctx bzl:type 1 rules/lib/builtins/repository_ctx - ctx.actions bzl:obj 1 rules/lib/builtins/ctx#actions - ctx.aspect_ids bzl:obj 1 rules/lib/builtins/ctx#aspect_ids - ctx.attr bzl:obj 1 rules/lib/builtins/ctx#attr - @@ -76,6 +81,8 @@ depset bzl:type 1 rules/lib/depset - dict bzl:type 1 rules/lib/dict - exec_compatible_with bzl:attr 1 reference/be/common-definitions#common.exec_compatible_with - exec_group bzl:function 1 rules/lib/globals/bzl#exec_group - +filegroup bzl:rule 1 reference/be/general#filegroup - +filegroup.data bzl:attr 1 reference/be/general#filegroup.data - int bzl:type 1 rules/lib/int - label bzl:type 1 concepts/labels - list bzl:type 1 rules/lib/list - @@ -96,6 +103,7 @@ module_ctx.report_progress bzl:function 1 rules/lib/builtins/module_ctx#report_p module_ctx.root_module_has_non_dev_dependency bzl:function 1 rules/lib/builtins/module_ctx#root_module_has_non_dev_dependency - module_ctx.watch bzl:function 1 rules/lib/builtins/module_ctx#watch - module_ctx.which bzl:function 1 rules/lib/builtins/module_ctx#which - +native bzl:obj 1 rules/lib/toplevel/native - native.existing_rule bzl:function 1 rules/lib/toplevel/native#existing_rule - native.existing_rules bzl:function 1 rules/lib/toplevel/native#existing_rules - native.exports_files bzl:function 1 rules/lib/toplevel/native#exports_files - @@ -140,6 +148,8 @@ repository_os bzl:type 1 rules/lib/builtins/repository_os - repository_os.arch bzl:obj 1 rules/lib/builtins/repository_os#arch repository_os.environ bzl:obj 1 rules/lib/builtins/repository_os#environ repository_os.name bzl:obj 1 rules/lib/builtins/repository_os#name +rule bzl:type 1 rules/lib/builtins/rule - +rule bzl:function rules/lib/globals/bzl.html#rule - runfiles bzl:type 1 rules/lib/builtins/runfiles - runfiles.empty_filenames bzl:type 1 rules/lib/builtins/runfiles#empty_filenames - runfiles.files bzl:type 1 rules/lib/builtins/runfiles#files - @@ -156,6 +166,8 @@ testing.TestEnvironment bzl:function 1 rules/lib/toplevel/testing#TestEnvironmen testing.analysis_test bzl:rule 1 rules/lib/toplevel/testing#analysis_test - toolchain bzl:rule 1 reference/be/platforms-and-toolchains#toolchain - toolchain.exec_compatible_with bzl:rule 1 reference/be/platforms-and-toolchains#toolchain.exec_compatible_with - -toolchain.target_settings bzl:attr 1 reference/be/platforms-and-toolchains#toolchain.target_settings - toolchain.target_compatible_with bzl:attr 1 reference/be/platforms-and-toolchains#toolchain.target_compatible_with - +toolchain.target_settings bzl:attr 1 reference/be/platforms-and-toolchains#toolchain.target_settings - toolchain_type bzl:type 1 rules/lib/builtins/toolchain_type.html - +transition bzl:type 1 rules/lib/builtins/transition - +tuple bzl:type 1 rules/lib/core/tuple - diff --git a/sphinxdocs/private/proto_to_markdown.py b/sphinxdocs/private/proto_to_markdown.py index 9dac71d51c..58fb79393d 100644 --- a/sphinxdocs/private/proto_to_markdown.py +++ b/sphinxdocs/private/proto_to_markdown.py @@ -216,7 +216,9 @@ def _render_repository_rule(self, repo_rule: stardoc_output_pb2.RepositoryRuleIn self._render_attributes(repo_rule.attribute) if repo_rule.environ: self._write(":envvars: ", ", ".join(sorted(repo_rule.environ))) - self._write("\n") + self._write("\n\n") + + self._write("::::::\n") def _render_rule(self, rule: stardoc_output_pb2.RuleInfo): rule_name = rule.rule_name diff --git a/sphinxdocs/private/sphinx.bzl b/sphinxdocs/private/sphinx.bzl index 8d19d87052..ee6b994e2e 100644 --- a/sphinxdocs/private/sphinx.bzl +++ b/sphinxdocs/private/sphinx.bzl @@ -103,6 +103,7 @@ def sphinx_docs( strip_prefix = "", extra_opts = [], tools = [], + allow_persistent_workers = True, **kwargs): """Generate docs using Sphinx. @@ -142,6 +143,9 @@ def sphinx_docs( tools: {type}`list[label]` Additional tools that are used by Sphinx and its plugins. This just makes the tools available during Sphinx execution. To locate them, use {obj}`extra_opts` and `$(location)`. + allow_persistent_workers: {type}`bool` (experimental) If true, allow + using persistent workers for running Sphinx, if Bazel decides to do so. + This can improve incremental building of docs. **kwargs: {type}`dict` Common attributes to pass onto rules. """ add_tag(kwargs, "@rules_python//sphinxdocs:sphinx_docs") @@ -165,6 +169,7 @@ def sphinx_docs( source_tree = internal_name + "/_sources", extra_opts = extra_opts, tools = tools, + allow_persistent_workers = allow_persistent_workers, **kwargs ) @@ -209,6 +214,7 @@ def _sphinx_docs_impl(ctx): source_path = source_dir_path, output_prefix = paths.join(ctx.label.name, "_build"), inputs = inputs, + allow_persistent_workers = ctx.attr.allow_persistent_workers, ) outputs[format] = output_dir per_format_args[format] = args_env @@ -229,6 +235,10 @@ def _sphinx_docs_impl(ctx): _sphinx_docs = rule( implementation = _sphinx_docs_impl, attrs = { + "allow_persistent_workers": attr.bool( + doc = "(experimental) Whether to invoke Sphinx as a persistent worker.", + default = False, + ), "extra_opts": attr.string_list( doc = "Additional options to pass onto Sphinx. These are added after " + "other options, but before the source/output args.", @@ -254,16 +264,27 @@ _sphinx_docs = rule( }, ) -def _run_sphinx(ctx, format, source_path, inputs, output_prefix): +def _run_sphinx(ctx, format, source_path, inputs, output_prefix, allow_persistent_workers): output_dir = ctx.actions.declare_directory(paths.join(output_prefix, format)) run_args = [] # Copy of the args to forward along to debug runner args = ctx.actions.args() # Args passed to the action + # An args file is required for persistent workers, but we don't know if + # the action will use worker mode or not (settings we can't see may + # force non-worker mode). For consistency, always use a params file. + args.use_param_file("@%s", use_always = True) + args.set_param_file_format("multiline") + + # NOTE: sphinx_build.py relies on the first two args being the srcdir and + # outputdir, in that order. + args.add(source_path) + args.add(output_dir.path) + args.add("--show-traceback") # Full tracebacks on error run_args.append("--show-traceback") - args.add("--builder", format) - run_args.extend(("--builder", format)) + args.add(format, format = "--builder=%s") + run_args.append("--builder={}".format(format)) if ctx.attr._quiet_flag[BuildSettingInfo].value: # Not added to run_args because run_args is for debugging @@ -271,11 +292,17 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix): # Build in parallel, if possible # Don't add to run_args: parallel building breaks interactive debugging - args.add("--jobs", "auto") - args.add("--fresh-env") # Don't try to use cache files. Bazel can't make use of them. - run_args.append("--fresh-env") - args.add("--write-all") # Write all files; don't try to detect "changed" files - run_args.append("--write-all") + args.add("--jobs=auto") + + # Put the doctree dir outside of the output directory. + # This allows it to be reused between invocations when possible; Bazel + # clears the output directory every action invocation. + # * For workers, they can fully re-use it. + # * For non-workers, it can be reused when sandboxing is disabled via + # the `no-sandbox` tag or execution requirement. + # + # We also use a non-dot prefixed name so it shows up more visibly. + args.add(paths.join(output_dir.path + "_doctrees"), format = "--doctree-dir=%s") for opt in ctx.attr.extra_opts: expanded = ctx.expand_location(opt) @@ -287,9 +314,6 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix): for define in extra_defines: run_args.extend(("--define", define)) - args.add(source_path) - args.add(output_dir.path) - env = dict([ v.split("=", 1) for v in ctx.attr._extra_env_flag[_FlagInfo].value @@ -299,6 +323,14 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix): for tool in ctx.attr.tools: tools.append(tool[DefaultInfo].files_to_run) + # NOTE: Command line flags or RBE capabilities may override the execution + # requirements and disable workers. Thus, we can't assume that these + # exec requirements will actually be respected. + execution_requirements = {} + if allow_persistent_workers: + execution_requirements["supports-workers"] = "1" + execution_requirements["requires-worker-protocol"] = "json" + ctx.actions.run( executable = ctx.executable.sphinx, arguments = [args], @@ -308,6 +340,7 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix): mnemonic = "SphinxBuildDocs", progress_message = "Sphinx building {} for %{{label}}".format(format), env = env, + execution_requirements = execution_requirements, ) return output_dir, struct(args = run_args, env = env) diff --git a/sphinxdocs/private/sphinx_build.py b/sphinxdocs/private/sphinx_build.py index 3b7b32eaf6..e9711042f6 100644 --- a/sphinxdocs/private/sphinx_build.py +++ b/sphinxdocs/private/sphinx_build.py @@ -1,8 +1,235 @@ +import contextlib +import io +import json +import logging import os -import pathlib +import shutil import sys +import traceback +import typing +import sphinx.application from sphinx.cmd.build import main +WorkRequest = object +WorkResponse = object + +logger = logging.getLogger("sphinxdocs_build") + +_WORKER_SPHINX_EXT_MODULE_NAME = "bazel_worker_sphinx_ext" + +# Config value name for getting the path to the request info file +_REQUEST_INFO_CONFIG_NAME = "bazel_worker_request_info_path" + + +class Worker: + + def __init__( + self, instream: "typing.TextIO", outstream: "typing.TextIO", exec_root: str + ): + # NOTE: Sphinx performs its own logging re-configuration, so any + # logging config we do isn't respected by Sphinx. Controlling where + # stdout and stderr goes are the main mechanisms. Recall that + # Bazel send worker stderr to the worker log file. + # outputBase=$(bazel info output_base) + # find $outputBase/bazel-workers/ -type f -printf '%T@ %p\n' | sort -n | tail -1 | awk '{print $2}' + logging.basicConfig(level=logging.WARN) + logger.info("Initializing worker") + + # The directory that paths are relative to. + self._exec_root = exec_root + # Where requests are read from. + self._instream = instream + # Where responses are written to. + self._outstream = outstream + + # dict[str srcdir, dict[str path, str digest]] + self._digests = {} + + # Internal output directories the worker gives to Sphinx that need + # to be cleaned up upon exit. + # set[str path] + self._worker_outdirs = set() + self._extension = BazelWorkerExtension() + + sys.modules[_WORKER_SPHINX_EXT_MODULE_NAME] = self._extension + sphinx.application.builtin_extensions += (_WORKER_SPHINX_EXT_MODULE_NAME,) + + def __enter__(self): + return self + + def __exit__(self): + for worker_outdir in self._worker_outdirs: + shutil.rmtree(worker_outdir, ignore_errors=True) + + def run(self) -> None: + logger.info("Worker started") + try: + while True: + request = None + try: + request = self._get_next_request() + if request is None: + logger.info("Empty request: exiting") + break + response = self._process_request(request) + if response: + self._send_response(response) + except Exception: + logger.exception("Unhandled error: request=%s", request) + output = ( + f"Unhandled error:\nRequest id: {request.get('id')}\n" + + traceback.format_exc() + ) + request_id = 0 if not request else request.get("requestId", 0) + self._send_response( + { + "exitCode": 3, + "output": output, + "requestId": request_id, + } + ) + finally: + logger.info("Worker shutting down") + + def _get_next_request(self) -> "object | None": + line = self._instream.readline() + if not line: + return None + return json.loads(line) + + def _send_response(self, response: "WorkResponse") -> None: + self._outstream.write(json.dumps(response) + "\n") + self._outstream.flush() + + def _prepare_sphinx(self, request): + sphinx_args = request["arguments"] + srcdir = sphinx_args[0] + + incoming_digests = {} + current_digests = self._digests.setdefault(srcdir, {}) + changed_paths = [] + request_info = {"exec_root": self._exec_root, "inputs": request["inputs"]} + for entry in request["inputs"]: + path = entry["path"] + digest = entry["digest"] + # Make the path srcdir-relative so Sphinx understands it. + path = path.removeprefix(srcdir + "/") + incoming_digests[path] = digest + + if path not in current_digests: + logger.info("path %s new", path) + changed_paths.append(path) + elif current_digests[path] != digest: + logger.info("path %s changed", path) + changed_paths.append(path) + + self._digests[srcdir] = incoming_digests + self._extension.changed_paths = changed_paths + request_info["changed_sources"] = changed_paths + + bazel_outdir = sphinx_args[1] + worker_outdir = bazel_outdir + ".worker-out.d" + self._worker_outdirs.add(worker_outdir) + sphinx_args[1] = worker_outdir + + request_info_path = os.path.join(srcdir, "_bazel_worker_request_info.json") + with open(request_info_path, "w") as fp: + json.dump(request_info, fp) + sphinx_args.append(f"--define={_REQUEST_INFO_CONFIG_NAME}={request_info_path}") + + return worker_outdir, bazel_outdir, sphinx_args + + @contextlib.contextmanager + def _redirect_streams(self): + out = io.StringIO() + orig_stdout = sys.stdout + try: + sys.stdout = out + yield out + finally: + sys.stdout = orig_stdout + + def _process_request(self, request: "WorkRequest") -> "WorkResponse | None": + logger.info("Request: %s", json.dumps(request, sort_keys=True, indent=2)) + if request.get("cancel"): + return None + + worker_outdir, bazel_outdir, sphinx_args = self._prepare_sphinx(request) + + # Prevent anything from going to stdout because it breaks the worker + # protocol. We have limited control over where Sphinx sends output. + with self._redirect_streams() as stdout: + logger.info("main args: %s", sphinx_args) + exit_code = main(sphinx_args) + + if exit_code: + raise Exception( + "Sphinx main() returned failure: " + + f" exit code: {exit_code}\n" + + "========== STDOUT START ==========\n" + + stdout.getvalue().rstrip("\n") + + "\n" + + "========== STDOUT END ==========\n" + ) + + # Copying is unfortunately necessary because Bazel doesn't know to + # implicily bring along what the symlinks point to. + shutil.copytree(worker_outdir, bazel_outdir, dirs_exist_ok=True) + + response = { + "requestId": request.get("requestId", 0), + "output": stdout.getvalue(), + "exitCode": 0, + } + return response + + +class BazelWorkerExtension: + """A Sphinx extension implemented as a class acting like a module.""" + + def __init__(self): + # Make it look like a Module object + self.__name__ = _WORKER_SPHINX_EXT_MODULE_NAME + # set[str] of src-dir relative path names + self.changed_paths = set() + + def setup(self, app): + app.add_config_value(_REQUEST_INFO_CONFIG_NAME, "", "") + app.connect("env-get-outdated", self._handle_env_get_outdated) + return {"parallel_read_safe": True, "parallel_write_safe": True} + + def _handle_env_get_outdated(self, app, env, added, changed, removed): + changed = { + # NOTE: path2doc returns None if it's not a doc path + env.path2doc(p) + for p in self.changed_paths + } + + logger.info("changed docs: %s", changed) + return changed + + +def _worker_main(stdin, stdout, exec_root): + with Worker(stdin, stdout, exec_root) as worker: + return worker.run() + + +def _non_worker_main(): + args = [] + for arg in sys.argv: + if arg.startswith("@"): + with open(arg.removeprefix("@")) as fp: + lines = [line.strip() for line in fp if line.strip()] + args.extend(lines) + else: + args.append(arg) + sys.argv[:] = args + return main() + + if __name__ == "__main__": - sys.exit(main()) + if "--persistent_worker" in sys.argv: + sys.exit(_worker_main(sys.stdin, sys.stdout, os.getcwd())) + else: + sys.exit(_non_worker_main()) diff --git a/sphinxdocs/src/sphinx_bzl/bzl.py b/sphinxdocs/src/sphinx_bzl/bzl.py index 90fb109614..8303b4d2a5 100644 --- a/sphinxdocs/src/sphinx_bzl/bzl.py +++ b/sphinxdocs/src/sphinx_bzl/bzl.py @@ -390,8 +390,12 @@ def _make_xrefs_for_arg_attr( descr=index_description, ), ), - # This allows referencing an arg as e.g `funcname.argname` - alt_names=[anchor_id], + alt_names=[ + # This allows referencing an arg as e.g `funcname.argname` + anchor_id, + # This allows referencing an arg as simply `argname` + arg_name, + ], ) # Two changes to how arg xrefs are created: @@ -498,7 +502,55 @@ def run(self) -> list[docutils_nodes.Node]: self.env.ref_context["bzl:file"] = file_label self.env.ref_context["bzl:object_id_stack"] = [] self.env.ref_context["bzl:doc_id_stack"] = [] - return [] + + package_label, _, basename = file_label.partition(":") + + # Transform //foo/bar:BUILD.bazel into "bar" + # This allows referencing "bar" as itself + extra_alt_names = [] + if basename in ("BUILD.bazel", "BUILD"): + # Allow xref //foo + extra_alt_names.append(package_label) + basename = os.path.basename(package_label) + # Handle //:BUILD.bazel + if not basename: + # There isn't a convention for referring to the root package + # besides `//:`, which is already the file_label. So just + # use some obvious value + basename = "__ROOT_BAZEL_PACKAGE__" + + index_description = f"File {label}" + absolute_label = repo + label + self.env.get_domain("bzl").add_object( + _ObjectEntry( + full_id=absolute_label, + display_name=absolute_label, + object_type="obj", + search_priority=1, + index_entry=domains.IndexEntry( + name=basename, + subtype=_INDEX_SUBTYPE_NORMAL, + docname=self.env.docname, + anchor="", + extra="", + qualifier="", + descr=index_description, + ), + ), + alt_names=[ + # Allow xref //foo:bar.bzl + file_label, + # Allow xref bar.bzl + basename, + ] + + extra_alt_names, + ) + index_node = addnodes.index( + entries=[ + _index_node_tuple("single", f"File; {label}", ""), + ] + ) + return [index_node] class _BzlAttrInfo(sphinx_docutils.SphinxDirective): @@ -1156,10 +1208,10 @@ class _BzlTagClass(_BzlCallable): doc_field_types = [ _BzlGroupedField( - "arg", + "attr", label=_("Attributes"), names=["attr"], - rolename="arg", + rolename="attr", can_collapse=False, ), ] @@ -1463,6 +1515,8 @@ class _BzlDomain(domains.Domain): # :obj:. # NOTE: We also use these object types for categorizing things in the # generated index page. + # NOTE: The object type keys control what object types are recognized + # in inventory files. object_types = { "arg": domains.ObjType("arg", "arg", "obj"), # macro/function arg "aspect": domains.ObjType("aspect", "aspect", "obj"), @@ -1486,6 +1540,8 @@ class _BzlDomain(domains.Domain): # types are objects that have a constructor and methods/attrs "type": domains.ObjType("type", "type", "obj"), "typedef": domains.ObjType("typedef", "typedef", "type", "obj"), + # generic objs usually come from inventories + "obj": domains.ObjType("object", "obj"), } # This controls: @@ -1710,6 +1766,11 @@ def _on_missing_reference(app, env: environment.BuildEnvironment, node, contnode # There's no Bazel docs for None, so prevent missing xrefs warning if node["reftarget"] == "None": return contnode + + # Any and object are just conventions from Python, but useful for + # indicating what something is in Starlark, so treat them specially. + if node["reftarget"] in ("Any", "object"): + return contnode return None diff --git a/sphinxdocs/tests/proto_to_markdown/proto_to_markdown_test.py b/sphinxdocs/tests/proto_to_markdown/proto_to_markdown_test.py index 9d15b830e3..da6edb21d4 100644 --- a/sphinxdocs/tests/proto_to_markdown/proto_to_markdown_test.py +++ b/sphinxdocs/tests/proto_to_markdown/proto_to_markdown_test.py @@ -272,6 +272,41 @@ def test_render_module_extension(self): ::::: +:::::: +""" + self.assertIn(expected, actual) + + def test_render_repo_rule(self): + proto_text = """ +file: "@repo//pkg:foo.bzl" +repository_rule_info: { + rule_name: "repository_rule", + doc_string: "REPOSITORY_RULE_DOC_STRING" + attribute: { + name: "repository_rule_attribute_a", + doc_string: "REPOSITORY_RULE_ATTRIBUTE_A_DOC_STRING" + type: BOOLEAN + default_value: "True" + } + environ: "ENV_VAR_A" +} +""" + actual = self._render(proto_text) + expected = """ +::::::{bzl:repo-rule} repository_rule(repository_rule_attribute_a=True) + +REPOSITORY_RULE_DOC_STRING + +:attr repository_rule_attribute_a: + {bzl:default-value}`True` + {type}`bool` + REPOSITORY_RULE_ATTRIBUTE_A_DOC_STRING + :::{bzl:attr-info} Info + ::: + + +:envvars: ENV_VAR_A + :::::: """ self.assertIn(expected, actual) diff --git a/sphinxdocs/tests/sphinx_docs/doc1.md b/sphinxdocs/tests/sphinx_docs/doc1.md new file mode 100644 index 0000000000..f6f70ba28c --- /dev/null +++ b/sphinxdocs/tests/sphinx_docs/doc1.md @@ -0,0 +1,3 @@ +# doc1 + +hello doc 1 diff --git a/sphinxdocs/tests/sphinx_docs/doc2.md b/sphinxdocs/tests/sphinx_docs/doc2.md new file mode 100644 index 0000000000..06eb76a596 --- /dev/null +++ b/sphinxdocs/tests/sphinx_docs/doc2.md @@ -0,0 +1,3 @@ +# doc 2 + +hello doc 3 diff --git a/sphinxdocs/tests/sphinx_stardoc/sphinx_output_test.py b/sphinxdocs/tests/sphinx_stardoc/sphinx_output_test.py index 6d65c920e1..c78089ac14 100644 --- a/sphinxdocs/tests/sphinx_stardoc/sphinx_output_test.py +++ b/sphinxdocs/tests/sphinx_stardoc/sphinx_output_test.py @@ -63,6 +63,12 @@ def _doc_element(self, doc): ("full_repo_provider", "@testrepo//lang:provider.bzl%LangInfo", "provider.html#LangInfo"), ("full_repo_aspect", "@testrepo//lang:aspect.bzl%myaspect", "aspect.html#myaspect"), ("full_repo_target", "@testrepo//lang:relativetarget", "target.html#relativetarget"), + ("tag_class_attr_using_attr_role", "myext.mytag.ta1", "module_extension.html#myext.mytag.ta1"), + ("tag_class_attr_using_attr_role_just_attr_name", "ta1", "module_extension.html#myext.mytag.ta1"), + ("file_without_repo", "//lang:rule.bzl", "rule.html"), + ("file_with_repo", "@testrepo//lang:rule.bzl", "rule.html"), + ("package_absolute", "//lang", "target.html"), + ("package_basename", "lang", "target.html"), # fmt: on ) def test_xrefs(self, text, href): diff --git a/sphinxdocs/tests/sphinx_stardoc/xrefs.md b/sphinxdocs/tests/sphinx_stardoc/xrefs.md index 83f6869a48..bbd415ce19 100644 --- a/sphinxdocs/tests/sphinx_stardoc/xrefs.md +++ b/sphinxdocs/tests/sphinx_stardoc/xrefs.md @@ -41,3 +41,18 @@ Various tests of cross referencing support ## Any xref * {any}`LangInfo` + +## Tag class refs + +* tag class attribute using attr role: {attr}`myext.mytag.ta1` +* tag class attribute, just attr name, attr role: {attr}`ta1` + +## File refs + +* without repo {obj}`//lang:rule.bzl` +* with repo {obj}`@testrepo//lang:rule.bzl` + +## Package refs + +* absolute label {obj}`//lang` +* package basename {obj}`lang` diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index 37707831fc..49cbb1586c 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl @@ -51,6 +51,7 @@ def _test_basic_windows(name, config): "//command_line_option:build_python_zip": "true", "//command_line_option:cpu": "windows_x86_64", "//command_line_option:crosstool_top": CROSSTOOL_TOP, + "//command_line_option:extra_execution_platforms": [WINDOWS_X86_64], "//command_line_option:extra_toolchains": [CC_TOOLCHAIN], "//command_line_option:platforms": [WINDOWS_X86_64], }, @@ -96,6 +97,7 @@ def _test_basic_zip(name, config): "//command_line_option:build_python_zip": "true", "//command_line_option:cpu": "linux_x86_64", "//command_line_option:crosstool_top": CROSSTOOL_TOP, + "//command_line_option:extra_execution_platforms": [LINUX_X86_64], "//command_line_option:extra_toolchains": [CC_TOOLCHAIN], "//command_line_option:platforms": [LINUX_X86_64], }, @@ -354,6 +356,7 @@ def _test_main_module_bootstrap_system_python(name, config): target = name + "_subject", config_settings = { BOOTSTRAP_IMPL: "system_python", + "//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", LINUX_X86_64], "//command_line_option:platforms": [LINUX_X86_64], }, expect_failure = True, @@ -378,6 +381,7 @@ def _test_main_module_bootstrap_script(name, config): target = name + "_subject", config_settings = { BOOTSTRAP_IMPL: "script", + "//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", LINUX_X86_64], "//command_line_option:platforms": [LINUX_X86_64], }, ) diff --git a/tests/base_rules/py_info/py_info_tests.bzl b/tests/base_rules/py_info/py_info_tests.bzl index e160e704de..aa252a2937 100644 --- a/tests/base_rules/py_info/py_info_tests.bzl +++ b/tests/base_rules/py_info/py_info_tests.bzl @@ -162,7 +162,7 @@ def _test_py_info_builder_impl(env, targets): direct_pyi, trans_pyi, ) = targets.misc[DefaultInfo].files.to_list() - builder = PyInfoBuilder() + builder = PyInfoBuilder.new() builder.direct_pyc_files.add(direct_pyc) builder.direct_original_sources.add(original_py) builder.direct_pyi_files.add(direct_pyi) diff --git a/tests/base_rules/py_test/py_test_tests.bzl b/tests/base_rules/py_test/py_test_tests.bzl index d4d839b392..c51aa53a95 100644 --- a/tests/base_rules/py_test/py_test_tests.bzl +++ b/tests/base_rules/py_test/py_test_tests.bzl @@ -59,6 +59,7 @@ def _test_mac_requires_darwin_for_execution(name, config): config_settings = { "//command_line_option:cpu": "darwin_x86_64", "//command_line_option:crosstool_top": CROSSTOOL_TOP, + "//command_line_option:extra_execution_platforms": [MAC_X86_64], "//command_line_option:extra_toolchains": CC_TOOLCHAIN, "//command_line_option:platforms": [MAC_X86_64], }, @@ -92,6 +93,7 @@ def _test_non_mac_doesnt_require_darwin_for_execution(name, config): config_settings = { "//command_line_option:cpu": "k8", "//command_line_option:crosstool_top": CROSSTOOL_TOP, + "//command_line_option:extra_execution_platforms": [LINUX_X86_64], "//command_line_option:extra_toolchains": CC_TOOLCHAIN, "//command_line_option:platforms": [LINUX_X86_64], }, diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index 28a0d21fb7..c3d44df240 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -1,5 +1,3 @@ -load("@rules_shell//shell:sh_test.bzl", "sh_test") - # Copyright 2023 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +11,10 @@ load("@rules_shell//shell:sh_test.bzl", "sh_test") # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_binary", "py_reconfig_test", "sh_py_run_test") +load("@rules_pkg//pkg:tar.bzl", "pkg_tar") +load("@rules_shell//shell:sh_test.bzl", "sh_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_binary", "py_reconfig_test") +load("//tests/support:sh_py_run_test.bzl", "sh_py_run_test") load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT") load(":venv_relative_path_tests.bzl", "relative_path_test_suite") @@ -157,4 +158,24 @@ py_reconfig_test( target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT, ) +pkg_tar( + name = "external_binary", + testonly = True, + srcs = ["@other//:external_main"], + include_runfiles = True, + tags = ["manual"], # Don't build as part of wildcards +) + +sh_test( + name = "external_binary_test", + srcs = ["external_binary_test.sh"], + data = [":external_binary"], + # For now, skip this test on Windows because it fails for reasons + # other than the code path being tested. + target_compatible_with = select({ + "@platforms//os:windows": ["@platforms//:incompatible"], + "//conditions:default": [], + }), +) + relative_path_test_suite(name = "relative_path_tests") diff --git a/tests/bootstrap_impls/a/b/c/BUILD.bazel b/tests/bootstrap_impls/a/b/c/BUILD.bazel index 8ffcbcd479..1659ef25bc 100644 --- a/tests/bootstrap_impls/a/b/c/BUILD.bazel +++ b/tests/bootstrap_impls/a/b/c/BUILD.bazel @@ -1,5 +1,5 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") _SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], diff --git a/tests/bootstrap_impls/bin.py b/tests/bootstrap_impls/bin.py index 1176107384..3d467dcf29 100644 --- a/tests/bootstrap_impls/bin.py +++ b/tests/bootstrap_impls/bin.py @@ -23,3 +23,4 @@ print("sys.flags.safe_path:", sys.flags.safe_path) print("file:", __file__) print("sys.executable:", sys.executable) +print("sys._base_executable:", sys._base_executable) diff --git a/tests/bootstrap_impls/external_binary_test.sh b/tests/bootstrap_impls/external_binary_test.sh new file mode 100755 index 0000000000..e3516af18e --- /dev/null +++ b/tests/bootstrap_impls/external_binary_test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -euxo pipefail + +tmpdir="${TEST_TMPDIR}/external_binary" +mkdir -p "${tmpdir}" +tar xf "tests/bootstrap_impls/external_binary.tar" -C "${tmpdir}" +test -x "${tmpdir}/external_main" +output="$("${tmpdir}/external_main")" +test "$output" = "token" diff --git a/tests/entry_points/py_console_script_gen_test.py b/tests/entry_points/py_console_script_gen_test.py index a5fceb67f9..1bbf5fbf25 100644 --- a/tests/entry_points/py_console_script_gen_test.py +++ b/tests/entry_points/py_console_script_gen_test.py @@ -47,6 +47,7 @@ def test_no_console_scripts_error(self): out=outfile, console_script=None, console_script_guess="", + shebang="", ) self.assertEqual( @@ -76,6 +77,7 @@ def test_no_entry_point_selected_error(self): out=outfile, console_script=None, console_script_guess="bar-baz", + shebang="", ) self.assertEqual( @@ -106,6 +108,7 @@ def test_incorrect_entry_point(self): out=outfile, console_script="baz", console_script_guess="", + shebang="", ) self.assertEqual( @@ -134,6 +137,7 @@ def test_a_single_entry_point(self): out=out, console_script=None, console_script_guess="foo", + shebang="", ) got = out.read_text() @@ -185,6 +189,7 @@ def test_a_second_entry_point_class_method(self): out=out, console_script="bar", console_script_guess="", + shebang="", ) got = out.read_text() @@ -192,6 +197,35 @@ def test_a_second_entry_point_class_method(self): self.assertRegex(got, "from foo\.baz import Bar") self.assertRegex(got, "sys\.exit\(Bar\.baz\(\)\)") + def test_shebang_included(self): + with tempfile.TemporaryDirectory() as tmpdir: + tmpdir = pathlib.Path(tmpdir) + given_contents = ( + textwrap.dedent( + """ + [console_scripts] + foo = foo.bar:baz + """ + ).strip() + + "\n" + ) + entry_points = tmpdir / "entry_points.txt" + entry_points.write_text(given_contents) + out = tmpdir / "foo.py" + + shebang = "#!/usr/bin/env python3" + run( + entry_points=entry_points, + out=out, + console_script=None, + console_script_guess="foo", + shebang=shebang, + ) + + got = out.read_text() + + self.assertTrue(got.startswith(shebang + "\n")) + if __name__ == "__main__": unittest.main() diff --git a/tests/integration/local_toolchains/.bazelrc b/tests/integration/local_toolchains/.bazelrc index 39df41d9f4..aed08b0790 100644 --- a/tests/integration/local_toolchains/.bazelrc +++ b/tests/integration/local_toolchains/.bazelrc @@ -4,3 +4,5 @@ test --test_output=errors # Windows requires these for multi-python support: build --enable_runfiles common:bazel7.x --incompatible_python_disallow_native_rules +build --//:py=local +common --announce_rc diff --git a/tests/integration/local_toolchains/BUILD.bazel b/tests/integration/local_toolchains/BUILD.bazel index 6fbf548901..6b731181a6 100644 --- a/tests/integration/local_toolchains/BUILD.bazel +++ b/tests/integration/local_toolchains/BUILD.bazel @@ -12,9 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") load("@rules_python//python:py_test.bzl", "py_test") py_test( name = "test", srcs = ["test.py"], + # Make this test better respect pyenv + env_inherit = ["PYENV_VERSION"], +) + +config_setting( + name = "is_py_local", + flag_values = { + ":py": "local", + }, +) + +# Set `--//:py=local` to use the local toolchain +# (This is set in this example's .bazelrc) +string_flag( + name = "py", + build_setting_default = "", ) diff --git a/tests/integration/local_toolchains/MODULE.bazel b/tests/integration/local_toolchains/MODULE.bazel index 98f1ed9ac4..6c06909cd7 100644 --- a/tests/integration/local_toolchains/MODULE.bazel +++ b/tests/integration/local_toolchains/MODULE.bazel @@ -14,6 +14,9 @@ module(name = "module_under_test") bazel_dep(name = "rules_python", version = "0.0.0") +bazel_dep(name = "bazel_skylib", version = "1.7.1") +bazel_dep(name = "platforms", version = "0.0.11") + local_path_override( module_name = "rules_python", path = "../../..", @@ -32,6 +35,16 @@ local_runtime_repo( local_runtime_toolchains_repo( name = "local_toolchains", runtimes = ["local_python3"], + target_compatible_with = { + "local_python3": [ + "HOST_CONSTRAINTS", + ], + }, + target_settings = { + "local_python3": [ + "@//:is_py_local", + ], + }, ) python = use_extension("@rules_python//python/extensions:python.bzl", "python") diff --git a/tests/integration/local_toolchains/test.py b/tests/integration/local_toolchains/test.py index d85a4c386b..8e37fff652 100644 --- a/tests/integration/local_toolchains/test.py +++ b/tests/integration/local_toolchains/test.py @@ -1,6 +1,8 @@ +import os.path import shutil import subprocess import sys +import tempfile import unittest @@ -8,19 +10,58 @@ class LocalToolchainTest(unittest.TestCase): maxDiff = None def test_python_from_path_used(self): + # NOTE: This is a bit brittle. It assumes the environment during the + # repo-phase and when the test is run are roughly the same. It's + # easy to violate this condition if there are shell-local changes + # that wouldn't be reflected when sub-shells are run later. shell_path = shutil.which("python3") # We call the interpreter and print its executable because of # things like pyenv: they install a shim that re-execs python. # The shim is e.g. /home/user/.pyenv/shims/python3, which then # runs e.g. /usr/bin/python3 - expected = subprocess.check_output( - [shell_path, "-c", "import sys; print(sys.executable)"], - text=True, - ) - expected = expected.strip().lower() + with tempfile.NamedTemporaryFile(suffix="_info.py", mode="w+") as f: + f.write( + """ +import sys +print(sys.executable) +print(sys._base_executable) +""" + ) + f.flush() + output_lines = ( + subprocess.check_output( + [shell_path, f.name], + text=True, + ) + .strip() + .splitlines() + ) + shell_exe, shell_base_exe = output_lines + + # Call realpath() to help normalize away differences from symlinks. + # Use base executable to ignore a venv the test may be running within. + expected = os.path.realpath(shell_base_exe.strip().lower()) + actual = os.path.realpath(sys._base_executable.lower()) + + msg = f""" +details of executables: +test's runtime: +{sys.executable=} +{sys._base_executable=} +realpath exe : {os.path.realpath(sys.executable)} +realpath base_exe: {os.path.realpath(sys._base_executable)} + +from shell resolution: +which python3: {shell_path=}: +{shell_exe=} +{shell_base_exe=} +realpath exe : {os.path.realpath(shell_exe)} +realpath base_exe: {os.path.realpath(shell_base_exe)} +""".strip() + # Normalize case: Windows may have case differences - self.assertEqual(expected.lower(), sys.executable.lower()) + self.assertEqual(expected.lower(), actual.lower(), msg=msg) if __name__ == "__main__": diff --git a/tests/integration/runner.py b/tests/integration/runner.py index 9414a865c0..2534ab2d90 100644 --- a/tests/integration/runner.py +++ b/tests/integration/runner.py @@ -23,12 +23,15 @@ _logger = logging.getLogger(__name__) + class ExecuteError(Exception): def __init__(self, result): self.result = result + def __str__(self): return self.result.describe() + class ExecuteResult: def __init__( self, @@ -83,7 +86,7 @@ def setUp(self): "TMP": str(self.tmp_dir), # For some reason, this is necessary for Bazel 6.4 to work. # If not present, it can't find some bash helpers in @bazel_tools - "RUNFILES_DIR": os.environ["TEST_SRCDIR"] + "RUNFILES_DIR": os.environ["TEST_SRCDIR"], } def run_bazel(self, *args: str, check: bool = True) -> ExecuteResult: diff --git a/tests/interpreter/interpreter_tests.bzl b/tests/interpreter/interpreter_tests.bzl index ad94f43423..3c5882afa0 100644 --- a/tests/interpreter/interpreter_tests.bzl +++ b/tests/interpreter/interpreter_tests.bzl @@ -14,7 +14,7 @@ """This file contains helpers for testing the interpreter rule.""" -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") # The versions of Python that we want to run the interpreter tests against. PYTHON_VERSIONS_TO_TEST = ( diff --git a/tests/modules/other/BUILD.bazel b/tests/modules/other/BUILD.bazel index e69de29bb2..46f1b96faa 100644 --- a/tests/modules/other/BUILD.bazel +++ b/tests/modules/other/BUILD.bazel @@ -0,0 +1,14 @@ +load("@rules_python//tests/support:py_reconfig.bzl", "py_reconfig_binary") + +package( + default_visibility = ["//visibility:public"], +) + +py_reconfig_binary( + name = "external_main", + srcs = [":external_main.py"], + # We're testing a system_python specific code path, + # so force using that bootstrap + bootstrap_impl = "system_python", + main = "external_main.py", +) diff --git a/tests/modules/other/external_main.py b/tests/modules/other/external_main.py new file mode 100644 index 0000000000..f742ebab60 --- /dev/null +++ b/tests/modules/other/external_main.py @@ -0,0 +1 @@ +print("token") diff --git a/tests/modules/other/nspkg_single/BUILD.bazel b/tests/modules/other/nspkg_single/BUILD.bazel new file mode 100644 index 0000000000..08cb4f373e --- /dev/null +++ b/tests/modules/other/nspkg_single/BUILD.bazel @@ -0,0 +1,10 @@ +load("@rules_python//python:py_library.bzl", "py_library") + +package(default_visibility = ["//visibility:public"]) + +py_library( + name = "nspkg_single", + srcs = glob(["site-packages/**/*.py"]), + experimental_venvs_site_packages = "@rules_python//python/config_settings:venvs_site_packages", + imports = [package_name() + "/site-packages"], +) diff --git a/tests/modules/other/nspkg_single/site-packages/__init__.py b/tests/modules/other/nspkg_single/site-packages/__init__.py new file mode 100644 index 0000000000..bb26c87599 --- /dev/null +++ b/tests/modules/other/nspkg_single/site-packages/__init__.py @@ -0,0 +1 @@ +# empty, will not be added to the site-packages dir diff --git a/tests/modules/other/nspkg_single/site-packages/single_file.py b/tests/modules/other/nspkg_single/site-packages/single_file.py new file mode 100644 index 0000000000..f6d7dfd640 --- /dev/null +++ b/tests/modules/other/nspkg_single/site-packages/single_file.py @@ -0,0 +1,5 @@ +__all__ = [ + "SOMETHING", +] + +SOMETHING = "nothing" diff --git a/tests/no_unsafe_paths/BUILD.bazel b/tests/no_unsafe_paths/BUILD.bazel index f12d1c9a70..c9a681daa9 100644 --- a/tests/no_unsafe_paths/BUILD.bazel +++ b/tests/no_unsafe_paths/BUILD.bazel @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT") py_reconfig_test( diff --git a/tests/no_unsafe_paths/test.py b/tests/no_unsafe_paths/test.py index 893add2f62..4727a02995 100644 --- a/tests/no_unsafe_paths/test.py +++ b/tests/no_unsafe_paths/test.py @@ -40,5 +40,5 @@ def test_no_unsafe_paths_in_search_path(self): self.assertEqual(os.path.basename(sys.path[0]), archive) -if __name__ == '__main__': - unittest.main() \ No newline at end of file +if __name__ == "__main__": + unittest.main() diff --git a/tests/packaging/BUILD.bazel b/tests/packaging/BUILD.bazel index bb12269e3d..d88a593006 100644 --- a/tests/packaging/BUILD.bazel +++ b/tests/packaging/BUILD.bazel @@ -14,7 +14,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@rules_pkg//pkg:tar.bzl", "pkg_tar") -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT") build_test( diff --git a/tests/py_wheel/py_wheel_tests.bzl b/tests/py_wheel/py_wheel_tests.bzl index 091e01c37d..43c068e597 100644 --- a/tests/py_wheel/py_wheel_tests.bzl +++ b/tests/py_wheel/py_wheel_tests.bzl @@ -17,7 +17,6 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") load("@rules_testing//lib:truth.bzl", "matching") load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:packaging.bzl", "py_wheel") -load("//python/private:py_wheel_normalize_pep440.bzl", "normalize_pep440") # buildifier: disable=bzl-visibility _basic_tests = [] _tests = [] @@ -168,106 +167,6 @@ def _test_content_type_from_description_impl(env, target): _tests.append(_test_content_type_from_description) -def _test_pep440_normalization(env): - prefixes = ["v", " v", " \t\r\nv"] - epochs = { - "": ["", "0!", "00!"], - "1!": ["1!", "001!"], - "200!": ["200!", "00200!"], - } - releases = { - "0.1": ["0.1", "0.01"], - "2023.7.19": ["2023.7.19", "2023.07.19"], - } - pres = { - "": [""], - "a0": ["a", ".a", "-ALPHA0", "_alpha0", ".a0"], - "a4": ["alpha4", ".a04"], - "b0": ["b", ".b", "-BETA0", "_beta0", ".b0"], - "b5": ["beta05", ".b5"], - "rc0": ["C", "_c0", "RC", "_rc0", "-preview_0"], - } - explicit_posts = { - "": [""], - ".post0": [], - ".post1": [".post1", "-r1", "_rev1"], - } - implicit_posts = [[".post1", "-1"], [".post2", "-2"]] - devs = { - "": [""], - ".dev0": ["dev", "-DEV", "_Dev-0"], - ".dev9": ["DEV9", ".dev09", ".dev9"], - ".dev{BUILD_TIMESTAMP}": [ - "-DEV{BUILD_TIMESTAMP}", - "_dev_{BUILD_TIMESTAMP}", - ], - } - locals = { - "": [""], - "+ubuntu.7": ["+Ubuntu_7", "+ubuntu-007"], - "+ubuntu.r007": ["+Ubuntu_R007"], - } - epochs = [ - [normalized_epoch, input_epoch] - for normalized_epoch, input_epochs in epochs.items() - for input_epoch in input_epochs - ] - releases = [ - [normalized_release, input_release] - for normalized_release, input_releases in releases.items() - for input_release in input_releases - ] - pres = [ - [normalized_pre, input_pre] - for normalized_pre, input_pres in pres.items() - for input_pre in input_pres - ] - explicit_posts = [ - [normalized_post, input_post] - for normalized_post, input_posts in explicit_posts.items() - for input_post in input_posts - ] - pres_and_posts = [ - [normalized_pre + normalized_post, input_pre + input_post] - for normalized_pre, input_pre in pres - for normalized_post, input_post in explicit_posts - ] + [ - [normalized_pre + normalized_post, input_pre + input_post] - for normalized_pre, input_pre in pres - for normalized_post, input_post in implicit_posts - if input_pre == "" or input_pre[-1].isdigit() - ] - devs = [ - [normalized_dev, input_dev] - for normalized_dev, input_devs in devs.items() - for input_dev in input_devs - ] - locals = [ - [normalized_local, input_local] - for normalized_local, input_locals in locals.items() - for input_local in input_locals - ] - postfixes = ["", " ", " \t\r\n"] - i = 0 - for nepoch, iepoch in epochs: - for nrelease, irelease in releases: - for nprepost, iprepost in pres_and_posts: - for ndev, idev in devs: - for nlocal, ilocal in locals: - prefix = prefixes[i % len(prefixes)] - postfix = postfixes[(i // len(prefixes)) % len(postfixes)] - env.expect.that_str( - normalize_pep440( - prefix + iepoch + irelease + iprepost + - idev + ilocal + postfix, - ), - ).equals( - nepoch + nrelease + nprepost + ndev + nlocal, - ) - i += 1 - -_basic_tests.append(_test_pep440_normalization) - def py_wheel_test_suite(name): test_suite( name = name, diff --git a/tests/pycross/0001-Add-new-file-for-testing-patch-support.patch b/tests/pycross/0001-Add-new-file-for-testing-patch-support.patch deleted file mode 100644 index fcbc3096ef..0000000000 --- a/tests/pycross/0001-Add-new-file-for-testing-patch-support.patch +++ /dev/null @@ -1,17 +0,0 @@ -From b2ebe6fe67ff48edaf2ae937d24b1f0b67c16f81 Mon Sep 17 00:00:00 2001 -From: Philipp Schrader -Date: Thu, 28 Sep 2023 09:02:44 -0700 -Subject: [PATCH] Add new file for testing patch support - ---- - site-packages/numpy/file_added_via_patch.txt | 1 + - 1 file changed, 1 insertion(+) - create mode 100644 site-packages/numpy/file_added_via_patch.txt - -diff --git a/site-packages/numpy/file_added_via_patch.txt b/site-packages/numpy/file_added_via_patch.txt -new file mode 100644 -index 0000000..9d947a4 ---- /dev/null -+++ b/site-packages/numpy/file_added_via_patch.txt -@@ -0,0 +1 @@ -+Hello from a patch! diff --git a/tests/pycross/BUILD.bazel b/tests/pycross/BUILD.bazel deleted file mode 100644 index e90b60e17e..0000000000 --- a/tests/pycross/BUILD.bazel +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -load("//python:py_test.bzl", "py_test") -load("//third_party/rules_pycross/pycross/private:wheel_library.bzl", "py_wheel_library") # buildifier: disable=bzl-visibility - -py_wheel_library( - name = "extracted_wheel_for_testing", - wheel = "@wheel_for_testing//file", -) - -py_test( - name = "py_wheel_library_test", - srcs = [ - "py_wheel_library_test.py", - ], - data = [ - ":extracted_wheel_for_testing", - ], - deps = [ - "//python/runfiles", - ], -) - -py_wheel_library( - name = "patched_extracted_wheel_for_testing", - patch_args = [ - "-p1", - ], - patch_tool = "patch", - patches = [ - "0001-Add-new-file-for-testing-patch-support.patch", - ], - target_compatible_with = select({ - # We don't have `patch` available on the Windows CI machines. - "@platforms//os:windows": ["@platforms//:incompatible"], - "//conditions:default": [], - }), - wheel = "@wheel_for_testing//file", -) - -py_test( - name = "patched_py_wheel_library_test", - srcs = [ - "patched_py_wheel_library_test.py", - ], - data = [ - ":patched_extracted_wheel_for_testing", - ], - deps = [ - "//python/runfiles", - ], -) diff --git a/tests/pycross/patched_py_wheel_library_test.py b/tests/pycross/patched_py_wheel_library_test.py deleted file mode 100644 index e1b404a0ef..0000000000 --- a/tests/pycross/patched_py_wheel_library_test.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -from pathlib import Path - -from python.runfiles import runfiles - -RUNFILES = runfiles.Create() - - -class TestPyWheelLibrary(unittest.TestCase): - def setUp(self): - self.extraction_dir = Path( - RUNFILES.Rlocation( - "rules_python/tests/pycross/patched_extracted_wheel_for_testing" - ) - ) - self.assertTrue(self.extraction_dir.exists(), self.extraction_dir) - self.assertTrue(self.extraction_dir.is_dir(), self.extraction_dir) - - def test_patched_file_contents(self): - """Validate that the patch got applied correctly.""" - file = self.extraction_dir / "site-packages/numpy/file_added_via_patch.txt" - self.assertEqual(file.read_text(), "Hello from a patch!\n") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/pycross/py_wheel_library_test.py b/tests/pycross/py_wheel_library_test.py deleted file mode 100644 index 25d896a1ae..0000000000 --- a/tests/pycross/py_wheel_library_test.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -from pathlib import Path - -from python.runfiles import runfiles - -RUNFILES = runfiles.Create() - - -class TestPyWheelLibrary(unittest.TestCase): - def setUp(self): - self.extraction_dir = Path( - RUNFILES.Rlocation("rules_python/tests/pycross/extracted_wheel_for_testing") - ) - self.assertTrue(self.extraction_dir.exists(), self.extraction_dir) - self.assertTrue(self.extraction_dir.is_dir(), self.extraction_dir) - - def test_file_presence(self): - """Validate that the basic file layout looks good.""" - for path in ( - "bin/f2py", - "site-packages/numpy.libs/libgfortran-daac5196.so.5.0.0", - "site-packages/numpy/dtypes.py", - "site-packages/numpy/core/_umath_tests.cpython-311-aarch64-linux-gnu.so", - ): - print(self.extraction_dir / path) - self.assertTrue( - (self.extraction_dir / path).exists(), f"{path} does not exist" - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/pypi/env_marker_setting/BUILD.bazel b/tests/pypi/env_marker_setting/BUILD.bazel new file mode 100644 index 0000000000..9605e650ce --- /dev/null +++ b/tests/pypi/env_marker_setting/BUILD.bazel @@ -0,0 +1,5 @@ +load(":env_marker_setting_tests.bzl", "env_marker_setting_test_suite") + +env_marker_setting_test_suite( + name = "env_marker_setting_tests", +) diff --git a/tests/pypi/env_marker_setting/env_marker_setting_tests.bzl b/tests/pypi/env_marker_setting/env_marker_setting_tests.bzl new file mode 100644 index 0000000000..e16f2c8ef6 --- /dev/null +++ b/tests/pypi/env_marker_setting/env_marker_setting_tests.bzl @@ -0,0 +1,104 @@ +"""env_marker_setting tests.""" + +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") +load("@rules_testing//lib:test_suite.bzl", "test_suite") +load("@rules_testing//lib:util.bzl", "TestingAspectInfo") +load("//python/private/pypi:env_marker_info.bzl", "EnvMarkerInfo") # buildifier: disable=bzl-visibility +load("//python/private/pypi:env_marker_setting.bzl", "env_marker_setting") # buildifier: disable=bzl-visibility +load("//tests/support:support.bzl", "PIP_ENV_MARKER_CONFIG", "PYTHON_VERSION") + +def _custom_env_markers_impl(ctx): + _ = ctx # @unused + return [EnvMarkerInfo(env = { + "os_name": "testos", + })] + +_custom_env_markers = rule( + implementation = _custom_env_markers_impl, +) + +_tests = [] + +def _test_custom_env_markers(name): + def _impl(env, target): + env.expect.where( + expression = target[TestingAspectInfo].attrs.expression, + ).that_str( + target[config_common.FeatureFlagInfo].value, + ).equals("TRUE") + + env_marker_setting( + name = name + "_subject", + expression = "os_name == 'testos'", + ) + _custom_env_markers(name = name + "_env") + analysis_test( + name = name, + impl = _impl, + target = name + "_subject", + config_settings = { + PIP_ENV_MARKER_CONFIG: str(Label(name + "_env")), + }, + ) + +_tests.append(_test_custom_env_markers) + +def _test_expr(name): + def impl(env, target): + env.expect.where( + expression = target[TestingAspectInfo].attrs.expression, + ).that_str( + target[config_common.FeatureFlagInfo].value, + ).equals( + env.ctx.attr.expected, + ) + + cases = { + "python_full_version_lt_negative": { + "config_settings": { + PYTHON_VERSION: "3.12.0", + }, + "expected": "FALSE", + "expression": "python_full_version < '3.8'", + }, + "python_version_gte": { + "config_settings": { + PYTHON_VERSION: "3.12.0", + }, + "expected": "TRUE", + "expression": "python_version >= '3.12.0'", + }, + } + + tests = [] + for case_name, case in cases.items(): + test_name = name + "_" + case_name + tests.append(test_name) + env_marker_setting( + name = test_name + "_subject", + expression = case["expression"], + ) + analysis_test( + name = test_name, + impl = impl, + target = test_name + "_subject", + config_settings = case["config_settings"], + attr_values = { + "expected": case["expected"], + }, + attrs = { + "expected": attr.string(), + }, + ) + native.test_suite( + name = name, + tests = tests, + ) + +_tests.append(_test_expr) + +def env_marker_setting_test_suite(name): + test_suite( + name = name, + tests = _tests, + ) diff --git a/tests/pypi/extension/extension_tests.bzl b/tests/pypi/extension/extension_tests.bzl index 4d86d6a6e0..8e325724f4 100644 --- a/tests/pypi/extension/extension_tests.bzl +++ b/tests/pypi/extension/extension_tests.bzl @@ -62,7 +62,12 @@ def _mod(*, name, parse = [], override = [], whl_mods = [], is_root = True): def _parse_modules(env, **kwargs): return env.expect.that_struct( - parse_modules(**kwargs), + parse_modules( + # TODO @aignas 2025-05-11: start integration testing the branch which + # includes this. + enable_pipstar = 0, + **kwargs + ), attrs = dict( exposed_packages = subjects.dict, hub_group_map = subjects.dict, @@ -136,6 +141,7 @@ def _parse( parallel_download = False, experimental_index_url_overrides = {}, simpleapi_skip = simpleapi_skip, + _evaluate_markers_srcs = [], **kwargs ) @@ -157,6 +163,7 @@ def _test_simple(env): available_interpreters = { "python_3_15_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.15": "3.15.19"}, ) pypi.exposed_packages().contains_exactly({"pypi": ["simple"]}) @@ -204,6 +211,7 @@ def _test_simple_multiple_requirements(env): available_interpreters = { "python_3_15_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.15": "3.15.19"}, ) pypi.exposed_packages().contains_exactly({"pypi": ["simple"]}) @@ -270,6 +278,15 @@ torch==2.4.1 ; platform_machine != 'x86_64' \ available_interpreters = { "python_3_15_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.15": "3.15.19"}, + evaluate_markers = lambda _, requirements, **__: { + key: [ + platform + for platform in platforms + if ("x86_64" in platform and "platform_machine ==" in key) or ("x86_64" not in platform and "platform_machine !=" in key) + ] + for key, platforms in requirements.items() + }, ) pypi.exposed_packages().contains_exactly({"pypi": ["torch"]}) @@ -392,7 +409,17 @@ torch==2.4.1+cpu ; platform_machine == 'x86_64' \ available_interpreters = { "python_3_12_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.12": "3.12.19"}, simpleapi_download = mocksimpleapi_download, + evaluate_markers = lambda _, requirements, **__: { + # todo once 2692 is merged, this is going to be easier to test. + key: [ + platform + for platform in platforms + if ("x86_64" in platform and "platform_machine ==" in key) or ("x86_64" not in platform and "platform_machine !=" in key) + ] + for key, platforms in requirements.items() + }, ) pypi.exposed_packages().contains_exactly({"pypi": ["torch"]}) @@ -436,7 +463,11 @@ torch==2.4.1+cpu ; platform_machine == 'x86_64' \ pypi.whl_libraries().contains_exactly({ "pypi_312_torch_cp312_cp312_linux_x86_64_8800deef": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp312_linux_x86_64"], + "experimental_target_platforms": [ + "linux_x86_64", + "osx_x86_64", + "windows_x86_64", + ], "filename": "torch-2.4.1+cpu-cp312-cp312-linux_x86_64.whl", "python_interpreter_target": "unit_test_interpreter_target", "requirement": "torch==2.4.1+cpu", @@ -445,7 +476,13 @@ torch==2.4.1+cpu ; platform_machine == 'x86_64' \ }, "pypi_312_torch_cp312_cp312_manylinux_2_17_aarch64_36109432": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp312_linux_aarch64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "osx_aarch64", + ], "filename": "torch-2.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", "python_interpreter_target": "unit_test_interpreter_target", "requirement": "torch==2.4.1", @@ -454,7 +491,11 @@ torch==2.4.1+cpu ; platform_machine == 'x86_64' \ }, "pypi_312_torch_cp312_cp312_win_amd64_3a570e5c": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp312_windows_x86_64"], + "experimental_target_platforms": [ + "linux_x86_64", + "osx_x86_64", + "windows_x86_64", + ], "filename": "torch-2.4.1+cpu-cp312-cp312-win_amd64.whl", "python_interpreter_target": "unit_test_interpreter_target", "requirement": "torch==2.4.1+cpu", @@ -463,7 +504,13 @@ torch==2.4.1+cpu ; platform_machine == 'x86_64' \ }, "pypi_312_torch_cp312_none_macosx_11_0_arm64_72b484d5": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp312_osx_aarch64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "osx_aarch64", + ], "filename": "torch-2.4.1-cp312-none-macosx_11_0_arm64.whl", "python_interpreter_target": "unit_test_interpreter_target", "requirement": "torch==2.4.1", @@ -519,6 +566,7 @@ simple==0.0.3 \ available_interpreters = { "python_3_15_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.15": "3.15.19"}, ) pypi.exposed_packages().contains_exactly({"pypi": ["simple"]}) @@ -548,7 +596,8 @@ simple==0.0.3 \ "pypi_315_extra": { "dep_template": "@pypi//{name}:{target}", "download_only": True, - "experimental_target_platforms": ["cp315_linux_x86_64"], + # TODO @aignas 2025-04-20: ensure that this is in the hub repo + # "experimental_target_platforms": ["cp315_linux_x86_64"], "extra_pip_args": ["--platform=manylinux_2_17_x86_64", "--python-version=315", "--implementation=cp", "--abi=cp315"], "python_interpreter_target": "unit_test_interpreter_target", "requirement": "extra==0.0.1 --hash=sha256:deadb00f", @@ -556,7 +605,6 @@ simple==0.0.3 \ "pypi_315_simple_linux_x86_64": { "dep_template": "@pypi//{name}:{target}", "download_only": True, - "experimental_target_platforms": ["cp315_linux_x86_64"], "extra_pip_args": ["--platform=manylinux_2_17_x86_64", "--python-version=315", "--implementation=cp", "--abi=cp315"], "python_interpreter_target": "unit_test_interpreter_target", "requirement": "simple==0.0.1 --hash=sha256:deadbeef", @@ -564,7 +612,6 @@ simple==0.0.3 \ "pypi_315_simple_osx_aarch64": { "dep_template": "@pypi//{name}:{target}", "download_only": True, - "experimental_target_platforms": ["cp315_osx_aarch64"], "extra_pip_args": ["--platform=macosx_10_9_arm64", "--python-version=315", "--implementation=cp", "--abi=cp315"], "python_interpreter_target": "unit_test_interpreter_target", "requirement": "simple==0.0.3 --hash=sha256:deadbaaf", @@ -652,6 +699,7 @@ git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef available_interpreters = { "python_3_15_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.15": "3.15.19"}, simpleapi_download = mocksimpleapi_download, ) @@ -750,20 +798,38 @@ git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef pypi.whl_libraries().contains_exactly({ "pypi_315_any_name": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "linux_x86_64", + "osx_aarch64", + "osx_x86_64", + "windows_x86_64", + ], "extra_pip_args": ["--extra-args-for-sdist-building"], "filename": "any-name.tar.gz", "python_interpreter_target": "unit_test_interpreter_target", - "requirement": "direct_sdist_without_sha @ some-archive/any-name.tar.gz", + "requirement": "direct_sdist_without_sha", "sha256": "", "urls": ["some-archive/any-name.tar.gz"], }, "pypi_315_direct_without_sha_0_0_1_py3_none_any": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "linux_x86_64", + "osx_aarch64", + "osx_x86_64", + "windows_x86_64", + ], "filename": "direct_without_sha-0.0.1-py3-none-any.whl", "python_interpreter_target": "unit_test_interpreter_target", - "requirement": "direct_without_sha==0.0.1 @ example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl", + "requirement": "direct_without_sha==0.0.1", "sha256": "", "urls": ["example-direct.org/direct_without_sha-0.0.1-py3-none-any.whl"], }, @@ -781,7 +847,16 @@ git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef }, "pypi_315_simple_py3_none_any_deadb00f": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "linux_x86_64", + "osx_aarch64", + "osx_x86_64", + "windows_x86_64", + ], "filename": "simple-0.0.1-py3-none-any.whl", "python_interpreter_target": "unit_test_interpreter_target", "requirement": "simple==0.0.1", @@ -790,7 +865,16 @@ git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef }, "pypi_315_simple_sdist_deadbeef": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "linux_x86_64", + "osx_aarch64", + "osx_x86_64", + "windows_x86_64", + ], "extra_pip_args": ["--extra-args-for-sdist-building"], "filename": "simple-0.0.1.tar.gz", "python_interpreter_target": "unit_test_interpreter_target", @@ -800,16 +884,34 @@ git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef }, "pypi_315_some_pkg_py3_none_any_deadbaaf": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "linux_x86_64", + "osx_aarch64", + "osx_x86_64", + "windows_x86_64", + ], "filename": "some_pkg-0.0.1-py3-none-any.whl", "python_interpreter_target": "unit_test_interpreter_target", - "requirement": "some_pkg==0.0.1 @ example-direct.org/some_pkg-0.0.1-py3-none-any.whl --hash=sha256:deadbaaf", + "requirement": "some_pkg==0.0.1", "sha256": "deadbaaf", "urls": ["example-direct.org/some_pkg-0.0.1-py3-none-any.whl"], }, "pypi_315_some_py3_none_any_deadb33f": { "dep_template": "@pypi//{name}:{target}", - "experimental_target_platforms": ["cp315_linux_aarch64", "cp315_linux_arm", "cp315_linux_ppc", "cp315_linux_s390x", "cp315_linux_x86_64", "cp315_osx_aarch64", "cp315_osx_x86_64", "cp315_windows_x86_64"], + "experimental_target_platforms": [ + "linux_aarch64", + "linux_arm", + "linux_ppc", + "linux_s390x", + "linux_x86_64", + "osx_aarch64", + "osx_x86_64", + "windows_x86_64", + ], "filename": "some-other-pkg-0.0.1-py3-none-any.whl", "python_interpreter_target": "unit_test_interpreter_target", "requirement": "some_other_pkg==0.0.1", @@ -860,6 +962,15 @@ optimum[onnxruntime-gpu]==1.17.1 ; sys_platform == 'linux' available_interpreters = { "python_3_15_host": "unit_test_interpreter_target", }, + minor_mapping = {"3.15": "3.15.19"}, + evaluate_markers = lambda _, requirements, **__: { + key: [ + platform + for platform in platforms + if ("darwin" in key and "osx" in platform) or ("linux" in key and "linux" in platform) + ] + for key, platforms in requirements.items() + }, ) pypi.exposed_packages().contains_exactly({"pypi": []}) diff --git a/tests/pypi/generate_whl_library_build_bazel/generate_whl_library_build_bazel_tests.bzl b/tests/pypi/generate_whl_library_build_bazel/generate_whl_library_build_bazel_tests.bzl index b0d8f6d17e..225b296ebf 100644 --- a/tests/pypi/generate_whl_library_build_bazel/generate_whl_library_build_bazel_tests.bzl +++ b/tests/pypi/generate_whl_library_build_bazel/generate_whl_library_build_bazel_tests.bzl @@ -19,7 +19,7 @@ load("//python/private/pypi:generate_whl_library_build_bazel.bzl", "generate_whl _tests = [] -def _test_all(env): +def _test_all_legacy(env): want = """\ load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets") @@ -38,19 +38,71 @@ whl_library_targets( "data_exclude_all", ], dep_template = "@pypi//{name}:{target}", - dependencies = [ + dependencies = ["foo"], + dependencies_by_platform = { + "baz": ["bar"], + }, + entry_points = { + "foo": "bar.py", + }, + group_deps = [ "foo", - "bar-baz", + "fox", "qux", ], - dependencies_by_platform = { - "linux_x86_64": [ - "box", - "box-amd64", - ], - "windows_x86_64": ["fox"], - "@platforms//os:linux": ["box"], + group_name = "qux", + name = "foo.whl", + srcs_exclude = ["srcs_exclude_all"], + tags = ["tag1"], +) + +# SOMETHING SPECIAL AT THE END +""" + actual = generate_whl_library_build_bazel( + dep_template = "@pypi//{name}:{target}", + name = "foo.whl", + dependencies = ["foo"], + dependencies_by_platform = {"baz": ["bar"]}, + entry_points = { + "foo": "bar.py", + }, + data_exclude = ["exclude_via_attr"], + annotation = struct( + copy_files = {"file_src": "file_dest"}, + copy_executables = {"exec_src": "exec_dest"}, + data = ["extra_target"], + data_exclude_glob = ["data_exclude_all"], + srcs_exclude_glob = ["srcs_exclude_all"], + additive_build_content = """# SOMETHING SPECIAL AT THE END""", + ), + group_name = "qux", + group_deps = ["foo", "fox", "qux"], + tags = ["tag1"], + ) + env.expect.that_str(actual.replace("@@", "@")).equals(want) + +_tests.append(_test_all_legacy) + +def _test_all(env): + want = """\ +load("@pypi//:config.bzl", "whl_map") +load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets_from_requires") + +package(default_visibility = ["//visibility:public"]) + +whl_library_targets_from_requires( + copy_executables = { + "exec_src": "exec_dest", + }, + copy_files = { + "file_src": "file_dest", }, + data = ["extra_target"], + data_exclude = [ + "exclude_via_attr", + "data_exclude_all", + ], + dep_template = "@pypi//{name}:{target}", entry_points = { "foo": "bar.py", }, @@ -60,12 +112,14 @@ whl_library_targets( "qux", ], group_name = "qux", + include = whl_map, name = "foo.whl", - srcs_exclude = ["srcs_exclude_all"], - tags = [ - "tag2", - "tag1", + requires_dist = [ + "foo", + "bar-baz", + "qux", ], + srcs_exclude = ["srcs_exclude_all"], ) # SOMETHING SPECIAL AT THE END @@ -73,13 +127,7 @@ whl_library_targets( actual = generate_whl_library_build_bazel( dep_template = "@pypi//{name}:{target}", name = "foo.whl", - dependencies = ["foo", "bar-baz", "qux"], - dependencies_by_platform = { - "linux_x86_64": ["box", "box-amd64"], - "windows_x86_64": ["fox"], - "@platforms//os:linux": ["box"], # buildifier: disable=unsorted-dict-items to check that we sort inside the test - }, - tags = ["tag2", "tag1"], + requires_dist = ["foo", "bar-baz", "qux"], entry_points = { "foo": "bar.py", }, @@ -99,6 +147,70 @@ whl_library_targets( _tests.append(_test_all) +def _test_all_with_loads(env): + want = """\ +load("@pypi//:config.bzl", "whl_map") +load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets_from_requires") + +package(default_visibility = ["//visibility:public"]) + +whl_library_targets_from_requires( + copy_executables = { + "exec_src": "exec_dest", + }, + copy_files = { + "file_src": "file_dest", + }, + data = ["extra_target"], + data_exclude = [ + "exclude_via_attr", + "data_exclude_all", + ], + dep_template = "@pypi//{name}:{target}", + entry_points = { + "foo": "bar.py", + }, + group_deps = [ + "foo", + "fox", + "qux", + ], + group_name = "qux", + include = whl_map, + name = "foo.whl", + requires_dist = [ + "foo", + "bar-baz", + "qux", + ], + srcs_exclude = ["srcs_exclude_all"], +) + +# SOMETHING SPECIAL AT THE END +""" + actual = generate_whl_library_build_bazel( + dep_template = "@pypi//{name}:{target}", + name = "foo.whl", + requires_dist = ["foo", "bar-baz", "qux"], + entry_points = { + "foo": "bar.py", + }, + data_exclude = ["exclude_via_attr"], + annotation = struct( + copy_files = {"file_src": "file_dest"}, + copy_executables = {"exec_src": "exec_dest"}, + data = ["extra_target"], + data_exclude_glob = ["data_exclude_all"], + srcs_exclude_glob = ["srcs_exclude_all"], + additive_build_content = """# SOMETHING SPECIAL AT THE END""", + ), + group_name = "qux", + group_deps = ["foo", "fox", "qux"], + ) + env.expect.that_str(actual.replace("@@", "@")).equals(want) + +_tests.append(_test_all_with_loads) + def generate_whl_library_build_bazel_test_suite(name): """Create the test suite. diff --git a/tests/pypi/index_sources/index_sources_tests.bzl b/tests/pypi/index_sources/index_sources_tests.bzl index ffeed87a7b..d4062b47fe 100644 --- a/tests/pypi/index_sources/index_sources_tests.bzl +++ b/tests/pypi/index_sources/index_sources_tests.bzl @@ -21,42 +21,85 @@ _tests = [] def _test_no_simple_api_sources(env): inputs = { + "foo @ git+https://github.com/org/foo.git@deadbeef": struct( + requirement = "foo @ git+https://github.com/org/foo.git@deadbeef", + requirement_line = "foo @ git+https://github.com/org/foo.git@deadbeef", + marker = "", + url = "git+https://github.com/org/foo.git@deadbeef", + shas = [], + version = "", + filename = "", + ), "foo==0.0.1": struct( requirement = "foo==0.0.1", + requirement_line = "foo==0.0.1", marker = "", url = "", + version = "0.0.1", + filename = "", ), "foo==0.0.1 @ https://someurl.org": struct( requirement = "foo==0.0.1 @ https://someurl.org", + requirement_line = "foo==0.0.1 @ https://someurl.org", marker = "", url = "https://someurl.org", + version = "0.0.1", + filename = "", ), "foo==0.0.1 @ https://someurl.org/package.whl": struct( - requirement = "foo==0.0.1 @ https://someurl.org/package.whl", + requirement = "foo==0.0.1", + requirement_line = "foo==0.0.1 @ https://someurl.org/package.whl", marker = "", url = "https://someurl.org/package.whl", + version = "0.0.1", + filename = "package.whl", ), "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef": struct( - requirement = "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef", + requirement = "foo==0.0.1", + requirement_line = "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef", marker = "", url = "https://someurl.org/package.whl", shas = ["deadbeef"], + version = "0.0.1", + filename = "package.whl", ), "foo==0.0.1 @ https://someurl.org/package.whl; python_version < \"2.7\"\\ --hash=sha256:deadbeef": struct( - requirement = "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef", + requirement = "foo==0.0.1", + requirement_line = "foo==0.0.1 @ https://someurl.org/package.whl --hash=sha256:deadbeef", marker = "python_version < \"2.7\"", url = "https://someurl.org/package.whl", shas = ["deadbeef"], + version = "0.0.1", + filename = "package.whl", + ), + "foo[extra] @ https://example.org/foo-1.0.tar.gz --hash=sha256:deadbe0f": struct( + requirement = "foo[extra]", + requirement_line = "foo[extra] @ https://example.org/foo-1.0.tar.gz --hash=sha256:deadbe0f", + marker = "", + url = "https://example.org/foo-1.0.tar.gz", + shas = ["deadbe0f"], + version = "", + filename = "foo-1.0.tar.gz", + ), + "torch @ https://download.pytorch.org/whl/cpu/torch-2.6.0%2Bcpu-cp311-cp311-linux_x86_64.whl#sha256=deadbeef": struct( + requirement = "torch", + requirement_line = "torch @ https://download.pytorch.org/whl/cpu/torch-2.6.0%2Bcpu-cp311-cp311-linux_x86_64.whl#sha256=deadbeef", + marker = "", + url = "https://download.pytorch.org/whl/cpu/torch-2.6.0%2Bcpu-cp311-cp311-linux_x86_64.whl", + shas = ["deadbeef"], + version = "", + filename = "torch-2.6.0+cpu-cp311-cp311-linux_x86_64.whl", ), } for input, want in inputs.items(): got = index_sources(input) env.expect.that_collection(got.shas).contains_exactly(want.shas if hasattr(want, "shas") else []) - env.expect.that_str(got.version).equals("0.0.1") + env.expect.that_str(got.version).equals(want.version) env.expect.that_str(got.requirement).equals(want.requirement) - env.expect.that_str(got.requirement_line).equals(got.requirement) + env.expect.that_str(got.requirement_line).equals(got.requirement_line) env.expect.that_str(got.marker).equals(want.marker) env.expect.that_str(got.url).equals(want.url) + env.expect.that_str(got.filename).equals(want.filename) _tests.append(_test_no_simple_api_sources) diff --git a/tests/pypi/namespace_pkgs/BUILD.bazel b/tests/pypi/namespace_pkgs/BUILD.bazel new file mode 100644 index 0000000000..57f7962524 --- /dev/null +++ b/tests/pypi/namespace_pkgs/BUILD.bazel @@ -0,0 +1,5 @@ +load(":namespace_pkgs_tests.bzl", "namespace_pkgs_test_suite") + +namespace_pkgs_test_suite( + name = "namespace_pkgs_tests", +) diff --git a/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl b/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl new file mode 100644 index 0000000000..7ac938ff17 --- /dev/null +++ b/tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl @@ -0,0 +1,167 @@ +"" + +load("@rules_testing//lib:analysis_test.bzl", "test_suite") +load("//python/private/pypi:namespace_pkgs.bzl", "get_files") # buildifier: disable=bzl-visibility + +_tests = [] + +def test_in_current_dir(env): + srcs = [ + "foo/bar/biz.py", + "foo/bee/boo.py", + "foo/buu/__init__.py", + "foo/buu/bii.py", + ] + got = get_files(srcs = srcs) + expected = [ + "foo", + "foo/bar", + "foo/bee", + ] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_in_current_dir) + +def test_find_correct_namespace_packages(env): + srcs = [ + "nested/root/foo/bar/biz.py", + "nested/root/foo/bee/boo.py", + "nested/root/foo/buu/__init__.py", + "nested/root/foo/buu/bii.py", + ] + + got = get_files(srcs = srcs, root = "nested/root") + expected = [ + "nested/root/foo", + "nested/root/foo/bar", + "nested/root/foo/bee", + ] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_find_correct_namespace_packages) + +def test_ignores_empty_directories(_): + # because globs do not add directories, this test is not needed + pass + +_tests.append(test_ignores_empty_directories) + +def test_empty_case(env): + srcs = [ + "foo/__init__.py", + "foo/bar/__init__.py", + "foo/bar/biz.py", + ] + + got = get_files(srcs = srcs) + expected = [] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_empty_case) + +def test_ignores_non_module_files_in_directories(env): + srcs = [ + "foo/__init__.pyi", + "foo/py.typed", + ] + + got = get_files(srcs = srcs) + expected = [] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_ignores_non_module_files_in_directories) + +def test_parent_child_relationship_of_namespace_pkgs(env): + srcs = [ + "foo/bar/biff/my_module.py", + "foo/bar/biff/another_module.py", + ] + + got = get_files(srcs = srcs) + expected = [ + "foo", + "foo/bar", + "foo/bar/biff", + ] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_parent_child_relationship_of_namespace_pkgs) + +def test_parent_child_relationship_of_namespace_and_standard_pkgs(env): + srcs = [ + "foo/bar/biff/__init__.py", + "foo/bar/biff/another_module.py", + ] + + got = get_files(srcs = srcs) + expected = [ + "foo", + "foo/bar", + ] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_parent_child_relationship_of_namespace_and_standard_pkgs) + +def test_parent_child_relationship_of_namespace_and_nested_standard_pkgs(env): + srcs = [ + "foo/bar/__init__.py", + "foo/bar/biff/another_module.py", + "foo/bar/biff/__init__.py", + "foo/bar/boof/big_module.py", + "foo/bar/boof/__init__.py", + "fim/in_a_ns_pkg.py", + ] + + got = get_files(srcs = srcs) + expected = [ + "foo", + "fim", + ] + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_parent_child_relationship_of_namespace_and_nested_standard_pkgs) + +def test_recognized_all_nonstandard_module_types(env): + srcs = [ + "ayy/my_module.pyc", + "bee/ccc/dee/eee.so", + "eff/jee/aych.pyd", + ] + + expected = [ + "ayy", + "bee", + "bee/ccc", + "bee/ccc/dee", + "eff", + "eff/jee", + ] + got = get_files(srcs = srcs) + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_recognized_all_nonstandard_module_types) + +def test_skips_ignored_directories(env): + srcs = [ + "root/foo/boo/my_module.py", + "root/foo/bar/another_module.py", + ] + + expected = [ + "root/foo", + "root/foo/bar", + ] + got = get_files( + srcs = srcs, + ignored_dirnames = ["root/foo/boo"], + root = "root", + ) + env.expect.that_collection(got).contains_exactly(expected) + +_tests.append(test_skips_ignored_directories) + +def namespace_pkgs_test_suite(name): + test_suite( + name = name, + basic_tests = _tests, + ) diff --git a/tests/pypi/parse_requirements/parse_requirements_tests.bzl b/tests/pypi/parse_requirements/parse_requirements_tests.bzl index c50482127b..82fdd0a051 100644 --- a/tests/pypi/parse_requirements/parse_requirements_tests.bzl +++ b/tests/pypi/parse_requirements/parse_requirements_tests.bzl @@ -27,18 +27,18 @@ foo==0.0.1 \ """, "requirements_direct": """\ foo[extra] @ https://some-url/package.whl -bar @ https://example.org/bar-1.0.whl --hash=sha256:deadbeef -baz @ https://test.com/baz-2.0.whl; python_version < "3.8" --hash=sha256:deadb00f -qux @ https://example.org/qux-1.0.tar.gz --hash=sha256:deadbe0f """, "requirements_extra_args": """\ --index-url=example.org foo[extra]==0.0.1 \ --hash=sha256:deadbeef +""", + "requirements_git": """ +foo @ git+https://github.com/org/foo.git@deadbeef """, "requirements_linux": """\ -foo==0.0.3 --hash=sha256:deadbaaf +foo==0.0.3 --hash=sha256:deadbaaf --hash=sha256:5d15t """, # download_only = True "requirements_linux_download_only": """\ @@ -67,7 +67,7 @@ foo==0.0.4 @ https://example.org/foo-0.0.4.whl foo==0.0.5 @ https://example.org/foo-0.0.5.whl --hash=sha256:deadbeef """, "requirements_osx": """\ -foo==0.0.3 --hash=sha256:deadbaaf +foo==0.0.3 --hash=sha256:deadbaaf --hash=sha256:deadb11f --hash=sha256:5d15t """, "requirements_osx_download_only": """\ --platform=macosx_10_9_arm64 @@ -100,141 +100,60 @@ def _test_simple(env): "requirements_lock": ["linux_x86_64", "windows_x86_64"], }, ) - env.expect.that_dict(got).contains_exactly({ - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "foo[extra]==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], requirement_line = "foo[extra]==0.0.1 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1", + target_platforms = [ + "linux_x86_64", + "windows_x86_64", + ], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = [ - "linux_x86_64", - "windows_x86_64", - ], - whls = [], - ), - ], - }) - env.expect.that_str( - select_requirement( - got["foo"], - platform = "linux_x86_64", - ).srcs.version, - ).equals("0.0.1") + ], + ), + ]) _tests.append(_test_simple) -def _test_direct_urls(env): +def _test_direct_urls_integration(env): + """Check that we are using the filename from index_sources.""" got = parse_requirements( ctx = _mock_ctx(), requirements_by_platform = { "requirements_direct": ["linux_x86_64"], }, ) - env.expect.that_dict(got).contains_exactly({ - "bar": [ - struct( - distribution = "bar", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "bar @ https://example.org/bar-1.0.whl --hash=sha256:deadbeef", - requirement_line = "bar @ https://example.org/bar-1.0.whl --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "", - url = "https://example.org/bar-1.0.whl", - ), - target_platforms = ["linux_x86_64"], - whls = [struct( - url = "https://example.org/bar-1.0.whl", - filename = "bar-1.0.whl", - sha256 = "deadbeef", - yanked = False, - )], - ), - ], - "baz": [ - struct( - distribution = "baz", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "python_version < \"3.8\"", - requirement = "baz @ https://test.com/baz-2.0.whl --hash=sha256:deadb00f", - requirement_line = "baz @ https://test.com/baz-2.0.whl --hash=sha256:deadb00f", - shas = ["deadb00f"], - version = "", - url = "https://test.com/baz-2.0.whl", - ), - target_platforms = ["linux_x86_64"], - whls = [struct( - url = "https://test.com/baz-2.0.whl", - filename = "baz-2.0.whl", - sha256 = "deadb00f", - yanked = False, - )], - ), - ], - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "foo[extra] @ https://some-url/package.whl", - requirement_line = "foo[extra] @ https://some-url/package.whl", - shas = [], - version = "", - url = "https://some-url/package.whl", - ), - target_platforms = ["linux_x86_64"], - whls = [struct( + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo[extra]", + target_platforms = ["linux_x86_64"], url = "https://some-url/package.whl", filename = "package.whl", sha256 = "", yanked = False, - )], - ), - ], - "qux": [ - struct( - distribution = "qux", - extra_pip_args = [], - sdist = struct( - url = "https://example.org/qux-1.0.tar.gz", - filename = "qux-1.0.tar.gz", - sha256 = "deadbe0f", - yanked = False, - ), - is_exposed = True, - srcs = struct( - marker = "", - requirement = "qux @ https://example.org/qux-1.0.tar.gz --hash=sha256:deadbe0f", - requirement_line = "qux @ https://example.org/qux-1.0.tar.gz --hash=sha256:deadbe0f", - shas = ["deadbe0f"], - version = "", - url = "https://example.org/qux-1.0.tar.gz", ), - target_platforms = ["linux_x86_64"], - whls = [], - ), - ], - }) + ], + ), + ]) -_tests.append(_test_direct_urls) +_tests.append(_test_direct_urls_integration) def _test_extra_pip_args(env): got = parse_requirements( @@ -244,34 +163,27 @@ def _test_extra_pip_args(env): }, extra_pip_args = ["--trusted-host=example.org"], ) - env.expect.that_dict(got).contains_exactly({ - "foo": [ - struct( - distribution = "foo", - extra_pip_args = ["--index-url=example.org", "--trusted-host=example.org"], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "foo[extra]==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = ["--index-url=example.org", "--trusted-host=example.org"], requirement_line = "foo[extra]==0.0.1 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1", + target_platforms = [ + "linux_x86_64", + ], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = [ - "linux_x86_64", - ], - whls = [], - ), - ], - }) - env.expect.that_str( - select_requirement( - got["foo"], - platform = "linux_x86_64", - ).srcs.version, - ).equals("0.0.1") + ], + ), + ]) _tests.append(_test_extra_pip_args) @@ -282,26 +194,25 @@ def _test_dupe_requirements(env): "requirements_lock_dupe": ["linux_x86_64"], }, ) - env.expect.that_dict(got).contains_exactly({ - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "foo[extra,extra_2]==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], requirement_line = "foo[extra,extra_2]==0.0.1 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1", + target_platforms = ["linux_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["linux_x86_64"], - whls = [], - ), - ], - }) + ], + ), + ]) _tests.append(_test_dupe_requirements) @@ -314,66 +225,58 @@ def _test_multi_os(env): }, ) - env.expect.that_dict(got).contains_exactly({ - "bar": [ - struct( - distribution = "bar", - extra_pip_args = [], - srcs = struct( - marker = "", - requirement = "bar==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "bar", + is_exposed = False, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "bar", + extra_pip_args = [], requirement_line = "bar==0.0.1 --hash=sha256:deadb00f", - shas = ["deadb00f"], - version = "0.0.1", + target_platforms = ["windows_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["windows_x86_64"], - whls = [], - sdist = None, - is_exposed = False, - ), - ], - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - srcs = struct( - marker = "", - requirement = "foo==0.0.3", - requirement_line = "foo==0.0.3 --hash=sha256:deadbaaf", - shas = ["deadbaaf"], - version = "0.0.3", + ], + ), + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = True, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo==0.0.3 --hash=sha256:deadbaaf --hash=sha256:5d15t", + target_platforms = ["linux_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["linux_x86_64"], - whls = [], - sdist = None, - is_exposed = True, - ), - struct( - distribution = "foo", - extra_pip_args = [], - srcs = struct( - marker = "", - requirement = "foo[extra]==0.0.2", + struct( + distribution = "foo", + extra_pip_args = [], requirement_line = "foo[extra]==0.0.2 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.2", + target_platforms = ["windows_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["windows_x86_64"], - whls = [], - sdist = None, - is_exposed = True, - ), - ], - }) + ], + ), + ]) env.expect.that_str( select_requirement( - got["foo"], + got[1].srcs, platform = "windows_x86_64", - ).srcs.version, - ).equals("0.0.2") + ).requirement_line, + ).equals("foo[extra]==0.0.2 --hash=sha256:deadbeef") _tests.append(_test_multi_os) @@ -386,60 +289,52 @@ def _test_multi_os_legacy(env): }, ) - env.expect.that_dict(got).contains_exactly({ - "bar": [ - struct( - distribution = "bar", - extra_pip_args = ["--platform=manylinux_2_17_x86_64", "--python-version=39", "--implementation=cp", "--abi=cp39"], - is_exposed = False, - sdist = None, - srcs = struct( - marker = "", - requirement = "bar==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "bar", + is_exposed = False, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "bar", + extra_pip_args = ["--platform=manylinux_2_17_x86_64", "--python-version=39", "--implementation=cp", "--abi=cp39"], requirement_line = "bar==0.0.1 --hash=sha256:deadb00f", - shas = ["deadb00f"], - version = "0.0.1", + target_platforms = ["cp39_linux_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["cp39_linux_x86_64"], - whls = [], - ), - ], - "foo": [ - struct( - distribution = "foo", - extra_pip_args = ["--platform=manylinux_2_17_x86_64", "--python-version=39", "--implementation=cp", "--abi=cp39"], - is_exposed = True, - sdist = None, - srcs = struct( - marker = "", - requirement = "foo==0.0.1", + ], + ), + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = True, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = ["--platform=manylinux_2_17_x86_64", "--python-version=39", "--implementation=cp", "--abi=cp39"], requirement_line = "foo==0.0.1 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1", + target_platforms = ["cp39_linux_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["cp39_linux_x86_64"], - whls = [], - ), - struct( - distribution = "foo", - extra_pip_args = ["--platform=macosx_10_9_arm64", "--python-version=39", "--implementation=cp", "--abi=cp39"], - is_exposed = True, - sdist = None, - srcs = struct( - marker = "", + struct( + distribution = "foo", + extra_pip_args = ["--platform=macosx_10_9_arm64", "--python-version=39", "--implementation=cp", "--abi=cp39"], requirement_line = "foo==0.0.3 --hash=sha256:deadbaaf", - requirement = "foo==0.0.3", - shas = ["deadbaaf"], - version = "0.0.3", + target_platforms = ["cp39_osx_aarch64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["cp39_osx_aarch64"], - whls = [], - ), - ], - }) + ], + ), + ]) _tests.append(_test_multi_os_legacy) @@ -458,7 +353,7 @@ def _test_select_requirement_none_platform(env): _tests.append(_test_select_requirement_none_platform) def _test_env_marker_resolution(env): - def _mock_eval_markers(input): + def _mock_eval_markers(_, input): ret = { "foo[extra]==0.0.1 ;marker --hash=sha256:deadbeef": ["cp311_windows_x86_64"], } @@ -474,50 +369,42 @@ def _test_env_marker_resolution(env): }, evaluate_markers = _mock_eval_markers, ) - env.expect.that_dict(got).contains_exactly({ - "bar": [ - struct( - distribution = "bar", - extra_pip_args = [], - is_exposed = True, - sdist = None, - srcs = struct( - marker = "", - requirement = "bar==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "bar", + is_exposed = True, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "bar", + extra_pip_args = [], requirement_line = "bar==0.0.1 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1", + target_platforms = ["cp311_linux_super_exotic", "cp311_windows_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["cp311_linux_super_exotic", "cp311_windows_x86_64"], - whls = [], - ), - ], - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - is_exposed = False, - sdist = None, - srcs = struct( - marker = "marker", - requirement = "foo[extra]==0.0.1", + ], + ), + struct( + name = "foo", + is_exposed = False, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], requirement_line = "foo[extra]==0.0.1 --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1", + target_platforms = ["cp311_windows_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["cp311_windows_x86_64"], - whls = [], - ), - ], - }) - env.expect.that_str( - select_requirement( - got["foo"], - platform = "windows_x86_64", - ).srcs.version, - ).equals("0.0.1") + ], + ), + ]) _tests.append(_test_env_marker_resolution) @@ -528,42 +415,35 @@ def _test_different_package_version(env): "requirements_different_package_version": ["linux_x86_64"], }, ) - env.expect.that_dict(got).contains_exactly({ - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - is_exposed = True, - sdist = None, - srcs = struct( - marker = "", - requirement = "foo==0.0.1", + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = True, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], requirement_line = "foo==0.0.1 --hash=sha256:deadb00f", - shas = ["deadb00f"], - version = "0.0.1", + target_platforms = ["linux_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["linux_x86_64"], - whls = [], - ), - struct( - distribution = "foo", - extra_pip_args = [], - is_exposed = True, - sdist = None, - srcs = struct( - marker = "", - requirement = "foo==0.0.1+local", + struct( + distribution = "foo", + extra_pip_args = [], requirement_line = "foo==0.0.1+local --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.1+local", + target_platforms = ["linux_x86_64"], url = "", + filename = "", + sha256 = "", + yanked = False, ), - target_platforms = ["linux_x86_64"], - whls = [], - ), - ], - }) + ], + ), + ]) _tests.append(_test_different_package_version) @@ -574,55 +454,145 @@ def _test_optional_hash(env): "requirements_optional_hash": ["linux_x86_64"], }, ) - env.expect.that_dict(got).contains_exactly({ - "foo": [ - struct( - distribution = "foo", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "foo==0.0.4 @ https://example.org/foo-0.0.4.whl", - requirement_line = "foo==0.0.4 @ https://example.org/foo-0.0.4.whl", - shas = [], - version = "0.0.4", - url = "https://example.org/foo-0.0.4.whl", - ), - target_platforms = ["linux_x86_64"], - whls = [struct( + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = True, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo==0.0.4", + target_platforms = ["linux_x86_64"], url = "https://example.org/foo-0.0.4.whl", filename = "foo-0.0.4.whl", sha256 = "", yanked = False, - )], - ), - struct( - distribution = "foo", - extra_pip_args = [], - sdist = None, - is_exposed = True, - srcs = struct( - marker = "", - requirement = "foo==0.0.5 @ https://example.org/foo-0.0.5.whl --hash=sha256:deadbeef", - requirement_line = "foo==0.0.5 @ https://example.org/foo-0.0.5.whl --hash=sha256:deadbeef", - shas = ["deadbeef"], - version = "0.0.5", - url = "https://example.org/foo-0.0.5.whl", ), - target_platforms = ["linux_x86_64"], - whls = [struct( + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo==0.0.5", + target_platforms = ["linux_x86_64"], url = "https://example.org/foo-0.0.5.whl", filename = "foo-0.0.5.whl", sha256 = "deadbeef", yanked = False, - )], - ), - ], - }) + ), + ], + ), + ]) _tests.append(_test_optional_hash) +def _test_git_sources(env): + got = parse_requirements( + ctx = _mock_ctx(), + requirements_by_platform = { + "requirements_git": ["linux_x86_64"], + }, + ) + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + is_multiple_versions = False, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], + requirement_line = "foo @ git+https://github.com/org/foo.git@deadbeef", + target_platforms = ["linux_x86_64"], + url = "", + filename = "", + sha256 = "", + yanked = False, + ), + ], + ), + ]) + +_tests.append(_test_git_sources) + +def _test_overlapping_shas_with_index_results(env): + got = parse_requirements( + ctx = _mock_ctx(), + requirements_by_platform = { + "requirements_linux": ["cp39_linux_x86_64"], + "requirements_osx": ["cp39_osx_x86_64"], + }, + get_index_urls = lambda _, __: { + "foo": struct( + sdists = { + "5d15t": struct( + url = "sdist", + sha256 = "5d15t", + filename = "foo-0.0.1.tar.gz", + yanked = False, + ), + }, + whls = { + "deadb11f": struct( + url = "super2", + sha256 = "deadb11f", + filename = "foo-0.0.1-py3-none-macosx_14_0_x86_64.whl", + yanked = False, + ), + "deadbaaf": struct( + url = "super2", + sha256 = "deadbaaf", + filename = "foo-0.0.1-py3-none-any.whl", + yanked = False, + ), + }, + ), + }, + ) + + env.expect.that_collection(got).contains_exactly([ + struct( + name = "foo", + is_exposed = True, + # TODO @aignas 2025-05-25: how do we rename this? + is_multiple_versions = True, + srcs = [ + struct( + distribution = "foo", + extra_pip_args = [], + filename = "foo-0.0.1-py3-none-any.whl", + requirement_line = "foo==0.0.3", + sha256 = "deadbaaf", + target_platforms = ["cp39_linux_x86_64", "cp39_osx_x86_64"], + url = "super2", + yanked = False, + ), + struct( + distribution = "foo", + extra_pip_args = [], + filename = "foo-0.0.1.tar.gz", + requirement_line = "foo==0.0.3", + sha256 = "5d15t", + target_platforms = ["cp39_linux_x86_64", "cp39_osx_x86_64"], + url = "sdist", + yanked = False, + ), + struct( + distribution = "foo", + extra_pip_args = [], + filename = "foo-0.0.1-py3-none-macosx_14_0_x86_64.whl", + requirement_line = "foo==0.0.3", + sha256 = "deadb11f", + target_platforms = ["cp39_osx_x86_64"], + url = "super2", + yanked = False, + ), + ], + ), + ]) + +_tests.append(_test_overlapping_shas_with_index_results) + def parse_requirements_test_suite(name): """Create the test suite. diff --git a/tests/pypi/parse_simpleapi_html/parse_simpleapi_html_tests.bzl b/tests/pypi/parse_simpleapi_html/parse_simpleapi_html_tests.bzl index abaa7a6a49..b96d02f990 100644 --- a/tests/pypi/parse_simpleapi_html/parse_simpleapi_html_tests.bzl +++ b/tests/pypi/parse_simpleapi_html/parse_simpleapi_html_tests.bzl @@ -86,6 +86,7 @@ def _test_sdist(env): got = parse_simpleapi_html(url = input.url, content = html) env.expect.that_collection(got.sdists).has_size(1) env.expect.that_collection(got.whls).has_size(0) + env.expect.that_collection(got.sha256s_by_version).has_size(1) if not got: fail("expected at least one element, but did not get anything from:\n{}".format(html)) @@ -303,6 +304,25 @@ def _test_whls(env): yanked = False, ), ), + ( + struct( + attrs = [ + 'href="/whl/cpu/torch-2.6.0%2Bcpu-cp39-cp39-manylinux_2_28_aarch64.whl#sha256=deadbeef"', + ], + filename = "torch-2.6.0+cpu-cp39-cp39-manylinux_2_28_aarch64.whl", + url = "https://example.org/", + ), + struct( + filename = "torch-2.6.0+cpu-cp39-cp39-manylinux_2_28_aarch64.whl", + metadata_sha256 = "", + metadata_url = "", + sha256 = "deadbeef", + version = "2.6.0+cpu", + # A URL with % could occur if directly written in requirements. + url = "https://example.org/whl/cpu/torch-2.6.0%2Bcpu-cp39-cp39-manylinux_2_28_aarch64.whl", + yanked = False, + ), + ), ] for (input, want) in tests: diff --git a/tests/pypi/pep508/deps_tests.bzl b/tests/pypi/pep508/deps_tests.bzl index 44031ab6a5..aaa3b2f7dd 100644 --- a/tests/pypi/pep508/deps_tests.bzl +++ b/tests/pypi/pep508/deps_tests.bzl @@ -37,50 +37,17 @@ def test_can_add_os_specific_deps(env): "posix_dep; os_name=='posix'", "win_dep; os_name=='nt'", ], - platforms = [ - "linux_x86_64", - "osx_x86_64", - "osx_aarch64", - "windows_x86_64", - ], - host_python_version = "3.3.1", ) env.expect.that_collection(got.deps).contains_exactly(["bar"]) env.expect.that_dict(got.deps_select).contains_exactly({ - "@platforms//os:linux": ["posix_dep"], - "@platforms//os:osx": ["an_osx_dep", "posix_dep"], - "@platforms//os:windows": ["win_dep"], + "an_osx_dep": "sys_platform == \"darwin\"", + "posix_dep": "os_name == \"posix\"", + "win_dep": "os_name == \"nt\"", }) _tests.append(test_can_add_os_specific_deps) -def test_can_add_os_specific_deps_with_python_version(env): - got = deps( - "foo", - requires_dist = [ - "bar", - "an_osx_dep; sys_platform=='darwin'", - "posix_dep; os_name=='posix'", - "win_dep; os_name=='nt'", - ], - platforms = [ - "cp33_linux_x86_64", - "cp33_osx_x86_64", - "cp33_osx_aarch64", - "cp33_windows_x86_64", - ], - ) - - env.expect.that_collection(got.deps).contains_exactly(["bar"]) - env.expect.that_dict(got.deps_select).contains_exactly({ - "@platforms//os:linux": ["posix_dep"], - "@platforms//os:osx": ["an_osx_dep", "posix_dep"], - "@platforms//os:windows": ["win_dep"], - }) - -_tests.append(test_can_add_os_specific_deps_with_python_version) - def test_deps_are_added_to_more_specialized_platforms(env): got = deps( "foo", @@ -88,69 +55,16 @@ def test_deps_are_added_to_more_specialized_platforms(env): "m1_dep; sys_platform=='darwin' and platform_machine=='arm64'", "mac_dep; sys_platform=='darwin'", ], - platforms = [ - "osx_x86_64", - "osx_aarch64", - ], - host_python_version = "3.8.4", ) env.expect.that_collection(got.deps).contains_exactly([]) env.expect.that_dict(got.deps_select).contains_exactly({ - "@platforms//os:osx": ["mac_dep"], - "osx_aarch64": ["m1_dep", "mac_dep"], + "m1_dep": "sys_platform == \"darwin\" and platform_machine == \"arm64\"", + "mac_dep": "sys_platform == \"darwin\"", }) _tests.append(test_deps_are_added_to_more_specialized_platforms) -def test_deps_from_more_specialized_platforms_are_propagated(env): - got = deps( - "foo", - requires_dist = [ - "a_mac_dep; sys_platform=='darwin'", - "m1_dep; sys_platform=='darwin' and platform_machine=='arm64'", - ], - platforms = [ - "osx_x86_64", - "osx_aarch64", - ], - host_python_version = "3.8.4", - ) - - env.expect.that_collection(got.deps).contains_exactly([]) - env.expect.that_dict(got.deps_select).contains_exactly( - { - "@platforms//os:osx": ["a_mac_dep"], - "osx_aarch64": ["a_mac_dep", "m1_dep"], - }, - ) - -_tests.append(test_deps_from_more_specialized_platforms_are_propagated) - -def test_non_platform_markers_are_added_to_common_deps(env): - got = deps( - "foo", - requires_dist = [ - "bar", - "baz; implementation_name=='cpython'", - "m1_dep; sys_platform=='darwin' and platform_machine=='arm64'", - ], - platforms = [ - "linux_x86_64", - "osx_x86_64", - "osx_aarch64", - "windows_x86_64", - ], - host_python_version = "3.8.4", - ) - - env.expect.that_collection(got.deps).contains_exactly(["bar", "baz"]) - env.expect.that_dict(got.deps_select).contains_exactly({ - "osx_aarch64": ["m1_dep"], - }) - -_tests.append(test_non_platform_markers_are_added_to_common_deps) - def test_self_is_ignored(env): got = deps( "foo", @@ -189,194 +103,63 @@ _tests.append(test_self_dependencies_can_come_in_any_order) def _test_can_get_deps_based_on_specific_python_version(env): requires_dist = [ "bar", - "baz; python_version < '3.8'", + "baz; python_full_version < '3.7.3'", "posix_dep; os_name=='posix' and python_version >= '3.8'", ] - py38 = deps( - "foo", - requires_dist = requires_dist, - platforms = ["cp38_linux_x86_64"], - ) - py37 = deps( - "foo", - requires_dist = requires_dist, - platforms = ["cp37_linux_x86_64"], - ) - - env.expect.that_collection(py37.deps).contains_exactly(["bar", "baz"]) - env.expect.that_dict(py37.deps_select).contains_exactly({}) - env.expect.that_collection(py38.deps).contains_exactly(["bar"]) - env.expect.that_dict(py38.deps_select).contains_exactly({"@platforms//os:linux": ["posix_dep"]}) - -_tests.append(_test_can_get_deps_based_on_specific_python_version) - -def _test_no_version_select_when_single_version(env): - requires_dist = [ - "bar", - "baz; python_version >= '3.8'", - "posix_dep; os_name=='posix'", - "posix_dep_with_version; os_name=='posix' and python_version >= '3.8'", - "arch_dep; platform_machine=='x86_64' and python_version >= '3.8'", - ] - host_python_version = "3.7.5" - got = deps( "foo", requires_dist = requires_dist, - platforms = [ - "cp38_linux_x86_64", - "cp38_windows_x86_64", - ], - host_python_version = host_python_version, ) - env.expect.that_collection(got.deps).contains_exactly(["bar", "baz"]) + # since there is a single target platform, the deps_select will be empty + env.expect.that_collection(got.deps).contains_exactly(["bar"]) env.expect.that_dict(got.deps_select).contains_exactly({ - "@platforms//os:linux": ["posix_dep", "posix_dep_with_version"], - "linux_x86_64": ["arch_dep", "posix_dep", "posix_dep_with_version"], - "windows_x86_64": ["arch_dep"], + "baz": "python_full_version < \"3.7.3\"", + "posix_dep": "os_name == \"posix\" and python_version >= \"3.8\"", }) -_tests.append(_test_no_version_select_when_single_version) +_tests.append(_test_can_get_deps_based_on_specific_python_version) -def _test_can_get_version_select(env): +def _test_include_only_particular_deps(env): requires_dist = [ "bar", - "baz; python_version < '3.8'", - "baz_new; python_version >= '3.8'", - "posix_dep; os_name=='posix'", - "posix_dep_with_version; os_name=='posix' and python_version >= '3.8'", - "arch_dep; platform_machine=='x86_64' and python_version < '3.8'", + "baz; python_full_version < '3.7.3'", + "posix_dep; os_name=='posix' and python_version >= '3.8'", ] - host_python_version = "3.7.4" got = deps( "foo", requires_dist = requires_dist, - platforms = [ - "cp3{}_{}_x86_64".format(minor, os) - for minor in [7, 8, 9] - for os in ["linux", "windows"] - ], - host_python_version = host_python_version, + include = ["bar", "posix_dep"], ) + # since there is a single target platform, the deps_select will be empty env.expect.that_collection(got.deps).contains_exactly(["bar"]) env.expect.that_dict(got.deps_select).contains_exactly({ - str(Label("//python/config_settings:is_python_3.7")): ["baz"], - str(Label("//python/config_settings:is_python_3.8")): ["baz_new"], - str(Label("//python/config_settings:is_python_3.9")): ["baz_new"], - "@platforms//os:linux": ["baz", "posix_dep"], - "cp37_linux_anyarch": ["baz", "posix_dep"], - "cp37_linux_x86_64": ["arch_dep", "baz", "posix_dep"], - "cp37_windows_x86_64": ["arch_dep", "baz"], - "cp38_linux_anyarch": [ - "baz_new", - "posix_dep", - "posix_dep_with_version", - ], - "cp39_linux_anyarch": [ - "baz_new", - "posix_dep", - "posix_dep_with_version", - ], - "linux_x86_64": ["arch_dep", "baz", "posix_dep"], - "windows_x86_64": ["arch_dep", "baz"], - "//conditions:default": ["baz"], + "posix_dep": "os_name == \"posix\" and python_version >= \"3.8\"", }) -_tests.append(_test_can_get_version_select) +_tests.append(_test_include_only_particular_deps) -def _test_deps_spanning_all_target_py_versions_are_added_to_common(env): +def test_all_markers_are_added(env): requires_dist = [ "bar", "baz (<2,>=1.11) ; python_version < '3.8'", "baz (<2,>=1.14) ; python_version >= '3.8'", ] - host_python_version = "3.8.4" - - got = deps( - "foo", - requires_dist = requires_dist, - platforms = [ - "cp3{}_linux_x86_64".format(minor) - for minor in [7, 8, 9] - ], - host_python_version = host_python_version, - ) - - env.expect.that_collection(got.deps).contains_exactly(["bar", "baz"]) - env.expect.that_dict(got.deps_select).contains_exactly({}) - -_tests.append(_test_deps_spanning_all_target_py_versions_are_added_to_common) - -def _test_deps_are_not_duplicated(env): - host_python_version = "3.7.4" - - # See an example in - # https://files.pythonhosted.org/packages/76/9e/db1c2d56c04b97981c06663384f45f28950a73d9acf840c4006d60d0a1ff/opencv_python-4.9.0.80-cp37-abi3-win32.whl.metadata - requires_dist = [ - "bar >=0.1.0 ; python_version < '3.7'", - "bar >=0.2.0 ; python_version >= '3.7'", - "bar >=0.4.0 ; python_version >= '3.6' and platform_system == 'Linux' and platform_machine == 'aarch64'", - "bar >=0.4.0 ; python_version >= '3.9'", - "bar >=0.5.0 ; python_version <= '3.9' and platform_system == 'Darwin' and platform_machine == 'arm64'", - "bar >=0.5.0 ; python_version >= '3.10' and platform_system == 'Darwin'", - "bar >=0.5.0 ; python_version >= '3.10'", - "bar >=0.6.0 ; python_version >= '3.11'", - ] got = deps( "foo", requires_dist = requires_dist, - platforms = [ - "cp3{}_{}_{}".format(minor, os, arch) - for minor in [7, 10] - for os in ["linux", "osx", "windows"] - for arch in ["x86_64", "aarch64"] - ], - host_python_version = host_python_version, ) env.expect.that_collection(got.deps).contains_exactly(["bar"]) - env.expect.that_dict(got.deps_select).contains_exactly({}) - -_tests.append(_test_deps_are_not_duplicated) - -def _test_deps_are_not_duplicated_when_encountering_platform_dep_first(env): - host_python_version = "3.7.1" - - # Note, that we are sorting the incoming `requires_dist` and we need to ensure that we are not getting any - # issues even if the platform-specific line comes first. - requires_dist = [ - "bar >=0.4.0 ; python_version >= '3.6' and platform_system == 'Linux' and platform_machine == 'aarch64'", - "bar >=0.5.0 ; python_version >= '3.9'", - ] - - got = deps( - "foo", - requires_dist = requires_dist, - platforms = [ - "cp37_linux_aarch64", - "cp37_linux_x86_64", - "cp310_linux_aarch64", - "cp310_linux_x86_64", - ], - host_python_version = host_python_version, - ) - - # TODO @aignas 2025-02-24: this test case in the python version is passing but - # I am not sure why. The starlark version behaviour looks more correct. - env.expect.that_collection(got.deps).contains_exactly([]) env.expect.that_dict(got.deps_select).contains_exactly({ - str(Label("//python/config_settings:is_python_3.10")): ["bar"], - "cp310_linux_aarch64": ["bar"], - "cp37_linux_aarch64": ["bar"], - "linux_aarch64": ["bar"], + "baz": "(python_version < \"3.8\") or (python_version >= \"3.8\")", }) -_tests.append(_test_deps_are_not_duplicated_when_encountering_platform_dep_first) +_tests.append(test_all_markers_are_added) def deps_test_suite(name): # buildifier: disable=function-docstring test_suite( diff --git a/tests/pypi/pep508/evaluate_tests.bzl b/tests/pypi/pep508/evaluate_tests.bzl index 14e5e40b43..7b6c064b94 100644 --- a/tests/pypi/pep508/evaluate_tests.bzl +++ b/tests/pypi/pep508/evaluate_tests.bzl @@ -19,6 +19,12 @@ load("//python/private/pypi:pep508_evaluate.bzl", "evaluate", "tokenize") # bui _tests = [] +def _check_evaluate(env, expr, expected, values, strict = True): + env.expect.where( + expression = expr, + values = values, + ).that_bool(evaluate(expr, env = values, strict = strict)).equals(expected) + def _tokenize_tests(env): for input, want in { "": [], @@ -68,27 +74,25 @@ def _evaluate_non_version_env_tests(env): # When for input, want in { - "{} == 'osx'".format(var_name): True, - "{} != 'osx'".format(var_name): False, - "'osx' == {}".format(var_name): True, "'osx' != {}".format(var_name): False, - "'x' in {}".format(var_name): True, + "'osx' < {}".format(var_name): False, + "'osx' <= {}".format(var_name): True, + "'osx' == {}".format(var_name): True, + "'osx' >= {}".format(var_name): True, "'w' not in {}".format(var_name): True, - }.items(): # buildifier: @unsorted-dict-items - got = evaluate( - input, - env = marker_env, - ) - env.expect.that_bool(got).equals(want) + "'x' in {}".format(var_name): True, + "{} != 'osx'".format(var_name): False, + "{} < 'osx'".format(var_name): False, + "{} <= 'osx'".format(var_name): True, + "{} == 'osx'".format(var_name): True, + "{} > 'osx'".format(var_name): False, + "{} >= 'osx'".format(var_name): True, + }.items(): + _check_evaluate(env, input, want, marker_env) # Check that the non-strict eval gives us back the input when no # env is supplied. - got = evaluate( - input, - env = {}, - strict = False, - ) - env.expect.that_bool(got).equals(input.replace("'", '"')) + _check_evaluate(env, input, input.replace("'", '"'), {}, strict = False) _tests.append(_evaluate_non_version_env_tests) @@ -113,6 +117,7 @@ def _evaluate_version_env_tests(env): "{} <= '3.7.10'".format(var_name): True, "{} <= '3.7.8'".format(var_name): False, "{} == '3.7.9'".format(var_name): True, + "{} == '3.7.*'".format(var_name): True, "{} != '3.7.9'".format(var_name): False, "{} ~= '3.7.1'".format(var_name): True, "{} ~= '3.7.10'".format(var_name): False, @@ -121,23 +126,32 @@ def _evaluate_version_env_tests(env): "{} === '3.7.9'".format(var_name): True, "{} == '3.7.9+rc2'".format(var_name): True, }.items(): # buildifier: @unsorted-dict-items - got = evaluate( - input, - env = marker_env, - ) - env.expect.that_collection((input, got)).contains_exactly((input, want)) + _check_evaluate(env, input, want, marker_env) # Check that the non-strict eval gives us back the input when no # env is supplied. - got = evaluate( - input, - env = {}, - strict = False, - ) - env.expect.that_bool(got).equals(input.replace("'", '"')) + _check_evaluate(env, input, input.replace("'", '"'), {}, strict = False) _tests.append(_evaluate_version_env_tests) +def _evaluate_platform_version_is_special(env): + # Given + marker_env = {"platform_version": "FooBar Linux v1.2.3"} + + # When the platform version is not + input = "platform_version == '0'" + _check_evaluate(env, input, False, marker_env) + + # And when I compare it as string + input = "'FooBar' in platform_version" + _check_evaluate(env, input, True, marker_env) + + # Check that the non-strict eval gives us back the input when no + # env is supplied. + _check_evaluate(env, input, input.replace("'", '"'), {}, strict = False) + +_tests.append(_evaluate_platform_version_is_special) + def _logical_expression_tests(env): for input, want in { # Basic @@ -185,13 +199,7 @@ def _logical_expression_tests(env): "not not os_name == 'foo'": True, "not not not os_name == 'foo'": False, }.items(): # buildifier: @unsorted-dict-items - got = evaluate( - input, - env = { - "os_name": "foo", - }, - ) - env.expect.that_collection((input, got)).contains_exactly((input, want)) + _check_evaluate(env, input, want, {"os_name": "foo"}) if not input.strip("()"): # These cases will just return True, because they will be evaluated @@ -200,12 +208,7 @@ def _logical_expression_tests(env): # Check that the non-strict eval gives us back the input when no env # is supplied. - got = evaluate( - input, - env = {}, - strict = False, - ) - env.expect.that_bool(got).equals(input.replace("'", '"')) + _check_evaluate(env, input, input.replace("'", '"'), {}, strict = False) _tests.append(_logical_expression_tests) @@ -234,6 +237,7 @@ def _evaluate_partial_only_extra(env): strict = False, ) env.expect.that_bool(got).equals(want) + _check_evaluate(env, input, want, {"extra": extra}, strict = False) _tests.append(_evaluate_partial_only_extra) @@ -258,14 +262,61 @@ def _evaluate_with_aliases(env): }, }.items(): # buildifier: @unsorted-dict-items for input, want in tests.items(): - got = evaluate( - input, - env = pep508_env(target_platform), - ) - env.expect.that_bool(got).equals(want) + _check_evaluate(env, input, want, pep508_env(target_platform)) _tests.append(_evaluate_with_aliases) +def _expr_case(expr, want, env): + return struct(expr = expr.strip(), want = want, env = env) + +_MISC_EXPRESSIONS = [ + _expr_case('python_version == "3.*"', True, {"python_version": "3.10.1"}), + _expr_case('python_version != "3.10.*"', False, {"python_version": "3.10.1"}), + _expr_case('python_version != "3.11.*"', True, {"python_version": "3.10.1"}), + _expr_case('python_version != "3.10"', False, {"python_version": "3.10.0"}), + _expr_case('python_version == "3.10"', True, {"python_version": "3.10.0"}), + # Cases for the '>' operator + # Taken from spec: https://peps.python.org/pep-0440/#exclusive-ordered-comparison + _expr_case('python_version > "1.7"', True, {"python_version": "1.7.1"}), + _expr_case('python_version > "1.7"', False, {"python_version": "1.7.0.post0"}), + _expr_case('python_version > "1.7"', True, {"python_version": "1.7.1"}), + _expr_case('python_version > "1.7.post2"', True, {"python_version": "1.7.1"}), + _expr_case('python_version > "1.7.post2"', True, {"python_version": "1.7.post3"}), + _expr_case('python_version > "1.7.post2"', False, {"python_version": "1.7.0"}), + _expr_case('python_version > "1.7.1+local"', False, {"python_version": "1.7.1"}), + _expr_case('python_version > "1.7.1+local"', True, {"python_version": "1.7.2"}), + # Extra cases for the '<' operator + _expr_case('python_version < "1.7.1"', False, {"python_version": "1.7.2"}), + _expr_case('python_version < "1.7.3"', True, {"python_version": "1.7.2"}), + _expr_case('python_version < "1.7.1"', True, {"python_version": "1.7"}), + _expr_case('python_version < "1.7.1"', False, {"python_version": "1.7.1-rc2"}), + _expr_case('python_version < "1.7.1-rc3"', True, {"python_version": "1.7.1-rc2"}), + _expr_case('python_version < "1.7.1-rc1"', False, {"python_version": "1.7.1-rc2"}), + # Extra tests + _expr_case('python_version <= "1.7.1"', True, {"python_version": "1.7.1"}), + _expr_case('python_version <= "1.7.2"', True, {"python_version": "1.7.1"}), + _expr_case('python_version >= "1.7.1"', True, {"python_version": "1.7.1"}), + _expr_case('python_version >= "1.7.0"', True, {"python_version": "1.7.1"}), + # Compatible version tests: + # https://packaging.python.org/en/latest/specifications/version-specifiers/#compatible-release + _expr_case('python_version ~= "2.2"', True, {"python_version": "2.3"}), + _expr_case('python_version ~= "2.2"', False, {"python_version": "2.1"}), + _expr_case('python_version ~= "2.2.post3"', False, {"python_version": "2.2"}), + _expr_case('python_version ~= "2.2.post3"', True, {"python_version": "2.3"}), + _expr_case('python_version ~= "2.2.post3"', False, {"python_version": "3.0"}), + _expr_case('python_version ~= "1!2.2"', False, {"python_version": "2.7"}), + _expr_case('python_version ~= "0!2.2"', True, {"python_version": "2.7"}), + _expr_case('python_version ~= "1!2.2"', True, {"python_version": "1!2.7"}), + _expr_case('python_version ~= "1.2.3"', True, {"python_version": "1.2.4"}), + _expr_case('python_version ~= "1.2.3"', False, {"python_version": "1.3.2"}), +] + +def _misc_expressions(env): + for case in _MISC_EXPRESSIONS: + _check_evaluate(env, case.expr, case.want, case.env) + +_tests.append(_misc_expressions) + def evaluate_test_suite(name): # buildifier: disable=function-docstring test_suite( name = name, diff --git a/tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl b/tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl index c60761bed7..416d50bd80 100644 --- a/tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl +++ b/tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl @@ -68,7 +68,8 @@ def _test_bzlmod_aliases(env): aliases = { "bar-baz": { whl_config_setting( - version = "3.2", + # Add one with micro version to mimic construction in the extension + version = "3.2.2", config_setting = "//:my_config_setting", ): "pypi_32_bar_baz", whl_config_setting( @@ -83,10 +84,10 @@ def _test_bzlmod_aliases(env): filename = "foo-0.0.0-py3-none-any.whl", ): "filename_repo", whl_config_setting( - version = "3.2", + version = "3.2.2", filename = "foo-0.0.0-py3-none-any.whl", target_platforms = [ - "cp32_linux_x86_64", + "cp32.2_linux_x86_64", ], ): "filename_repo_linux_x86_64", }, @@ -117,7 +118,7 @@ pkg_aliases( whl_config_setting( filename = "foo-0.0.0-py3-none-any.whl", target_platforms = ("cp32_linux_x86_64",), - version = "3.2", + version = "3.2.2", ): "filename_repo_linux_x86_64", }, extra_aliases = ["foo"], diff --git a/tests/pypi/simpleapi_download/simpleapi_download_tests.bzl b/tests/pypi/simpleapi_download/simpleapi_download_tests.bzl index ce214d6e34..a96815c12c 100644 --- a/tests/pypi/simpleapi_download/simpleapi_download_tests.bzl +++ b/tests/pypi/simpleapi_download/simpleapi_download_tests.bzl @@ -43,6 +43,7 @@ def _test_simple(env): contents = simpleapi_download( ctx = struct( os = struct(environ = {}), + report_progress = lambda _: None, ), attr = struct( index_url_overrides = {}, @@ -95,6 +96,7 @@ def _test_fail(env): simpleapi_download( ctx = struct( os = struct(environ = {}), + report_progress = lambda _: None, ), attr = struct( index_url_overrides = {}, @@ -136,6 +138,7 @@ def _test_download_url(env): ctx = struct( os = struct(environ = {}), download = download, + report_progress = lambda _: None, read = lambda i: "contents of " + i, path = lambda i: "path/for/" + i, ), @@ -171,6 +174,7 @@ def _test_download_url_parallel(env): ctx = struct( os = struct(environ = {}), download = download, + report_progress = lambda _: None, read = lambda i: "contents of " + i, path = lambda i: "path/for/" + i, ), @@ -206,6 +210,7 @@ def _test_download_envsubst_url(env): ctx = struct( os = struct(environ = {"INDEX_URL": "https://example.com/main/simple/"}), download = download, + report_progress = lambda _: None, read = lambda i: "contents of " + i, path = lambda i: "path/for/" + i, ), diff --git a/tests/pypi/whl_installer/BUILD.bazel b/tests/pypi/whl_installer/BUILD.bazel index fea6a46d01..060d2bce62 100644 --- a/tests/pypi/whl_installer/BUILD.bazel +++ b/tests/pypi/whl_installer/BUILD.bazel @@ -17,11 +17,12 @@ py_test( ) py_test( - name = "namespace_pkgs_test", + name = "platform_test", size = "small", srcs = [ - "namespace_pkgs_test.py", + "platform_test.py", ], + data = ["//examples/wheel:minimal_with_py_package"], deps = [ ":lib", ], @@ -38,3 +39,15 @@ py_test( ":lib", ], ) + +py_test( + name = "wheel_test", + size = "small", + srcs = [ + "wheel_test.py", + ], + data = ["//examples/wheel:minimal_with_py_package"], + deps = [ + ":lib", + ], +) diff --git a/tests/pypi/whl_installer/arguments_test.py b/tests/pypi/whl_installer/arguments_test.py index 9f73ae96a9..2352d8e48b 100644 --- a/tests/pypi/whl_installer/arguments_test.py +++ b/tests/pypi/whl_installer/arguments_test.py @@ -15,7 +15,7 @@ import json import unittest -from python.private.pypi.whl_installer import arguments +from python.private.pypi.whl_installer import arguments, wheel class ArgumentsTestCase(unittest.TestCase): @@ -36,7 +36,6 @@ def test_arguments(self) -> None: self.assertIn("requirement", args_dict) self.assertIn("extra_pip_args", args_dict) self.assertEqual(args_dict["pip_data_exclude"], []) - self.assertEqual(args_dict["enable_implicit_namespace_pkgs"], False) self.assertEqual(args_dict["extra_pip_args"], extra_pip_args) def test_deserialize_structured_args(self) -> None: @@ -49,6 +48,18 @@ def test_deserialize_structured_args(self) -> None: self.assertEqual(args["environment"], {"PIP_DO_SOMETHING": "True"}) self.assertEqual(args["extra_pip_args"], []) + def test_platform_aggregation(self) -> None: + parser = arguments.parser() + args = parser.parse_args( + args=[ + "--platform=linux_*", + "--platform=osx_*", + "--platform=windows_*", + "--requirement=foo", + ] + ) + self.assertEqual(set(wheel.Platform.all()), arguments.get_platforms(args)) + if __name__ == "__main__": unittest.main() diff --git a/tests/pypi/whl_installer/namespace_pkgs_test.py b/tests/pypi/whl_installer/namespace_pkgs_test.py deleted file mode 100644 index fbbd50926a..0000000000 --- a/tests/pypi/whl_installer/namespace_pkgs_test.py +++ /dev/null @@ -1,192 +0,0 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os -import pathlib -import shutil -import tempfile -import unittest -from typing import Optional, Set - -from python.private.pypi.whl_installer import namespace_pkgs - - -class TempDir: - def __init__(self) -> None: - self.dir = tempfile.mkdtemp() - - def root(self) -> str: - return self.dir - - def add_dir(self, rel_path: str) -> None: - d = pathlib.Path(self.dir, rel_path) - d.mkdir(parents=True) - - def add_file(self, rel_path: str, contents: Optional[str] = None) -> None: - f = pathlib.Path(self.dir, rel_path) - f.parent.mkdir(parents=True, exist_ok=True) - if contents: - with open(str(f), "w") as writeable_f: - writeable_f.write(contents) - else: - f.touch() - - def remove(self) -> None: - shutil.rmtree(self.dir) - - -class TestImplicitNamespacePackages(unittest.TestCase): - def assertPathsEqual(self, actual: Set[pathlib.Path], expected: Set[str]) -> None: - self.assertEqual(actual, {pathlib.Path(p) for p in expected}) - - def test_in_current_directory(self) -> None: - directory = TempDir() - directory.add_file("foo/bar/biz.py") - directory.add_file("foo/bee/boo.py") - directory.add_file("foo/buu/__init__.py") - directory.add_file("foo/buu/bii.py") - cwd = os.getcwd() - os.chdir(directory.root()) - expected = { - "foo", - "foo/bar", - "foo/bee", - } - try: - actual = namespace_pkgs.implicit_namespace_packages(".") - self.assertPathsEqual(actual, expected) - finally: - os.chdir(cwd) - directory.remove() - - def test_finds_correct_namespace_packages(self) -> None: - directory = TempDir() - directory.add_file("foo/bar/biz.py") - directory.add_file("foo/bee/boo.py") - directory.add_file("foo/buu/__init__.py") - directory.add_file("foo/buu/bii.py") - - expected = { - directory.root() + "/foo", - directory.root() + "/foo/bar", - directory.root() + "/foo/bee", - } - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertPathsEqual(actual, expected) - - def test_ignores_empty_directories(self) -> None: - directory = TempDir() - directory.add_file("foo/bar/biz.py") - directory.add_dir("foo/cat") - - expected = { - directory.root() + "/foo", - directory.root() + "/foo/bar", - } - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertPathsEqual(actual, expected) - - def test_empty_case(self) -> None: - directory = TempDir() - directory.add_file("foo/__init__.py") - directory.add_file("foo/bar/__init__.py") - directory.add_file("foo/bar/biz.py") - - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertEqual(actual, set()) - - def test_ignores_non_module_files_in_directories(self) -> None: - directory = TempDir() - directory.add_file("foo/__init__.pyi") - directory.add_file("foo/py.typed") - - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertEqual(actual, set()) - - def test_parent_child_relationship_of_namespace_pkgs(self): - directory = TempDir() - directory.add_file("foo/bar/biff/my_module.py") - directory.add_file("foo/bar/biff/another_module.py") - - expected = { - directory.root() + "/foo", - directory.root() + "/foo/bar", - directory.root() + "/foo/bar/biff", - } - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertPathsEqual(actual, expected) - - def test_parent_child_relationship_of_namespace_and_standard_pkgs(self): - directory = TempDir() - directory.add_file("foo/bar/biff/__init__.py") - directory.add_file("foo/bar/biff/another_module.py") - - expected = { - directory.root() + "/foo", - directory.root() + "/foo/bar", - } - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertPathsEqual(actual, expected) - - def test_parent_child_relationship_of_namespace_and_nested_standard_pkgs(self): - directory = TempDir() - directory.add_file("foo/bar/__init__.py") - directory.add_file("foo/bar/biff/another_module.py") - directory.add_file("foo/bar/biff/__init__.py") - directory.add_file("foo/bar/boof/big_module.py") - directory.add_file("foo/bar/boof/__init__.py") - directory.add_file("fim/in_a_ns_pkg.py") - - expected = { - directory.root() + "/foo", - directory.root() + "/fim", - } - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertPathsEqual(actual, expected) - - def test_recognized_all_nonstandard_module_types(self): - directory = TempDir() - directory.add_file("ayy/my_module.pyc") - directory.add_file("bee/ccc/dee/eee.so") - directory.add_file("eff/jee/aych.pyd") - - expected = { - directory.root() + "/ayy", - directory.root() + "/bee", - directory.root() + "/bee/ccc", - directory.root() + "/bee/ccc/dee", - directory.root() + "/eff", - directory.root() + "/eff/jee", - } - actual = namespace_pkgs.implicit_namespace_packages(directory.root()) - self.assertPathsEqual(actual, expected) - - def test_skips_ignored_directories(self): - directory = TempDir() - directory.add_file("foo/boo/my_module.py") - directory.add_file("foo/bar/another_module.py") - - expected = { - directory.root() + "/foo", - directory.root() + "/foo/bar", - } - actual = namespace_pkgs.implicit_namespace_packages( - directory.root(), - ignored_dirnames=[directory.root() + "/foo/boo"], - ) - self.assertPathsEqual(actual, expected) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/pypi/whl_installer/platform_test.py b/tests/pypi/whl_installer/platform_test.py new file mode 100644 index 0000000000..ad65650779 --- /dev/null +++ b/tests/pypi/whl_installer/platform_test.py @@ -0,0 +1,97 @@ +import unittest +from random import shuffle + +from python.private.pypi.whl_installer.platform import ( + OS, + Arch, + Platform, + host_interpreter_version, +) + + +class MinorVersionTest(unittest.TestCase): + def test_host(self): + host = host_interpreter_version() + self.assertIsNotNone(host) + + +class PlatformTest(unittest.TestCase): + def test_can_get_host(self): + host = Platform.host() + self.assertIsNotNone(host) + self.assertEqual(1, len(Platform.from_string("host"))) + self.assertEqual(host, Platform.from_string("host")) + + def test_can_get_linux_x86_64_without_py_version(self): + got = Platform.from_string("linux_x86_64") + want = Platform(os=OS.linux, arch=Arch.x86_64) + self.assertEqual(want, got[0]) + + def test_can_get_specific_from_string(self): + got = Platform.from_string("cp33_linux_x86_64") + want = Platform(os=OS.linux, arch=Arch.x86_64, minor_version=3) + self.assertEqual(want, got[0]) + + got = Platform.from_string("cp33.0_linux_x86_64") + want = Platform(os=OS.linux, arch=Arch.x86_64, minor_version=3, micro_version=0) + self.assertEqual(want, got[0]) + + def test_can_get_all_for_py_version(self): + cp39 = Platform.all(minor_version=9, micro_version=0) + self.assertEqual(21, len(cp39), f"Got {cp39}") + self.assertEqual(cp39, Platform.from_string("cp39.0_*")) + + def test_can_get_all_for_os(self): + linuxes = Platform.all(OS.linux, minor_version=9) + self.assertEqual(7, len(linuxes)) + self.assertEqual(linuxes, Platform.from_string("cp39_linux_*")) + + def test_can_get_all_for_os_for_host_python(self): + linuxes = Platform.all(OS.linux) + self.assertEqual(7, len(linuxes)) + self.assertEqual(linuxes, Platform.from_string("linux_*")) + + def test_platform_sort(self): + platforms = [ + Platform(os=OS.linux, arch=None), + Platform(os=OS.linux, arch=Arch.x86_64), + Platform(os=OS.osx, arch=None), + Platform(os=OS.osx, arch=Arch.x86_64), + Platform(os=OS.osx, arch=Arch.aarch64), + ] + shuffle(platforms) + platforms.sort() + want = [ + Platform(os=OS.linux, arch=None), + Platform(os=OS.linux, arch=Arch.x86_64), + Platform(os=OS.osx, arch=None), + Platform(os=OS.osx, arch=Arch.x86_64), + Platform(os=OS.osx, arch=Arch.aarch64), + ] + + self.assertEqual(want, platforms) + + def test_wheel_os_alias(self): + self.assertEqual("osx", str(OS.osx)) + self.assertEqual(str(OS.darwin), str(OS.osx)) + + def test_wheel_arch_alias(self): + self.assertEqual("x86_64", str(Arch.x86_64)) + self.assertEqual(str(Arch.amd64), str(Arch.x86_64)) + + def test_wheel_platform_alias(self): + give = Platform( + os=OS.darwin, + arch=Arch.amd64, + ) + alias = Platform( + os=OS.osx, + arch=Arch.x86_64, + ) + + self.assertEqual("osx_x86_64", str(give)) + self.assertEqual(str(alias), str(give)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/pypi/whl_installer/wheel_installer_test.py b/tests/pypi/whl_installer/wheel_installer_test.py index 3c118af3c4..7040b0cfd8 100644 --- a/tests/pypi/whl_installer/wheel_installer_test.py +++ b/tests/pypi/whl_installer/wheel_installer_test.py @@ -22,6 +22,39 @@ from python.private.pypi.whl_installer import wheel_installer +class TestRequirementExtrasParsing(unittest.TestCase): + def test_parses_requirement_for_extra(self) -> None: + cases = [ + ("name[foo]", ("name", frozenset(["foo"]))), + ("name[ Foo123 ]", ("name", frozenset(["Foo123"]))), + (" name1[ foo ] ", ("name1", frozenset(["foo"]))), + ("Name[foo]", ("name", frozenset(["foo"]))), + ("name_foo[bar]", ("name-foo", frozenset(["bar"]))), + ( + "name [fred,bar] @ http://foo.com ; python_version=='2.7'", + ("name", frozenset(["fred", "bar"])), + ), + ( + "name[quux, strange];python_version<'2.7' and platform_version=='2'", + ("name", frozenset(["quux", "strange"])), + ), + ( + "name; (os_name=='a' or os_name=='b') and os_name=='c'", + (None, None), + ), + ( + "name@http://foo.com", + (None, None), + ), + ] + + for case, expected in cases: + with self.subTest(): + self.assertTupleEqual( + wheel_installer._parse_requirement_for_extra(case), expected + ) + + class TestWhlFilegroup(unittest.TestCase): def setUp(self) -> None: self.wheel_name = "example_minimal_package-0.0.1-py3-none-any.whl" @@ -35,8 +68,10 @@ def tearDown(self): def test_wheel_exists(self) -> None: wheel_installer._extract_wheel( Path(self.wheel_path), - enable_implicit_namespace_pkgs=False, installation_dir=Path(self.wheel_dir), + extras={}, + platforms=[], + enable_pipstar=False, ) want_files = [ @@ -57,8 +92,11 @@ def test_wheel_exists(self) -> None: metadata_file_content = json.load(metadata_file) want = dict( + deps=[], + deps_by_platform={}, entry_points=[], - python_version="3.11.11", + name="example-minimal-package", + version="0.0.1", ) self.assertEqual(want, metadata_file_content) diff --git a/tests/pypi/whl_installer/wheel_test.py b/tests/pypi/whl_installer/wheel_test.py new file mode 100644 index 0000000000..3599fd1868 --- /dev/null +++ b/tests/pypi/whl_installer/wheel_test.py @@ -0,0 +1,345 @@ +import unittest +from unittest import mock + +from python.private.pypi.whl_installer import wheel +from python.private.pypi.whl_installer.platform import OS, Arch, Platform + +_HOST_INTERPRETER_FN = ( + "python.private.pypi.whl_installer.wheel.host_interpreter_version" +) + + +class DepsTest(unittest.TestCase): + def test_simple(self): + deps = wheel.Deps("foo", requires_dist=["bar", 'baz; extra=="foo"']) + + got = deps.build() + + self.assertIsInstance(got, wheel.FrozenDeps) + self.assertEqual(["bar"], got.deps) + self.assertEqual({}, got.deps_select) + + def test_can_add_os_specific_deps(self): + for platforms in [ + { + Platform(os=OS.linux, arch=Arch.x86_64), + Platform(os=OS.osx, arch=Arch.x86_64), + Platform(os=OS.osx, arch=Arch.aarch64), + Platform(os=OS.windows, arch=Arch.x86_64), + }, + { + Platform(os=OS.linux, arch=Arch.x86_64, minor_version=8), + Platform(os=OS.osx, arch=Arch.x86_64, minor_version=8), + Platform(os=OS.osx, arch=Arch.aarch64, minor_version=8), + Platform(os=OS.windows, arch=Arch.x86_64, minor_version=8), + }, + { + Platform( + os=OS.linux, arch=Arch.x86_64, minor_version=8, micro_version=1 + ), + Platform(os=OS.osx, arch=Arch.x86_64, minor_version=8, micro_version=1), + Platform( + os=OS.osx, arch=Arch.aarch64, minor_version=8, micro_version=1 + ), + Platform( + os=OS.windows, arch=Arch.x86_64, minor_version=8, micro_version=1 + ), + }, + ]: + with self.subTest(): + deps = wheel.Deps( + "foo", + requires_dist=[ + "bar", + "an_osx_dep; sys_platform=='darwin'", + "posix_dep; os_name=='posix'", + "win_dep; os_name=='nt'", + ], + platforms=platforms, + ) + + got = deps.build() + + self.assertEqual(["bar"], got.deps) + self.assertEqual( + { + "linux_x86_64": ["posix_dep"], + "osx_aarch64": ["an_osx_dep", "posix_dep"], + "osx_x86_64": ["an_osx_dep", "posix_dep"], + "windows_x86_64": ["win_dep"], + }, + got.deps_select, + ) + + def test_non_platform_markers_are_added_to_common_deps(self): + got = wheel.Deps( + "foo", + requires_dist=[ + "bar", + "baz; implementation_name=='cpython'", + "m1_dep; sys_platform=='darwin' and platform_machine=='arm64'", + ], + platforms={ + Platform(os=OS.linux, arch=Arch.x86_64), + Platform(os=OS.osx, arch=Arch.x86_64), + Platform(os=OS.osx, arch=Arch.aarch64), + Platform(os=OS.windows, arch=Arch.x86_64), + }, + ).build() + + self.assertEqual(["bar", "baz"], got.deps) + self.assertEqual( + { + "osx_aarch64": ["m1_dep"], + }, + got.deps_select, + ) + + def test_self_is_ignored(self): + deps = wheel.Deps( + "foo", + requires_dist=[ + "bar", + "req_dep; extra == 'requests'", + "foo[requests]; extra == 'ssl'", + "ssl_lib; extra == 'ssl'", + ], + extras={"ssl"}, + ) + + got = deps.build() + + self.assertEqual(["bar", "req_dep", "ssl_lib"], got.deps) + self.assertEqual({}, got.deps_select) + + def test_self_dependencies_can_come_in_any_order(self): + deps = wheel.Deps( + "foo", + requires_dist=[ + "bar", + "baz; extra == 'feat'", + "foo[feat2]; extra == 'all'", + "foo[feat]; extra == 'feat2'", + "zdep; extra == 'all'", + ], + extras={"all"}, + ) + + got = deps.build() + + self.assertEqual(["bar", "baz", "zdep"], got.deps) + self.assertEqual({}, got.deps_select) + + def test_can_get_deps_based_on_specific_python_version(self): + requires_dist = [ + "bar", + "baz; python_full_version < '3.7.3'", + "posix_dep; os_name=='posix' and python_version >= '3.8'", + ] + + py38_deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=[ + Platform(os=OS.linux, arch=Arch.x86_64, minor_version=8), + ], + ).build() + py373_deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=[ + Platform( + os=OS.linux, arch=Arch.x86_64, minor_version=7, micro_version=3 + ), + ], + ).build() + py37_deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=[ + Platform(os=OS.linux, arch=Arch.x86_64, minor_version=7), + ], + ).build() + + self.assertEqual(["bar", "baz"], py37_deps.deps) + self.assertEqual({}, py37_deps.deps_select) + self.assertEqual(["bar"], py373_deps.deps) + self.assertEqual({}, py37_deps.deps_select) + self.assertEqual(["bar", "posix_dep"], py38_deps.deps) + self.assertEqual({}, py38_deps.deps_select) + + def test_no_version_select_when_single_version(self): + requires_dist = [ + "bar", + "baz; python_version >= '3.8'", + "posix_dep; os_name=='posix'", + "posix_dep_with_version; os_name=='posix' and python_version >= '3.8'", + "arch_dep; platform_machine=='x86_64' and python_version >= '3.8'", + ] + + self.maxDiff = None + + deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=[ + Platform( + os=os, arch=Arch.x86_64, minor_version=minor, micro_version=micro + ) + for minor, micro in [(8, 4)] + for os in [OS.linux, OS.windows] + ], + ) + got = deps.build() + + self.assertEqual(["arch_dep", "bar", "baz"], got.deps) + self.assertEqual( + { + "linux_x86_64": ["posix_dep", "posix_dep_with_version"], + }, + got.deps_select, + ) + + @mock.patch(_HOST_INTERPRETER_FN) + def test_can_get_version_select(self, mock_host_interpreter_version): + requires_dist = [ + "bar", + "baz; python_version < '3.8'", + "baz_new; python_version >= '3.8'", + "posix_dep; os_name=='posix'", + "posix_dep_with_version; os_name=='posix' and python_version >= '3.8'", + "arch_dep; platform_machine=='x86_64' and python_version < '3.8'", + ] + mock_host_interpreter_version.return_value = (7, 4) + + self.maxDiff = None + + deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=[ + Platform( + os=os, arch=Arch.x86_64, minor_version=minor, micro_version=micro + ) + for minor, micro in [(7, 4), (8, 8), (9, 8)] + for os in [OS.linux, OS.windows] + ], + ) + got = deps.build() + + self.assertEqual(["bar"], got.deps) + self.assertEqual( + { + "cp37.4_linux_x86_64": ["arch_dep", "baz", "posix_dep"], + "cp37.4_windows_x86_64": ["arch_dep", "baz"], + "cp38.8_linux_x86_64": [ + "baz_new", + "posix_dep", + "posix_dep_with_version", + ], + "cp38.8_windows_x86_64": ["baz_new"], + "cp39.8_linux_x86_64": [ + "baz_new", + "posix_dep", + "posix_dep_with_version", + ], + "cp39.8_windows_x86_64": ["baz_new"], + "linux_x86_64": ["arch_dep", "baz", "posix_dep"], + "windows_x86_64": ["arch_dep", "baz"], + }, + got.deps_select, + ) + + @mock.patch(_HOST_INTERPRETER_FN) + def test_deps_spanning_all_target_py_versions_are_added_to_common( + self, mock_host_version + ): + requires_dist = [ + "bar", + "baz (<2,>=1.11) ; python_version < '3.8'", + "baz (<2,>=1.14) ; python_version >= '3.8'", + ] + mock_host_version.return_value = (8, 4) + + self.maxDiff = None + + deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=Platform.from_string(["cp37_*", "cp38_*", "cp39_*"]), + ) + got = deps.build() + + self.assertEqual({}, got.deps_select) + self.assertEqual(["bar", "baz"], got.deps) + + @mock.patch(_HOST_INTERPRETER_FN) + def test_deps_are_not_duplicated(self, mock_host_version): + mock_host_version.return_value = (7, 4) + + # See an example in + # https://files.pythonhosted.org/packages/76/9e/db1c2d56c04b97981c06663384f45f28950a73d9acf840c4006d60d0a1ff/opencv_python-4.9.0.80-cp37-abi3-win32.whl.metadata + requires_dist = [ + "bar >=0.1.0 ; python_version < '3.7'", + "bar >=0.2.0 ; python_version >= '3.7'", + "bar >=0.4.0 ; python_version >= '3.6' and platform_system == 'Linux' and platform_machine == 'aarch64'", + "bar >=0.4.0 ; python_version >= '3.9'", + "bar >=0.5.0 ; python_version <= '3.9' and platform_system == 'Darwin' and platform_machine == 'arm64'", + "bar >=0.5.0 ; python_version >= '3.10' and platform_system == 'Darwin'", + "bar >=0.5.0 ; python_version >= '3.10'", + "bar >=0.6.0 ; python_version >= '3.11'", + ] + + deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=Platform.from_string(["cp37_*", "cp310_*"]), + ) + got = deps.build() + + self.assertEqual(["bar"], got.deps) + self.assertEqual({}, got.deps_select) + + @mock.patch(_HOST_INTERPRETER_FN) + def test_deps_are_not_duplicated_when_encountering_platform_dep_first( + self, mock_host_version + ): + mock_host_version.return_value = (7, 1) + + # Note, that we are sorting the incoming `requires_dist` and we need to ensure that we are not getting any + # issues even if the platform-specific line comes first. + requires_dist = [ + "bar >=0.4.0 ; python_version >= '3.6' and platform_system == 'Linux' and platform_machine == 'aarch64'", + "bar >=0.5.0 ; python_version >= '3.9'", + ] + + self.maxDiff = None + + deps = wheel.Deps( + "foo", + requires_dist=requires_dist, + platforms=Platform.from_string( + [ + "cp37.1_linux_x86_64", + "cp37.1_linux_aarch64", + "cp310_linux_x86_64", + "cp310_linux_aarch64", + ] + ), + ) + got = deps.build() + + self.assertEqual([], got.deps) + self.assertEqual( + { + "cp310_linux_aarch64": ["bar"], + "cp310_linux_x86_64": ["bar"], + "cp37.1_linux_aarch64": ["bar"], + "linux_aarch64": ["bar"], + }, + got.deps_select, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl index f738e03b5d..f0e5f57ac0 100644 --- a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl +++ b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl @@ -16,7 +16,7 @@ load("@rules_testing//lib:test_suite.bzl", "test_suite") load("//python/private:glob_excludes.bzl", "glob_excludes") # buildifier: disable=bzl-visibility -load("//python/private/pypi:whl_library_targets.bzl", "whl_library_targets") # buildifier: disable=bzl-visibility +load("//python/private/pypi:whl_library_targets.bzl", "whl_library_targets", "whl_library_targets_from_requires") # buildifier: disable=bzl-visibility _tests = [] @@ -68,9 +68,8 @@ def _test_platforms(env): "@//python/config_settings:is_python_3.9": ["py39_dep"], "@platforms//cpu:aarch64": ["arm_dep"], "@platforms//os:windows": ["win_dep"], + "cp310.11_linux_ppc64le": ["full_version_dep"], "cp310_linux_ppc64le": ["py310_linux_ppc64le_dep"], - "cp39_anyos_aarch64": ["py39_arm_dep"], - "cp39_linux_anyarch": ["py39_linux_dep"], "linux_x86_64": ["linux_intel_dep"], }, filegroups = {}, @@ -82,39 +81,34 @@ def _test_platforms(env): env.expect.that_collection(calls).contains_exactly([ { - "name": "is_python_3.10_linux_ppc64le", - "flag_values": { - "@rules_python//python/config_settings:python_version_major_minor": "3.10", - }, + "name": "is_python_3.10.11_linux_ppc64le", + "visibility": ["//visibility:private"], "constraint_values": [ "@platforms//cpu:ppc64le", "@platforms//os:linux", ], - "visibility": ["//visibility:private"], - }, - { - "name": "is_python_3.9_anyos_aarch64", "flag_values": { - "@rules_python//python/config_settings:python_version_major_minor": "3.9", + Label("//python/config_settings:python_version"): "3.10.11", }, - "constraint_values": ["@platforms//cpu:aarch64"], - "visibility": ["//visibility:private"], }, { - "name": "is_python_3.9_linux_anyarch", + "name": "is_python_3.10_linux_ppc64le", + "visibility": ["//visibility:private"], + "constraint_values": [ + "@platforms//cpu:ppc64le", + "@platforms//os:linux", + ], "flag_values": { - "@rules_python//python/config_settings:python_version_major_minor": "3.9", + Label("//python/config_settings:python_version"): "3.10", }, - "constraint_values": ["@platforms//os:linux"], - "visibility": ["//visibility:private"], }, { "name": "is_linux_x86_64", + "visibility": ["//visibility:private"], "constraint_values": [ "@platforms//cpu:x86_64", "@platforms//os:linux", ], - "visibility": ["//visibility:private"], }, ]) # buildifier: @unsorted-dict-items @@ -183,6 +177,87 @@ def _test_entrypoints(env): _tests.append(_test_entrypoints) +def _test_whl_and_library_deps_from_requires(env): + filegroup_calls = [] + py_library_calls = [] + env_marker_setting_calls = [] + + whl_library_targets_from_requires( + name = "foo-0-py3-none-any.whl", + metadata_name = "Foo", + metadata_version = "0", + dep_template = "@pypi//{name}:{target}", + requires_dist = [ + "foo", # this self-edge will be ignored + "bar", + "bar-baz; python_version < \"8.2\"", + "booo", # this is effectively excluded due to the list below + ], + include = ["foo", "bar", "bar_baz"], + data_exclude = [], + # Overrides for testing + filegroups = {}, + native = struct( + filegroup = lambda **kwargs: filegroup_calls.append(kwargs), + config_setting = lambda **_: None, + glob = _glob, + select = _select, + ), + rules = struct( + py_library = lambda **kwargs: py_library_calls.append(kwargs), + env_marker_setting = lambda **kwargs: env_marker_setting_calls.append(kwargs), + ), + ) + + env.expect.that_collection(filegroup_calls).contains_exactly([ + { + "name": "whl", + "srcs": ["foo-0-py3-none-any.whl"], + "data": ["@pypi//bar:whl"] + _select({ + ":is_include_bar_baz_true": ["@pypi//bar_baz:whl"], + "//conditions:default": [], + }), + "visibility": ["//visibility:public"], + }, + ]) # buildifier: @unsorted-dict-items + env.expect.that_collection(py_library_calls).contains_exactly([ + { + "name": "pkg", + "srcs": _glob( + ["site-packages/**/*.py"], + exclude = [], + allow_empty = True, + ), + "pyi_srcs": _glob(["site-packages/**/*.pyi"], allow_empty = True), + "data": [] + _glob( + ["site-packages/**/*"], + exclude = [ + "**/*.py", + "**/*.pyc", + "**/*.pyc.*", + "**/*.dist-info/RECORD", + ] + glob_excludes.version_dependent_exclusions(), + ), + "imports": ["site-packages"], + "deps": ["@pypi//bar:pkg"] + _select({ + ":is_include_bar_baz_true": ["@pypi//bar_baz:pkg"], + "//conditions:default": [], + }), + "tags": ["pypi_name=Foo", "pypi_version=0"], + "visibility": ["//visibility:public"], + "experimental_venvs_site_packages": Label("//python/config_settings:venvs_site_packages"), + }, + ]) # buildifier: @unsorted-dict-items + env.expect.that_collection(env_marker_setting_calls).contains_exactly([ + { + "name": "include_bar_baz", + "expression": "python_version < \"8.2\"", + "visibility": ["//visibility:private"], + }, + ]) # buildifier: @unsorted-dict-items + +_tests.append(_test_whl_and_library_deps_from_requires) + def _test_whl_and_library_deps(env): filegroup_calls = [] py_library_calls = [] diff --git a/tests/pypi/whl_metadata/whl_metadata_tests.bzl b/tests/pypi/whl_metadata/whl_metadata_tests.bzl index 4acbc9213d..329423a26c 100644 --- a/tests/pypi/whl_metadata/whl_metadata_tests.bzl +++ b/tests/pypi/whl_metadata/whl_metadata_tests.bzl @@ -140,6 +140,37 @@ Requires-Dist: this will be ignored _tests.append(_test_parse_metadata_all) +def _test_parse_metadata_multiline_license(env): + got = _parse_whl_metadata( + env, + # NOTE: The trailing whitespace here is meaningful as an empty line + # denotes the end of the header. + contents = """\ +Name: foo +Version: 0.0.1 +License: some License + + some line + + another line + +Requires-Dist: bar; extra == "all" +Provides-Extra: all + +Requires-Dist: this will be ignored +""", + ) + got.name().equals("foo") + got.version().equals("0.0.1") + got.requires_dist().contains_exactly([ + "bar; extra == \"all\"", + ]) + got.provides_extra().contains_exactly([ + "all", + ]) + +_tests.append(_test_parse_metadata_multiline_license) + def whl_metadata_test_suite(name): # buildifier: disable=function-docstring test_suite( name = name, diff --git a/tests/pypi/whl_repo_name/whl_repo_name_tests.bzl b/tests/pypi/whl_repo_name/whl_repo_name_tests.bzl index f0d1d059e1..35e6bcdf9f 100644 --- a/tests/pypi/whl_repo_name/whl_repo_name_tests.bzl +++ b/tests/pypi/whl_repo_name/whl_repo_name_tests.bzl @@ -54,6 +54,18 @@ def _test_platform_whl(env): _tests.append(_test_platform_whl) +def _test_name_with_plus(env): + got = whl_repo_name("gptqmodel-2.0.0+cu126torch2.6-cp312-cp312-linux_x86_64.whl", "") + env.expect.that_str(got).equals("gptqmodel_2_0_0_cu126torch2_6_cp312_cp312_linux_x86_64") + +_tests.append(_test_name_with_plus) + +def _test_name_with_percent(env): + got = whl_repo_name("gptqmodel-2.0.0%2Bcu126torch2.6-cp312-cp312-linux_x86_64.whl", "") + env.expect.that_str(got).equals("gptqmodel_2_0_0_2Bcu126torch2_6_cp312_cp312_linux_x86_64") + +_tests.append(_test_name_with_percent) + def whl_repo_name_test_suite(name): """Create the test suite. diff --git a/tests/pypi/whl_target_platforms/select_whl_tests.bzl b/tests/pypi/whl_target_platforms/select_whl_tests.bzl index 8ab24138d1..1674ac5ef2 100644 --- a/tests/pypi/whl_target_platforms/select_whl_tests.bzl +++ b/tests/pypi/whl_target_platforms/select_whl_tests.bzl @@ -289,6 +289,22 @@ def _test_freethreaded_wheels(env): _tests.append(_test_freethreaded_wheels) +def _test_micro_version_freethreaded(env): + # Check we prefer platform specific wheels + got = _select_whls(whls = WHL_LIST, want_platforms = ["cp313.3_linux_x86_64"]) + _match( + env, + got, + "pkg-0.0.1-cp313-cp313t-musllinux_1_1_x86_64.whl", + "pkg-0.0.1-cp313-cp313-musllinux_1_1_x86_64.whl", + "pkg-0.0.1-cp313-abi3-musllinux_1_1_x86_64.whl", + "pkg-0.0.1-cp313-none-musllinux_1_1_x86_64.whl", + "pkg-0.0.1-cp39-abi3-any.whl", + "pkg-0.0.1-py3-none-any.whl", + ) + +_tests.append(_test_micro_version_freethreaded) + def select_whl_test_suite(name): """Create the test suite. diff --git a/tests/python/python_tests.bzl b/tests/python/python_tests.bzl index 97c47b57db..116afa76ad 100644 --- a/tests/python/python_tests.bzl +++ b/tests/python/python_tests.bzl @@ -17,6 +17,7 @@ load("@pythons_hub//:versions.bzl", "MINOR_MAPPING") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("//python/private:python.bzl", "parse_modules") # buildifier: disable=bzl-visibility +load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl-visibility _tests = [] @@ -131,6 +132,10 @@ def _single_version_platform_override( python_version = python_version, patch_strip = patch_strip, patches = patches, + target_compatible_with = [], + target_settings = [], + os_name = "", + arch = "", ) def _test_default(env): @@ -138,6 +143,7 @@ def _test_default(env): module_ctx = _mock_mctx( _mod(name = "rules_python", toolchain = [_toolchain("3.11")]), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) # The value there should be consistent in bzlmod with the automatically @@ -149,6 +155,7 @@ def _test_default(env): "base_url", "ignore_root_user_error", "tool_versions", + "platforms", ]) env.expect.that_bool(py.config.default["ignore_root_user_error"]).equals(True) env.expect.that_str(py.default_python_version).equals("3.11") @@ -167,6 +174,7 @@ def _test_default_some_module(env): module_ctx = _mock_mctx( _mod(name = "rules_python", toolchain = [_toolchain("3.11")], is_root = False), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.11") @@ -185,6 +193,7 @@ def _test_default_with_patch_version(env): module_ctx = _mock_mctx( _mod(name = "rules_python", toolchain = [_toolchain("3.11.2")]), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.11.2") @@ -206,6 +215,7 @@ def _test_default_non_rules_python(env): # does not make any calls to the extension. _mod(name = "rules_python", toolchain = [_toolchain("3.11")], is_root = False), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.11") @@ -227,6 +237,7 @@ def _test_default_non_rules_python_ignore_root_user_error(env): ), _mod(name = "rules_python", toolchain = [_toolchain("3.11")]), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_bool(py.config.default["ignore_root_user_error"]).equals(False) @@ -256,6 +267,7 @@ def _test_default_non_rules_python_ignore_root_user_error_non_root_module(env): _mod(name = "some_module", toolchain = [_toolchain("3.12", ignore_root_user_error = False)]), _mod(name = "rules_python", toolchain = [_toolchain("3.11")]), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.13") @@ -301,6 +313,7 @@ def _test_toolchain_ordering(env): ), _mod(name = "rules_python", toolchain = [_toolchain("3.11")]), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) got_versions = [ t.python_version @@ -346,6 +359,7 @@ def _test_default_from_defaults(env): is_root = True, ), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.11") @@ -373,6 +387,7 @@ def _test_default_from_defaults_env(env): ), environ = {"PYENV_VERSION": "3.12"}, ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.12") @@ -400,6 +415,7 @@ def _test_default_from_defaults_file(env): ), mocked_files = {"@@//:.python-version": "3.12\n"}, ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.12") @@ -426,6 +442,7 @@ def _test_first_occurance_of_the_toolchain_wins(env): "RULES_PYTHON_BZLMOD_DEBUG": "1", }, ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.12") @@ -471,6 +488,7 @@ def _test_auth_overrides(env): ), _mod(name = "rules_python", toolchain = [_toolchain("3.11")]), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_dict(py.config.default).contains_at_least({ @@ -540,6 +558,7 @@ def _test_add_new_version(env): ], ), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.13") @@ -608,6 +627,7 @@ def _test_register_all_versions(env): ], ), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.13") @@ -684,6 +704,7 @@ def _test_add_patches(env): ], ), ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_str(py.default_python_version).equals("3.13") @@ -730,6 +751,7 @@ def _test_fail_two_overrides(env): ), ), _fail = errors.append, + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_collection(errors).contains_exactly([ "Only a single 'python.override' can be present", @@ -746,12 +768,6 @@ def _test_single_version_override_errors(env): ], want_error = "Only a single 'python.single_version_override' can be present for '3.12.4'", ), - struct( - overrides = [ - _single_version_override(python_version = "3.12.4+3", distutils_content = "foo"), - ], - want_error = "The 'python_version' attribute needs to specify an 'X.Y.Z' semver-compatible version, got: '3.12.4+3'", - ), ]: errors = [] parse_modules( @@ -763,6 +779,7 @@ def _test_single_version_override_errors(env): ), ), _fail = errors.append, + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_collection(errors).contains_exactly([test.want_error]) @@ -781,13 +798,13 @@ def _test_single_version_platform_override_errors(env): overrides = [ _single_version_platform_override(python_version = "3.12", platform = "foo"), ], - want_error = "The 'python_version' attribute needs to specify an 'X.Y.Z' semver-compatible version, got: '3.12'", + want_error = "The 'python_version' attribute needs to specify the full version in at least 'X.Y.Z' format, got: '3.12'", ), struct( overrides = [ - _single_version_platform_override(python_version = "3.12.1+my_build", platform = "foo"), + _single_version_platform_override(python_version = "foo", platform = "foo"), ], - want_error = "The 'python_version' attribute needs to specify an 'X.Y.Z' semver-compatible version, got: '3.12.1+my_build'", + want_error = "Failed to parse PEP 440 version identifier 'foo'. Parse error at 'foo'", ), ]: errors = [] @@ -799,7 +816,8 @@ def _test_single_version_platform_override_errors(env): single_version_platform_override = test.overrides, ), ), - _fail = errors.append, + _fail = lambda *a: errors.append(" ".join(a)), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), ) env.expect.that_collection(errors).contains_exactly([test.want_error]) diff --git a/tests/repl/BUILD.bazel b/tests/repl/BUILD.bazel new file mode 100644 index 0000000000..b3986cc023 --- /dev/null +++ b/tests/repl/BUILD.bazel @@ -0,0 +1,44 @@ +load("//python:py_library.bzl", "py_library") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") + +# A library that adds a special import path only when this is specified as a +# dependency. This makes it easy for a dependency to have this import path +# available without the top-level target being able to import the module. +py_library( + name = "helper/test_module", + srcs = [ + "helper/test_module.py", + ], + imports = [ + "helper", + ], +) + +py_reconfig_test( + name = "repl_without_dep_test", + srcs = ["repl_test.py"], + data = [ + "//python/bin:repl", + ], + env = { + # The helper/test_module should _not_ be importable for this test. + "EXPECT_TEST_MODULE_IMPORTABLE": "0", + }, + main = "repl_test.py", + python_version = "3.12", +) + +py_reconfig_test( + name = "repl_with_dep_test", + srcs = ["repl_test.py"], + data = [ + "//python/bin:repl", + ], + env = { + # The helper/test_module _should_ be importable for this test. + "EXPECT_TEST_MODULE_IMPORTABLE": "1", + }, + main = "repl_test.py", + python_version = "3.12", + repl_dep = ":helper/test_module", +) diff --git a/tests/repl/helper/test_module.py b/tests/repl/helper/test_module.py new file mode 100644 index 0000000000..0c4a309b01 --- /dev/null +++ b/tests/repl/helper/test_module.py @@ -0,0 +1,5 @@ +"""This is a file purely intended for validating //python/bin:repl.""" + + +def print_hello(): + print("Hello World") diff --git a/tests/repl/repl_test.py b/tests/repl/repl_test.py new file mode 100644 index 0000000000..37c9a37a0d --- /dev/null +++ b/tests/repl/repl_test.py @@ -0,0 +1,122 @@ +import os +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path +from typing import Iterable + +from python import runfiles + +rfiles = runfiles.Create() + +# Signals the tests below whether we should be expecting the import of +# helpers/test_module.py on the REPL to work or not. +EXPECT_TEST_MODULE_IMPORTABLE = os.environ["EXPECT_TEST_MODULE_IMPORTABLE"] == "1" + + +# An arbitrary piece of code that sets some kind of variable. The variable needs to persist into the +# actual shell. +PYTHONSTARTUP_SETS_VAR = """\ +foo = 1234 +""" + + +class ReplTest(unittest.TestCase): + def setUp(self): + self.repl = rfiles.Rlocation("rules_python/python/bin/repl") + assert self.repl + + def run_code_in_repl(self, lines: Iterable[str], *, env=None) -> str: + """Runs the lines of code in the REPL and returns the text output.""" + return subprocess.check_output( + [self.repl], + text=True, + stderr=subprocess.STDOUT, + input="\n".join(lines), + env=env, + ).strip() + + def test_repl_version(self): + """Validates that we can successfully execute arbitrary code on the REPL.""" + + result = self.run_code_in_repl( + [ + "import sys", + "v = sys.version_info", + "print(f'version: {v.major}.{v.minor}')", + ] + ) + self.assertIn("version: 3.12", result) + + def test_cannot_import_test_module_directly(self): + """Validates that we cannot import helper/test_module.py since it's not a direct dep.""" + with self.assertRaises(ModuleNotFoundError): + import test_module + + @unittest.skipIf( + not EXPECT_TEST_MODULE_IMPORTABLE, "test only works without repl_dep set" + ) + def test_import_test_module_success(self): + """Validates that we can import helper/test_module.py when repl_dep is set.""" + result = self.run_code_in_repl( + [ + "import test_module", + "test_module.print_hello()", + ] + ) + self.assertIn("Hello World", result) + + @unittest.skipIf( + EXPECT_TEST_MODULE_IMPORTABLE, "test only works without repl_dep set" + ) + def test_import_test_module_failure(self): + """Validates that we cannot import helper/test_module.py when repl_dep isn't set.""" + result = self.run_code_in_repl( + [ + "import test_module", + ] + ) + self.assertIn("ModuleNotFoundError: No module named 'test_module'", result) + + def test_pythonstartup_gets_executed(self): + """Validates that we can use the variables from PYTHONSTARTUP in the console itself.""" + with tempfile.TemporaryDirectory() as tempdir: + pythonstartup = Path(tempdir) / "pythonstartup.py" + pythonstartup.write_text(PYTHONSTARTUP_SETS_VAR) + + env = os.environ.copy() + env["PYTHONSTARTUP"] = str(pythonstartup) + + result = self.run_code_in_repl( + [ + "print(f'The value of foo is {foo}')", + ], + env=env, + ) + + self.assertIn("The value of foo is 1234", result) + + def test_pythonstartup_doesnt_leak(self): + """Validates that we don't accidentally leak code into the console. + + This test validates that a few of the variables we use in the template and stub are not + accessible in the REPL itself. + """ + with tempfile.TemporaryDirectory() as tempdir: + pythonstartup = Path(tempdir) / "pythonstartup.py" + pythonstartup.write_text(PYTHONSTARTUP_SETS_VAR) + + env = os.environ.copy() + env["PYTHONSTARTUP"] = str(pythonstartup) + + for var_name in ("exitmsg", "sys", "code", "bazel_runfiles", "STUB_PATH"): + with self.subTest(var_name=var_name): + result = self.run_code_in_repl([f"print({var_name})"], env=env) + self.assertIn( + f"NameError: name '{var_name}' is not defined", result + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/runtime_env_toolchain/BUILD.bazel b/tests/runtime_env_toolchain/BUILD.bazel index afc6b587f0..2f82d204ff 100644 --- a/tests/runtime_env_toolchain/BUILD.bazel +++ b/tests/runtime_env_toolchain/BUILD.bazel @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("@rules_python_runtime_env_tc_info//:info.bzl", "PYTHON_VERSION") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") load("//tests/support:support.bzl", "CC_TOOLCHAIN") load(":runtime_env_toolchain_tests.bzl", "runtime_env_toolchain_test_suite") @@ -30,5 +31,12 @@ py_reconfig_test( CC_TOOLCHAIN, ], main = "toolchain_runs_test.py", + # With bootstrap=script, the build version must match the runtime version + # because the venv has the version in the lib/site-packages dir name. + python_version = PYTHON_VERSION, + # Our RBE has Python 3.6, which is too old for the language features + # we use now. Using the runtime-env toolchain on RBE is pretty + # questionable anyways. + tags = ["no-remote-exec"], deps = ["//python/runfiles"], ) diff --git a/tests/semver/BUILD.bazel b/tests/semver/BUILD.bazel deleted file mode 100644 index e12b1e5300..0000000000 --- a/tests/semver/BUILD.bazel +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2024 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -load(":semver_test.bzl", "semver_test_suite") - -semver_test_suite(name = "semver_tests") diff --git a/tests/semver/semver_test.bzl b/tests/semver/semver_test.bzl deleted file mode 100644 index aef3deca82..0000000000 --- a/tests/semver/semver_test.bzl +++ /dev/null @@ -1,131 +0,0 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"" - -load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("//python/private:semver.bzl", "semver") # buildifier: disable=bzl-visibility - -_tests = [] - -def _test_semver_from_major(env): - actual = semver("3") - env.expect.that_int(actual.major).equals(3) - env.expect.that_int(actual.minor).equals(None) - env.expect.that_int(actual.patch).equals(None) - env.expect.that_str(actual.build).equals("") - -_tests.append(_test_semver_from_major) - -def _test_semver_from_major_minor_version(env): - actual = semver("4.9") - env.expect.that_int(actual.major).equals(4) - env.expect.that_int(actual.minor).equals(9) - env.expect.that_int(actual.patch).equals(None) - env.expect.that_str(actual.build).equals("") - -_tests.append(_test_semver_from_major_minor_version) - -def _test_semver_with_build_info(env): - actual = semver("1.2.3+mybuild") - env.expect.that_int(actual.major).equals(1) - env.expect.that_int(actual.minor).equals(2) - env.expect.that_int(actual.patch).equals(3) - env.expect.that_str(actual.build).equals("mybuild") - -_tests.append(_test_semver_with_build_info) - -def _test_semver_with_build_info_multiple_pluses(env): - actual = semver("1.2.3-rc0+build+info") - env.expect.that_int(actual.major).equals(1) - env.expect.that_int(actual.minor).equals(2) - env.expect.that_int(actual.patch).equals(3) - env.expect.that_str(actual.pre_release).equals("rc0") - env.expect.that_str(actual.build).equals("build+info") - -_tests.append(_test_semver_with_build_info_multiple_pluses) - -def _test_semver_alpha_beta(env): - actual = semver("1.2.3-alpha.beta") - env.expect.that_int(actual.major).equals(1) - env.expect.that_int(actual.minor).equals(2) - env.expect.that_int(actual.patch).equals(3) - env.expect.that_str(actual.pre_release).equals("alpha.beta") - -_tests.append(_test_semver_alpha_beta) - -def _test_semver_sort(env): - want = [ - semver(item) - for item in [ - # The items are sorted from lowest to highest version - "0.0.1", - "0.1.0-rc", - "0.1.0", - "0.9.11", - "0.9.12", - "1.0.0-alpha", - "1.0.0-alpha.1", - "1.0.0-alpha.beta", - "1.0.0-beta", - "1.0.0-beta.2", - "1.0.0-beta.11", - "1.0.0-rc.1", - "1.0.0-rc.2", - "1.0.0", - # Also handle missing minor and patch version strings - "2.0", - "3", - # Alphabetic comparison for different builds - "3.0.0+build0", - "3.0.0+build1", - ] - ] - actual = sorted(want, key = lambda x: x.key()) - env.expect.that_collection(actual).contains_exactly(want).in_order() - for i, greater in enumerate(want[1:]): - smaller = actual[i] - if greater.key() <= smaller.key(): - env.fail("Expected '{}' to be smaller than '{}', but got otherwise".format( - smaller.str(), - greater.str(), - )) - -_tests.append(_test_semver_sort) - -def _test_upper(env): - for input, want in { - # Depending on how many version numbers are specified we will increase - # the upper bound differently. See https://packaging.python.org/en/latest/specifications/version-specifiers/#compatible-release for docs - "0.0.1": "0.1.0", - "0.1": "1.0", - "0.1.0": "0.2.0", - "1": "2", - "1.0.0-pre": "1.1.0", # pre-release info is dropped - "1.2.0": "1.3.0", - "2.0.0+build0": "2.1.0", # build info is dropped - }.items(): - actual = semver(input).upper().key() - want = semver(want).key() - env.expect.that_collection(actual).contains_exactly(want).in_order() - -_tests.append(_test_upper) - -def semver_test_suite(name): - """Create the test suite. - - Args: - name: the name of the test suite - """ - test_suite(name = name, basic_tests = _tests) diff --git a/tests/support/BUILD.bazel b/tests/support/BUILD.bazel index 9fb5cd0760..303dbafbdf 100644 --- a/tests/support/BUILD.bazel +++ b/tests/support/BUILD.bazel @@ -18,6 +18,7 @@ # to force them to resolve in the proper context. # ==================== +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") load(":sh_py_run_test.bzl", "current_build_settings") package( @@ -90,3 +91,15 @@ platform( current_build_settings( name = "current_build_settings", ) + +string_flag( + name = "custom_runtime", + build_setting_default = "", +) + +config_setting( + name = "is_custom_runtime_linux-x86-install-only-stripped", + flag_values = { + ":custom_runtime": "linux-x86-install-only-stripped", + }, +) diff --git a/tests/support/py_reconfig.bzl b/tests/support/py_reconfig.bzl new file mode 100644 index 0000000000..b33f679e77 --- /dev/null +++ b/tests/support/py_reconfig.bzl @@ -0,0 +1,101 @@ +# Copyright 2024 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Run a py_binary/py_test with altered config settings. + +This facilitates verify running binaries with different configuration settings +without the overhead of a bazel-in-bazel integration test. +""" + +load("//python/private:attr_builders.bzl", "attrb") # buildifier: disable=bzl-visibility +load("//python/private:py_binary_macro.bzl", "py_binary_macro") # buildifier: disable=bzl-visibility +load("//python/private:py_binary_rule.bzl", "create_py_binary_rule_builder") # buildifier: disable=bzl-visibility +load("//python/private:py_test_macro.bzl", "py_test_macro") # buildifier: disable=bzl-visibility +load("//python/private:py_test_rule.bzl", "create_py_test_rule_builder") # buildifier: disable=bzl-visibility +load("//tests/support:support.bzl", "VISIBLE_FOR_TESTING") + +def _perform_transition_impl(input_settings, attr, base_impl): + settings = {k: input_settings[k] for k in _RECONFIG_INHERITED_OUTPUTS if k in input_settings} + settings.update(base_impl(input_settings, attr)) + + settings[VISIBLE_FOR_TESTING] = True + settings["//command_line_option:build_python_zip"] = attr.build_python_zip + if attr.bootstrap_impl: + settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl + if attr.extra_toolchains: + settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains + if attr.python_src: + settings["//python/bin:python_src"] = attr.python_src + if attr.repl_dep: + settings["//python/bin:repl_dep"] = attr.repl_dep + if attr.venvs_use_declare_symlink: + settings["//python/config_settings:venvs_use_declare_symlink"] = attr.venvs_use_declare_symlink + if attr.venvs_site_packages: + settings["//python/config_settings:venvs_site_packages"] = attr.venvs_site_packages + return settings + +_RECONFIG_INPUTS = [ + "//python/config_settings:bootstrap_impl", + "//python/bin:python_src", + "//python/bin:repl_dep", + "//command_line_option:extra_toolchains", + "//python/config_settings:venvs_use_declare_symlink", + "//python/config_settings:venvs_site_packages", +] +_RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [ + "//command_line_option:build_python_zip", + VISIBLE_FOR_TESTING, +] +_RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_INPUTS] + +_RECONFIG_ATTRS = { + "bootstrap_impl": attrb.String(), + "build_python_zip": attrb.String(default = "auto"), + "extra_toolchains": attrb.StringList( + doc = """ +Value for the --extra_toolchains flag. + +NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or some CC toolchain) +to make the RBE presubmits happy, which disable auto-detection of a CC +toolchain. +""", + ), + "python_src": attrb.Label(), + "repl_dep": attrb.Label(), + "venvs_site_packages": attrb.String(), + "venvs_use_declare_symlink": attrb.String(), +} + +def _create_reconfig_rule(builder): + builder.attrs.update(_RECONFIG_ATTRS) + + base_cfg_impl = builder.cfg.implementation() + builder.cfg.set_implementation(lambda *args: _perform_transition_impl(base_impl = base_cfg_impl, *args)) + builder.cfg.update_inputs(_RECONFIG_INPUTS) + builder.cfg.update_outputs(_RECONFIG_OUTPUTS) + return builder.build() + +_py_reconfig_binary = _create_reconfig_rule(create_py_binary_rule_builder()) + +_py_reconfig_test = _create_reconfig_rule(create_py_test_rule_builder()) + +def py_reconfig_test(**kwargs): + """Create a py_test with customized build settings for testing. + + Args: + **kwargs: kwargs to pass along to _py_reconfig_test. + """ + py_test_macro(_py_reconfig_test, **kwargs) + +def py_reconfig_binary(**kwargs): + py_binary_macro(_py_reconfig_binary, **kwargs) diff --git a/tests/support/sh_py_run_test.bzl b/tests/support/sh_py_run_test.bzl index 9c8134ff40..69141fe8a4 100644 --- a/tests/support/sh_py_run_test.bzl +++ b/tests/support/sh_py_run_test.bzl @@ -32,25 +32,24 @@ def _perform_transition_impl(input_settings, attr, base_impl): settings[VISIBLE_FOR_TESTING] = True settings["//command_line_option:build_python_zip"] = attr.build_python_zip - if attr.bootstrap_impl: - settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl - if attr.extra_toolchains: - settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains - if attr.python_src: - settings["//python/bin:python_src"] = attr.python_src - if attr.venvs_use_declare_symlink: - settings["//python/config_settings:venvs_use_declare_symlink"] = attr.venvs_use_declare_symlink - if attr.venvs_site_packages: - settings["//python/config_settings:venvs_site_packages"] = attr.venvs_site_packages + + for attr_name, setting_label in _RECONFIG_ATTR_SETTING_MAP.items(): + if getattr(attr, attr_name): + settings[setting_label] = getattr(attr, attr_name) return settings -_RECONFIG_INPUTS = [ - "//python/config_settings:bootstrap_impl", - "//python/bin:python_src", - "//command_line_option:extra_toolchains", - "//python/config_settings:venvs_use_declare_symlink", - "//python/config_settings:venvs_site_packages", -] +# Attributes that, if non-falsey (`if attr.`), will copy their +# value into the output settings +_RECONFIG_ATTR_SETTING_MAP = { + "bootstrap_impl": "//python/config_settings:bootstrap_impl", + "custom_runtime": "//tests/support:custom_runtime", + "extra_toolchains": "//command_line_option:extra_toolchains", + "python_src": "//python/bin:python_src", + "venvs_site_packages": "//python/config_settings:venvs_site_packages", + "venvs_use_declare_symlink": "//python/config_settings:venvs_use_declare_symlink", +} + +_RECONFIG_INPUTS = _RECONFIG_ATTR_SETTING_MAP.values() _RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [ "//command_line_option:build_python_zip", VISIBLE_FOR_TESTING, @@ -60,6 +59,7 @@ _RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_IN _RECONFIG_ATTRS = { "bootstrap_impl": attrb.String(), "build_python_zip": attrb.String(default = "auto"), + "custom_runtime": attrb.String(), "extra_toolchains": attrb.StringList( doc = """ Value for the --extra_toolchains flag. diff --git a/tests/support/support.bzl b/tests/support/support.bzl index 6330155d8c..7bab263c66 100644 --- a/tests/support/support.bzl +++ b/tests/support/support.bzl @@ -37,6 +37,7 @@ CROSSTOOL_TOP = Label("//tests/support/cc_toolchains:cc_toolchain_suite") ADD_SRCS_TO_RUNFILES = str(Label("//python/config_settings:add_srcs_to_runfiles")) BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl")) EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain")) +PIP_ENV_MARKER_CONFIG = str(Label("//python/config_settings:pip_env_marker_config")) PRECOMPILE = str(Label("//python/config_settings:precompile")) PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention")) PYC_COLLECTION = str(Label("//python/config_settings:pyc_collection")) diff --git a/tests/toolchains/BUILD.bazel b/tests/toolchains/BUILD.bazel index c55dc92a7d..f346651d46 100644 --- a/tests/toolchains/BUILD.bazel +++ b/tests/toolchains/BUILD.bazel @@ -12,8 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility +load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") load(":defs.bzl", "define_toolchain_tests") define_toolchain_tests( name = "toolchain_tests", ) + +py_reconfig_test( + name = "custom_platform_toolchain_test", + srcs = ["custom_platform_toolchain_test.py"], + custom_runtime = "linux-x86-install-only-stripped", + python_version = "3.13.1", + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ] if BZLMOD_ENABLED else ["@platforms//:incompatible"], +) diff --git a/tests/toolchains/custom_platform_toolchain_test.py b/tests/toolchains/custom_platform_toolchain_test.py new file mode 100644 index 0000000000..d6c083a6a2 --- /dev/null +++ b/tests/toolchains/custom_platform_toolchain_test.py @@ -0,0 +1,15 @@ +import sys +import unittest + + +class VerifyCustomPlatformToolchainTest(unittest.TestCase): + + def test_custom_platform_interpreter_used(self): + # We expect the repo name, and thus path, to have the + # platform name in it. + self.assertIn("linux-x86-install-only-stripped", sys._base_executable) + print(sys._base_executable) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/toolchains/defs.bzl b/tests/toolchains/defs.bzl index fbb70820c9..a883b0af33 100644 --- a/tests/toolchains/defs.bzl +++ b/tests/toolchains/defs.bzl @@ -15,7 +15,7 @@ "" load("//python:versions.bzl", "PLATFORMS", "TOOL_VERSIONS") -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") def define_toolchain_tests(name): """Define the toolchain tests. diff --git a/tests/uv/lock/lock_tests.bzl b/tests/uv/lock/lock_tests.bzl index 35c7c19328..1eb5b1d903 100644 --- a/tests/uv/lock/lock_tests.bzl +++ b/tests/uv/lock/lock_tests.bzl @@ -16,7 +16,7 @@ load("@bazel_skylib//rules:native_binary.bzl", "native_test") load("//python/uv:lock.bzl", "lock") -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") def lock_test_suite(name): """The test suite with various lock-related integration tests diff --git a/tests/uv/uv/uv_tests.bzl b/tests/uv/uv/uv_tests.bzl index bf0deefa88..b464dab55c 100644 --- a/tests/uv/uv/uv_tests.bzl +++ b/tests/uv/uv/uv_tests.bzl @@ -100,7 +100,7 @@ def _mod(*, name = None, default = [], configure = [], is_root = True): ) def _process_modules(env, **kwargs): - result = process_modules(hub_repo = struct, **kwargs) + result = process_modules(hub_repo = struct, get_auth = lambda *_, **__: None, **kwargs) return env.expect.that_struct( struct( @@ -124,6 +124,8 @@ def _default( platform = None, target_settings = None, version = None, + netrc = None, + auth_patterns = None, **kwargs): return struct( base_url = base_url, @@ -132,6 +134,8 @@ def _default( platform = platform, target_settings = [] + (target_settings or []), # ensure that the type is correct version = version, + netrc = netrc, + auth_patterns = {} | (auth_patterns or {}), # ensure that the type is correct **kwargs ) @@ -377,6 +381,11 @@ def _test_complex_configuring(env): platform = "linux", compatible_with = ["@platforms//os:linux"], ), + _configure( + version = "1.0.4", + netrc = "~/.my_netrc", + auth_patterns = {"foo": "bar"}, + ), # use auth ], ), ), @@ -388,18 +397,21 @@ def _test_complex_configuring(env): "1_0_1_osx", "1_0_2_osx", "1_0_3_linux", + "1_0_4_osx", ]) uv.implementations().contains_exactly({ "1_0_0_osx": "@uv_1_0_0_osx//:uv_toolchain", "1_0_1_osx": "@uv_1_0_1_osx//:uv_toolchain", "1_0_2_osx": "@uv_1_0_2_osx//:uv_toolchain", "1_0_3_linux": "@uv_1_0_3_linux//:uv_toolchain", + "1_0_4_osx": "@uv_1_0_4_osx//:uv_toolchain", }) uv.compatible_with().contains_exactly({ "1_0_0_osx": ["@platforms//os:os"], "1_0_1_osx": ["@platforms//os:os"], "1_0_2_osx": ["@platforms//os:different"], "1_0_3_linux": ["@platforms//os:linux"], + "1_0_4_osx": ["@platforms//os:os"], }) uv.target_settings().contains_exactly({}) env.expect.that_collection(calls).contains_exactly([ @@ -431,6 +443,15 @@ def _test_complex_configuring(env): "urls": ["https://example.org/1.0.3/linux"], "version": "1.0.3", }, + { + "auth_patterns": {"foo": "bar"}, + "name": "uv_1_0_4_osx", + "netrc": "~/.my_netrc", + "platform": "osx", + "sha256": "deadb00f", + "urls": ["https://example.org/1.0.4/osx"], + "version": "1.0.4", + }, ]) _tests.append(_test_complex_configuring) diff --git a/tests/venv_site_packages_libs/BUILD.bazel b/tests/venv_site_packages_libs/BUILD.bazel index 5d02708800..d5a4fe6750 100644 --- a/tests/venv_site_packages_libs/BUILD.bazel +++ b/tests/venv_site_packages_libs/BUILD.bazel @@ -1,4 +1,4 @@ -load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test") +load("//tests/support:py_reconfig.bzl", "py_reconfig_test") load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT") py_reconfig_test( @@ -13,5 +13,6 @@ py_reconfig_test( "//tests/venv_site_packages_libs/nspkg_beta", "@other//nspkg_delta", "@other//nspkg_gamma", + "@other//nspkg_single", ], ) diff --git a/tests/venv_site_packages_libs/bin.py b/tests/venv_site_packages_libs/bin.py index b944be69e3..58572a2a1e 100644 --- a/tests/venv_site_packages_libs/bin.py +++ b/tests/venv_site_packages_libs/bin.py @@ -26,6 +26,7 @@ def test_imported_from_venv(self): self.assert_imported_from_venv("nspkg.subnspkg.beta") self.assert_imported_from_venv("nspkg.subnspkg.gamma") self.assert_imported_from_venv("nspkg.subnspkg.delta") + self.assert_imported_from_venv("single_file") if __name__ == "__main__": diff --git a/tests/venv_site_packages_libs/nspkg_alpha/BUILD.bazel b/tests/venv_site_packages_libs/nspkg_alpha/BUILD.bazel index c40c3b4080..aec415f7a0 100644 --- a/tests/venv_site_packages_libs/nspkg_alpha/BUILD.bazel +++ b/tests/venv_site_packages_libs/nspkg_alpha/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_python//python:py_library.bzl", "py_library") +load("//python:py_library.bzl", "py_library") package(default_visibility = ["//visibility:public"]) diff --git a/tests/venv_site_packages_libs/venv_site_packages_pypi_test.py b/tests/venv_site_packages_libs/venv_site_packages_pypi_test.py deleted file mode 100644 index 519b258044..0000000000 --- a/tests/venv_site_packages_libs/venv_site_packages_pypi_test.py +++ /dev/null @@ -1,36 +0,0 @@ -import os -import sys -import unittest - - -class VenvSitePackagesLibraryTest(unittest.TestCase): - def test_imported_from_venv(self): - self.assertNotEqual(sys.prefix, sys.base_prefix, "Not running under a venv") - venv = sys.prefix - - from nspkg.subnspkg import alpha - - self.assertEqual(alpha.whoami, "alpha") - self.assertEqual(alpha.__name__, "nspkg.subnspkg.alpha") - - self.assertTrue( - alpha.__file__.startswith(sys.prefix), - f"\nalpha was imported, not from within the venv.\n" - + f"venv : {venv}\n" - + f"actual: {alpha.__file__}", - ) - - from nspkg.subnspkg import beta - - self.assertEqual(beta.whoami, "beta") - self.assertEqual(beta.__name__, "nspkg.subnspkg.beta") - self.assertTrue( - beta.__file__.startswith(sys.prefix), - f"\nbeta was imported, not from within the venv.\n" - + f"venv : {venv}\n" - + f"actual: {beta.__file__}", - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/version/BUILD.bazel b/tests/version/BUILD.bazel new file mode 100644 index 0000000000..d6fdecd4cf --- /dev/null +++ b/tests/version/BUILD.bazel @@ -0,0 +1,3 @@ +load(":version_test.bzl", "version_test_suite") + +version_test_suite(name = "version_tests") diff --git a/tests/version/version_test.bzl b/tests/version/version_test.bzl new file mode 100644 index 0000000000..589f9ac05d --- /dev/null +++ b/tests/version/version_test.bzl @@ -0,0 +1,157 @@ +"" + +load("@rules_testing//lib:analysis_test.bzl", "test_suite") +load("//python/private:version.bzl", "version") # buildifier: disable=bzl-visibility + +_tests = [] + +def _test_normalization(env): + prefixes = ["v", " v", " \t\r\nv"] + epochs = { + "": ["", "0!", "00!"], + "1!": ["1!", "001!"], + "200!": ["200!", "00200!"], + } + releases = { + "0.1": ["0.1", "0.01"], + "2023.7.19": ["2023.7.19", "2023.07.19"], + } + pres = { + "": [""], + "a0": ["a", ".a", "-ALPHA0", "_alpha0", ".a0"], + "a4": ["alpha4", ".a04"], + "b0": ["b", ".b", "-BETA0", "_beta0", ".b0"], + "b5": ["beta05", ".b5"], + "rc0": ["C", "_c0", "RC", "_rc0", "-preview_0"], + } + explicit_posts = { + "": [""], + ".post0": [], + ".post1": [".post1", "-r1", "_rev1"], + } + implicit_posts = [[".post1", "-1"], [".post2", "-2"]] + devs = { + "": [""], + ".dev0": ["dev", "-DEV", "_Dev-0"], + ".dev9": ["DEV9", ".dev09", ".dev9"], + ".dev{BUILD_TIMESTAMP}": [ + "-DEV{BUILD_TIMESTAMP}", + "_dev_{BUILD_TIMESTAMP}", + ], + } + locals = { + "": [""], + "+ubuntu.7": ["+Ubuntu_7", "+ubuntu-007"], + "+ubuntu.r007": ["+Ubuntu_R007"], + } + epochs = [ + [normalized_epoch, input_epoch] + for normalized_epoch, input_epochs in epochs.items() + for input_epoch in input_epochs + ] + releases = [ + [normalized_release, input_release] + for normalized_release, input_releases in releases.items() + for input_release in input_releases + ] + pres = [ + [normalized_pre, input_pre] + for normalized_pre, input_pres in pres.items() + for input_pre in input_pres + ] + explicit_posts = [ + [normalized_post, input_post] + for normalized_post, input_posts in explicit_posts.items() + for input_post in input_posts + ] + pres_and_posts = [ + [normalized_pre + normalized_post, input_pre + input_post] + for normalized_pre, input_pre in pres + for normalized_post, input_post in explicit_posts + ] + [ + [normalized_pre + normalized_post, input_pre + input_post] + for normalized_pre, input_pre in pres + for normalized_post, input_post in implicit_posts + if input_pre == "" or input_pre[-1].isdigit() + ] + devs = [ + [normalized_dev, input_dev] + for normalized_dev, input_devs in devs.items() + for input_dev in input_devs + ] + locals = [ + [normalized_local, input_local] + for normalized_local, input_locals in locals.items() + for input_local in input_locals + ] + postfixes = ["", " ", " \t\r\n"] + i = 0 + for nepoch, iepoch in epochs: + for nrelease, irelease in releases: + for nprepost, iprepost in pres_and_posts: + for ndev, idev in devs: + for nlocal, ilocal in locals: + prefix = prefixes[i % len(prefixes)] + postfix = postfixes[(i // len(prefixes)) % len(postfixes)] + env.expect.that_str( + version.normalize( + prefix + iepoch + irelease + iprepost + + idev + ilocal + postfix, + ), + ).equals( + nepoch + nrelease + nprepost + ndev + nlocal, + ) + i += 1 + +_tests.append(_test_normalization) + +def _test_ordering(env): + want = [ + # Taken from https://peps.python.org/pep-0440/#summary-of-permitted-suffixes-and-relative-ordering + "1.dev0", + "1.0.dev456", + "1.0a1", + "1.0a2.dev456", + "1.0a12.dev456", + "1.0a12", + "1.0b1.dev456", + "1.0b1.dev457", + "1.0b2", + "1.0b2.post345.dev456", + "1.0b2.post345.dev457", + "1.0b2.post345", + "1.0rc1.dev456", + "1.0rc1", + "1.0", + "1.0+abc.5", + "1.0+abc.7", + "1.0+5", + "1.0.post456.dev34", + "1.0.post456", + "1.0.15", + "1.1.dev1", + "1!0.1", + ] + + for lower, higher in zip(want[:-1], want[1:]): + lower = version.parse(lower, strict = True) + higher = version.parse(higher, strict = True) + + lower_key = version.key(lower) + higher_key = version.key(higher) + + if not lower_key < higher_key: + env.fail("Expected '{}'.key() to be smaller than '{}'.key(), but got otherwise: {} > {}".format( + lower.string, + higher.string, + lower_key, + higher_key, + )) + +_tests.append(_test_ordering) + +def version_test_suite(name): + test_suite( + name = name, + basic_tests = _tests, + ) diff --git a/third_party/rules_pycross/LICENSE b/third_party/rules_pycross/LICENSE deleted file mode 100644 index 261eeb9e9f..0000000000 --- a/third_party/rules_pycross/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/third_party/rules_pycross/pycross/private/BUILD.bazel b/third_party/rules_pycross/pycross/private/BUILD.bazel deleted file mode 100644 index f59b087027..0000000000 --- a/third_party/rules_pycross/pycross/private/BUILD.bazel +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2023 Jeremy Volkman. All rights reserved. -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. diff --git a/third_party/rules_pycross/pycross/private/providers.bzl b/third_party/rules_pycross/pycross/private/providers.bzl deleted file mode 100644 index 47fc9f7271..0000000000 --- a/third_party/rules_pycross/pycross/private/providers.bzl +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2023 Jeremy Volkman. All rights reserved. -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Python providers.""" - -PyWheelInfo = provider( - doc = "Information about a Python wheel.", - fields = { - "name_file": "File: A file containing the canonical name of the wheel.", - "wheel_file": "File: The wheel file itself.", - }, -) - -PyTargetEnvironmentInfo = provider( - doc = "A target environment description.", - fields = { - "file": "The JSON file containing target environment information.", - "python_compatible_with": "A list of constraints used to select this platform.", - }, -) diff --git a/third_party/rules_pycross/pycross/private/tools/BUILD.bazel b/third_party/rules_pycross/pycross/private/tools/BUILD.bazel deleted file mode 100644 index 41485c18a3..0000000000 --- a/third_party/rules_pycross/pycross/private/tools/BUILD.bazel +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2023 Jeremy Volkman. All rights reserved. -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -load("//python:defs.bzl", "py_binary") - -py_binary( - name = "wheel_installer", - srcs = ["wheel_installer.py"], - visibility = ["//visibility:public"], - deps = [ - "//python/private/pypi/whl_installer:lib", - "@pypi__installer//:lib", - ], -) diff --git a/third_party/rules_pycross/pycross/private/tools/wheel_installer.py b/third_party/rules_pycross/pycross/private/tools/wheel_installer.py deleted file mode 100644 index a122e67733..0000000000 --- a/third_party/rules_pycross/pycross/private/tools/wheel_installer.py +++ /dev/null @@ -1,196 +0,0 @@ -# Copyright 2023 Jeremy Volkman. All rights reserved. -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -""" -A tool that invokes pypa/build to build the given sdist tarball. -""" - -import argparse -import os -import shutil -import subprocess -import sys -import tempfile -from pathlib import Path -from typing import Any - -from installer import install -from installer.destinations import SchemeDictionaryDestination -from installer.sources import WheelFile - -from python.private.pypi.whl_installer import namespace_pkgs - - -def setup_namespace_pkg_compatibility(wheel_dir: Path) -> None: - """Converts native namespace packages to pkgutil-style packages - - Namespace packages can be created in one of three ways. They are detailed here: - https://packaging.python.org/guides/packaging-namespace-packages/#creating-a-namespace-package - - 'pkgutil-style namespace packages' (2) and 'pkg_resources-style namespace packages' (3) works in Bazel, but - 'native namespace packages' (1) do not. - - We ensure compatibility with Bazel of method 1 by converting them into method 2. - - Args: - wheel_dir: the directory of the wheel to convert - """ - - namespace_pkg_dirs = namespace_pkgs.implicit_namespace_packages( - str(wheel_dir), - ignored_dirnames=["%s/bin" % wheel_dir], - ) - - for ns_pkg_dir in namespace_pkg_dirs: - namespace_pkgs.add_pkgutil_style_namespace_pkg_init(ns_pkg_dir) - - -def main(args: Any) -> None: - dest_dir = args.directory - lib_dir = dest_dir / "site-packages" - destination = SchemeDictionaryDestination( - scheme_dict={ - "platlib": str(lib_dir), - "purelib": str(lib_dir), - "headers": str(dest_dir / "include"), - "scripts": str(dest_dir / "bin"), - "data": str(dest_dir / "data"), - }, - interpreter="/usr/bin/env python3", # Generic; it's not feasible to run these scripts directly. - script_kind="posix", - bytecode_optimization_levels=[0, 1], - ) - - link_dir = Path(tempfile.mkdtemp()) - if args.wheel_name_file: - with open(args.wheel_name_file, "r") as f: - wheel_name = f.read().strip() - else: - wheel_name = os.path.basename(args.wheel) - - link_path = link_dir / wheel_name - os.symlink(os.path.join(os.getcwd(), args.wheel), link_path) - - try: - with WheelFile.open(link_path) as source: - install( - source=source, - destination=destination, - # Additional metadata that is generated by the installation tool. - additional_metadata={ - "INSTALLER": b"https://github.com/bazel-contrib/rules_python/tree/main/third_party/rules_pycross", - }, - ) - finally: - shutil.rmtree(link_dir, ignore_errors=True) - - setup_namespace_pkg_compatibility(lib_dir) - - if args.patch: - if not args.patch_tool and not args.patch_tool_target: - raise ValueError("Specify one of 'patch_tool' or 'patch_tool_target'.") - - patch_args = [ - args.patch_tool or Path.cwd() / args.patch_tool_target - ] + args.patch_arg - for patch in args.patch: - with patch.open("r") as stdin: - try: - subprocess.run( - patch_args, - stdin=stdin, - check=True, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - cwd=args.directory, - ) - except subprocess.CalledProcessError as error: - print(f"Patch {patch} failed to apply:") - print(error.stdout.decode("utf-8")) - raise - - -def parse_flags(argv) -> Any: - parser = argparse.ArgumentParser(description="Extract a Python wheel.") - - parser.add_argument( - "--wheel", - type=Path, - required=True, - help="The wheel file path.", - ) - - parser.add_argument( - "--wheel-name-file", - type=Path, - required=False, - help="A file containing the canonical name of the wheel.", - ) - - parser.add_argument( - "--enable-implicit-namespace-pkgs", - action="store_true", - help="If true, disables conversion of implicit namespace packages and will unzip as-is.", - ) - - parser.add_argument( - "--directory", - type=Path, - help="The output path.", - ) - - parser.add_argument( - "--patch", - type=Path, - default=[], - action="append", - help="A patch file to apply.", - ) - - parser.add_argument( - "--patch-arg", - type=str, - default=[], - action="append", - help="An argument for the patch tool when applying the patches.", - ) - - parser.add_argument( - "--patch-tool", - type=str, - help=( - "The tool from PATH to invoke when applying patches. " - "If set, --patch-tool-target is ignored." - ), - ) - - parser.add_argument( - "--patch-tool-target", - type=Path, - help=( - "The path to the tool to invoke when applying patches. " - "Ignored when --patch-tool is set." - ), - ) - - return parser.parse_args(argv[1:]) - - -if __name__ == "__main__": - # When under `bazel run`, change to the actual working dir. - if "BUILD_WORKING_DIRECTORY" in os.environ: - os.chdir(os.environ["BUILD_WORKING_DIRECTORY"]) - - main(parse_flags(sys.argv)) diff --git a/third_party/rules_pycross/pycross/private/wheel_library.bzl b/third_party/rules_pycross/pycross/private/wheel_library.bzl deleted file mode 100644 index 00d85f71b1..0000000000 --- a/third_party/rules_pycross/pycross/private/wheel_library.bzl +++ /dev/null @@ -1,174 +0,0 @@ -# Copyright 2023 Jeremy Volkman. All rights reserved. -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Implementation of the py_wheel_library rule.""" - -load("@bazel_skylib//lib:paths.bzl", "paths") -load("//python:py_info.bzl", "PyInfo") -load(":providers.bzl", "PyWheelInfo") - -def _py_wheel_library_impl(ctx): - out = ctx.actions.declare_directory(ctx.attr.name) - - wheel_target = ctx.attr.wheel - if PyWheelInfo in wheel_target: - wheel_file = wheel_target[PyWheelInfo].wheel_file - name_file = wheel_target[PyWheelInfo].name_file - else: - wheel_file = ctx.file.wheel - name_file = None - - args = ctx.actions.args().use_param_file("--flagfile=%s") - args.add("--wheel", wheel_file) - args.add("--directory", out.path) - args.add_all(ctx.files.patches, format_each = "--patch=%s") - args.add_all(ctx.attr.patch_args, format_each = "--patch-arg=%s") - args.add("--patch-tool", ctx.attr.patch_tool) - - tools = [] - inputs = [wheel_file] + ctx.files.patches - if name_file: - inputs.append(name_file) - args.add("--wheel-name-file", name_file) - - if ctx.attr.patch_tool_target: - args.add("--patch-tool-target", ctx.attr.patch_tool_target.files_to_run.executable) - tools.append(ctx.executable.patch_tool_target) - - if ctx.attr.enable_implicit_namespace_pkgs: - args.add("--enable-implicit-namespace-pkgs") - - # We apply patches in the same action as the extraction to minimize the - # number of times we cache the wheel contents. If we were to split this - # into 2 actions, then the wheel contents would be cached twice. - ctx.actions.run( - inputs = inputs, - outputs = [out], - executable = ctx.executable._tool, - tools = tools, - arguments = [args], - # Set environment variables to make generated .pyc files reproducible. - env = { - "PYTHONHASHSEED": "0", - "SOURCE_DATE_EPOCH": "315532800", - }, - mnemonic = "WheelInstall", - progress_message = "Installing %s" % ctx.file.wheel.basename, - ) - - has_py2_only_sources = ctx.attr.python_version == "PY2" - has_py3_only_sources = ctx.attr.python_version == "PY3" - if not has_py2_only_sources: - for d in ctx.attr.deps: - if d[PyInfo].has_py2_only_sources: - has_py2_only_sources = True - break - if not has_py3_only_sources: - for d in ctx.attr.deps: - if d[PyInfo].has_py3_only_sources: - has_py3_only_sources = True - break - - # TODO: Is there a more correct way to get this runfiles-relative import path? - imp = paths.join( - ctx.label.repo_name or ctx.workspace_name, # Default to the local workspace. - ctx.label.package, - ctx.label.name, - "site-packages", # we put lib files in this subdirectory. - ) - - imports = depset( - direct = [imp], - transitive = [d[PyInfo].imports for d in ctx.attr.deps], - ) - transitive_sources = depset( - direct = [out], - transitive = [dep[PyInfo].transitive_sources for dep in ctx.attr.deps if PyInfo in dep], - ) - runfiles = ctx.runfiles(files = [out]) - for d in ctx.attr.deps: - runfiles = runfiles.merge(d[DefaultInfo].default_runfiles) - - return [ - DefaultInfo( - files = depset(direct = [out]), - runfiles = runfiles, - ), - PyInfo( - has_py2_only_sources = has_py2_only_sources, - has_py3_only_sources = has_py3_only_sources, - imports = imports, - transitive_sources = transitive_sources, - uses_shared_libraries = True, # Docs say this is unused - ), - ] - -py_wheel_library = rule( - implementation = _py_wheel_library_impl, - attrs = { - "deps": attr.label_list( - doc = "A list of this wheel's Python library dependencies.", - providers = [DefaultInfo, PyInfo], - ), - "enable_implicit_namespace_pkgs": attr.bool( - default = True, - doc = """ -If true, disables conversion of native namespace packages into pkg-util style namespace packages. When set all py_binary -and py_test targets must specify either `legacy_create_init=False` or the global Bazel option -`--incompatible_default_to_explicit_init_py` to prevent `__init__.py` being automatically generated in every directory. -This option is required to support some packages which cannot handle the conversion to pkg-util style. - """, - ), - "patch_args": attr.string_list( - default = ["-p0"], - doc = - "The arguments given to the patch tool. Defaults to -p0, " + - "however -p1 will usually be needed for patches generated by " + - "git. If multiple -p arguments are specified, the last one will take effect.", - ), - "patch_tool": attr.string( - doc = "The patch(1) utility from the host to use. " + - "If set, overrides `patch_tool_target`. Please note that setting " + - "this means that builds are not completely hermetic.", - ), - "patch_tool_target": attr.label( - executable = True, - cfg = "exec", - doc = "The label of the patch(1) utility to use. " + - "Only used if `patch_tool` is not set.", - ), - "patches": attr.label_list( - allow_files = True, - default = [], - doc = - "A list of files that are to be applied as patches after " + - "extracting the archive. This will use the patch command line tool.", - ), - "python_version": attr.string( - doc = "The python version required for this wheel ('PY2' or 'PY3')", - values = ["PY2", "PY3", ""], - ), - "wheel": attr.label( - doc = "The wheel file.", - allow_single_file = [".whl"], - mandatory = True, - ), - "_tool": attr.label( - default = Label("//third_party/rules_pycross/pycross/private/tools:wheel_installer"), - cfg = "exec", - executable = True, - ), - }, -) diff --git a/tools/precompiler/precompiler.py b/tools/precompiler/precompiler.py index 310f2eb097..e7c693c195 100644 --- a/tools/precompiler/precompiler.py +++ b/tools/precompiler/precompiler.py @@ -68,12 +68,12 @@ def _compile(options: "argparse.Namespace") -> None: # A stub type alias for readability. # See the Bazel WorkRequest object definition: # https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/worker_protocol.proto -JsonWorkerRequest = object +JsonWorkRequest = object # A stub type alias for readability. # See the Bazel WorkResponse object definition: # https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/worker_protocol.proto -JsonWorkerResponse = object +JsonWorkResponse = object class _SerialPersistentWorker: diff --git a/tools/publish/requirements_darwin.txt b/tools/publish/requirements_darwin.txt index eaec72c01c..483f88444e 100644 --- a/tools/publish/requirements_darwin.txt +++ b/tools/publish/requirements_darwin.txt @@ -142,9 +142,9 @@ mdurl==0.1.2 \ --hash=sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8 \ --hash=sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba # via markdown-it-py -more-itertools==10.5.0 \ - --hash=sha256:037b0d3203ce90cca8ab1defbbdac29d5f993fc20131f3664dc8d6acfa872aef \ - --hash=sha256:5482bfef7849c25dc3c6dd53a6173ae4795da2a41a80faea6700d9f5846c5da6 +more-itertools==10.7.0 \ + --hash=sha256:9fddd5403be01a94b204faadcff459ec3568cf110265d3c54323e1e866ad29d3 \ + --hash=sha256:d43980384673cb07d2f7d2d918c616b30c659c089ee23953f601d6609c67510e # via # jaraco-classes # jaraco-functools diff --git a/tools/publish/requirements_linux.txt b/tools/publish/requirements_linux.txt index 5fdc742a88..62dbf1eb77 100644 --- a/tools/publish/requirements_linux.txt +++ b/tools/publish/requirements_linux.txt @@ -250,9 +250,9 @@ mdurl==0.1.2 \ --hash=sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8 \ --hash=sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba # via markdown-it-py -more-itertools==10.5.0 \ - --hash=sha256:037b0d3203ce90cca8ab1defbbdac29d5f993fc20131f3664dc8d6acfa872aef \ - --hash=sha256:5482bfef7849c25dc3c6dd53a6173ae4795da2a41a80faea6700d9f5846c5da6 +more-itertools==10.7.0 \ + --hash=sha256:9fddd5403be01a94b204faadcff459ec3568cf110265d3c54323e1e866ad29d3 \ + --hash=sha256:d43980384673cb07d2f7d2d918c616b30c659c089ee23953f601d6609c67510e # via # jaraco-classes # jaraco-functools diff --git a/tools/publish/requirements_universal.txt b/tools/publish/requirements_universal.txt index 97cbef0221..e4e876b176 100644 --- a/tools/publish/requirements_universal.txt +++ b/tools/publish/requirements_universal.txt @@ -250,9 +250,9 @@ mdurl==0.1.2 \ --hash=sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8 \ --hash=sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba # via markdown-it-py -more-itertools==10.5.0 \ - --hash=sha256:037b0d3203ce90cca8ab1defbbdac29d5f993fc20131f3664dc8d6acfa872aef \ - --hash=sha256:5482bfef7849c25dc3c6dd53a6173ae4795da2a41a80faea6700d9f5846c5da6 +more-itertools==10.7.0 \ + --hash=sha256:9fddd5403be01a94b204faadcff459ec3568cf110265d3c54323e1e866ad29d3 \ + --hash=sha256:d43980384673cb07d2f7d2d918c616b30c659c089ee23953f601d6609c67510e # via # jaraco-classes # jaraco-functools diff --git a/tools/publish/requirements_windows.txt b/tools/publish/requirements_windows.txt index 458414009e..043de9ecb1 100644 --- a/tools/publish/requirements_windows.txt +++ b/tools/publish/requirements_windows.txt @@ -142,9 +142,9 @@ mdurl==0.1.2 \ --hash=sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8 \ --hash=sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba # via markdown-it-py -more-itertools==10.5.0 \ - --hash=sha256:037b0d3203ce90cca8ab1defbbdac29d5f993fc20131f3664dc8d6acfa872aef \ - --hash=sha256:5482bfef7849c25dc3c6dd53a6173ae4795da2a41a80faea6700d9f5846c5da6 +more-itertools==10.7.0 \ + --hash=sha256:9fddd5403be01a94b204faadcff459ec3568cf110265d3c54323e1e866ad29d3 \ + --hash=sha256:d43980384673cb07d2f7d2d918c616b30c659c089ee23953f601d6609c67510e # via # jaraco-classes # jaraco-functools diff --git a/tools/wheelmaker.py b/tools/wheelmaker.py index 908b3fe956..8b775e1541 100644 --- a/tools/wheelmaker.py +++ b/tools/wheelmaker.py @@ -154,7 +154,7 @@ def arcname_from(name): hash = hashlib.sha256() size = 0 with open(real_filename, "rb") as fsrc: - with self.open(zinfo, "w") as fdst: + with self.open(zinfo, "w", force_zip64=True) as fdst: while True: block = fsrc.read(2**20) if not block: @@ -217,9 +217,11 @@ def add_recordfile(self): filename = filename.lstrip("/") writer.writerow( ( - c - if isinstance(c, str) - else c.decode("utf-8", "surrogateescape") + ( + c + if isinstance(c, str) + else c.decode("utf-8", "surrogateescape") + ) for c in (filename, digest, size) ) ) @@ -560,13 +562,14 @@ def main() -> None: def get_new_requirement_line(reqs_text, extra): req = Requirement(reqs_text.strip()) + req_extra_deps = f"[{','.join(req.extras)}]" if req.extras else "" if req.marker: if extra: - return f"Requires-Dist: {req.name}{req.specifier}; ({req.marker}) and {extra}" + return f"Requires-Dist: {req.name}{req_extra_deps}{req.specifier}; ({req.marker}) and {extra}" else: - return f"Requires-Dist: {req.name}{req.specifier}; {req.marker}" + return f"Requires-Dist: {req.name}{req_extra_deps}{req.specifier}; {req.marker}" else: - return f"Requires-Dist: {req.name}{req.specifier}; {extra}".strip(" ;") + return f"Requires-Dist: {req.name}{req_extra_deps}{req.specifier}; {extra}".strip(" ;") for meta_line in metadata.splitlines(): if not meta_line.startswith("Requires-Dist: "): @@ -604,7 +607,9 @@ def get_new_requirement_line(reqs_text, extra): # File is empty # So replace the meta_line entirely, including removing newline chars else: - metadata = re.sub(re.escape(meta_line) + r"(?:\r?\n)?", "", metadata, count=1) + metadata = re.sub( + re.escape(meta_line) + r"(?:\r?\n)?", "", metadata, count=1 + ) maker.add_metadata( metadata=metadata,