From dab70a661ad73ebd7e382aaccbe8d60eaa81670b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 6 Oct 2023 15:42:58 -0400 Subject: [PATCH] Change `install` to `sync` (with sync semantics) (#24) For better separate at this stage (and following `pip-tools`), it's now `puffin sync`, and it assumes `--no-deps`. --- README.md | 2 +- crates/puffin-cli/src/commands/compile.rs | 9 ++- crates/puffin-cli/src/commands/mod.rs | 4 +- .../src/commands/{install.rs => sync.rs} | 19 ++++-- crates/puffin-cli/src/main.rs | 14 ++--- crates/puffin-resolver/Cargo.toml | 1 + crates/puffin-resolver/src/lib.rs | 60 +++++++++++-------- 7 files changed, 70 insertions(+), 39 deletions(-) rename crates/puffin-cli/src/commands/{install.rs => sync.rs} (76%) diff --git a/README.md b/README.md index ca1ce764a..209e19e1e 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ cargo run -p puffin-cli -- compile requirements.in To install from a resolved `requirements.txt` file: ```shell -cargo run -p puffin-cli -- install requirements.txt +cargo run -p puffin-cli -- sync requirements.txt ``` ## Benchmarks diff --git a/crates/puffin-cli/src/commands/compile.rs b/crates/puffin-cli/src/commands/compile.rs index 0bcd3cc30..4383e0bb3 100644 --- a/crates/puffin-cli/src/commands/compile.rs +++ b/crates/puffin-cli/src/commands/compile.rs @@ -43,7 +43,14 @@ pub(crate) async fn compile(src: &Path, cache: Option<&Path>) -> Result) -> Result { +/// Install a set of locked requirements into the current Python environment. +pub(crate) async fn sync(src: &Path, cache: Option<&Path>) -> Result { // Read the `requirements.txt` from disk. let requirements_txt = std::fs::read_to_string(src)?; @@ -43,12 +43,23 @@ pub(crate) async fn install(src: &Path, cache: Option<&Path>) -> Result>(); puffin_installer::install(&wheels, &python, &client).await?; + #[allow(clippy::print_stdout)] + { + println!("Installed {} wheels", wheels.len()); + } + Ok(ExitStatus::Success) } diff --git a/crates/puffin-cli/src/main.rs b/crates/puffin-cli/src/main.rs index 73610e17f..0838e06f4 100644 --- a/crates/puffin-cli/src/main.rs +++ b/crates/puffin-cli/src/main.rs @@ -22,8 +22,8 @@ struct Cli { enum Commands { /// Compile a `requirements.in` file to a `requirements.txt` file. Compile(CompileArgs), - /// Install dependencies from a `requirements.txt` file. - Install(InstallArgs), + /// Sync dependencies from a `requirements.txt` file. + Sync(SyncArgs), } #[derive(Args)] @@ -37,7 +37,7 @@ struct CompileArgs { } #[derive(Args)] -struct InstallArgs { +struct SyncArgs { /// Path to the `requirements.txt` file to install. src: PathBuf, @@ -59,16 +59,16 @@ async fn main() -> ExitCode { commands::compile( &args.src, dirs.as_ref() - .map(directories::ProjectDirs::cache_dir) + .map(ProjectDirs::cache_dir) .filter(|_| !args.no_cache), ) .await } - Commands::Install(args) => { - commands::install( + Commands::Sync(args) => { + commands::sync( &args.src, dirs.as_ref() - .map(directories::ProjectDirs::cache_dir) + .map(ProjectDirs::cache_dir) .filter(|_| !args.no_cache), ) .await diff --git a/crates/puffin-resolver/Cargo.toml b/crates/puffin-resolver/Cargo.toml index 23cc45680..281b75094 100644 --- a/crates/puffin-resolver/Cargo.toml +++ b/crates/puffin-resolver/Cargo.toml @@ -19,3 +19,4 @@ futures = "0.3.28" anyhow = "1.0.75" tracing = "0.1.37" pep508_rs = "0.2.3" +bitflags = "2.4.0" diff --git a/crates/puffin-resolver/src/lib.rs b/crates/puffin-resolver/src/lib.rs index 453a75031..219b1fc47 100644 --- a/crates/puffin-resolver/src/lib.rs +++ b/crates/puffin-resolver/src/lib.rs @@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet}; use std::str::FromStr; use anyhow::Result; +use bitflags::bitflags; use futures::future::Either; use futures::{StreamExt, TryFutureExt}; use pep440_rs::Version; @@ -42,12 +43,21 @@ impl PinnedPackage { } } +bitflags! { + #[derive(Debug, Copy, Clone, Default)] + pub struct Flags: u8 { + /// Don't install package dependencies. + const NO_DEPS = 1 << 0; + } +} + /// Resolve a set of requirements into a set of pinned versions. pub async fn resolve( requirements: &Requirements, markers: &MarkerEnvironment, tags: &Tags, client: &PypiClient, + flags: Flags, ) -> Result { // A channel to fetch package metadata (e.g., given `flask`, fetch all versions) and version // metadata (e.g., given `flask==1.0.0`, fetch the metadata for that version). @@ -141,31 +151,33 @@ pub async fn resolve( }, ); - // Enqueue its dependencies. - for dependency in metadata.requires_dist { - if !dependency.evaluate_markers( - markers, - // TODO(charlie): Remove this clone. - requirement.extras.clone().unwrap_or_default(), - ) { - debug!("--> ignoring {dependency} due to environment mismatch"); - continue; + if !flags.intersects(Flags::NO_DEPS) { + // Enqueue its dependencies. + for dependency in metadata.requires_dist { + if !dependency.evaluate_markers( + markers, + // TODO(charlie): Remove this clone. + requirement.extras.clone().unwrap_or_default(), + ) { + debug!("--> ignoring {dependency} due to environment mismatch"); + continue; + } + + let normalized_name = PackageName::normalize(&dependency.name); + + if resolution.contains_key(&normalized_name) { + continue; + } + + if !in_flight.insert(normalized_name) { + continue; + } + + debug!("--> adding transitive dependency: {}", dependency); + + package_sink.unbounded_send(Request::Package(dependency))?; } - - let normalized_name = PackageName::normalize(&dependency.name); - - if resolution.contains_key(&normalized_name) { - continue; - } - - if !in_flight.insert(normalized_name) { - continue; - } - - debug!("--> adding transitive dependency: {}", dependency); - - package_sink.unbounded_send(Request::Package(dependency))?; - } + }; } } }