-
-
Notifications
You must be signed in to change notification settings - Fork 658
feat: add package metadata for wheel libraries #3531
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,12 @@ _TEMPLATE = """\ | |
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| package_metadata( | ||
| name = "package_metadata", | ||
| purl = {purl}, | ||
| visibility = ["//:__subpackages__"], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why limited visibility when the rest is public?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed go from the gazelle repo: https://github.com/bazel-contrib/bazel-gazelle/blob/7b9d7f36a9278df5637cdb82f660c46b8df2aae4/internal/go_repository.bzl#L432-L438 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah... but I wonder if they did it right :-) Usually, tools pick up the metadata by aspect traversal, so visibility is not needed. @Yannic: you did the go thing. Why limited visiblity? |
||
| ) | ||
|
|
||
| {fn}( | ||
| {kwargs} | ||
| ) | ||
|
|
@@ -50,6 +56,7 @@ def generate_whl_library_build_bazel( | |
| *, | ||
| annotation = None, | ||
| default_python_version = None, | ||
| purl = None, | ||
| **kwargs): | ||
| """Generate a BUILD file for an unzipped Wheel | ||
|
|
||
|
|
@@ -63,7 +70,10 @@ def generate_whl_library_build_bazel( | |
| A complete BUILD file as a string | ||
| """ | ||
|
|
||
| loads = [] | ||
| loads = [ | ||
| """load("@package_metadata//rules:package_metadata.bzl", "package_metadata")""", | ||
| ] | ||
|
|
||
| if kwargs.get("tags"): | ||
| fn = "whl_library_targets" | ||
|
|
||
|
|
@@ -132,6 +142,7 @@ def generate_whl_library_build_bazel( | |
| "{} = {},".format(k, _RENDER.get(k, repr)(v)) | ||
| for k, v in sorted(kwargs.items()) | ||
| ])), | ||
| purl = repr(purl), | ||
| ), | ||
| ] + additional_content, | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -297,6 +297,16 @@ def _extract_whl_py(rctx, *, python_interpreter, args, whl_path, environment, lo | |
| logger = logger, | ||
| ) | ||
|
|
||
| def _to_purl(metadata): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if it's a wheel library from outside of pypi? I've worked on this before and I took this approach: https://github.com/bazel-contrib-supply-chain-extras/rules_python/pull/1/changes |
||
| """ | ||
| Produce a PyPI PURL from the metadata. | ||
|
|
||
| https://github.com/package-url/purl-spec/blob/main/types-doc/pypi-definition.md | ||
| """ | ||
| # https://github.com/package-url/purl-spec/blob/main/types-doc/pypi-definition.md#name-definition | ||
| name = metadata.name.replace("_", "-").lower() | ||
| return "pkg:pypi/{}@{}".format(name, metadata.version) | ||
|
|
||
| def _whl_library_impl(rctx): | ||
| logger = repo_utils.logger(rctx) | ||
| python_interpreter = pypi_repo_utils.resolve_python_interpreter( | ||
|
|
@@ -476,6 +486,7 @@ def _whl_library_impl(rctx): | |
| group_name = rctx.attr.group_name, | ||
| namespace_package_files = namespace_package_files, | ||
| extras = requirement(rctx.attr.requirement).extras, | ||
| purl = _to_purl(metadata), | ||
| ) | ||
| else: | ||
| metadata = json.decode(rctx.read("metadata.json")) | ||
|
|
@@ -526,14 +537,21 @@ def _whl_library_impl(rctx): | |
| "pypi_version={}".format(metadata["version"]), | ||
| ], | ||
| namespace_package_files = namespace_package_files, | ||
| purl = _to_purl(metadata), | ||
| ) | ||
|
|
||
| # Delete these in case the wheel had them. They generally don't cause | ||
| # a problem, but let's avoid the chance of that happening. | ||
| rctx.file("WORKSPACE") | ||
| rctx.file("WORKSPACE.bazel") | ||
| rctx.file("MODULE.bazel") | ||
| rctx.file("REPO.bazel") | ||
| rctx.file("REPO.bazel", """\ | ||
| repo( | ||
| default_package_metadata = [ | ||
| "//:package_metadata", | ||
| ], | ||
| ) | ||
| """) | ||
|
|
||
| paths = list(rctx.path(".").readdir()) | ||
| for _ in range(10000000): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.0.7 is out now