Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions examples/pip_install/BUILD
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -64,3 +66,31 @@ compile_pip_requirements(
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",
)
9 changes: 9 additions & 0 deletions examples/pip_install/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ workspace(name = "example_repo")

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",
Expand Down
6 changes: 6 additions & 0 deletions python/pip_install/extract_wheels/lib/bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def generate_build_file_contents(
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
Expand All @@ -87,6 +88,8 @@ def generate_build_file_contents(
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:
Expand Down Expand Up @@ -143,13 +146,15 @@ def generate_build_file_contents(
# search path for anything that depends on this.
imports = ["."],
deps = [{dependencies}],
tags = [{tags}],
)
""".format(
name=name,
dependencies=",".join(dependencies),
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,
Expand Down Expand Up @@ -367,6 +372,7 @@ def extract_wheel(
sanitised_dependencies,
sanitised_wheel_file_dependencies,
pip_data_exclude,
["pypi_name=" + whl.name, "pypi_version=" + whl.metadata.version],
entry_points,
)
build_file.write(contents)
Expand Down