Skip to content

Commit bc23825

Browse files
committed
spike the override tag classes
1 parent 160c4e1 commit bc23825

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

python/private/python.bzl

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"Python toolchain module extensions for use with bzlmod"
1616

1717
load("@bazel_features//:features.bzl", "bazel_features")
18-
load("//python:versions.bzl", "TOOL_VERSIONS")
18+
load("//python:versions.bzl", "PLATFORMS", "TOOL_VERSIONS")
1919
load(":python_repositories.bzl", "python_register_toolchains")
2020
load(":pythons_hub.bzl", "hub_repo")
2121
load(":repo_utils.bzl", "repo_utils")
@@ -266,11 +266,35 @@ def _process_tag_classes(mod):
266266
seen_versions[tag.python_version] = True
267267

268268
if mod.is_root:
269+
for tag in mod.tags.version_override:
270+
sha256 = {}
271+
for p, sha in tag.sha256.items():
272+
if p not in PLATFORMS:
273+
fail("The platform must be one of {allowed} but got '{got}'".format(
274+
allowed = sorted(PLATFORMS),
275+
got = p,
276+
))
277+
278+
sha256[p] = sha
279+
280+
available_versions[tag.version] = {
281+
"sha256": sha256,
282+
"strip_prefix": tag.strip_prefix,
283+
"url": tag.url,
284+
}
285+
286+
for tag in mod.tags.override:
287+
available_versions = {
288+
v: available_versions[v]
289+
for v in tag.available_python_versions
290+
}
291+
269292
register_all = False
270293
for tag in mod.tags.rules_python_private_testing:
271294
if tag.register_all_versions:
272295
register_all = True
273296
break
297+
274298
if register_all:
275299
arg_structs.extend([
276300
_create_toolchain_attrs_struct(python_version = v)
@@ -376,13 +400,48 @@ can result in spurious build failures.
376400
},
377401
)
378402

403+
_override = tag_class(
404+
doc = """Tag class used to override defaults and behaviour of the module extension.""",
405+
attrs = {
406+
"available_python_versions": attr.string_list(
407+
mandatory = True,
408+
doc = "The list of available python tool versions to use. Must be in `X.Y.Z` format.",
409+
),
410+
},
411+
)
412+
413+
_version_override = tag_class(
414+
doc = """Tag class used to override single python version settings.""",
415+
attrs = {
416+
"sha256s": attr.string_dict(
417+
mandatory = True,
418+
doc = "The python platform to sha256 dict. The platform key must be present in the PLATFORMS dict.",
419+
),
420+
"strip_prefix": attr.string(
421+
mandatory = False,
422+
doc = "The 'strip_prefix' for the archive, defaults to 'python'.",
423+
default = "python",
424+
),
425+
"url": attr.string(
426+
mandatory = True,
427+
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 PLATFORMS dict.",
428+
),
429+
"version": attr.string(
430+
mandatory = True,
431+
doc = "The python version to override URLs for. Must be in `X.Y.Z` format.",
432+
),
433+
},
434+
)
435+
379436
python = module_extension(
380437
doc = """Bzlmod extension that is used to register Python toolchains.
381438
""",
382439
implementation = _python_impl,
383440
tag_classes = {
441+
"override": _override,
384442
"rules_python_private_testing": _rules_python_private_testing,
385443
"toolchain": _toolchain,
444+
"version_override": _version_override,
386445
},
387446
**_get_bazel_version_specific_kwargs()
388447
)

0 commit comments

Comments
 (0)