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 --skip-install option to maturin develop #1654

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
106 changes: 73 additions & 33 deletions src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,57 @@ use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

/// Install the crate as module in the current virtualenv
#[derive(Debug, clap::Parser)]
pub struct DevelopOptions {
/// Which kind of bindings to use
#[arg(
short = 'b',
long = "bindings",
alias = "binding-crate",
value_parser = ["pyo3", "pyo3-ffi", "rust-cpython", "cffi", "uniffi", "bin"]
)]
pub bindings: Option<String>,
/// Pass --release to cargo
#[arg(short = 'r', long)]
pub release: bool,
/// Strip the library for minimum file size
#[arg(long)]
pub strip: bool,
/// Install extra requires aka. optional dependencies
///
/// Use as `--extras=extra1,extra2`
#[arg(
short = 'E',
long,
value_delimiter = ',',
action = clap::ArgAction::Append
)]
pub extras: Vec<String>,
/// Skip installation, only build the extension module inplace
///
/// Only works with mixed Rust/Python project layout
#[arg(long)]
pub skip_install: bool,
/// `cargo rustc` options
#[command(flatten)]
pub cargo_options: CargoOptions,
}

/// Installs a crate by compiling it and copying the shared library to site-packages.
/// Also adds the dist-info directory to make sure pip and other tools detect the library
///
/// Works only in a virtualenv.
#[allow(clippy::too_many_arguments)]
pub fn develop(
bindings: Option<String>,
cargo_options: CargoOptions,
venv_dir: &Path,
release: bool,
strip: bool,
extras: Vec<String>,
) -> Result<()> {
pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
let DevelopOptions {
bindings,
release,
strip,
extras,
skip_install,
cargo_options,
} = develop_options;
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 @@ -117,41 +155,43 @@ pub fn develop(
}

let wheels = build_context.build_wheels()?;
for (filename, _supported_version) in wheels.iter() {
let command = [
"-m",
"pip",
"--disable-pip-version-check",
"install",
"--no-deps",
"--force-reinstall",
];
let output = Command::new(&python)
.args(command)
.arg(dunce::simplified(filename))
.output()
.context(format!("pip install failed with {python:?}"))?;
if !output.status.success() {
bail!(
if !skip_install {
for (filename, _supported_version) in wheels.iter() {
let command = [
"-m",
"pip",
"--disable-pip-version-check",
"install",
"--no-deps",
"--force-reinstall",
];
let output = Command::new(&python)
.args(command)
.arg(dunce::simplified(filename))
.output()
.context(format!("pip install failed with {python:?}"))?;
if !output.status.success() {
bail!(
"pip install in {} failed running {:?}: {}\n--- Stdout:\n{}\n--- Stderr:\n{}\n---\n",
venv_dir.display(),
&command,
output.status,
String::from_utf8_lossy(&output.stdout).trim(),
String::from_utf8_lossy(&output.stderr).trim(),
);
}
if !output.stderr.is_empty() {
}
if !output.stderr.is_empty() {
eprintln!(
"⚠️ Warning: pip raised a warning running {:?}:\n{}",
&command,
String::from_utf8_lossy(&output.stderr).trim(),
);
}
eprintln!(
"⚠️ Warning: pip raised a warning running {:?}:\n{}",
&command,
String::from_utf8_lossy(&output.stderr).trim(),
"🛠 Installed {}-{}",
build_context.metadata21.name, build_context.metadata21.version
);
}
eprintln!(
"🛠 Installed {}-{}",
build_context.metadata21.name, build_context.metadata21.version
);
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use crate::build_context::{BridgeModel, BuildContext, BuiltWheelMetadata};
pub use crate::build_options::{BuildOptions, CargoOptions};
pub use crate::cargo_toml::CargoToml;
pub use crate::compile::{compile, BuildArtifact};
pub use crate::develop::develop;
pub use crate::develop::{develop, DevelopOptions};
pub use crate::metadata::{Metadata21, WheelMetadata};
pub use crate::module_writer::{
write_dist_info, ModuleWriter, PathWriter, SDistWriter, WheelWriter,
Expand Down
47 changes: 6 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use clap::{Parser, Subcommand};
#[cfg(feature = "scaffolding")]
use maturin::{ci::GenerateCI, init_project, new_project, GenerateProjectOptions};
use maturin::{
develop, write_dist_info, BridgeModel, BuildOptions, CargoOptions, PathWriter, PlatformTag,
PythonInterpreter, Target,
develop, write_dist_info, BridgeModel, BuildOptions, CargoOptions, DevelopOptions, PathWriter,
PlatformTag, PythonInterpreter, Target,
};
#[cfg(feature = "upload")]
use maturin::{upload_ui, PublishOpt};
Expand Down Expand Up @@ -73,36 +73,7 @@ enum Opt {
},
#[command(name = "develop", alias = "dev")]
/// Install the crate as module in the current virtualenv
///
/// Note that this command doesn't create entrypoints
Develop {
/// Which kind of bindings to use
#[arg(
short = 'b',
long = "bindings",
alias = "binding-crate",
value_parser = ["pyo3", "pyo3-ffi", "rust-cpython", "cffi", "uniffi", "bin"]
)]
bindings: Option<String>,
/// Pass --release to cargo
#[arg(short = 'r', long)]
release: bool,
/// Strip the library for minimum file size
#[arg(long)]
strip: bool,
/// Install extra requires aka. optional dependencies
///
/// Use as `--extras=extra1,extra2`
#[arg(
short = 'E',
long,
value_delimiter = ',',
action = clap::ArgAction::Append
)]
extras: Vec<String>,
#[command(flatten)]
cargo_options: CargoOptions,
},
Develop(DevelopOptions),
/// Build only a source distribution (sdist) without compiling.
///
/// Building a source distribution requires a pyproject.toml with a `[build-system]` table.
Expand Down Expand Up @@ -404,16 +375,10 @@ fn run() -> Result<()> {
eprintln!(" - {interpreter}");
}
}
Opt::Develop {
bindings,
release,
strip,
extras,
cargo_options,
} => {
let target = Target::from_target_triple(cargo_options.target.clone())?;
Opt::Develop(develop_options) => {
let target = Target::from_target_triple(develop_options.cargo_options.target.clone())?;
let venv_dir = detect_venv(&target)?;
develop(bindings, cargo_options, &venv_dir, release, strip, extras)?;
develop(develop_options, &venv_dir)?;
}
Opt::SDist { manifest_path, out } => {
let build_options = BuildOptions {
Expand Down
7 changes: 5 additions & 2 deletions tests/cmd/develop.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Install the crate as module in the current virtualenv

Note that this command doesn't create entrypoints

Usage: maturin[EXE] develop [OPTIONS] [ARGS]...

Arguments:
Expand All @@ -25,6 +23,11 @@ Options:

Use as `--extras=extra1,extra2`

--skip-install
Skip installation, only build the extension module inplace

Only works with mixed Rust/Python project layout

-q, --quiet
Do not print cargo log messages

Expand Down
17 changes: 9 additions & 8 deletions tests/common/develop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::{check_installed, create_conda_env, create_virtualenv, maybe_mock_cargo};
use anyhow::Result;
use maturin::{develop, CargoOptions};
use maturin::{develop, CargoOptions, DevelopOptions};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
Expand Down Expand Up @@ -44,19 +44,20 @@ pub fn test_develop(
}

let manifest_file = package.join("Cargo.toml");
develop(
let develop_options = DevelopOptions {
bindings,
CargoOptions {
release: false,
strip: false,
extras: Vec::new(),
skip_install: false,
cargo_options: CargoOptions {
manifest_path: Some(manifest_file),
quiet: true,
target_dir: Some(PathBuf::from(format!("test-crates/targets/{unique_name}"))),
..Default::default()
},
&venv_dir,
false,
cfg!(feature = "faster-tests"),
vec![],
)?;
};
develop(develop_options, &venv_dir)?;

check_installed(package, &python)?;
Ok(())
Expand Down