Skip to content

Commit

Permalink
fix: Obtain default Python version for Python hub repo from versions.…
Browse files Browse the repository at this point in the history
…bzl file (#138)
  • Loading branch information
gfringeli authored Jan 6, 2025
1 parent c43ddf8 commit 2bff754
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Fixed

- Obtain default Python version for Python hub repo from `versions.bzl` file, falling back to `interpreters.bzl` for backwards compatibility. `DEFAULT_PYTHON_VERSION` was [removed](https://github.com/bazelbuild/rules_python/blob/6a04d3832e82fec0a7b0675e9964b360bc358554/CHANGELOG.md?plain=1#L211) from `interpreters.bzl` in rules_python version 1.0.0.

## [0.6.1]

### Added
Expand Down
17 changes: 13 additions & 4 deletions pycross/private/toolchain_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,19 @@ def _get_multi_python_versions(rctx, python_toolchain_repo):
return versions

def _get_default_python_version_bzlmod(rctx, pythons_hub_repo):
interpreters_bzl_file = Label("@@{}//:interpreters.bzl".format(pythons_hub_repo.workspace_name))
build_content = rctx.read(interpreters_bzl_file)

for line in build_content.splitlines():
# Check if Python hub repo has versions.bzl file. versions.bzl was introduced in rules_python 0.37.0.
pythons_hub_build_file = pythons_hub_repo.relative("//:BUILD.bazel")
pythons_hub_repo_dir = rctx.path(pythons_hub_build_file).dirname
if pythons_hub_repo_dir.get_child("versions.bzl").exists:
# Use versions.bzl for rules_python 0.37.0+.
versions_bzl_file = Label("@@{}//:versions.bzl".format(pythons_hub_repo.workspace_name))
content = rctx.read(versions_bzl_file)
else:
# Fall back to interpreters.bzl as versions.bzl does not exists for rules_python < 0.37.0.
# DEFAULT_PYTHON_VERSION was removed from interpreters.bzl in rules_python 1.0.0.
interpreters_bzl_file = Label("@@{}//:interpreters.bzl".format(pythons_hub_repo.workspace_name))
content = rctx.read(interpreters_bzl_file)
for line in content.splitlines():
if line.startswith("DEFAULT_PYTHON_VERSION"):
_, val = line.split("=")
val = val.strip(" \"'")
Expand Down

0 comments on commit 2bff754

Please sign in to comment.