Skip to content

Commit ccdfece

Browse files
committed
Merge branch 'refactor.toolchains.repo.constants' into refactor.bzlmod.creates.host.repos
2 parents 4948054 + c827bdd commit ccdfece

File tree

1 file changed

+147
-130
lines changed

1 file changed

+147
-130
lines changed

python/private/toolchains_repo.bzl

Lines changed: 147 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,144 @@ py_toolchain_suite(
4343
)
4444
""".lstrip()
4545

46+
_WORKSPACE_TOOLCHAINS_BUILD_TEMPLATE = """
47+
# Generated by python/private/toolchains_repo.bzl
48+
#
49+
# These can be registered in the workspace file or passed to --extra_toolchains
50+
# flag. By default all these toolchains are registered by the
51+
# python_register_toolchains macro so you don't normally need to interact with
52+
# these targets.
53+
54+
load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")
55+
56+
""".lstrip()
57+
58+
_TOOLCHAIN_ALIASES_BUILD_TEMPLATE = """
59+
# Generated by python/private/toolchains_repo.bzl
60+
load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases")
61+
62+
package(default_visibility = ["//visibility:public"])
63+
64+
exports_files(["defs.bzl"])
65+
66+
PLATFORMS = [
67+
{loaded_platforms}
68+
]
69+
toolchain_aliases(
70+
name = "{py_repository}",
71+
platforms = PLATFORMS,
72+
)
73+
""".lstrip()
74+
75+
_TOOLCHAIN_ALIASES_DEFS_TEMPLATE = """
76+
# Generated by python/private/toolchains_repo.bzl
77+
78+
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
79+
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
80+
load("@@{rules_python}//python/private:text_util.bzl", "render")
81+
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
82+
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
83+
load(
84+
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
85+
_py_console_script_binary = "py_console_script_binary",
86+
)
87+
88+
def _with_deprecation(kwargs, *, name):
89+
kwargs["python_version"] = "{python_version}"
90+
return with_deprecation.symbol(
91+
kwargs,
92+
symbol_name = name,
93+
old_load = "@{name}//:defs.bzl",
94+
new_load = "@rules_python//python:{{}}.bzl".format(name),
95+
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
96+
)
97+
98+
def py_binary(**kwargs):
99+
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))
100+
101+
def py_console_script_binary(**kwargs):
102+
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))
103+
104+
def py_test(**kwargs):
105+
return _py_test(**_with_deprecation(kwargs, name = "py_test"))
106+
107+
def compile_pip_requirements(**kwargs):
108+
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
109+
""".lstrip()
110+
111+
_HOST_TOOLCHAIN_BUILD_CONTENT = """
112+
# Generated by python/private/toolchains_repo.bzl
113+
114+
exports_files(["python"], visibility = ["//visibility:public"])
115+
""".lstrip()
116+
117+
_HOST_PYTHON_TESTER_TEMPLATE = """
118+
from pathlib import Path
119+
import sys
120+
121+
python = Path(sys.executable)
122+
want_python = str(Path("{python}").resolve())
123+
got_python = str(Path(sys.executable).resolve())
124+
125+
assert want_python == got_python, \
126+
"Expected to use a different interpreter:\\nwant: '{{}}'\\n got: '{{}}'".format(
127+
want_python,
128+
got_python,
129+
)
130+
""".lstrip()
131+
132+
_MULTI_TOOLCHAIN_ALIASES_DEFS_TEMPLATE = """
133+
# Generated by python/private/toolchains_repo.bzl
134+
135+
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
136+
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
137+
load("@@{rules_python}//python/private:text_util.bzl", "render")
138+
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
139+
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
140+
load(
141+
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
142+
_py_console_script_binary = "py_console_script_binary",
143+
)
144+
145+
def _with_deprecation(kwargs, *, name):
146+
kwargs["python_version"] = "{python_version}"
147+
return with_deprecation.symbol(
148+
kwargs,
149+
symbol_name = name,
150+
old_load = "@{name}//{python_version}:defs.bzl",
151+
new_load = "@rules_python//python:{{}}.bzl".format(name),
152+
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
153+
)
154+
155+
def py_binary(**kwargs):
156+
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))
157+
158+
def py_console_script_binary(**kwargs):
159+
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))
160+
161+
def py_test(**kwargs):
162+
return _py_test(**_with_deprecation(kwargs, name = "py_test"))
163+
164+
def compile_pip_requirements(**kwargs):
165+
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
166+
""".lstrip()
167+
168+
_MULTI_TOOLCHAIN_ALIASES_PIP_TEMPLATE = """
169+
# Generated by python/private/toolchains_repo.bzl
170+
171+
load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")
172+
173+
def multi_pip_parse(name, requirements_lock, **kwargs):
174+
return _multi_pip_parse(
175+
name = name,
176+
python_versions = {python_versions},
177+
requirements_lock = requirements_lock,
178+
minor_mapping = {minor_mapping},
179+
**kwargs
180+
)
181+
182+
""".lstrip()
183+
46184
def python_toolchain_build_file_content(
47185
prefix,
48186
python_version,
@@ -101,17 +239,7 @@ def toolchain_suite_content(
101239
)
102240

103241
def _toolchains_repo_impl(rctx):
104-
build_content = """\
105-
# Generated by python/private/toolchains_repo.bzl
106-
#
107-
# These can be registered in the workspace file or passed to --extra_toolchains
108-
# flag. By default all these toolchains are registered by the
109-
# python_register_toolchains macro so you don't normally need to interact with
110-
# these targets.
111-
112-
load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")
113-
114-
""".format(
242+
build_content = _WORKSPACE_TOOLCHAINS_BUILD_TEMPLATE.format(
115243
rules_python = rctx.attr._rules_python_workspace.repo_name,
116244
)
117245

@@ -144,64 +272,15 @@ toolchains_repo = repository_rule(
144272

145273
def _toolchain_aliases_impl(rctx):
146274
# Base BUILD file for this repository.
147-
build_contents = """\
148-
# Generated by python/private/toolchains_repo.bzl
149-
load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases")
150-
151-
package(default_visibility = ["//visibility:public"])
152-
153-
exports_files(["defs.bzl"])
154-
155-
PLATFORMS = [
156-
{loaded_platforms}
157-
]
158-
toolchain_aliases(
159-
name = "{py_repository}",
160-
platforms = PLATFORMS,
161-
)
162-
""".format(
275+
build_contents = _TOOLCHAIN_ALIASES_BUILD_TEMPLATE.format(
163276
py_repository = rctx.attr.user_repository_name,
164277
loaded_platforms = "\n".join([" \"{}\",".format(p) for p in rctx.attr.platforms]),
165278
)
166279
rctx.file("BUILD.bazel", build_contents)
167280

168281
# Expose a Starlark file so rules can know what host platform we used and where to find an interpreter
169282
# when using repository_ctx.path, which doesn't understand aliases.
170-
rctx.file("defs.bzl", content = """\
171-
# Generated by python/private/toolchains_repo.bzl
172-
173-
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
174-
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
175-
load("@@{rules_python}//python/private:text_util.bzl", "render")
176-
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
177-
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
178-
load(
179-
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
180-
_py_console_script_binary = "py_console_script_binary",
181-
)
182-
183-
def _with_deprecation(kwargs, *, name):
184-
kwargs["python_version"] = "{python_version}"
185-
return with_deprecation.symbol(
186-
kwargs,
187-
symbol_name = name,
188-
old_load = "@{name}//:defs.bzl",
189-
new_load = "@rules_python//python:{{}}.bzl".format(name),
190-
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
191-
)
192-
193-
def py_binary(**kwargs):
194-
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))
195-
196-
def py_console_script_binary(**kwargs):
197-
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))
198-
199-
def py_test(**kwargs):
200-
return _py_test(**_with_deprecation(kwargs, name = "py_test"))
201-
202-
def compile_pip_requirements(**kwargs):
203-
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
204-
""".format(
283+
rctx.file("defs.bzl", content = _TOOLCHAIN_ALIASES_DEFS_TEMPLATE.format(
205284
name = rctx.attr.name,
206285
python_version = rctx.attr.python_version,
207286
rules_python = rctx.attr._rules_python_workspace.repo_name,
@@ -229,11 +308,7 @@ actions.""",
229308
)
230309

231310
def _host_toolchain_impl(rctx):
232-
rctx.file("BUILD.bazel", """\
233-
# Generated by python/private/toolchains_repo.bzl
234-
235-
exports_files(["python"], visibility = ["//visibility:public"])
236-
""")
311+
rctx.file("BUILD.bazel", _HOST_TOOLCHAIN_BUILD_CONTENT)
237312

238313
os_name = repo_utils.get_platforms_os_name(rctx)
239314
host_platform = _get_host_platform(
@@ -279,20 +354,10 @@ exports_files(["python"], visibility = ["//visibility:public"])
279354

280355
# Ensure that we can run the interpreter and check that we are not
281356
# using the host interpreter.
282-
python_tester_contents = """\
283-
from pathlib import Path
284-
import sys
285-
286-
python = Path(sys.executable)
287-
want_python = str(Path("{python}").resolve())
288-
got_python = str(Path(sys.executable).resolve())
289-
290-
assert want_python == got_python, \
291-
"Expected to use a different interpreter:\\nwant: '{{}}'\\n got: '{{}}'".format(
292-
want_python,
293-
got_python,
357+
python_tester_contents = _HOST_PYTHON_TESTER_TEMPLATE.format(
358+
repo = repo.strip("@"),
359+
python = python_binary,
294360
)
295-
""".format(repo = repo.strip("@"), python = python_binary)
296361
python_tester = rctx.path("python_tester.py")
297362
rctx.file(python_tester, python_tester_contents)
298363
repo_utils.execute_checked(
@@ -331,63 +396,15 @@ def _multi_toolchain_aliases_impl(rctx):
331396

332397
for python_version, repository_name in rctx.attr.python_versions.items():
333398
file = "{}/defs.bzl".format(python_version)
334-
rctx.file(file, content = """\
335-
# Generated by python/private/toolchains_repo.bzl
336-
337-
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
338-
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
339-
load("@@{rules_python}//python/private:text_util.bzl", "render")
340-
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
341-
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
342-
load(
343-
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
344-
_py_console_script_binary = "py_console_script_binary",
345-
)
346-
347-
def _with_deprecation(kwargs, *, name):
348-
kwargs["python_version"] = "{python_version}"
349-
return with_deprecation.symbol(
350-
kwargs,
351-
symbol_name = name,
352-
old_load = "@{name}//{python_version}:defs.bzl",
353-
new_load = "@rules_python//python:{{}}.bzl".format(name),
354-
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
355-
)
356-
357-
def py_binary(**kwargs):
358-
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))
359-
360-
def py_console_script_binary(**kwargs):
361-
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))
362-
363-
def py_test(**kwargs):
364-
return _py_test(**_with_deprecation(kwargs, name = "py_test"))
365-
366-
def compile_pip_requirements(**kwargs):
367-
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
368-
""".format(
399+
rctx.file(file, content = _MULTI_TOOLCHAIN_ALIASES_DEFS_TEMPLATE.format(
369400
repository_name = repository_name,
370401
name = rctx.attr.name,
371402
python_version = python_version,
372403
rules_python = rules_python,
373404
))
374405
rctx.file("{}/BUILD.bazel".format(python_version), "")
375406

376-
pip_bzl = """\
377-
# Generated by python/private/toolchains_repo.bzl
378-
379-
load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")
380-
381-
def multi_pip_parse(name, requirements_lock, **kwargs):
382-
return _multi_pip_parse(
383-
name = name,
384-
python_versions = {python_versions},
385-
requirements_lock = requirements_lock,
386-
minor_mapping = {minor_mapping},
387-
**kwargs
388-
)
389-
390-
""".format(
407+
pip_bzl = _MULTI_TOOLCHAIN_ALIASES_PIP_TEMPLATE.format(
391408
python_versions = rctx.attr.python_versions.keys(),
392409
minor_mapping = render.indent(render.dict(rctx.attr.minor_mapping), indent = " " * 8).lstrip(),
393410
rules_python = rules_python,

0 commit comments

Comments
 (0)