forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.bzl
1029 lines (903 loc) · 39.9 KB
/
python.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 toolchain module extensions for use with bzlmod."
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(":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(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER")
# 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):
"""Parse the modules and return a struct for registrations.
Args:
module_ctx: {type}`module_ctx` module context.
_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.
"""
if module_ctx.os.environ.get("RULES_PYTHON_BZLMOD_DEBUG", "0") == "1":
debug_info = {
"toolchains_registered": [],
}
else:
debug_info = None
# The toolchain_info structs to register, in the order to register them in.
# NOTE: The last element is special: it is treated as the default toolchain,
# so there is special handling to ensure the last entry is the correct one.
toolchains = []
# We store the default toolchain separately to ensure it is the last
# toolchain added to toolchains.
# This is a toolchain_info struct.
default_toolchain = None
# Map of string Major.Minor or Major.Minor.Patch to the toolchain_info struct
global_toolchain_versions = {}
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:
ignore_root_user_error = True
config = _get_toolchain_config(modules = module_ctx.modules, _fail = _fail)
default_python_version = None
for mod in module_ctx.modules:
defaults_attr_structs = _create_defaults_attr_structs(mod = mod)
default_python_version_env = None
default_python_version_file = None
# Only the root module and rules_python are allowed to specify the default
# toolchain for a couple reasons:
# * It prevents submodules from specifying different defaults and only
# one of them winning.
# * rules_python needs to set a soft default in case the root module doesn't,
# e.g. if the root module doesn't use Python itself.
# * The root module is allowed to override the rules_python default.
if mod.is_root or (mod.name == "rules_python" and not default_python_version):
for defaults_attr in defaults_attr_structs:
default_python_version = _one_or_the_same(
default_python_version,
defaults_attr.python_version,
onerror = _fail_multiple_defaults_python_version,
)
default_python_version_env = _one_or_the_same(
default_python_version_env,
defaults_attr.python_version_env,
onerror = _fail_multiple_defaults_python_version_env,
)
default_python_version_file = _one_or_the_same(
default_python_version_file,
defaults_attr.python_version_file,
onerror = _fail_multiple_defaults_python_version_file,
)
if default_python_version_file:
default_python_version = _one_or_the_same(
default_python_version,
module_ctx.read(default_python_version_file, watch = "yes").strip(),
)
if default_python_version_env:
default_python_version = module_ctx.getenv(
default_python_version_env,
default_python_version,
)
seen_versions = {}
for mod in module_ctx.modules:
module_toolchain_versions = []
toolchain_attr_structs = _create_toolchain_attr_structs(
mod = mod,
seen_versions = seen_versions,
config = config,
)
for toolchain_attr in toolchain_attr_structs:
toolchain_version = toolchain_attr.python_version
toolchain_name = "python_" + toolchain_version.replace(".", "_")
# Duplicate versions within a module indicate a misconfigured module.
if toolchain_version in module_toolchain_versions:
_fail_duplicate_module_toolchain_version(toolchain_version, mod.name)
module_toolchain_versions.append(toolchain_version)
if mod.is_root:
# Only the root module and rules_python are allowed to specify the default
# toolchain for a couple reasons:
# * It prevents submodules from specifying different defaults and only
# one of them winning.
# * rules_python needs to set a soft default in case the root module doesn't,
# e.g. if the root module doesn't use Python itself.
# * The root module is allowed to override the rules_python default.
if default_python_version:
is_default = default_python_version == toolchain_version
if toolchain_attr.is_default and not is_default:
fail("The 'is_default' attribute doesn't work if you set " +
"the default Python version with the `defaults` tag.")
else:
is_default = toolchain_attr.is_default
# Also only the root module should be able to decide ignore_root_user_error.
# Modules being depended upon don't know the final environment, so they aren't
# in the right position to know or decide what the correct setting is.
# If an inconsistency in the ignore_root_user_error among multiple toolchains is detected, fail.
if ignore_root_user_error != None and toolchain_attr.ignore_root_user_error != ignore_root_user_error:
fail("Toolchains in the root module must have consistent 'ignore_root_user_error' attributes")
ignore_root_user_error = toolchain_attr.ignore_root_user_error
elif mod.name == "rules_python" and not default_toolchain and not default_python_version:
# We don't do the len() check because we want the default that rules_python
# sets to be clearly visible.
is_default = toolchain_attr.is_default
else:
is_default = False
if is_default and default_toolchain != None:
_fail_multiple_default_toolchains(
first = default_toolchain.name,
second = toolchain_name,
)
# Ignore version collisions in the global scope because there isn't
# much else that can be done. Modules don't know and can't control
# what other modules do, so the first in the dependency graph wins.
if toolchain_version in global_toolchain_versions:
# If the python version is explicitly provided by the root
# module, they should not be warned for choosing the same
# version that rules_python provides as default.
first = global_toolchain_versions[toolchain_version]
if mod.name != "rules_python" or not first.module.is_root:
# The warning can be enabled by setting the verbosity:
# env RULES_PYTHON_REPO_DEBUG_VERBOSITY=INFO bazel build //...
_warn_duplicate_global_toolchain_version(
toolchain_version,
first = first,
second_toolchain_name = toolchain_name,
second_module_name = mod.name,
logger = logger,
)
toolchain_info = None
else:
toolchain_info = struct(
python_version = toolchain_attr.python_version,
name = toolchain_name,
register_coverage_tool = toolchain_attr.configure_coverage_tool,
module = struct(name = mod.name, is_root = mod.is_root),
)
global_toolchain_versions[toolchain_version] = toolchain_info
if debug_info:
debug_info["toolchains_registered"].append({
"ignore_root_user_error": ignore_root_user_error,
"module": {"is_root": mod.is_root, "name": mod.name},
"name": toolchain_name,
})
if is_default:
# This toolchain is setting the default, but the actual
# registration was performed previously, by a different module.
if toolchain_info == None:
default_toolchain = global_toolchain_versions[toolchain_version]
# Remove it because later code will add it at the end to
# ensure it is last in the list.
toolchains.remove(default_toolchain)
else:
default_toolchain = toolchain_info
elif toolchain_info:
toolchains.append(toolchain_info)
config.default.setdefault("ignore_root_user_error", ignore_root_user_error)
# 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`?")
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(
python_version = default_toolchain.python_version,
module_name = default_toolchain.module.name,
))
# The last toolchain in the BUILD file is set as the default
# 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))
return struct(
config = config,
debug_info = debug_info,
default_python_version = toolchains[-1].python_version,
toolchains = [
struct(
python_version = t.python_version,
name = t.name,
register_coverage_tool = t.register_coverage_tool,
)
for t in toolchains
],
)
def _python_impl(module_ctx):
py = parse_modules(module_ctx = module_ctx)
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,
minor_mapping = py.config.minor_mapping,
)
kwargs = {
"python_version": full_python_version,
"register_coverage_tool": toolchain_info.register_coverage_tool,
}
# Allow overrides per python version
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(
name = toolchain_info.name,
_internal_bzlmod_toolchain_call = True,
**kwargs
)
# Create the pythons_hub repo for the interpreter meta data and the
# the various toolchains.
hub_repo(
name = "pythons_hub",
# Last toolchain is default
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
# and py_binary
multi_toolchain_aliases(
name = "python_versions",
python_versions = {
toolchain.python_version: toolchain.name
for toolchain in py.toolchains
},
)
if py.debug_info != None:
_debug_repo(
name = "rules_python_bzlmod_debug",
debug_info = json.encode_indent(py.debug_info),
)
if bazel_features.external_deps.extension_metadata_has_reproducible:
return module_ctx.extension_metadata(reproducible = True)
else:
return None
def _one_or_the_same(first, second, *, onerror = None):
if not first:
return second
if not second or second == first:
return first
if onerror:
return onerror(first, second)
else:
fail("Unique value needed, got both '{}' and '{}', which are different".format(
first,
second,
))
def _fail_duplicate_module_toolchain_version(version, module):
fail(("Duplicate module toolchain version: module '{module}' attempted " +
"to use version '{version}' multiple times in itself").format(
version = version,
module = module,
))
def _warn_duplicate_global_toolchain_version(version, first, second_toolchain_name, second_module_name, logger):
if not logger:
return
logger.info(lambda: (
"Ignoring toolchain '{second_toolchain}' from module '{second_module}': " +
"Toolchain '{first_toolchain}' from module '{first_module}' " +
"already registered Python version {version} and has precedence."
).format(
first_toolchain = first.name,
first_module = first.module.name,
second_module = second_module_name,
second_toolchain = second_toolchain_name,
version = version,
))
def _fail_multiple_defaults_python_version(first, second):
fail(("Multiple python_version entries in defaults: " +
"First default was python_version '{first}'. " +
"Second was python_version '{second}'").format(
first = first,
second = second,
))
def _fail_multiple_defaults_python_version_file(first, second):
fail(("Multiple python_version_file entries in defaults: " +
"First default was python_version_file '{first}'. " +
"Second was python_version_file '{second}'").format(
first = first,
second = second,
))
def _fail_multiple_defaults_python_version_env(first, second):
fail(("Multiple python_version_env entries in defaults: " +
"First default was python_version_env '{first}'. " +
"Second was python_version_env '{second}'").format(
first = first,
second = second,
))
def _fail_multiple_default_toolchains(first, second):
fail(("Multiple default toolchains: only one toolchain " +
"can have is_default=True. First default " +
"was toolchain '{first}'. Second was '{second}'").format(
first = first,
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))
return False
return True
def _process_single_version_overrides(*, tag, _fail = fail, default):
if not _validate_version(version = tag.python_version, _fail = _fail):
return
available_versions = default["tool_versions"]
kwargs = default.setdefault("kwargs", {})
if tag.sha256 or tag.urls:
if not (tag.sha256 and tag.urls):
_fail("Both `sha256` and `urls` overrides need to be provided together")
return
for platform in (tag.sha256 or []):
if platform not in PLATFORMS:
_fail("The platform must be one of {allowed} but got '{got}'".format(
allowed = sorted(PLATFORMS),
got = platform,
))
return
sha256 = dict(tag.sha256) or available_versions[tag.python_version]["sha256"]
override = {
"sha256": sha256,
"strip_prefix": {
platform: tag.strip_prefix
for platform in sha256
},
"url": {
platform: list(tag.urls)
for platform in tag.sha256
} or available_versions[tag.python_version]["url"],
}
if tag.patches:
override["patch_strip"] = {
platform: tag.patch_strip
for platform in sha256
}
override["patches"] = {
platform: list(tag.patches)
for platform in sha256
}
available_versions[tag.python_version] = {k: v for k, v in override.items() if v}
if tag.distutils_content:
kwargs.setdefault(tag.python_version, {})["distutils_content"] = tag.distutils_content
if tag.distutils:
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):
return
available_versions = default["tool_versions"]
if tag.python_version not in available_versions:
if not tag.urls or not tag.sha256 or not tag.strip_prefix:
_fail("When introducing a new python_version '{}', 'sha256', 'strip_prefix' and 'urls' must be specified".format(tag.python_version))
return
available_versions[tag.python_version] = {}
if tag.coverage_tool:
available_versions[tag.python_version].setdefault("coverage_tool", {})[tag.platform] = tag.coverage_tool
if tag.patch_strip:
available_versions[tag.python_version].setdefault("patch_strip", {})[tag.platform] = tag.patch_strip
if tag.patches:
available_versions[tag.python_version].setdefault("patches", {})[tag.platform] = list(tag.patches)
if tag.sha256:
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
def _process_global_overrides(*, tag, default, _fail = fail):
if tag.available_python_versions:
available_versions = default["tool_versions"]
all_versions = dict(available_versions)
available_versions.clear()
for v in tag.available_python_versions:
if v not in all_versions:
_fail("unknown version '{}', known versions are: {}".format(
v,
sorted(all_versions),
))
return
available_versions[v] = all_versions[v]
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))
default["minor_mapping"] = tag.minor_mapping
forwarded_attrs = sorted(AUTH_ATTRS) + [
"ignore_root_user_error",
"base_url",
"register_all_versions",
]
for key in forwarded_attrs:
if getattr(tag, key, None):
default[key] = getattr(tag, key)
def _override_defaults(*overrides, modules, _fail = fail, default):
mod = modules[0] if modules else None
if not mod or not mod.is_root:
return
overriden_keys = []
for override in overrides:
for tag in getattr(mod.tags, override.name):
key = override.key(tag)
if key not in overriden_keys:
overriden_keys.append(key)
elif key:
_fail("Only a single 'python.{}' can be present for '{}'".format(override.name, key))
return
else:
_fail("Only a single 'python.{}' can be present".format(override.name))
return
override.fn(tag = tag, _fail = _fail, default = default)
def _get_toolchain_config(*, modules, _fail = fail):
# 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()
}
default = {
"base_url": DEFAULT_RELEASE_BASE_URL,
"tool_versions": available_versions,
}
_override_defaults(
# First override by single version, because the sha256 will replace
# anything that has been there before.
struct(
name = "single_version_override",
key = lambda t: t.python_version,
fn = _process_single_version_overrides,
),
# Then override particular platform entries if they need to be overridden.
struct(
name = "single_version_platform_override",
key = lambda t: (t.python_version, t.platform),
fn = _process_single_version_platform_overrides,
),
# Then finally add global args and remove the unnecessary toolchains.
# This ensures that we can do further validations when removing.
struct(
name = "override",
key = lambda t: None,
fn = _process_global_overrides,
),
modules = modules,
default = default,
_fail = _fail,
)
register_all_versions = default.pop("register_all_versions", False)
kwargs = default.pop("kwargs", {})
versions = {}
for version_string in available_versions:
v = semver(version_string)
versions.setdefault("{}.{}".format(v.major, v.minor), []).append((int(v.patch), version_string))
minor_mapping = {
major_minor: max(subset)[1]
for major_minor, subset in versions.items()
}
# The following ensures that all of the versions will be present in the minor_mapping
minor_mapping_overrides = default.pop("minor_mapping", {})
for major_minor, full in minor_mapping_overrides.items():
minor_mapping[major_minor] = full
return struct(
kwargs = kwargs,
minor_mapping = minor_mapping,
default = default,
register_all_versions = register_all_versions,
)
def _create_defaults_attr_structs(*, mod):
arg_structs = []
for tag in mod.tags.defaults:
arg_structs.append(_create_defaults_attr_struct(tag = tag))
return arg_structs
def _create_defaults_attr_struct(*, tag):
return struct(
python_version = getattr(tag, "python_version", None),
python_version_env = getattr(tag, "python_version_env", None),
python_version_file = getattr(tag, "python_version_file", None),
)
def _create_toolchain_attr_structs(*, mod, config, seen_versions):
arg_structs = []
for tag in mod.tags.toolchain:
arg_structs.append(_create_toolchain_attrs_struct(
tag = tag,
toolchain_tag_count = len(mod.tags.toolchain),
))
seen_versions[tag.python_version] = True
if config.register_all_versions:
arg_structs.extend([
_create_toolchain_attrs_struct(python_version = v)
for v in config.default["tool_versions"].keys() + config.minor_mapping.keys()
if v not in seen_versions
])
return arg_structs
def _create_toolchain_attrs_struct(*, tag = None, python_version = None, toolchain_tag_count = None):
if tag and python_version:
fail("Only one of tag and python version can be specified")
if tag:
# A single toolchain is treated as the default because it's unambiguous.
is_default = tag.is_default or toolchain_tag_count == 1
else:
is_default = False
return struct(
is_default = is_default,
python_version = python_version if python_version else tag.python_version,
configure_coverage_tool = getattr(tag, "configure_coverage_tool", False),
ignore_root_user_error = getattr(tag, "ignore_root_user_error", True),
)
def _get_bazel_version_specific_kwargs():
kwargs = {}
if IS_BAZEL_6_4_OR_HIGHER:
kwargs["environ"] = ["RULES_PYTHON_BZLMOD_DEBUG"]
return kwargs
_defaults = tag_class(
doc = """Tag class to specify the default Python version.""",
attrs = {
"python_version": attr.string(
mandatory = False,
doc = """\
String saying what the default Python version should be. If the string
matches the {attr}`python_version` attribute of a toolchain, this
toolchain is the default version. If this attribute is set, the
{attr}`is_default` attribute of the toolchain is ignored.
:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
),
"python_version_env": attr.string(
mandatory = False,
doc = """\
Environment variable saying what the default Python version should be.
If the string matches the {attr}`python_version` attribute of a
toolchain, this toolchain is the default version. If this attribute is
set, the {attr}`is_default` attribute of the toolchain is ignored.
:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
),
"python_version_file": attr.label(
mandatory = False,
allow_single_file = True,
doc = """\
File saying what the default Python version should be. If the contents
of the file match the {attr}`python_version` attribute of a toolchain,
this toolchain is the default version. If this attribute is set, the
{attr}`is_default` attribute of the toolchain is ignored.
:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
),
},
)
_toolchain = tag_class(
doc = """Tag class used to register Python toolchains.
Use this tag class to register one or more Python toolchains. This class
is also potentially called by sub modules. The following covers different
business rules and use cases.
:::{topic} Toolchains in the Root Module
This class registers all toolchains in the root module.
:::
:::{topic} Toolchains in Sub Modules
It will create a toolchain that is in a sub module, if the toolchain
of the same name does not exist in the root module. The extension stops name
clashing between toolchains in the root module and toolchains in sub modules.
You cannot configure more than one toolchain as the default toolchain.
:::
:::{topic} Toolchain set as the default version
This extension will not create a toolchain that exists in a sub module,
if the sub module toolchain is marked as the default version. If you have
more than one toolchain in your root module, you need to set one of the
toolchains as the default version. If there is only one toolchain it
is set as the default toolchain.
:::
:::{topic} Toolchain repository name
A toolchain's repository name uses the format `python_{major}_{minor}`, e.g.
`python_3_10`. The `major` and `minor` components are
`major` and `minor` are the Python version from the `python_version` attribute.
If a toolchain is registered in `X.Y.Z`, then similarly the toolchain name will
be `python_{major}_{minor}_{patch}`, e.g. `python_3_10_19`.
:::
:::{topic} Toolchain detection
The definition of the first toolchain wins, which means that the root module
can override settings for any python toolchain available. This relies on the
documented module traversal from the {obj}`module_ctx.modules`.
:::
:::{tip}
In order to use a different name than the above, you can use the following `MODULE.bazel`
syntax:
```starlark
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
is_default = True,
python_version = "3.11",
)
use_repo(python, my_python_name = "python_3_11")
```
Then the python interpreter will be available as `my_python_name`.
:::
""",
attrs = {
"configure_coverage_tool": attr.bool(
mandatory = False,
doc = "Whether or not to configure the default coverage tool provided by `rules_python` for the compatible toolchains.",
),
"ignore_root_user_error": attr.bool(
default = True,
doc = """\
The Python runtime installation is made read only. This improves the ability for
Bazel to cache it by preventing the interpreter from creating `.pyc` files for
the standard library dynamically at runtime as they are loaded (this often leads
to spurious cache misses or build failures).
However, if the user is running Bazel as root, this read-onlyness is not
respected. Bazel will print a warning message when it detects that the runtime
installation is writable despite being made read only (i.e. it's running with
root access). If this attribute is set to `False`, Bazel will make it a hard
error to run with root access instead.
""",
mandatory = False,
),
"is_default": attr.bool(
mandatory = False,
doc = """\
Whether the toolchain is the default version.
:::{versionchanged} VERSION_NEXT_FEATURE
This setting is ignored if the default version is set using the `defaults`
tag class.
:::
""",
),
"python_version": attr.string(
mandatory = True,
doc = """\
The Python version, in `major.minor` or `major.minor.patch` format, e.g
`3.12` (or `3.12.3`), to create a toolchain for.
""",
),
},
)
_override = tag_class(
doc = """Tag class used to override defaults and behaviour of the module extension.
:::{versionadded} 0.36.0
:::
""",
attrs = {
"available_python_versions": attr.string_list(
mandatory = False,
doc = """\
The list of available python tool versions to use. Must be in `X.Y.Z` format.
If the unknown version given the processing of the extension will fail - all of
the versions in the list have to be defined with
{obj}`python.single_version_override` or
{obj}`python.single_version_platform_override` before they are used in this
list.
This attribute is usually used in order to ensure that no unexpected transitive
dependencies are introduced.
""",
),
"base_url": attr.string(
mandatory = False,
doc = "The base URL to be used when downloading toolchains.",
default = DEFAULT_RELEASE_BASE_URL,
),
"ignore_root_user_error": attr.bool(
default = True,
doc = """Deprecated; do not use. This attribute has no effect.""",
mandatory = False,
),
"minor_mapping": attr.string_dict(
mandatory = False,
doc = """\
The mapping between `X.Y` to `X.Y.Z` versions to be used when setting up
toolchains. It defaults to the interpreter with the highest available patch
version for each minor version. For example if one registers `3.10.3`, `3.10.4`
and `3.11.4` then the default for the `minor_mapping` dict will be:
```starlark
{
"3.10": "3.10.4",
"3.11": "3.11.4",
}
```
:::{versionchanged} 0.37.0
The values in this mapping override the default values and do not replace them.
:::
""",
default = {},
),
"register_all_versions": attr.bool(default = False, doc = "Add all versions"),
} | AUTH_ATTRS,
)
_single_version_override = tag_class(
doc = """Override single python version URLs and patches for all platforms.
:::{note}
This will replace any existing configuration for the given python version.
:::
:::{tip}
If you would like to modify the configuration for a specific `(version,
platform)`, please use the {obj}`single_version_platform_override` tag
class.
:::
:::{versionadded} 0.36.0
:::
""",
attrs = {
# NOTE @aignas 2024-09-01: all of the attributes except for `version`
# can be part of the `python.toolchain` call. That would make it more
# ergonomic to define new toolchains and to override values for old
# toolchains. The same semantics of the `first one wins` would apply,
# so technically there is no need for any overrides?
#
# Although these attributes would override the code that is used by the
# code in non-root modules, so technically this could be thought as
# being overridden.
#
# rules_go has a single download call:
# https://github.com/bazelbuild/rules_go/blob/master/go/private/extensions.bzl#L38
#
# However, we need to understand how to accommodate the fact that
# {attr}`single_version_override.version` only allows patch versions.
"distutils": attr.label(
allow_single_file = True,
doc = "A distutils.cfg file to be included in the Python installation. " +
"Either {attr}`distutils` or {attr}`distutils_content` can be specified, but not both.",
mandatory = False,
),
"distutils_content": attr.string(
doc = "A distutils.cfg file content to be included in the Python installation. " +
"Either {attr}`distutils` or {attr}`distutils_content` can be specified, but not both.",
mandatory = False,
),
"patch_strip": attr.int(
mandatory = False,
doc = "Same as the --strip argument of Unix patch.",
default = 0,
),
"patches": attr.label_list(
mandatory = False,
doc = "A list of labels pointing to patch files to apply for the interpreter repository. They are applied in the list order and are applied before any platform-specific patches are applied.",
),
"python_version": attr.string(
mandatory = True,
doc = "The python version to override URLs for. Must be in `X.Y.Z` format.",
),
"sha256": attr.string_dict(
mandatory = False,
doc = "The python platform to sha256 dict. See {attr}`python.single_version_platform_override.platform` for allowed key values.",
),
"strip_prefix": attr.string(
mandatory = False,
doc = "The 'strip_prefix' for the archive, defaults to 'python'.",
default = "python",
),
"urls": attr.string_list(
mandatory = False,
doc = "The URL template to fetch releases for this Python version. See {attr}`python.single_version_platform_override.urls` for documentation.",
),
},
)
_single_version_platform_override = tag_class(
doc = """Override single python version for a single existing platform.
If the `(version, platform)` is new, we will add it to the existing versions and will
use the same `url` template.
:::{tip}
If you would like to add or remove platforms to a single python version toolchain
configuration, please use {obj}`single_version_override`.
:::
:::{versionadded} 0.36.0
:::
""",
attrs = {
"coverage_tool": attr.label(
doc = """\
The coverage tool to be used for a particular Python interpreter. This can override
`rules_python` defaults.
""",
),
"patch_strip": attr.int(
mandatory = False,
doc = "Same as the --strip argument of Unix patch.",
default = 0,
),
"patches": attr.label_list(
mandatory = False,
doc = "A list of labels pointing to patch files to apply for the interpreter repository. They are applied in the list order and are applied after the common patches are applied.",
),
"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]))),
),
"python_version": attr.string(
mandatory = True,
doc = "The python version to override URLs for. Must be in `X.Y.Z` format.",
),
"sha256": attr.string(
mandatory = False,
doc = "The sha256 for the archive",
),
"strip_prefix": attr.string(
mandatory = False,
doc = "The 'strip_prefix' for the archive, defaults to 'python'.",
default = "python",
),
"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.",
),
},
)
python = module_extension(
doc = """Bzlmod extension that is used to register Python toolchains.