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
diff --git a/examples/pip_install/BUILD b/examples/pip_install/BUILD
index c57ffbd..ceb3dd3 100644
--- a/examples/pip_install/BUILD
+++ b/examples/pip_install/BUILD
@@ -1,3 +1,5 @@
+load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
+load("@bazel_skylib//rules:write_file.bzl", "write_file")
load(
"@pip//:requirements.bzl",
"entry_point",
@@ -64,3 +66,31 @@
name = "requirements",
extra_args = ["--allow-unsafe"],
)
+
+# Assert that tags are present on resulting py_library,
+# which is useful for tooling that needs to reflect on the dep graph
+# to determine the packages it was built from.
+genquery(
+ name = "yamllint_lib_by_version",
+ expression = """
+ attr("tags", "\\bpypi_version=1.26.3\\b", "@pip//pypi__yamllint")
+ intersect
+ attr("tags", "\\bpypi_name=yamllint\\b", "@pip//pypi__yamllint")
+ """,
+ scope = [requirement("yamllint")],
+)
+
+write_file(
+ name = "write_expected",
+ out = "expected",
+ content = [
+ "@pip//pypi__yamllint:pypi__yamllint",
+ "",
+ ],
+)
+
+diff_test(
+ name = "test_query_result",
+ file1 = "expected",
+ file2 = "yamllint_lib_by_version",
+)
diff --git a/examples/pip_install/WORKSPACE b/examples/pip_install/WORKSPACE
index 245b5f9..ce5333b 100644
--- a/examples/pip_install/WORKSPACE
+++ b/examples/pip_install/WORKSPACE
@@ -3,6 +3,15 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
+ name = "bazel_skylib",
+ sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
+ urls = [
+ "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
+ "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
+ ],
+)
+
+http_archive(
name = "rules_python",
sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
diff --git a/python/pip_install/extract_wheels/lib/bazel.py b/python/pip_install/extract_wheels/lib/bazel.py
index 7a91191..a0a1ccd 100644
--- a/python/pip_install/extract_wheels/lib/bazel.py
+++ b/python/pip_install/extract_wheels/lib/bazel.py
@@ -79,6 +79,7 @@
dependencies: List[str],
whl_file_deps: List[str],
pip_data_exclude: List[str],
+ tags: List[str],
additional_targets: List[str] = [],
) -> str:
"""Generate a BUILD file for an unzipped Wheel
@@ -87,6 +88,8 @@
name: the target name of the py_library
dependencies: a list of Bazel labels pointing to dependencies of the library
whl_file_deps: a list of Bazel labels pointing to wheel file dependencies of this wheel.
+ pip_data_exclude: more patterns to exclude from the data attribute of generated py_library rules.
+ tags: list of tags to apply to generated py_library rules.
additional_targets: A list of additional targets to append to the BUILD file contents.
Returns:
@@ -143,6 +146,7 @@
# search path for anything that depends on this.
imports = ["."],
deps = [{dependencies}],
+ tags = [{tags}],
)
""".format(
name=name,
@@ -150,6 +154,7 @@
data_exclude=json.dumps(data_exclude),
whl_file_label=WHEEL_FILE_LABEL,
whl_file_deps=",".join(whl_file_deps),
+ tags = ",".join(["\"%s\"" % t for t in tags]),
data_label=DATA_LABEL,
dist_info_label=DIST_INFO_LABEL,
entry_point_prefix=WHEEL_ENTRY_POINT_PREFIX,
@@ -367,6 +372,7 @@
sanitised_dependencies,
sanitised_wheel_file_dependencies,
pip_data_exclude,
+ ["pypi_name=" + whl.name, "pypi_version=" + whl.metadata.version],
entry_points,
)
build_file.write(contents)