Skip to content
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 uv as develop backend command #2015

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ use std::process::Command;
use tempfile::TempDir;
use url::Url;

enum Backend {
Pip,
Uv,
}
/// Install the crate as module in the current virtualenv
#[derive(Debug, clap::Parser)]
pub struct DevelopOptions {
Expand Down Expand Up @@ -57,20 +61,30 @@ pub struct DevelopOptions {
/// `cargo rustc` options
#[command(flatten)]
pub cargo_options: CargoOptions,
/// Use `uv` as develop backend instead of `pip`
#[arg(long)]
pub uv: bool,
}

fn make_pip_command(python_path: &Path, pip_path: Option<&Path>) -> Command {
match pip_path {
Some(pip_path) => {
let mut cmd = Command::new(pip_path);
cmd.arg("--python")
.arg(python_path)
.arg("--disable-pip-version-check");
cmd
}
None => {
fn make_backend_command(python_path: &Path, pip_path: Option<&Path>, backend: &Backend) -> Command {
match backend {
Backend::Pip => match pip_path {
Some(pip_path) => {
let mut cmd = Command::new(pip_path);
cmd.arg("--python")
.arg(python_path)
.arg("--disable-pip-version-check");
cmd
}
None => {
let mut cmd = Command::new(python_path);
cmd.arg("-m").arg("pip").arg("--disable-pip-version-check");
cmd
}
},
Backend::Uv => {
let mut cmd = Command::new(python_path);
cmd.arg("-m").arg("pip").arg("--disable-pip-version-check");
cmd.arg("-m").arg("uv").arg("pip");
cmd
}
}
Expand All @@ -81,6 +95,7 @@ fn install_dependencies(
extras: &[String],
interpreter: &PythonInterpreter,
pip_path: Option<&Path>,
backend: &Backend,
) -> Result<()> {
if !build_context.metadata23.requires_dist.is_empty() {
let mut args = vec!["install".to_string()];
Expand Down Expand Up @@ -110,7 +125,7 @@ fn install_dependencies(
}
pkg.to_string()
}));
let status = make_pip_command(&interpreter.executable, pip_path)
let status = make_backend_command(&interpreter.executable, pip_path, backend)
.args(&args)
.status()
.context("Failed to run pip install")?;
Expand All @@ -127,8 +142,9 @@ fn pip_install_wheel(
venv_dir: &Path,
pip_path: Option<&Path>,
wheel_filename: &Path,
backend: &Backend,
) -> Result<()> {
let mut pip_cmd = make_pip_command(python, pip_path);
let mut pip_cmd = make_backend_command(python, pip_path, backend);
let output = pip_cmd
.args(["install", "--no-deps", "--force-reinstall"])
.arg(dunce::simplified(wheel_filename))
Expand All @@ -148,12 +164,15 @@ fn pip_install_wheel(
String::from_utf8_lossy(&output.stderr).trim(),
);
}
// uv pip install sends logs to stderr thus only print this warning for pip
if !output.stderr.is_empty() {
eprintln!(
"⚠️ Warning: pip raised a warning running {:?}:\n{}",
&pip_cmd.get_args().collect::<Vec<_>>(),
String::from_utf8_lossy(&output.stderr).trim(),
);
if let Backend::Pip = backend {
eprintln!(
"⚠️ Warning: pip raised a warning running {:?}:\n{}",
&pip_cmd.get_args().collect::<Vec<_>>(),
String::from_utf8_lossy(&output.stderr).trim(),
);
}
}
fix_direct_url(build_context, python, pip_path)?;
Ok(())
Expand All @@ -172,7 +191,9 @@ fn fix_direct_url(
pip_path: Option<&Path>,
) -> Result<()> {
println!("✏️ Setting installed package as editable");
let mut pip_cmd = make_pip_command(python, pip_path);
// pip show --files is not supported by uv, thus
// default to using pip all the time
let mut pip_cmd = make_backend_command(python, pip_path, &Backend::Pip);
let output = pip_cmd
.args(["show", "--files"])
.arg(&build_context.metadata23.name)
Expand Down Expand Up @@ -227,7 +248,9 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
skip_install,
pip_path,
cargo_options,
uv,
} = develop_options;
let backend = if uv { Backend::Uv } else { Backend::Pip };
let mut target_triple = cargo_options.target.as_ref().map(|x| x.to_string());
let target = Target::from_target_triple(cargo_options.target)?;
let python = target.get_venv_python(venv_dir);
Expand Down Expand Up @@ -278,7 +301,13 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
|| anyhow!("Expected `python` to be a python interpreter inside a virtualenv ಠ_ಠ"),
)?;

install_dependencies(&build_context, &extras, &interpreter, pip_path.as_deref())?;
install_dependencies(
&build_context,
&extras,
&interpreter,
pip_path.as_deref(),
&backend,
)?;

let wheels = build_context.build_wheels()?;
if !skip_install {
Expand All @@ -289,6 +318,7 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
venv_dir,
pip_path.as_deref(),
filename,
&backend,
)?;
eprintln!(
"🛠 Installed {}-{}",
Expand Down
3 changes: 3 additions & 0 deletions tests/cmd/develop.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Options:
--future-incompat-report
Outputs a future incompatibility report at the end of the build (unstable)

--uv
Use `uv` as develop backend instead of `pip`

-h, --help
Print help (see a summary with '-h')

Expand Down
3 changes: 3 additions & 0 deletions tests/common/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn test_develop(
bindings: Option<String>,
unique_name: &str,
conda: bool,
uv: bool,
) -> Result<()> {
maybe_mock_cargo();

Expand All @@ -31,6 +32,7 @@ pub fn test_develop(
"pip",
"install",
"--disable-pip-version-check",
"uv",
"cffi",
])
.output()?;
Expand All @@ -57,6 +59,7 @@ pub fn test_develop(
target_dir: Some(PathBuf::from(format!("test-crates/targets/{unique_name}"))),
..Default::default()
},
uv,
};
develop(develop_options, &venv_dir)?;

Expand Down
27 changes: 27 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn develop_pyo3_pure() {
None,
"develop-pyo3-pure",
false,
false,
));
}

Expand All @@ -30,6 +31,7 @@ fn develop_pyo3_pure_conda() {
None,
"develop-pyo3-pure-conda",
true,
false,
));
}
}
Expand All @@ -41,6 +43,7 @@ fn develop_pyo3_mixed() {
None,
"develop-pyo3-mixed",
false,
false,
));
}

