-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::convert::Infallible; | ||
use std::os::unix::ffi::OsStringExt; | ||
use std::{ | ||
ffi::OsString, | ||
process::{Command, ExitCode, ExitStatus}, | ||
}; | ||
|
||
/// Spawns a command exec style. | ||
fn exec_spawn(cmd: &mut Command) -> std::io::Result<Infallible> { | ||
#[cfg(unix)] | ||
{ | ||
use std::os::unix::process::CommandExt; | ||
let err = cmd.exec(); | ||
Err(err) | ||
} | ||
#[cfg(windows)] | ||
{ | ||
cmd.stdin(std::process::Stdio::inherit()); | ||
let status = cmd.status()?; | ||
|
||
#[allow(clippy::exit)] | ||
std::process::exit(status.code().unwrap()) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Error { | ||
Io(std::io::Error), | ||
Which(which::Error), | ||
NoInterpreter(String), | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match self { | ||
Self::Io(err) => write!(f, "{err}"), | ||
Self::Which(err) => write!(f, "Failed to find uv binary: {err}"), | ||
Self::NoInterpreter(inner) => write!(f, "{inner}"), | ||
} | ||
} | ||
} | ||
|
||
/// Parse a `+<request>` Python version request from the arguments. | ||
fn parse_python_request(mut args: Vec<OsString>) -> (Vec<OsString>, Option<String>) { | ||
let Some(arg) = args.first() else { | ||
return (args, None); | ||
}; | ||
let arg = arg.to_string_lossy(); | ||
let Some(version) = arg.strip_prefix('+') else { | ||
return (args, None); | ||
}; | ||
let version = version.to_string(); | ||
(args.split_off(1), Some(version)) | ||
} | ||
|
||
fn run() -> Result<ExitStatus, Error> { | ||
let args = std::env::args_os().skip(1).collect::<Vec<_>>(); | ||
let (args, request) = parse_python_request(args); | ||
let uv = which::which("uv").map_err(Error::Which)?; | ||
let mut cmd = Command::new(uv); | ||
let uv_args = ["python", "find"] | ||
.iter() | ||
.copied() | ||
.chain(request.iter().map(std::string::String::as_str)); | ||
cmd.args(uv_args); | ||
let output = cmd.output().map_err(Error::Io)?; | ||
if !output.status.success() { | ||
return Err(Error::NoInterpreter( | ||
OsString::from_vec(output.stderr) | ||
.to_string_lossy() | ||
.to_string(), | ||
)); | ||
} | ||
let python = | ||
std::path::PathBuf::from(OsString::from_vec(output.stdout).to_string_lossy().trim()); | ||
let mut cmd = Command::new(python); | ||
cmd.args(&args); | ||
match exec_spawn(&mut cmd).map_err(Error::Io)? {} | ||
} | ||
|
||
#[allow(clippy::print_stderr)] | ||
fn main() -> ExitCode { | ||
let result = run(); | ||
match result { | ||
// Fail with 2 if the status cannot be cast to an exit code | ||
Ok(status) => u8::try_from(status.code().unwrap_or(2)).unwrap_or(2).into(), | ||
Err(err) => { | ||
eprintln!("error: {err}"); | ||
ExitCode::from(2) | ||
} | ||
} | ||
} |