Skip to content

Commit c804a13

Browse files
aignasrickeylev
andauthored
feat(toolchain): add patch_strip attr for python_repository (bazel-contrib#2201)
This value has been hardcoded for a long time, so let's add the `patch_strip` attribute to the `WORKSPACE` setups now so that we add less tech debt in the `bzlmod` world later and need to migrate `bzlmod` users from the hard coded value to something that can be configured. This should be backwards compatible because of the default `int` value of `1` for the `patch_strip` attribute. Summary: - feat: add `patch_strip` to `python_repository`. - feat: add the default value of patch_strip to get_release_info. - refactor: handle patch_strip key in `TOOL_VERSIONS`. Work towards bazel-contrib#2081. --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
1 parent a18ae49 commit c804a13

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ A brief description of the categories of changes:
6363
have it installed.
6464
* (docs) Automatically generated documentation for {bzl:obj}`python_register_toolchains`
6565
and related symbols.
66+
* (toolchains) Added {attr}`python_repository.patch_strip` attribute for
67+
allowing values that are other than `1`, which has been hard-coded up until
68+
now. If you are relying on the undocumented `patches` support in
69+
`TOOL_VERSIONS` for registering patched toolchains please consider setting
70+
the `patch_strip` explicitly to `1` if you depend on this value - in the
71+
future the value may change to default to `0`.
72+
6673

6774
### Removed
6875
* (toolchains): Removed accidentally exposed `http_archive` symbol from

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/python_repositories.bzl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _python_repository_impl(rctx):
179179
if patches:
180180
for patch in patches:
181181
# Should take the strip as an attr, but this is fine for the moment
182-
rctx.patch(patch, strip = 1)
182+
rctx.patch(patch, strip = rctx.attr.patch_strip)
183183

184184
# Write distutils.cfg to the Python installation.
185185
if "windows" in platform:
@@ -450,6 +450,7 @@ py_exec_tools_toolchain(
450450
"ignore_root_user_error": rctx.attr.ignore_root_user_error,
451451
"name": rctx.attr.name,
452452
"netrc": rctx.attr.netrc,
453+
"patch_strip": rctx.attr.patch_strip,
453454
"patches": rctx.attr.patches,
454455
"platform": platform,
455456
"python_version": python_version,
@@ -515,6 +516,21 @@ For more information see the official bazel docs
515516
"netrc": attr.string(
516517
doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive",
517518
),
519+
"patch_strip": attr.int(
520+
doc = """
521+
Same as the --strip argument of Unix patch.
522+
523+
:::{note}
524+
In the future the default value will be set to `0`, to mimic the well known
525+
function defaults (e.g. `single_version_override` for `MODULE.bazel` files.
526+
:::
527+
528+
:::{versionadded} 0.36.0
529+
:::
530+
""",
531+
default = 1,
532+
mandatory = False,
533+
),
518534
"patches": attr.label_list(
519535
doc = "A list of patch files to apply to the unpacked interpreter",
520536
mandatory = False,
@@ -627,7 +643,7 @@ def python_register_toolchains(
627643
continue
628644

629645
loaded_platforms.append(platform)
630-
(release_filename, urls, strip_prefix, patches) = get_release_info(platform, python_version, base_url, tool_versions)
646+
(release_filename, urls, strip_prefix, patches, patch_strip) = get_release_info(platform, python_version, base_url, tool_versions)
631647

632648
# allow passing in a tool version
633649
coverage_tool = None
@@ -653,6 +669,7 @@ def python_register_toolchains(
653669
),
654670
sha256 = sha256,
655671
patches = patches,
672+
patch_strip = patch_strip,
656673
platform = platform,
657674
python_version = python_version,
658675
release_filename = release_filename,

python/versions.bzl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ DEFAULT_RELEASE_BASE_URL = "https://github.com/indygreg/python-build-standalone/
4141
# "strip_prefix": "python",
4242
# },
4343
#
44-
# It is possible to provide lists in "url".
44+
# It is possible to provide lists in "url". It is also possible to provide patches or patch_strip.
4545
#
4646
# buildifier: disable=unsorted-dict-items
4747
TOOL_VERSIONS = {
@@ -636,7 +636,7 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
636636
tool_versions: A dict listing the interpreter versions, their SHAs and URL
637637
638638
Returns:
639-
A tuple of (filename, url, and archive strip prefix)
639+
A tuple of (filename, url, archive strip prefix, patches, patch_strip)
640640
"""
641641

642642
url = tool_versions[python_version]["url"]
@@ -673,8 +673,14 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
673673
patches = patches[platform]
674674
else:
675675
patches = []
676+
patch_strip = tool_versions[python_version].get("patch_strip", None)
677+
if type(patch_strip) == type({}):
678+
if platform in patch_strip.keys():
679+
patch_strip = patch_strip[platform]
680+
else:
681+
patch_strip = None
676682

677-
return (release_filename, rendered_urls, strip_prefix, patches)
683+
return (release_filename, rendered_urls, strip_prefix, patches, patch_strip)
678684

679685
def print_toolchains_checksums(name):
680686
native.genrule(

0 commit comments

Comments
 (0)