8000 Add modules_map generation based on the whl list · bazel-contrib/rules_python@c2a07ad · GitHub
[go: up one dir, main page]

Skip to content

Commit c2a07ad

Browse files
committed
Add modules_map generation based on the whl list
Fix the `all_whl_requirements` and `all_requirements` macros to be usable when using bzlmod.
1 parent aa900a5 commit c2a07ad

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

examples/bzlmod_build_file_generation/BUILD.bazel

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# The `load` statement imports the symbol for the rule, in the defined
44
# ruleset. When the symbol is loaded you can use the rule.
55
load("@gazelle//:def.bzl", "gazelle")
6+
load("@pip//:requirements.bzl", "all_whl_requirements")
67
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
8+
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
79
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
810
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
911

@@ -15,6 +17,19 @@ compile_pip_requirements(
1517
requirements_windows = "requirements_windows.txt",
1618
)
1719

20+
# This repository rule fetches the metadata for python packages we
21+
# depend on. That data is required for the gazelle_python_manifest
22+
# rule to update our manifest file.
23+
# To see what this rule does, try `bazel run @modules_map//:print`
24+
modules_mapping(
25+
name = "modules_map",
26+
exclude_patterns = [
27+
"^_|(\\._)+", # This is the default.
28+
"(\\.tests)+", # Add a custom one to get rid of the psutil tests.
29+
],
30+
wheels = all_whl_requirements,
31+
)
32+
1833
# Our gazelle target points to the python gazelle binary.
1934
# This is the simple case where we only need one language supported.
2035
# If you also had proto, go, or other gazelle-supported languages,
@@ -69,17 +84,3 @@ py_test(
6984
main = "__test__.py",
7085
deps = [":bzlmod_build_file_generation"],
7186
)
72-
73-
# This repository rule fetches the metadata for python packages we
74-
# depend on. That data is required for the gazelle_python_manifest
75-
# rule to update our manifest file.
76-
# To see what this rule does, try `bazel run @modules_map//:print`
77-
modules_mapping(
78-
name = "modules_map",
79-
exclude_patterns = [
80-
"^_|(\\._)+", # This is the default.
81-
"(\\.tests)+", # Add a custom one to get rid of the psutil tests.
82-
],
83-
# TODO @aignas 2023-01-02: Think of a better way to do this
84-
wheels = [r.replace("@pip_", "@pip//:").replace("//:whl", "_whl") for r in all_whl_requirements],
85-
)

python/pip_install/extract_wheels/bazel.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,22 @@ def sanitise_name(name: str, prefix: str) -> str:
2525
return prefix + name.replace("-", "_").replace(".", "_").lower()
2626

2727

28-
def _whl_name_to_repo_root(whl_name: str, repo_prefix: str) -> str:
29-
return "@{}//".format(sanitise_name(whl_name, prefix=repo_prefix))
30-
31-
32-
def sanitised_repo_library_label(whl_name: str, repo_prefix: str) -> str:
33-
return '"{}:{}"'.format(
34-
_whl_name_to_repo_root(whl_name, repo_prefix), PY_LIBRARY_LABEL
28+
def _sanitized_label(whl_name: str, repo_prefix: str, label: str, bzlmod: bool = False) -> str:
29+
if bzlmod:
30+
return '"@{}//:{}_{}"'.format(
31+
repo[:-1],
32+
sanitise_name(whl_name, prefix=""),
33+
label,
34+
)
35+
36+
return '"@{}//:{}"'.format(
37+
sanitise_name(whl_name, prefix=repo_prefix),
38+
label,
3539
)
3640

41+
def sanitised_repo_library_label(whl_name: str, repo_prefix: str, bzlmod: bool = False) -> str:
42+
return _sanitized_label(whl_name, repo_prefix, PY_LIBRARY_LABEL)
3743

38-
def sanitised_repo_file_label(whl_name: str, repo_prefix: str) -> str:
39-
return '"{}:{}"'.format(
40-
_whl_name_to_repo_root(whl_name, repo_prefix), WHEEL_FILE_LABEL
41-
)
44+
45+
def sanitised_repo_file_label(whl_name: str, repo_prefix: str, bzlmod: bool = False) -> str:
46+
return _sanitized_label(whl_name, repo_prefix, WHEEL_FILE_LABEL)

python/pip_install/extract_wheels/parse_requirements_to_bzl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ def generate_parsed_requirements_contents(
117117

118118
all_requirements = ", ".join(
119119
[
120-
bazel.sanitised_repo_library_label(ir.name, repo_prefix=repo_prefix)
120+
bazel.sanitised_repo_library_label(ir.name, repo_prefix=repo_prefix, bzlmod=True)
121121
for ir, _ in install_req_and_lines
122122
]
123123
)
124+
124125
all_whl_requirements = ", ".join(
125126
[
126-
bazel.sanitised_repo_file_label(ir.name, repo_prefix=repo_prefix)
127+
bazel.sanitised_repo_file_label(ir.name, repo_prefix=repo_prefix, bzlmod=True)
127128
for ir, _ in install_req_and_lines
128129
]
129130
)

0 commit comments

Comments
 (0)
0