-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add a trampoline variant that just executes python
#8637
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9f7fd2e
Add a trampoline variant that executes `python`
zanieb 7909dcc
Bump rust toolchain version
zanieb 357431b
Add `uv-trampoline-builder` crate for shared trampoline launcher cons…
zanieb c7fdb95
Update build instructions
zanieb f446582
Move tests to new crate
zanieb c29b9d9
Bump allowed launcher size
zanieb b420b40
Rebuild bundled trampolines
zanieb cbb90ba
Separate size tests
zanieb 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -1,51 +1,26 @@ | ||
use std::collections::HashMap; | ||
use std::io::{BufReader, Cursor, Read, Seek, Write}; | ||
use std::io; | ||
use std::io::{BufReader, Read, Seek, Write}; | ||
use std::path::{Path, PathBuf}; | ||
use std::{env, io}; | ||
|
||
use crate::record::RecordEntry; | ||
use crate::script::Script; | ||
use crate::{Error, Layout}; | ||
use data_encoding::BASE64URL_NOPAD; | ||
use fs_err as fs; | ||
use fs_err::{DirEntry, File}; | ||
use mailparse::parse_headers; | ||
use rustc_hash::FxHashMap; | ||
use sha2::{Digest, Sha256}; | ||
use tracing::{instrument, warn}; | ||
use walkdir::WalkDir; | ||
|
||
use uv_cache_info::CacheInfo; | ||
use uv_fs::{relative_to, Simplified}; | ||
use uv_normalize::PackageName; | ||
use uv_pypi_types::DirectUrl; | ||
use walkdir::WalkDir; | ||
use zip::write::FileOptions; | ||
use zip::ZipWriter; | ||
|
||
const LAUNCHER_MAGIC_NUMBER: [u8; 4] = [b'U', b'V', b'U', b'V']; | ||
use uv_trampoline_builder::windows_script_launcher; | ||
|
||
#[cfg(all(windows, target_arch = "x86"))] | ||
const LAUNCHER_I686_GUI: &[u8] = | ||
include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-i686-gui.exe"); | ||
|
||
#[cfg(all(windows, target_arch = "x86"))] | ||
const LAUNCHER_I686_CONSOLE: &[u8] = | ||
include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-i686-console.exe"); | ||
|
||
#[cfg(all(windows, target_arch = "x86_64"))] | ||
const LAUNCHER_X86_64_GUI: &[u8] = | ||
include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-x86_64-gui.exe"); | ||
|
||
#[cfg(all(windows, target_arch = "x86_64"))] | ||
const LAUNCHER_X86_64_CONSOLE: &[u8] = | ||
include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-x86_64-console.exe"); | ||
|
||
#[cfg(all(windows, target_arch = "aarch64"))] | ||
const LAUNCHER_AARCH64_GUI: &[u8] = | ||
include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-aarch64-gui.exe"); | ||
|
||
#[cfg(all(windows, target_arch = "aarch64"))] | ||
const LAUNCHER_AARCH64_CONSOLE: &[u8] = | ||
include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-aarch64-console.exe"); | ||
use crate::record::RecordEntry; | ||
use crate::script::Script; | ||
use crate::{Error, Layout}; | ||
|
||
/// Wrapper script template function | ||
/// | ||
|
@@ -158,87 +133,6 @@ fn format_shebang(executable: impl AsRef<Path>, os_name: &str, relocatable: bool | |
format!("#!{executable}") | ||
} | ||
|
||
/// A Windows script is a minimal .exe launcher binary with the python entrypoint script appended as | ||
/// stored zip file. | ||
/// | ||
/// <https://github.com/pypa/pip/blob/fd0ea6bc5e8cb95e518c23d901c26ca14db17f89/src/pip/_vendor/distlib/scripts.py#L248-L262> | ||
#[allow(unused_variables)] | ||
pub(crate) fn windows_script_launcher( | ||
launcher_python_script: &str, | ||
is_gui: bool, | ||
python_executable: impl AsRef<Path>, | ||
) -> Result<Vec<u8>, Error> { | ||
// This method should only be called on Windows, but we avoid `#[cfg(windows)]` to retain | ||
// compilation on all platforms. | ||
if cfg!(not(windows)) { | ||
return Err(Error::NotWindows); | ||
} | ||
|
||
let launcher_bin: &[u8] = match env::consts::ARCH { | ||
#[cfg(all(windows, target_arch = "x86"))] | ||
"x86" => { | ||
if is_gui { | ||
LAUNCHER_I686_GUI | ||
} else { | ||
LAUNCHER_I686_CONSOLE | ||
} | ||
} | ||
#[cfg(all(windows, target_arch = "x86_64"))] | ||
"x86_64" => { | ||
if is_gui { | ||
LAUNCHER_X86_64_GUI | ||
} else { | ||
LAUNCHER_X86_64_CONSOLE | ||
} | ||
} | ||
#[cfg(all(windows, target_arch = "aarch64"))] | ||
"aarch64" => { | ||
if is_gui { | ||
LAUNCHER_AARCH64_GUI | ||
} else { | ||
LAUNCHER_AARCH64_CONSOLE | ||
} | ||
} | ||
#[cfg(windows)] | ||
arch => { | ||
return Err(Error::UnsupportedWindowsArch(arch)); | ||
} | ||
#[cfg(not(windows))] | ||
arch => &[], | ||
}; | ||
|
||
let mut payload: Vec<u8> = Vec::new(); | ||
{ | ||
// We're using the zip writer, but with stored compression | ||
// https://github.com/njsmith/posy/blob/04927e657ca97a5e35bb2252d168125de9a3a025/src/trampolines/mod.rs#L75-L82 | ||
// https://github.com/pypa/distlib/blob/8ed03aab48add854f377ce392efffb79bb4d6091/PC/launcher.c#L259-L271 | ||
let stored = FileOptions::default().compression_method(zip::CompressionMethod::Stored); | ||
let mut archive = ZipWriter::new(Cursor::new(&mut payload)); | ||
let error_msg = "Writing to Vec<u8> should never fail"; | ||
archive.start_file("__main__.py", stored).expect(error_msg); | ||
archive | ||
.write_all(launcher_python_script.as_bytes()) | ||
.expect(error_msg); | ||
archive.finish().expect(error_msg); | ||
} | ||
|
||
let python = python_executable.as_ref(); | ||
let python_path = python.simplified_display().to_string(); | ||
|
||
let mut launcher: Vec<u8> = Vec::with_capacity(launcher_bin.len() + payload.len()); | ||
launcher.extend_from_slice(launcher_bin); | ||
launcher.extend_from_slice(&payload); | ||
launcher.extend_from_slice(python_path.as_bytes()); | ||
launcher.extend_from_slice( | ||
&u32::try_from(python_path.as_bytes().len()) | ||
.expect("File Path to be smaller than 4GB") | ||
.to_le_bytes(), | ||
); | ||
launcher.extend_from_slice(&LAUNCHER_MAGIC_NUMBER); | ||
|
||
Ok(launcher) | ||
} | ||
|
||
/// Returns a [`PathBuf`] to `python[w].exe` for script execution. | ||
/// | ||
/// <https://github.com/pypa/pip/blob/76e82a43f8fb04695e834810df64f2d9a2ff6020/src/pip/_vendor/distlib/scripts.py#L121-L126> | ||
|
@@ -1075,54 +969,6 @@ mod test { | |
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[cfg(all(windows, target_arch = "x86"))] | ||
fn test_launchers_are_small() { | ||
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. This moved into |
||
// At time of writing, they are 45kb~ bytes. | ||
assert!( | ||
super::LAUNCHER_I686_GUI.len() < 45 * 1024, | ||
"GUI launcher: {}", | ||
super::LAUNCHER_I686_GUI.len() | ||
); | ||
assert!( | ||
super::LAUNCHER_I686_CONSOLE.len() < 45 * 1024, | ||
"CLI launcher: {}", | ||
super::LAUNCHER_I686_CONSOLE.len() | ||
); | ||
} | ||
|
||
#[test] | ||
#[cfg(all(windows, target_arch = "x86_64"))] | ||
fn test_launchers_are_small() { | ||
// At time of writing, they are 45kb~ bytes. | ||
assert!( | ||
super::LAUNCHER_X86_64_GUI.len() < 45 * 1024, | ||
"GUI launcher: {}", | ||
super::LAUNCHER_X86_64_GUI.len() | ||
); | ||
assert!( | ||
super::LAUNCHER_X86_64_CONSOLE.len() < 45 * 1024, | ||
"CLI launcher: {}", | ||
super::LAUNCHER_X86_64_CONSOLE.len() | ||
); | ||
} | ||
|
||
#[test] | ||
#[cfg(all(windows, target_arch = "aarch64"))] | ||
fn test_launchers_are_small() { | ||
// At time of writing, they are 45kb~ bytes. | ||
assert!( | ||
super::LAUNCHER_AARCH64_GUI.len() < 45 * 1024, | ||
"GUI launcher: {}", | ||
super::LAUNCHER_AARCH64_GUI.len() | ||
); | ||
assert!( | ||
super::LAUNCHER_AARCH64_CONSOLE.len() < 45 * 1024, | ||
"CLI launcher: {}", | ||
super::LAUNCHER_AARCH64_CONSOLE.len() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_script_executable() -> Result<()> { | ||
// Test with adjacent pythonw.exe | ||
|
This file contains 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,34 @@ | ||
[package] | ||
name = "uv-trampoline-builder" | ||
version = "0.0.1" | ||
publish = false | ||
description = "Builds launchers for `uv-trampoline`" | ||
|
||
edition = { workspace = true } | ||
rust-version = { workspace = true } | ||
homepage = { workspace = true } | ||
documentation = { workspace = true } | ||
repository = { workspace = true } | ||
authors = { workspace = true } | ||
license = { workspace = true } | ||
|
||
[features] | ||
default = ["production"] | ||
|
||
# Expect tests to run against production builds of `uv-trampoline` binaries, rather than debug builds | ||
production = [] | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
uv-fs = { workspace = true } | ||
thiserror = { workspace = true } | ||
zip = { workspace = true } | ||
|
||
[dev-dependencies] | ||
assert_cmd = { version = "2.0.16" } | ||
assert_fs = { version = "1.1.2" } | ||
anyhow = { version = "1.0.89" } | ||
fs-err = { workspace = true } | ||
which = { workspace = true } |
Oops, something went wrong.
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.
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.
This moved ~unchanged into
uv-trampoline-builder