|
1 |
| -use std::io::{Cursor, Write}; |
2 |
| -use std::path::Path; |
| 1 | +use std::io::{self, Cursor, Read, Seek, Write}; |
| 2 | +use std::path::{Path, PathBuf}; |
3 | 3 |
|
| 4 | +use fs_err::File; |
4 | 5 | use thiserror::Error;
|
5 | 6 | use uv_fs::Simplified;
|
6 | 7 | use zip::write::FileOptions;
|
@@ -30,28 +31,138 @@ const LAUNCHER_AARCH64_GUI: &[u8] =
|
30 | 31 | const LAUNCHER_AARCH64_CONSOLE: &[u8] =
|
31 | 32 | include_bytes!("../../uv-trampoline/trampolines/uv-trampoline-aarch64-console.exe");
|
32 | 33 |
|
| 34 | +// See `uv-trampoline::bounce`. These numbers must match. |
| 35 | +const PATH_LENGTH_SIZE: usize = size_of::<u32>(); |
| 36 | +const MAX_PATH_LENGTH: u32 = 32 * 1024; |
| 37 | +const MAGIC_NUMBER_SIZE: usize = 4; |
| 38 | + |
| 39 | +#[derive(Debug)] |
| 40 | +pub struct Launcher { |
| 41 | + pub kind: LauncherKind, |
| 42 | + pub python_path: PathBuf, |
| 43 | +} |
| 44 | + |
| 45 | +impl Launcher { |
| 46 | + #[allow(clippy::cast_possible_wrap)] |
| 47 | + pub fn try_from_path(path: &Path) -> Result<Option<Self>, Error> { |
| 48 | + let mut file = File::open(path)?; |
| 49 | + |
| 50 | + let Some(kind) = LauncherKind::try_from_file(&mut file)? else { |
| 51 | + return Ok(None); |
| 52 | + }; |
| 53 | + |
| 54 | + // Seek to the start of the path length. |
| 55 | + let Ok(_) = file.seek(io::SeekFrom::End( |
| 56 | + -((MAGIC_NUMBER_SIZE + PATH_LENGTH_SIZE) as i64), |
| 57 | + )) else { |
| 58 | + return Err(Error::InvalidLauncher( |
| 59 | + "Unable to seek to the start of the path length".to_string(), |
| 60 | + )); |
| 61 | + }; |
| 62 | + |
| 63 | + let mut buffer = [0; PATH_LENGTH_SIZE]; |
| 64 | + file.read_exact(&mut buffer) |
| 65 | + .map_err(|err| Error::InvalidLauncherRead("path length".to_string(), err))?; |
| 66 | + |
| 67 | + let path_length = { |
| 68 | + let raw_length = u32::from_le_bytes(buffer); |
| 69 | + |
| 70 | + if raw_length > MAX_PATH_LENGTH { |
| 71 | + return Err(Error::InvalidLauncher(format!( |
| 72 | + "Only paths with a length up to 32KBs are supported but the Python executable path has a length of {raw_length}" |
| 73 | + ))); |
| 74 | + } |
| 75 | + |
| 76 | + // SAFETY: Above we guarantee the length is less than 32KB |
| 77 | + raw_length as usize |
| 78 | + }; |
| 79 | + |
| 80 | + let Ok(_) = file.seek(io::SeekFrom::End( |
| 81 | + -((MAGIC_NUMBER_SIZE + PATH_LENGTH_SIZE + path_length) as i64), |
| 82 | + )) else { |
| 83 | + return Err(Error::InvalidLauncher( |
| 84 | + "Unable to seek to the start of the path".to_string(), |
| 85 | + )); |
| 86 | + }; |
| 87 | + |
| 88 | + let mut buffer = vec![0u8; path_length]; |
| 89 | + file.read_exact(&mut buffer) |
| 90 | + .map_err(|err| Error::InvalidLauncherRead("executable path".to_string(), err))?; |
| 91 | + |
| 92 | + let path = PathBuf::from(String::from_utf8(buffer).map_err(|_| { |
| 93 | + Error::InvalidLauncher("Python executable path was not valid UTF-8".to_string()) |
| 94 | + })?); |
| 95 | + |
| 96 | + Ok(Some(Self { |
| 97 | + kind, |
| 98 | + python_path: path, |
| 99 | + })) |
| 100 | + } |
| 101 | +} |
| 102 | + |
33 | 103 | /// The kind of trampoline launcher to create.
|
34 | 104 | ///
|
35 | 105 | /// See [`uv-trampoline::bounce::TrampolineKind`].
|
36 |
| -enum LauncherKind { |
| 106 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 107 | +pub enum LauncherKind { |
37 | 108 | /// The trampoline should execute itself, it's a zipped Python script.
|
38 | 109 | Script,
|
39 | 110 | /// The trampoline should just execute Python, it's a proxy Python executable.
|
40 | 111 | Python,
|
41 | 112 | }
|
42 | 113 |
|
43 | 114 | impl LauncherKind {
|
44 |
| - const fn magic_number(&self) -> &'static [u8; 4] { |
| 115 | + const fn magic_number(self) -> &'static [u8; 4] { |
45 | 116 | match self {
|
46 | 117 | Self::Script => b"UVSC",
|
47 | 118 | Self::Python => b"UVPY",
|
48 | 119 | }
|
49 | 120 | }
|
| 121 | + |
| 122 | + /// Read a [`LauncherKind`] from 4 byte buffer. |
| 123 | + /// |
| 124 | + /// If the buffer does not contain a matching magic number, `None` is returned. |
| 125 | + fn try_from_bytes(bytes: [u8; MAGIC_NUMBER_SIZE]) -> Option<Self> { |
| 126 | + if &bytes == Self::Script.magic_number() { |
| 127 | + return Some(Self::Script); |
| 128 | + } |
| 129 | + if &bytes == Self::Python.magic_number() { |
| 130 | + return Some(Self::Python); |
| 131 | + } |
| 132 | + None |
| 133 | + } |
| 134 | + |
| 135 | + /// Read a [`LauncherKind`] from a file handle. |
| 136 | + /// |
| 137 | + /// This will mutate the file handle, seeking to the end of the file. |
| 138 | + /// |
| 139 | + /// If the file cannot be read, an [`io::Error`] is returned. If the path is not a launcher, |
| 140 | + /// `None` is returned. |
| 141 | + #[allow(clippy::cast_possible_wrap)] |
| 142 | + pub fn try_from_file(file: &mut File) -> Result<Option<Self>, Error> { |
| 143 | + let mut buffer = [0; MAGIC_NUMBER_SIZE]; |
| 144 | + |
| 145 | + // If the file is less than four bytes, it's not a launcher. |
| 146 | + let Ok(_) = file.seek(io::SeekFrom::End(-(MAGIC_NUMBER_SIZE as i64))) else { |
| 147 | + return Ok(None); |
| 148 | + }; |
| 149 | + |
| 150 | + file.read_exact(&mut buffer) |
| 151 | + .map_err(|err| Error::InvalidLauncherRead("magic number".to_string(), err))?; |
| 152 | + |
| 153 | + Ok(Self::try_from_bytes(buffer)) |
| 154 | + } |
50 | 155 | }
|
51 | 156 |
|
52 | 157 | /// Note: The caller is responsible for adding the path of the wheel we're installing.
|
53 | 158 | #[derive(Error, Debug)]
|
54 | 159 | pub enum Error {
|
| 160 | + #[error(transparent)] |
| 161 | + Io(#[from] io::Error), |
| 162 | + #[error("Invalid launcher: {0}")] |
| 163 | + InvalidLauncher(String), |
| 164 | + #[error("Failed to read launcher {0}")] |
| 165 | + InvalidLauncherRead(String, #[source] io::Error), |
55 | 166 | #[error(
|
56 | 167 | "Unable to create Windows launcher for: {0} (only x86_64, x86, and arm64 are supported)"
|
57 | 168 | )]
|
@@ -192,7 +303,7 @@ mod test {
|
192 | 303 |
|
193 | 304 | use which::which;
|
194 | 305 |
|
195 |
| - use super::{windows_python_launcher, windows_script_launcher}; |
| 306 | + use super::{windows_python_launcher, windows_script_launcher, Launcher, LauncherKind}; |
196 | 307 |
|
197 | 308 | #[test]
|
198 | 309 | #[cfg(all(windows, target_arch = "x86", feature = "production"))]
|
@@ -340,6 +451,13 @@ if __name__ == "__main__":
|
340 | 451 | .stdout(stdout_predicate)
|
341 | 452 | .stderr(stderr_predicate);
|
342 | 453 |
|
| 454 | + let launcher = Launcher::try_from_path(console_bin_path.path()) |
| 455 | + .expect("We should succeed at reading the launcher") |
| 456 | + .expect("The launcher should be valid"); |
| 457 | + |
| 458 | + assert!(launcher.kind == LauncherKind::Script); |
| 459 | + assert!(launcher.python_path == python_executable_path); |
| 460 | + |
343 | 461 | Ok(())
|
344 | 462 | }
|
345 | 463 |
|
@@ -371,6 +489,13 @@ if __name__ == "__main__":
|
371 | 489 | .success()
|
372 | 490 | .stdout("Hello from Python Launcher\r\n");
|
373 | 491 |
|
| 492 | + let launcher = Launcher::try_from_path(console_bin_path.path()) |
| 493 | + .expect("We should succeed at reading the launcher") |
| 494 | + .expect("The launcher should be valid"); |
| 495 | + |
| 496 | + assert!(launcher.kind == LauncherKind::Python); |
| 497 | + assert!(launcher.python_path == python_executable_path); |
| 498 | + |
374 | 499 | Ok(())
|
375 | 500 | }
|
376 | 501 |
|
|
0 commit comments