Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: move inline code strings to top-level constants #2886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 147 additions & 130 deletions python/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,144 @@ py_toolchain_suite(
)
""".lstrip()

_WORKSPACE_TOOLCHAINS_BUILD_TEMPLATE = """
# Generated by python/private/toolchains_repo.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains
# flag. By default all these toolchains are registered by the
# python_register_toolchains macro so you don't normally need to interact with
# these targets.

load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")

""".lstrip()

_TOOLCHAIN_ALIASES_BUILD_TEMPLATE = """
# Generated by python/private/toolchains_repo.bzl
load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases")

package(default_visibility = ["//visibility:public"])

exports_files(["defs.bzl"])

PLATFORMS = [
{loaded_platforms}
]
toolchain_aliases(
name = "{py_repository}",
platforms = PLATFORMS,
)
""".lstrip()

_TOOLCHAIN_ALIASES_DEFS_TEMPLATE = """
# Generated by python/private/toolchains_repo.bzl

load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("@@{rules_python}//python/private:text_util.bzl", "render")
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
load(
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
_py_console_script_binary = "py_console_script_binary",
)

def _with_deprecation(kwargs, *, name):
kwargs["python_version"] = "{python_version}"
return with_deprecation.symbol(
kwargs,
symbol_name = name,
old_load = "@{name}//:defs.bzl",
new_load = "@rules_python//python:{{}}.bzl".format(name),
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
)

def py_binary(**kwargs):
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))

def py_console_script_binary(**kwargs):
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))

def py_test(**kwargs):
return _py_test(**_with_deprecation(kwargs, name = "py_test"))

def compile_pip_requirements(**kwargs):
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
""".lstrip()

_HOST_TOOLCHAIN_BUILD_CONTENT = """
# Generated by python/private/toolchains_repo.bzl

exports_files(["python"], visibility = ["//visibility:public"])
""".lstrip()

_HOST_PYTHON_TESTER_TEMPLATE = """
from pathlib import Path
import sys

python = Path(sys.executable)
want_python = str(Path("{python}").resolve())
got_python = str(Path(sys.executable).resolve())

assert want_python == got_python, \
"Expected to use a different interpreter:\\nwant: '{{}}'\\n got: '{{}}'".format(
want_python,
got_python,
)
""".lstrip()

_MULTI_TOOLCHAIN_ALIASES_DEFS_TEMPLATE = """
# Generated by python/private/toolchains_repo.bzl

load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("@@{rules_python}//python/private:text_util.bzl", "render")
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
load(
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
_py_console_script_binary = "py_console_script_binary",
)

def _with_deprecation(kwargs, *, name):
kwargs["python_version"] = "{python_version}"
return with_deprecation.symbol(
kwargs,
symbol_name = name,
old_load = "@{name}//{python_version}:defs.bzl",
new_load = "@rules_python//python:{{}}.bzl".format(name),
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
)

def py_binary(**kwargs):
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))

def py_console_script_binary(**kwargs):
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))

def py_test(**kwargs):
return _py_test(**_with_deprecation(kwargs, name = "py_test"))

def compile_pip_requirements(**kwargs):
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
""".lstrip()

_MULTI_TOOLCHAIN_ALIASES_PIP_TEMPLATE = """
# Generated by python/private/toolchains_repo.bzl

load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")

def multi_pip_parse(name, requirements_lock, **kwargs):
return _multi_pip_parse(
name = name,
python_versions = {python_versions},
requirements_lock = requirements_lock,
minor_mapping = {minor_mapping},
**kwargs
)

""".lstrip()

def python_toolchain_build_file_content(
prefix,
python_version,
Expand Down Expand Up @@ -101,17 +239,7 @@ def toolchain_suite_content(
)

def _toolchains_repo_impl(rctx):
build_content = """\
# Generated by python/private/toolchains_repo.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains
# flag. By default all these toolchains are registered by the
# python_register_toolchains macro so you don't normally need to interact with
# these targets.

load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")

""".format(
build_content = _WORKSPACE_TOOLCHAINS_BUILD_TEMPLATE.format(
rules_python = rctx.attr._rules_python_workspace.repo_name,
)

Expand Down Expand Up @@ -144,64 +272,15 @@ toolchains_repo = repository_rule(

def _toolchain_aliases_impl(rctx):
# Base BUILD file for this repository.
build_contents = """\
# Generated by python/private/toolchains_repo.bzl
load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases")

package(default_visibility = ["//visibility:public"])

exports_files(["defs.bzl"])

