-
-
Notifications
You must be signed in to change notification settings - Fork 681
feat(venv): make wheel scripts runnable in venv #3743
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
85
commits into
bazel-contrib:main
Choose a base branch
from
rickeylev:venv.bin.runnable
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
85 commits
Select commit
Hold shift + click to select a range
b4e6496
basic impl
rickeylev c676d6e
fix tests
rickeylev 10ace3b
dont pass select to repo-generated init creation
rickeylev 0fd7c6d
fix testing on workspace
rickeylev 7d2893f
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev 16c5100
format, lint fix
rickeylev 9e53c53
fix pip example
rickeylev 76cc0e0
format
rickeylev 9470aac
test: fix windows paths for whl_with_data test
rickeylev 5b57de2
fix: prevent grouping of top-level bin, include, and data in venvs
rickeylev 103ec9a
fix: add alias for is_venvs_site_packages and add whl_with_data to wo…
rickeylev f41bfda
fix: add WORKSPACE file and export all files in whl_with_data repo
rickeylev fe1ad5f
revert: rename of is_venvs_site_packages and whl_with_data workspace …
rickeylev b35951a
fix: add bazel_skylib to pip_parse_vendored example
rickeylev 52dfac5
fix: add bazel_skylib to multi_python_versions and pip_parse examples
rickeylev 45611a5
docs: add DATA field to VenvSymlinkKind docstring
rickeylev fd6cda6
fix: undefined data_arg in whl_library_targets.bzl
rickeylev 958e61e
fix: robustness in venvs_site_packages_libs_test for Windows paths
rickeylev 194693c
fix: make bin/s3cmd expectation conditional in pip_parse_test.py
rickeylev 82b9acc
refactor: rename is_venvs_site_packages to _is_venvs_site_packages
rickeylev dd2b0b3
pass create_inits() non-selet value, add select value afterwards
rickeylev 3ed727e
change test to enforce windows uses Scripts/Include in venv
rickeylev 7561c72
make pip_parse_test.py work with venv_site_packages
rickeylev 24a94a2
add second wheel with data to test merging/overlap. rename first
rickeylev 1fabeea
format
rickeylev 473966b
revert passing bzlmod/venv_site_packages to pip_parse_test. not neede…
rickeylev dd5d8c2
remove defunct comment
rickeylev 08aeea4
(re)add _yes suffix to is_venv_site_packages config setting
rickeylev 747f7cb
add basis for data overlap test
rickeylev 03da47f
add overlap tests for include, bin; cleanup test
rickeylev de2a11f
format
rickeylev 82e9e2a
make tests pass with workspace
rickeylev cdb2cf1
lint
rickeylev e7ca80d
tests: support whl_from_dir_repo on Windows
rickeylev e16757e
Merge branch 'main' into chore.win.whl.tests
rickeylev e35d1a4
move data scheme to venv root
rickeylev e38fd1c
move cannot_be_linked_directly init earlier
rickeylev 9184bcc
re-add data prefix to pip_parse test. It is verifying the whl_library…
rickeylev db05136
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev 51f614b
handle when directly linking to a file on windows
rickeylev 9681318
always incldue data label
rickeylev 50cc8f0
update changelog
rickeylev cd18acc
cleanup
rickeylev 8c3daaa
Merge branch 'chore.win.whl.tests' into whl.with.data
rickeylev 835d0a9
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev f08b096
feat(venv): make installed wheel scripts runnable
rickeylev ec6926e
feat: make wheel entry points runnable in venv
rickeylev 181c4a5
skip runnable test on windows
rickeylev 827baf2
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev 5894daf
Merge branch 'refs/heads/whl.with.data' into venv.bin.runnable
rickeylev 36b8dbd
checkpoint entry point template windows bat file
rickeylev 85ea206
py_venv_entry_point et al rule for whl_library
rickeylev cfefcfb
format
rickeylev 61bef05
bazel 7 fix
rickeylev 47d4aae
fix: address CI failures on Windows and cross-compilation
rickeylev 6a3dda0
fix: avoid target collisions and ensure scripts are movable on Bazel 7
rickeylev 8012fbb
fix: ensure scripts have execute permissions in whl_extract
rickeylev 0c9a831
fix: define os_name in whl_extract
rickeylev ec2cac0
fix: use root-relative labels in whl_library_targets.bzl
rickeylev a7c586c
fix: ensure destination directories exist in whl_extract
rickeylev 2271e18
refactor(pypi): factor out permission fixing logic in whl_extract
rickeylev 02be9e0
refactor(pypi): factor out permission fixing logic in whl_extract
rickeylev e276160
cleanup wheel.py, rn newlines
rickeylev da40401
fix cm name, normalize group name, remove defunct comment
rickeylev ad1202a
ignore scripts that have entry points, change parse_entry_points to r…
rickeylev 8f97ae3
clean up how entry points are passed and parsed
rickeylev a5eda25
better dupe detection
rickeylev 62a50c9
fix: address CI failures, deduplicate entry points, and improve cross…
rickeylev b9eb87e
format
rickeylev 89c1a5a
switch to set for pip_parse_test
rickeylev cf5ab40
fix pip_parse example test: rewritten bins werent added to data label
rickeylev 9cf9e5b
get pythonw test passing on windows
rickeylev fb4a7cf
format
rickeylev 61a9bc0
fix pip_parse test on windows
rickeylev cbae47e
fix test_filegroups
rickeylev c2d7916
add todo comment, re-instate rmtree code
rickeylev 4f2746b
add guard for rewrite-bin generation
rickeylev c83eae2
have test check shebang for bat
rickeylev ac4c326
skip pythonw test on bazel 7
rickeylev 1af4ea5
Merge branch 'venv.bin.runnable' of https://github.com/rickeylev/rule…
rickeylev 957fd85
format
rickeylev c1df167
set env var to detect version
rickeylev c452484
Merge branch 'venv.bin.runnable' of https://github.com/rickeylev/rule…
rickeylev 473f801
fix syntax error
rickeylev 2322861
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
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
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
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,50 @@ | ||
| """Rule for generating venv entry point scripts.""" | ||
|
|
||
| load("//python/private:attributes.bzl", "WINDOWS_CONSTRAINTS_ATTRS") | ||
| load("//python/private:common.bzl", "is_windows_platform") | ||
| load("//python/private:rule_builders.bzl", "ruleb") | ||
|
|
||
| def _venv_entry_point_impl(ctx): | ||
| is_windows = is_windows_platform(ctx) | ||
|
|
||
| out_name = ctx.label.name | ||
| python_exe = "" | ||
| if is_windows: | ||
| out_name += ".bat" | ||
| python_exe = "pythonw.exe" if ctx.attr.group == "gui_scripts" else "python.exe" | ||
|
|
||
| out = ctx.actions.declare_file(out_name) | ||
|
|
||
| ctx.actions.expand_template( | ||
| template = ctx.file._template, | ||
| output = out, | ||
| substitutions = { | ||
| "{ATTRIBUTE}": ctx.attr.attribute, | ||
| "{MODULE}": ctx.attr.module, | ||
| "{PYTHON_EXE}": python_exe, | ||
| }, | ||
| is_executable = True, | ||
| ) | ||
|
|
||
| return [DefaultInfo( | ||
| files = depset([out]), | ||
| executable = out, | ||
| )] | ||
|
|
||
| _builder = ruleb.Rule( | ||
| implementation = _venv_entry_point_impl, | ||
| executable = True, | ||
| ) | ||
| _builder.attrs.update({ | ||
| "attribute": attr.string(mandatory = False, doc = "The attribute to call"), | ||
| "extras": attr.string(mandatory = False, doc = "The extras for the entry point"), | ||
| "group": attr.string(mandatory = False, doc = "The entry point group (e.g. console_scripts)"), | ||
| "module": attr.string(mandatory = True, doc = "The module to import"), | ||
| "_template": attr.label( | ||
| default = Label("//python/private/pypi:venv_entry_point_template"), | ||
| allow_single_file = True, | ||
| ), | ||
| }) | ||
| _builder.attrs.update(WINDOWS_CONSTRAINTS_ATTRS) | ||
|
|
||
| venv_entry_point = _builder.build() |
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,8 @@ | ||
| @setlocal enabledelayedexpansion & "%~dp0{PYTHON_EXE}" -x "%~f0" %* & exit /b !ERRORLEVEL! | ||
| # -*- coding: utf-8 -*- | ||
| import re | ||
| import sys | ||
| from {MODULE} import {ATTRIBUTE} | ||
| if __name__ == "__main__": | ||
| sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) | ||
| sys.exit({ATTRIBUTE}()) |
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,10 @@ | ||
| #!/bin/sh | ||
| '''exec' "$(dirname "$0")/python3" "$0" "$@" | ||
| ' ''' | ||
| # -*- coding: utf-8 -*- | ||
| import re | ||
| import sys | ||
| from {MODULE} import {ATTRIBUTE} | ||
| if __name__ == '__main__': | ||
| sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) | ||
| sys.exit({ATTRIBUTE}()) | ||
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,82 @@ | ||
| """Rule for rewriting portable shebangs.""" | ||
|
|
||
| load("//python/private:attributes.bzl", "WINDOWS_CONSTRAINTS_ATTRS") | ||
| load("//python/private:common.bzl", "is_windows_platform", "runfiles_root_path") | ||
| load("//python/private:py_info.bzl", "PyInfoBuilder", "VenvSymlinkEntry", "VenvSymlinkKind") | ||
| load("//python/private:rule_builders.bzl", "ruleb") | ||
|
|
||
| def _venv_rewrite_shebang_impl(ctx): | ||
| is_windows = is_windows_platform(ctx) | ||
|
|
||
| out_name = ctx.label.name | ||
| if is_windows: | ||
| out_name += ".bat" | ||
|
|
||
| out_file = ctx.actions.declare_file(out_name) | ||
| in_file = ctx.file.src | ||
|
|
||
| action_args = ctx.actions.args() | ||
| rewriter_file = ctx.files._venv_shebang_rewriter[0] | ||
| 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._venv_shebang_rewriter[DefaultInfo].files_to_run | ||
|
|
||
| action_args.add(in_file) | ||
| action_args.add(out_file) | ||
| action_args.add("windows" if is_windows else "unix") | ||
|
|
||
| ctx.actions.run( | ||
| inputs = inputs, | ||
| outputs = [out_file], | ||
| executable = action_exe, | ||
| arguments = [action_args], | ||
| mnemonic = "PyVenvRewriteBin", | ||
| progress_message = "Rewriting venv bin script %{input}", | ||
| toolchain = None, | ||
| ) | ||
|
|
||
| symlink = VenvSymlinkEntry( | ||
| kind = VenvSymlinkKind.BIN, | ||
| link_to_path = runfiles_root_path(ctx, out_file.short_path), | ||
| link_to_file = out_file, | ||
| venv_path = out_name, | ||
| package = ctx.attr.package, | ||
| version = ctx.attr.version, | ||
| files = depset([out_file]), | ||
| ) | ||
| builder = PyInfoBuilder.new() | ||
| builder.venv_symlinks.add([symlink]) | ||
| py_info = builder.build() | ||
|
|
||
| return [ | ||
| DefaultInfo(files = depset([out_file]), executable = out_file), | ||
| py_info, | ||
| ] | ||
|
|
||
| _builder = ruleb.Rule( | ||
| implementation = _venv_rewrite_shebang_impl, | ||
| executable = True, | ||
| ) | ||
| _builder.attrs.update({ | ||
| "package": attr.string(), | ||
| "src": attr.label(mandatory = True, allow_single_file = True), | ||
| "version": attr.string(), | ||
| "_venv_shebang_rewriter": attr.label( | ||
| default = "//python/private/pypi:venv_shebang_rewriter", | ||
| allow_files = True, | ||
| cfg = "exec", | ||
| ), | ||
| }) | ||
| _builder.attrs.update(WINDOWS_CONSTRAINTS_ATTRS) | ||
|
|
||
| venv_rewrite_shebang = _builder.build() |
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,44 @@ | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Position=0, Mandatory=$true)] | ||
| [string]$InFile, | ||
|
|
||
| [Parameter(Position=1, Mandatory=$true)] | ||
| [string]$OutFile, | ||
|
|
||
| [Parameter(Position=2, Mandatory=$true)] | ||
| [string]$TargetOs | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| $firstLine = Get-Content -Path $InFile -TotalCount 1 -ErrorAction SilentlyContinue | ||
| $content = Get-Content -Path $InFile | Select-Object -Skip 1 | ||
|
|
||
| $Utf8NoBom = New-Object System.Text.UTF8Encoding $False | ||
|
|
||
| if ($TargetOs -eq "windows") { | ||
| if ($firstLine -match "^#!pythonw") { | ||
| $pythonExe = "pythonw.exe" | ||
| } else { | ||
| $pythonExe = "python.exe" | ||
| } | ||
| # A Batch-Python polyglot. Batch executes the first line and exits, | ||
| # while Python (via -x) ignores the first line and executes the rest. | ||
| $wrapper = "@setlocal enabledelayedexpansion & `"%~dp0$pythonExe`" -x `"%~f0`" %* & exit /b !ERRORLEVEL!" | ||
| [System.IO.File]::WriteAllText($OutFile, $wrapper + "`r`n", $Utf8NoBom) | ||
| } else { | ||
| # A Shell-Python polyglot. The shell executes the triple-quoted 'exec' | ||
| # command, re-running the script with python3 from the scripts directory. | ||
| # Python ignores the triple-quoted string and continues. | ||
| $wrapper = @" | ||
| #!/bin/sh | ||
| '''exec' "`$(dirname "`$0")/python3" "`$0" "`$@" | ||
| ' ''' | ||
| "@ | ||
| [System.IO.File]::WriteAllText($OutFile, $wrapper + "`n", $Utf8NoBom) | ||
| } | ||
|
|
||
| if ($null -ne $content) { | ||
| [System.IO.File]::AppendAllLines($OutFile, [string[]]$content, $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,27 @@ | ||
| #!/bin/sh | ||
| set -eu | ||
|
|
||
| IN="$1" | ||
| OUT="$2" | ||
| TARGET_OS="$3" | ||
|
|
||
| FIRST_LINE=$(head -n 1 "$IN") | ||
|
|
||
| if [ "$TARGET_OS" = "windows" ]; then | ||
| case "$FIRST_LINE" in | ||
| "#!pythonw"*) PYTHON_EXE="pythonw.exe" ;; | ||
| *) PYTHON_EXE="python.exe" ;; | ||
| esac | ||
| # A Batch-Python polyglot. Batch executes the first line and exits, | ||
| # while Python (via -x) ignores the first line and executes the rest. | ||
| printf "@setlocal enabledelayedexpansion & \"%%~dp0$PYTHON_EXE\" -x \"%%~f0\" %%* & exit /b !ERRORLEVEL!\r\n" > "$OUT" | ||
| else | ||
| printf "#!/bin/sh\n" > "$OUT" | ||
| # A Shell-Python polyglot. The shell executes the triple-quoted 'exec' | ||
| # command, re-running the script with python3 from the scripts directory. | ||
| # Python ignores the triple-quoted string and continues. | ||
| printf "'''exec' \"\$(dirname \"\$0\")/python3\" \"\$0\" \"\$@\"\n' '''\n" >> "$OUT" | ||
| fi | ||
|
|
||
| tail -n +2 "$IN" >> "$OUT" | ||
| chmod +x "$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
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.