Skip to content

Commit c303849

Browse files
authored
Add tags pypi_name and pypi_version to generated py_library targets (#530)
This allows tooling to use a bazel query to reverse-engineer a requirements.txt from a transitive closure of a py_binary
1 parent ea1bb2b commit c303849

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

examples/pip_install/BUILD

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
2+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
13
load(
24
"@pip//:requirements.bzl",
35
"entry_point",
@@ -64,3 +66,31 @@ compile_pip_requirements(
6466
name = "requirements",
6567
extra_args = ["--allow-unsafe"],
6668
)
69+
70+
# Assert that tags are present on resulting py_library,
71+
# which is useful for tooling that needs to reflect on the dep graph
72+
# to determine the packages it was built from.
73+
genquery(
74+
name = "yamllint_lib_by_version",
75+
expression = """
76+
attr("tags", "\\bpypi_version=1.26.3\\b", "@pip//pypi__yamllint")
77+
intersect
78+
attr("tags", "\\bpypi_name=yamllint\\b", "@pip//pypi__yamllint")
79+
""",
80+
scope = [requirement("yamllint")],
81+
)
82+
83+
write_file(
84+
name = "write_expected",
85+
out = "expected",
86+
content = [
87+
"@pip//pypi__yamllint:pypi__yamllint",
88+
"",
89+
],
90+
)
91+
92+
diff_test(
93+
name = "test_query_result",
94+
file1 = "expected",
95+
file2 = "yamllint_lib_by_version",
96+
)

examples/pip_install/WORKSPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ workspace(name = "example_repo")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

5+
http_archive(
6+
name = "bazel_skylib",
7+
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
8+
urls = [
9+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
10+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
11+
],
12+
)
13+
514
http_archive(
615
name = "rules_python",
716
sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",

python/pip_install/extract_wheels/lib/bazel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def generate_build_file_contents(
7979
dependencies: List[str],
8080
whl_file_deps: List[str],
8181
pip_data_exclude: List[str],
82+
tags: List[str],
8283
additional_targets: List[str] = [],
8384
) -> str:
8485
"""Generate a BUILD file for an unzipped Wheel
@@ -87,6 +88,8 @@ def generate_build_file_contents(
8788
name: the target name of the py_library
8889
dependencies: a list of Bazel labels pointing to dependencies of the library
8990
whl_file_deps: a list of Bazel labels pointing to wheel file dependencies of this wheel.
91+
pip_data_exclude: more patterns to exclude from the data attribute of generated py_library rules.
92+
tags: list of tags to apply to generated py_library rules.
9093
additional_targets: A list of additional targets to append to the BUILD file contents.
9194
9295
Returns:
@@ -143,13 +146,15 @@ def generate_build_file_contents(
143146
# search path for anything that depends on this.
144147
imports = ["."],
145148
deps = [{dependencies}],
149+
tags = [{tags}],
146150
)
147151
""".format(
148152
name=name,
149153
dependencies=",".join(dependencies),
150154
data_exclude=json.dumps(data_exclude),
151155
whl_file_label=WHEEL_FILE_LABEL,
152156
whl_file_deps=",".join(whl_file_deps),
157+
tags = ",".join(["\"%s\"" % t for t in tags]),
153158
data_label=DATA_LABEL,
154159
dist_info_label=DIST_INFO_LABEL,
155160
entry_point_prefix=WHEEL_ENTRY_POINT_PREFIX,
@@ -367,6 +372,7 @@ def extract_wheel(
367372
sanitised_dependencies,
368373
sanitised_wheel_file_dependencies,
369374
pip_data_exclude,
375+
["pypi_name=" + whl.name, "pypi_version=" + whl.metadata.version],
370376
entry_points,
371377
)
372378
build_file.write(contents)

0 commit comments

Comments
 (0)