Expand All @@ -51,6 +54,7 @@ fn develop_pyo3_mixed_include_exclude() {
None,
"develop-pyo3-mixed-include-exclude",
false,
false,
));
}

Expand All @@ -61,6 +65,7 @@ fn develop_pyo3_mixed_submodule() {
None,
"develop-pyo3-mixed-submodule",
false,
false,
));
}

Expand All @@ -71,6 +76,7 @@ fn develop_pyo3_mixed_with_path_dep() {
None,
"develop-pyo3-mixed-with-path-dep",
false,
false,
));
}

Expand All @@ -81,6 +87,7 @@ fn develop_pyo3_mixed_implicit() {
None,
"develop-pyo3-mixed-implicit",
false,
false,
));
}

Expand All @@ -91,6 +98,7 @@ fn develop_pyo3_mixed_py_subdir() {
None,
"develop-pyo3-mixed-py-subdir",
false,
false,
));
}

Expand All @@ -101,6 +109,7 @@ fn develop_pyo3_mixed_src_layout() {
None,
"develop-pyo3-mixed-src",
false,
false,
));
}

Expand All @@ -116,6 +125,7 @@ fn develop_cffi_pure() {
None,
"develop-cffi-pure",
false,
false,
));
}

Expand All @@ -131,6 +141,7 @@ fn develop_cffi_mixed() {
None,
"develop-cffi-mixed",
false,
false,
));
}

Expand All @@ -142,6 +153,7 @@ fn develop_uniffi_pure() {
None,
"develop-uniffi-pure",
false,
false,
));
}
}
Expand All @@ -153,6 +165,7 @@ fn develop_uniffi_pure_proc_macro() {
None,
"develop-uniffi-pure-proc-macro",
false,
false,
));
}

Expand All @@ -164,6 +177,7 @@ fn develop_uniffi_mixed() {
None,
"develop-uniffi-mixed",
false,
false,
));
}
}
Expand All @@ -175,6 +189,18 @@ fn develop_hello_world() {
None,
"develop-hello-world",
false,
false,
));
}

#[test]
fn develop_hello_world_uv() {
handle_result(develop::test_develop(
"test-crates/hello-world",
None,
"develop-hello-world-uv",
false,
true,
));
}

Expand All @@ -185,6 +211,7 @@ fn develop_pyo3_ffi_pure() {
None,
"develop-pyo3-ffi-pure",
false,
false,
));
}

Expand Down