|
15 | 15 | "Python toolchain module extensions for use with bzlmod"
|
16 | 16 |
|
17 | 17 | load("@bazel_features//:features.bzl", "bazel_features")
|
18 |
| -load("//python:versions.bzl", "TOOL_VERSIONS") |
| 18 | +load("//python:versions.bzl", "PLATFORMS", "TOOL_VERSIONS") |
19 | 19 | load(":python_repositories.bzl", "python_register_toolchains")
|
20 | 20 | load(":pythons_hub.bzl", "hub_repo")
|
21 | 21 | load(":repo_utils.bzl", "repo_utils")
|
@@ -266,11 +266,35 @@ def _process_tag_classes(mod):
|
266 | 266 | seen_versions[tag.python_version] = True
|
267 | 267 |
|
268 | 268 | 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 | + |
269 | 292 | register_all = False
|
270 | 293 | for tag in mod.tags.rules_python_private_testing:
|
271 | 294 | if tag.register_all_versions:
|
272 | 295 | register_all = True
|
273 | 296 | break
|
| 297 | + |
274 | 298 | if register_all:
|
275 | 299 | arg_structs.extend([
|
276 | 300 | _create_toolchain_attrs_struct(python_version = v)
|
@@ -376,13 +400,48 @@ can result in spurious build failures.
|
376 | 400 | },
|
377 | 401 | )
|
378 | 402 |
|
| 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 | + |
379 | 436 | python = module_extension(
|
380 | 437 | doc = """Bzlmod extension that is used to register Python toolchains.
|
381 | 438 | """,
|
382 | 439 | implementation = _python_impl,
|
383 | 440 | tag_classes = {
|
| 441 | + "override": _override, |
384 | 442 | "rules_python_private_testing": _rules_python_private_testing,
|
385 | 443 | "toolchain": _toolchain,
|
| 444 | + "version_override": _version_override, |
386 | 445 | },
|
387 | 446 | **_get_bazel_version_specific_kwargs()
|
388 | 447 | )
|
|
0 commit comments