PLATFORMS = [
{loaded_platforms}
]
toolchain_aliases(
name = "{py_repository}",
platforms = PLATFORMS,
)
""".format(
build_contents = _TOOLCHAIN_ALIASES_BUILD_TEMPLATE.format(
py_repository = rctx.attr.user_repository_name,
loaded_platforms = "\n".join([" \"{}\",".format(p) for p in rctx.attr.platforms]),
)
rctx.file("BUILD.bazel", build_contents)

# Expose a Starlark file so rules can know what host platform we used and where to find an interpreter
# when using repository_ctx.path, which doesn't understand aliases.
rctx.file("defs.bzl", content = """\
# Generated by python/private/toolchains_repo.bzl

load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("@@{rules_python}//python/private:text_util.bzl", "render")
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
load(
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
_py_console_script_binary = "py_console_script_binary",
)

def _with_deprecation(kwargs, *, name):
kwargs["python_version"] = "{python_version}"
return with_deprecation.symbol(
kwargs,
symbol_name = name,
old_load = "@{name}//:defs.bzl",
new_load = "@rules_python//python:{{}}.bzl".format(name),
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
)

def py_binary(**kwargs):
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))

def py_console_script_binary(**kwargs):
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))

def py_test(**kwargs):
return _py_test(**_with_deprecation(kwargs, name = "py_test"))

def compile_pip_requirements(**kwargs):
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
""".format(
rctx.file("defs.bzl", content = _TOOLCHAIN_ALIASES_DEFS_TEMPLATE.format(
name = rctx.attr.name,
python_version = rctx.attr.python_version,
rules_python = rctx.attr._rules_python_workspace.repo_name,
Expand Down Expand Up @@ -229,11 +308,7 @@ actions.""",
)

def _host_toolchain_impl(rctx):
rctx.file("BUILD.bazel", """\
# Generated by python/private/toolchains_repo.bzl

exports_files(["python"], visibility = ["//visibility:public"])
""")
rctx.file("BUILD.bazel", _HOST_TOOLCHAIN_BUILD_CONTENT)

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

# Ensure that we can run the interpreter and check that we are not
# using the host interpreter.
python_tester_contents = """\
from pathlib import Path
import sys

python = Path(sys.executable)
want_python = str(Path("{python}").resolve())
got_python = str(Path(sys.executable).resolve())

assert want_python == got_python, \
"Expected to use a different interpreter:\\nwant: '{{}}'\\n got: '{{}}'".format(
want_python,
got_python,
python_tester_contents = _HOST_PYTHON_TESTER_TEMPLATE.format(
repo = repo.strip("@"),
python = python_binary,
)
""".format(repo = repo.strip("@"), python = python_binary)
python_tester = rctx.path("python_tester.py")
rctx.file(python_tester, python_tester_contents)
repo_utils.execute_checked(
Expand Down Expand Up @@ -331,63 +396,15 @@ def _multi_toolchain_aliases_impl(rctx):

for python_version, repository_name in rctx.attr.python_versions.items():
file = "{}/defs.bzl".format(python_version)
rctx.file(file, content = """\
# Generated by python/private/toolchains_repo.bzl

load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("@@{rules_python}//python/private:text_util.bzl", "render")
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
load(
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
_py_console_script_binary = "py_console_script_binary",
)

def _with_deprecation(kwargs, *, name):
kwargs["python_version"] = "{python_version}"
return with_deprecation.symbol(
kwargs,
symbol_name = name,
old_load = "@{name}//{python_version}:defs.bzl",
new_load = "@rules_python//python:{{}}.bzl".format(name),
snippet = render.call(name, **{{k: repr(v) for k,v in kwargs.items()}})
)

def py_binary(**kwargs):
return _py_binary(**_with_deprecation(kwargs, name = "py_binary"))

def py_console_script_binary(**kwargs):
return _py_console_script_binary(**_with_deprecation(kwargs, name = "py_console_script_binary"))

def py_test(**kwargs):
return _py_test(**_with_deprecation(kwargs, name = "py_test"))

def compile_pip_requirements(**kwargs):
return _compile_pip_requirements(**_with_deprecation(kwargs, name = "compile_pip_requirements"))
""".format(
rctx.file(file, content = _MULTI_TOOLCHAIN_ALIASES_DEFS_TEMPLATE.format(
repository_name = repository_name,
name = rctx.attr.name,
python_version = python_version,
rules_python = rules_python,
))
rctx.file("{}/BUILD.bazel".format(python_version), "")

pip_bzl = """\
# Generated by python/private/toolchains_repo.bzl

load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")

def multi_pip_parse(name, requirements_lock, **kwargs):
return _multi_pip_parse(
name = name,
python_versions = {python_versions},
requirements_lock = requirements_lock,
minor_mapping = {minor_mapping},
**kwargs
)

""".format(
pip_bzl = _MULTI_TOOLCHAIN_ALIASES_PIP_TEMPLATE.format(
python_versions = rctx.attr.python_versions.keys(),
minor_mapping = render.indent(render.dict(rctx.attr.minor_mapping), indent = " " * 8).lstrip(),
rules_python = rules_python,
Expand Down