-
-
Notifications
You must be signed in to change notification settings - Fork 708
fix(pypi): rewrite RECORD file entries for extracted .data contents #4025
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
Open
rickeylev
wants to merge
16
commits into
bazel-contrib:main
Choose a base branch
from
rickeylev:verify_importlib_metadata_files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
44dca9a
test(venv): expand importlib_metadata_test to verify all files exist …
rickeylev caeb209
test(venv): update importlib_metadata_test to verify all files are fo…
rickeylev 866c9eb
test(venv): assert exact expected file paths in importlib_metadata_test
rickeylev bd0da7e
test(venv): sort expected paths in importlib_metadata_test
rickeylev a399575
fix(pypi): rewrite RECORD file entries for extracted .data contents
rickeylev f57b25f
Merge remote-tracking branch 'upstream/main' into verify_importlib_me…
rickeylev cd2f6e4
refactor(pypi): unify wheel .data extraction and RECORD rewrite mapping
rickeylev b720667
test(venv): handle Windows venv directory depth in importlib_metadata…
rickeylev d54bede
Generate platform-specific RECORD files at build time
rickeylev 56921ab
test(pypi): fix windows test runner and assertions for RECORD rewriting
rickeylev e30d1b7
test(pypi): fix windows path resolution and diff crlf in tests
rickeylev ccb8660
fix(pypi): emit lf line endings in powershell wheel record rewriter
rickeylev 957e731
refactor(pypi): address review feedback on RECORD rewriting and comments
rickeylev 3534a13
refactor(pypi): use direct attr dict in attributes.bzl and restore me…
rickeylev a94faba
refactor(pypi): add WINDOWS_CONSTRAINTS_PLAIN_ATTRS and use dict unio…
rickeylev 203e233
Merge branch 'upstream/main' into verify_importlib_metadata_files
rickeylev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (pypi) Fixed {obj}`RECORD` file paths for extracted `.data` directory contents | ||
| so that {obj}`importlib.metadata.files()` correctly locates installed | ||
| distribution files ([#4025](https://github.com/bazel-contrib/rules_python/pull/4025)). |
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,79 @@ | ||
| """Rule for generating platform-specific RECORD files.""" | ||
|
|
||
| load("//python/private:attributes.bzl", "WINDOWS_CONSTRAINTS_PLAIN_ATTRS") | ||
| load("//python/private:common.bzl", "is_windows_platform") | ||
|
|
||
| def _gen_wheel_record_impl(ctx): | ||
| is_windows = is_windows_platform(ctx) | ||
| rewriter_file = ctx.files._wheel_record_rewriter[0] | ||
| out_files = [] | ||
|
|
||
| for in_file in ctx.files.srcs: | ||
| dist_info_name = in_file.dirname.rpartition("/")[2] | ||
| if dist_info_name: | ||
| if dist_info_name.endswith(".dist-info"): | ||
| data_dir_basename = ( | ||
| dist_info_name[:-len(".dist-info")] + ".data" | ||
| ) | ||
| else: | ||
| data_dir_basename = dist_info_name + ".data" | ||
| out_file = ctx.actions.declare_file( | ||
| "site-packages/{}/RECORD".format(dist_info_name), | ||
| ) | ||
| else: | ||
| data_dir_basename = "data" | ||
| out_file = ctx.actions.declare_file("site-packages/RECORD") | ||
|
|
||
| out_files.append(out_file) | ||
|
|
||
| action_args = ctx.actions.args() | ||
| inputs = depset([in_file, rewriter_file]) | ||
|
|
||
| if rewriter_file.path.endswith(".ps1"): | ||
| action_exe = "powershell.exe" | ||
| action_args.add_all([ | ||
| "-ExecutionPolicy", | ||
| "Bypass", | ||
| "-NoProfile", | ||
| "-File", | ||
| rewriter_file, | ||
| ]) | ||
| else: | ||
| action_exe = ( | ||
| ctx.attr._wheel_record_rewriter[DefaultInfo].files_to_run | ||
| ) | ||
|
|
||
| action_args.add(in_file) | ||
| action_args.add(out_file) | ||
| action_args.add("windows" if is_windows else "unix") | ||
| action_args.add(data_dir_basename) | ||
|
|
||
| ctx.actions.run( | ||
| inputs = inputs, | ||
| outputs = [out_file], | ||
| executable = action_exe, | ||
| arguments = [action_args], | ||
| mnemonic = "PyRewriteWheelRecord", | ||
| progress_message = "Rewriting wheel RECORD %{output}", | ||
| toolchain = None, | ||
| ) | ||
|
|
||
| return [ | ||
| DefaultInfo(files = depset(out_files)), | ||
| ] | ||
|
|
||
| gen_wheel_record = rule( | ||
| implementation = _gen_wheel_record_impl, | ||
| attrs = WINDOWS_CONSTRAINTS_PLAIN_ATTRS | { | ||
| "srcs": attr.label_list( | ||
| doc = "The original RECORD files to rewrite.", | ||
| mandatory = True, | ||
| allow_files = True, | ||
| ), | ||
| "_wheel_record_rewriter": attr.label( | ||
| default = "//python/private/pypi:wheel_record_rewriter", | ||
| allow_files = True, | ||
| cfg = "exec", | ||
| ), | ||
| }, | ||
| ) |
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,66 @@ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Position=0, Mandatory=$true)] | ||
| [string]$InFile, | ||
|
|
||
| [Parameter(Position=1, Mandatory=$true)] | ||
| [string]$OutFile, | ||
|
|
||
| [Parameter(Position=2, Mandatory=$true)] | ||
| [string]$TargetOs, | ||
|
|
||
| [Parameter(Position=3, Mandatory=$true)] | ||
| [string]$DataDirBasename | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| $dataPrefix = "$DataDirBasename/" | ||
| $quotedDataPrefix = "`"$DataDirBasename/" | ||
|
|
||
| if ($TargetOs -eq "windows") { | ||
| $dataRepl = "../../" | ||
| $headersRepl = "../../Include/" | ||
| $platlibRepl = "" | ||
| $purelibRepl = "" | ||
| $scriptsRepl = "../../Scripts/" | ||
| } else { | ||
| $dataRepl = "../../../" | ||
| $headersRepl = "../../../include/" | ||
| $platlibRepl = "" | ||
| $purelibRepl = "" | ||
| $scriptsRepl = "../../../bin/" | ||
| } | ||
|
|
||
| $lines = Get-Content -Path $InFile | ||
| $outLines = [System.Collections.Generic.List[string]]::new() | ||
| $Utf8NoBom = New-Object System.Text.UTF8Encoding $False | ||
|
|
||
| foreach ($line in $lines) { | ||
| if ($line.StartsWith($quotedDataPrefix)) { | ||
| $quote = "`"" | ||
| $rest = $line.Substring($quotedDataPrefix.Length) | ||
| } elseif ($line.StartsWith($dataPrefix)) { | ||
| $quote = "" | ||
| $rest = $line.Substring($dataPrefix.Length) | ||
| } else { | ||
| $outLines.Add($line) | ||
| continue | ||
| } | ||
|
|
||
| if ($rest.StartsWith("purelib/")) { | ||
| $outLines.Add($quote + $purelibRepl + $rest.Substring(8)) | ||
| } elseif ($rest.StartsWith("platlib/")) { | ||
| $outLines.Add($quote + $platlibRepl + $rest.Substring(8)) | ||
| } elseif ($rest.StartsWith("scripts/")) { | ||
| $outLines.Add($quote + $scriptsRepl + $rest.Substring(8)) | ||
| } elseif ($rest.StartsWith("headers/")) { | ||
| $outLines.Add($quote + $headersRepl + $rest.Substring(8)) | ||
| } elseif ($rest.StartsWith("data/")) { | ||
| $outLines.Add($quote + $dataRepl + $rest.Substring(5)) | ||
| } else { | ||
| $outLines.Add($line) | ||
| } | ||
| } | ||
|
|
||
| [System.IO.File]::WriteAllText($OutFile, ($outLines -join "`n") + "`n", $Utf8NoBom) |
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,60 @@ | ||
| #!/bin/sh | ||
| set -eu | ||
|
|
||
| IN="$1" | ||
| OUT="$2" | ||
| TARGET_OS="$3" | ||
| DATA_DIR_BASENAME="$4" | ||
|
|
||
| DATA_PREFIX="${DATA_DIR_BASENAME}/" | ||
| QUOTED_DATA_PREFIX="\"${DATA_DIR_BASENAME}/" | ||
|
|
||
| if [ "$TARGET_OS" = "windows" ]; then | ||
| DATA_REPL="../../" | ||
| HEADERS_REPL="../../Include/" | ||
| PLATLIB_REPL="" | ||
| PURELIB_REPL="" | ||
| SCRIPTS_REPL="../../Scripts/" | ||
| else | ||
| DATA_REPL="../../../" | ||
| HEADERS_REPL="../../../include/" | ||
| PLATLIB_REPL="" | ||
| PURELIB_REPL="" | ||
| SCRIPTS_REPL="../../../bin/" | ||
| fi | ||
|
|
||
| awk -v data_prefix="$DATA_PREFIX" \ | ||
| -v quoted_data_prefix="$QUOTED_DATA_PREFIX" \ | ||
| -v data_repl="$DATA_REPL" \ | ||
| -v headers_repl="$HEADERS_REPL" \ | ||
| -v platlib_repl="$PLATLIB_REPL" \ | ||
| -v purelib_repl="$PURELIB_REPL" \ | ||
| -v scripts_repl="$SCRIPTS_REPL" ' | ||
| { | ||
| line = $0 | ||
| quote = "" | ||
| if (substr(line, 1, length(quoted_data_prefix)) == quoted_data_prefix) { | ||
| quote = "\"" | ||
| rest = substr(line, length(quoted_data_prefix) + 1) | ||
| } else if (substr(line, 1, length(data_prefix)) == data_prefix) { | ||
| rest = substr(line, length(data_prefix) + 1) | ||
| } else { | ||
| print line | ||
| next | ||
| } | ||
|
|
||
| if (substr(rest, 1, 8) == "purelib/") { | ||
| print quote purelib_repl substr(rest, 9) | ||
| } else if (substr(rest, 1, 8) == "platlib/") { | ||
| print quote platlib_repl substr(rest, 9) | ||
| } else if (substr(rest, 1, 8) == "scripts/") { | ||
| print quote scripts_repl substr(rest, 9) | ||
| } else if (substr(rest, 1, 8) == "headers/") { | ||
| print quote headers_repl substr(rest, 9) | ||
| } else if (substr(rest, 1, 5) == "data/") { | ||
| print quote data_repl substr(rest, 6) | ||
| } else { | ||
| print line | ||
| } | ||
| } | ||
| ' "$IN" > "$OUT" |
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,11 @@ | ||
| load("@rules_shell//shell:sh_test.bzl", "sh_test") | ||
| load(":whl_extract_tests.bzl", "whl_extract_test_suite") | ||
|
|
||
| whl_extract_test_suite(name = "whl_extract_tests") | ||
|
|
||
| sh_test( | ||
| name = "wheel_record_rewriter_test", | ||
| srcs = ["wheel_record_rewriter_test.sh"], | ||
| args = ["$(location //python/private/pypi:wheel_record_rewriter)"], | ||
| data = ["//python/private/pypi:wheel_record_rewriter"], | ||
| ) |
Oops, something went wrong.
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.