-
-
Notifications
You must be signed in to change notification settings - Fork 650
fix(pipstar): correctly handle platlib and purelib in .data #3501
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
Merged
rickeylev
merged 3 commits into
bazel-contrib:main
from
aignas:aignas.fix.pipstar-extract
Jan 10, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ s3cmd~=2.1.0 | |
| yamllint~=1.28.0 | ||
| sphinx | ||
| sphinxcontrib-serializinghtml | ||
| libclang | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """A simple whl extractor.""" | ||
|
|
||
| load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") | ||
| load("//python/private:repo_utils.bzl", "repo_utils") | ||
| load(":whl_metadata.bzl", "find_whl_metadata") | ||
|
|
||
| def whl_extract(rctx, *, whl_path, logger): | ||
| """Extract whls in Starlark. | ||
|
|
||
| Args: | ||
| rctx: the repository ctx. | ||
| whl_path: the whl path to extract. | ||
| logger: The logger to use | ||
| """ | ||
| install_dir_path = whl_path.dirname.get_child("site-packages") | ||
| repo_utils.extract( | ||
| rctx, | ||
| archive = whl_path, | ||
| output = install_dir_path, | ||
| supports_whl_extraction = rp_config.supports_whl_extraction, | ||
| ) | ||
| metadata_file = find_whl_metadata( | ||
| install_dir = install_dir_path, | ||
| logger = logger, | ||
| ) | ||
|
|
||
| # Get the <prefix>.dist_info dir name | ||
| dist_info_dir = metadata_file.dirname | ||
| rctx.file( | ||
| dist_info_dir.get_child("INSTALLER"), | ||
| "https://github.com/bazel-contrib/rules_python#pipstar", | ||
| ) | ||
| repo_root_dir = whl_path.dirname | ||
|
|
||
| # Get the <prefix>.dist_info dir name | ||
| data_dir = dist_info_dir.dirname.get_child(dist_info_dir.basename[:-len(".dist-info")] + ".data") | ||
| if data_dir.exists: | ||
| for prefix, dest_prefix in { | ||
| # https://docs.python.org/3/library/sysconfig.html#posix-prefix | ||
| # We are taking this from the legacy whl installer config | ||
| "data": "data", | ||
| "headers": "include", | ||
| # In theory there may be directory collisions here, so it would be best to | ||
| # merge the paths here. We are doing for quite a few levels deep. What is | ||
| # more, this code has to be reasonably efficient because some packages like | ||
| # to not put everything to the top level, but to indicate explicitly if | ||
| # something is in `platlib` or `purelib` (e.g. libclang wheel). | ||
| "platlib": "site-packages", | ||
| "purelib": "site-packages", | ||
| "scripts": "bin", | ||
| }.items(): | ||
| src = data_dir.get_child(prefix) | ||
| if not src.exists: | ||
| # The prefix does not exist in the wheel, we can continue | ||
| continue | ||
|
|
||
| for (src, dest) in merge_trees(src, repo_root_dir.get_child(dest_prefix)): | ||
| logger.debug(lambda: "Renaming: {} -> {}".format(src, dest)) | ||
| rctx.rename(src, dest) | ||
|
|
||
| # TODO @aignas 2025-12-16: when moving scripts to `bin`, rewrite the #!python | ||
| # shebang to be something else, for inspiration look at the hermetic | ||
| # toolchain wrappers | ||
|
|
||
| # Ensure that there is no data dir left | ||
| rctx.delete(data_dir) | ||
|
|
||
| def merge_trees(src, dest): | ||
| """Merge src into the destination path. | ||
|
|
||
| This will attempt to merge-move src files to the destination directory if there are | ||
| existing files. Fails at directory depth is 10000 or if there are collisions. | ||
|
|
||
| Args: | ||
| src: {type}`path` a src path to rename. | ||
| dest: {type}`path` a dest path to rename to. | ||
|
|
||
| Returns: | ||
| A list of tuples for src and destination paths. | ||
| """ | ||
| ret = [] | ||
| remaining = [(src, dest)] | ||
| collisions = [] | ||
| for _ in range(10000): | ||
| if collisions or not remaining: | ||
| break | ||
|
|
||
| tmp = [] | ||
| for (src, dest) in remaining: | ||
| if not dest.exists: | ||
| ret.append((src, dest)) | ||
| continue | ||
|
|
||
| if not src.is_dir: | ||
| collisions.append(src) | ||
| continue | ||
|
|
||
| for f in src.readdir(): | ||
| tmp.append((f, dest.get_child(f.basename))) | ||
|
|
||
| remaining = tmp | ||
|
|
||
| if collisions: | ||
| fail(lambda: "detected collisions between platlib and purelib data: {}".format(collisions)) | ||
|
|
||
| return ret | ||
aignas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.