mirror of https://github.com/astral-sh/uv
Avoid embedding launcher scripts on non-Windows (#1124)
Just to reduce binary size on all other platforms.
This commit is contained in:
parent
f946d46273
commit
cc0e211074
|
|
@ -69,6 +69,8 @@ pub enum Error {
|
||||||
BrokenVenv(String),
|
BrokenVenv(String),
|
||||||
#[error("Unable to create Windows launch for {0} (only x64_64 is supported)")]
|
#[error("Unable to create Windows launch for {0} (only x64_64 is supported)")]
|
||||||
UnsupportedWindowsArch(&'static str),
|
UnsupportedWindowsArch(&'static str),
|
||||||
|
#[error("Unable to create Windows launcher on non-Windows platform")]
|
||||||
|
NotWindows,
|
||||||
#[error("Failed to detect the current platform")]
|
#[error("Failed to detect the current platform")]
|
||||||
PlatformInfo(#[source] PlatformInfoError),
|
PlatformInfo(#[source] PlatformInfoError),
|
||||||
#[error("Invalid version specification, only none or == is supported")]
|
#[error("Invalid version specification, only none or == is supported")]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use std::io::{BufRead, BufReader, BufWriter, Cursor, Read, Seek, Write};
|
use std::io::{BufRead, BufReader, BufWriter, Cursor, Read, Seek, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::{Command, ExitStatus, Stdio};
|
use std::process::{Command, ExitStatus, Stdio};
|
||||||
|
|
@ -34,11 +33,20 @@ use crate::{find_dist_info, Error};
|
||||||
/// `#!/usr/bin/env python`
|
/// `#!/usr/bin/env python`
|
||||||
pub const SHEBANG_PYTHON: &str = "#!/usr/bin/env python";
|
pub const SHEBANG_PYTHON: &str = "#!/usr/bin/env python";
|
||||||
|
|
||||||
pub(crate) const LAUNCHER_X86_64_GUI: &[u8] =
|
#[cfg(windows)]
|
||||||
|
const LAUNCHER_X86_64_GUI: &[u8] =
|
||||||
include_bytes!("../../puffin-trampoline/trampolines/puffin-trampoline-gui.exe");
|
include_bytes!("../../puffin-trampoline/trampolines/puffin-trampoline-gui.exe");
|
||||||
pub(crate) const LAUNCHER_X86_64_CONSOLE: &[u8] =
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
const LAUNCHER_X86_64_CONSOLE: &[u8] =
|
||||||
include_bytes!("../../puffin-trampoline/trampolines/puffin-trampoline-console.exe");
|
include_bytes!("../../puffin-trampoline/trampolines/puffin-trampoline-console.exe");
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
const LAUNCHER_X86_64_GUI: &[u8] = &[];
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
const LAUNCHER_X86_64_CONSOLE: &[u8] = &[];
|
||||||
|
|
||||||
/// Wrapper script template function
|
/// Wrapper script template function
|
||||||
///
|
///
|
||||||
/// <https://github.com/pypa/pip/blob/7f8a6844037fb7255cfd0d34ff8e8cf44f2598d4/src/pip/_vendor/distlib/scripts.py#L41-L48>
|
/// <https://github.com/pypa/pip/blob/7f8a6844037fb7255cfd0d34ff8e8cf44f2598d4/src/pip/_vendor/distlib/scripts.py#L41-L48>
|
||||||
|
|
@ -284,14 +292,21 @@ pub(crate) fn get_shebang(location: &InstallLocation<impl AsRef<Path>>) -> Strin
|
||||||
format!("#!{path}")
|
format!("#!{path}")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A windows script is a minimal .exe launcher binary with the python entrypoint script appended as stored zip file.
|
/// A Windows script is a minimal .exe launcher binary with the python entrypoint script appended as
|
||||||
/// The launcher will look for `python[w].exe` adjacent to it in the same directory to start the embedded script.
|
/// stored zip file. The launcher will look for `python[w].exe` adjacent to it in the same directory
|
||||||
|
/// to start the embedded script.
|
||||||
///
|
///
|
||||||
/// <https://github.com/pypa/pip/blob/fd0ea6bc5e8cb95e518c23d901c26ca14db17f89/src/pip/_vendor/distlib/scripts.py#L248-L262>
|
/// <https://github.com/pypa/pip/blob/fd0ea6bc5e8cb95e518c23d901c26ca14db17f89/src/pip/_vendor/distlib/scripts.py#L248-L262>
|
||||||
pub(crate) fn windows_script_launcher(
|
pub(crate) fn windows_script_launcher(
|
||||||
launcher_python_script: &str,
|
launcher_python_script: &str,
|
||||||
is_gui: bool,
|
is_gui: bool,
|
||||||
) -> Result<Vec<u8>, Error> {
|
) -> 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 = match env::consts::ARCH {
|
let launcher_bin = match env::consts::ARCH {
|
||||||
"x86_64" => {
|
"x86_64" => {
|
||||||
if is_gui {
|
if is_gui {
|
||||||
|
|
@ -352,14 +367,22 @@ pub(crate) fn write_script_entrypoints(
|
||||||
} else {
|
} else {
|
||||||
bin_rel().join(&entrypoint.script_name)
|
bin_rel().join(&entrypoint.script_name)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Generate the launcher script.
|
||||||
let launcher_python_script = get_script_launcher(
|
let launcher_python_script = get_script_launcher(
|
||||||
&entrypoint.module,
|
&entrypoint.module,
|
||||||
&entrypoint.function,
|
&entrypoint.function,
|
||||||
&get_shebang(location),
|
&get_shebang(location),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// If necessary, wrap the launcher script in a Windows launcher binary.
|
||||||
if cfg!(windows) {
|
if cfg!(windows) {
|
||||||
let launcher = windows_script_launcher(&launcher_python_script, is_gui)?;
|
write_file_recorded(
|
||||||
write_file_recorded(site_packages, &entrypoint_relative, &launcher, record)?;
|
site_packages,
|
||||||
|
&entrypoint_relative,
|
||||||
|
&windows_script_launcher(&launcher_python_script, is_gui)?,
|
||||||
|
record,
|
||||||
|
)?;
|
||||||
} else {
|
} else {
|
||||||
write_file_recorded(
|
write_file_recorded(
|
||||||
site_packages,
|
site_packages,
|
||||||
|
|
@ -367,7 +390,8 @@ pub(crate) fn write_script_entrypoints(
|
||||||
&launcher_python_script,
|
&launcher_python_script,
|
||||||
record,
|
record,
|
||||||
)?;
|
)?;
|
||||||
// We need to make the launcher executable
|
|
||||||
|
// Make the launcher executable.
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
@ -1143,12 +1167,10 @@ mod test {
|
||||||
|
|
||||||
use indoc::{formatdoc, indoc};
|
use indoc::{formatdoc, indoc};
|
||||||
|
|
||||||
use crate::wheel::{
|
use super::{
|
||||||
read_record_file, relative_to, LAUNCHER_X86_64_CONSOLE, LAUNCHER_X86_64_GUI,
|
parse_key_value_file, parse_wheel_version, read_record_file, relative_to, Script,
|
||||||
|
LAUNCHER_X86_64_CONSOLE, LAUNCHER_X86_64_GUI,
|
||||||
};
|
};
|
||||||
use crate::{parse_key_value_file, Script};
|
|
||||||
|
|
||||||
use super::parse_wheel_version;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_key_value_file() {
|
fn test_parse_key_value_file() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue