Skip to content

Commit

Permalink
feat(config_settings): accept minor versions of Python in `python_ver…
Browse files Browse the repository at this point in the history
…sion` flag

Currently user has to specify a full (x.y.z) version of Python when setting the
`python_version` flag. When they upgrade `rules_python` or change `MINOR_MAPPING`
in some other way, user has to update the flag's value to keep it in sync with
`MINOR_MAPPING`. This patch allows `python_version` flag to accept minor (x.y)
versions and resolves them to the corresponding full versions using `MINOR_MAPPING`.
  • Loading branch information
dizzy57 committed Nov 13, 2023
1 parent 9facc3e commit 7de7189
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Breaking changes:
* (utils) Added a `pip_utils` struct with a `normalize_name` function to allow users
to find out how `rules_python` would normalize a PyPI distribution name.

* (config_settings) The `python_version` flag now accepts minor Python versions.

## [0.26.0] - 2023-10-06

### Changed
Expand Down
7 changes: 5 additions & 2 deletions python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//python:versions.bzl", "TOOL_VERSIONS")
load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS")
load(":config_settings.bzl", "construct_config_settings")

filegroup(
Expand All @@ -10,4 +10,7 @@ filegroup(
visibility = ["//python:__pkg__"],
)

construct_config_settings(python_versions = TOOL_VERSIONS.keys())
construct_config_settings(
minor_mapping = MINOR_MAPPING,
python_versions = TOOL_VERSIONS.keys(),
)
38 changes: 32 additions & 6 deletions python/config_settings/config_settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,52 @@
"""This module is used to construct the config settings in the BUILD file in this same package.
"""

load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

# buildifier: disable=unnamed-macro
def construct_config_settings(python_versions):
def construct_config_settings(minor_mapping, python_versions):
"""Constructs a set of configs for all Python versions.
Args:
python_versions: The Python versions supported by rules_python.
minor_mapping: mapping from minor (x.y) versions to the corresponding full (x.y.z) versions.
python_versions: list of full (x.y.z) Python versions supported by rules_python.
"""

minor_versions = list(minor_mapping.keys())
allowed_flag_values = python_versions + minor_versions

string_flag(
name = "python_version",
build_setting_default = python_versions[0],
values = python_versions,
values = allowed_flag_values,
visibility = ["//visibility:public"],
)

for python_version in python_versions:
python_version_constraint_setting = "is_python_" + python_version
for flag_value in allowed_flag_values:
flag_value_constraint_setting = "python_version_flag_equals_" + flag_value
native.config_setting(
name = flag_value_constraint_setting,
flag_values = {":python_version": flag_value},
visibility = ["//visibility:public"],
)

flag_values_that_enable_version = {
full_version: [full_version]
for full_version in python_versions
}

for minor_version, full_version in minor_mapping.items():
flag_values_that_enable_version[full_version].append(minor_version)

for full_version, flag_values in flag_values_that_enable_version.items():
python_version_constraint_setting = "is_python_" + full_version
flag_value_constraints = [
":python_version_flag_equals_" + flag_value
for flag_value in flag_values
]
selects.config_setting_group(
name = python_version_constraint_setting,
flag_values = {":python_version": python_version},
match_any = flag_value_constraints,
visibility = ["//visibility:public"],
)

0 comments on commit 7de7189

Please sign in to comment.