From bf2ee6bc312fe5e3996c4539b4873ca643ad9aac Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 15 Feb 2024 15:01:31 +0100 Subject: [PATCH] Adds support for --no-deps to pip compile (#1311) Mostly throwing this up here as a discussion topic. Having something like this is primarily useful for enabling use cases similar to `rye add` where I want to use this currently. One can accomplish something similar with `unearth` today or by abusing regular `pip install`: ``` $ ~/.rye/self/bin/pip install --no-deps --dry-run flask --report - -q | jq '.install[0].metadata | {name, version}' { "name": "Flask", "version": "3.0.2" } ``` Another option would be to have a `puffin resolve` command or similar that works like `pip compile` without dependencies, takes the requirements as arguments and returns a line for each resolution. That would be a larger change. --- crates/puffin/src/commands/pip_compile.rs | 6 +++-- crates/puffin/src/main.rs | 11 +++++++++ crates/puffin/tests/pip_compile.rs | 27 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/crates/puffin/src/commands/pip_compile.rs b/crates/puffin/src/commands/pip_compile.rs index 70cdced24..972216b00 100644 --- a/crates/puffin/src/commands/pip_compile.rs +++ b/crates/puffin/src/commands/pip_compile.rs @@ -27,8 +27,8 @@ use puffin_installer::{Downloader, NoBinary}; use puffin_interpreter::{Interpreter, PythonVersion}; use puffin_normalize::{ExtraName, PackageName}; use puffin_resolver::{ - DisplayResolutionGraph, InMemoryIndex, Manifest, OptionsBuilder, PreReleaseMode, - ResolutionMode, Resolver, + DependencyMode, DisplayResolutionGraph, InMemoryIndex, Manifest, OptionsBuilder, + PreReleaseMode, ResolutionMode, Resolver, }; use puffin_traits::{InFlight, NoBuild, SetupPyStrategy}; use puffin_warnings::warn_user; @@ -51,6 +51,7 @@ pub(crate) async fn pip_compile( output_file: Option<&Path>, resolution_mode: ResolutionMode, prerelease_mode: PreReleaseMode, + dependency_mode: DependencyMode, upgrade: Upgrade, generate_hashes: bool, include_annotations: bool, @@ -212,6 +213,7 @@ pub(crate) async fn pip_compile( let options = OptionsBuilder::new() .resolution_mode(resolution_mode) .prerelease_mode(prerelease_mode) + .dependency_mode(dependency_mode) .exclude_newer(exclude_newer) .build(); diff --git a/crates/puffin/src/main.rs b/crates/puffin/src/main.rs index 35ecad4d7..74826cdca 100644 --- a/crates/puffin/src/main.rs +++ b/crates/puffin/src/main.rs @@ -191,6 +191,11 @@ struct PipCompileArgs { #[clap(long, conflicts_with = "extra")] all_extras: bool, + /// Ignore package dependencies, instead only add those packages explicitly listed + /// on the command line to the resulting the requirements file. + #[clap(long)] + no_deps: bool, + #[clap(long, value_enum, default_value_t = ResolutionMode::default())] resolution: ResolutionMode, @@ -807,6 +812,11 @@ async fn run() -> Result { }; let upgrade = Upgrade::from_args(args.upgrade, args.upgrade_package); let no_build = NoBuild::from_args(args.only_binary, args.no_build); + let dependency_mode = if args.no_deps { + DependencyMode::Direct + } else { + DependencyMode::Transitive + }; commands::pip_compile( &requirements, &constraints, @@ -815,6 +825,7 @@ async fn run() -> Result { args.output_file.as_deref(), args.resolution, args.prerelease, + dependency_mode, upgrade, args.generate_hashes, !args.no_annotate, diff --git a/crates/puffin/tests/pip_compile.rs b/crates/puffin/tests/pip_compile.rs index eb5427b84..fc0cca862 100644 --- a/crates/puffin/tests/pip_compile.rs +++ b/crates/puffin/tests/pip_compile.rs @@ -480,6 +480,33 @@ fn compile_python_312() -> Result<()> { Ok(()) } +/// Resolve a specific version of Black at Python 3.12 without deps. +#[test] +fn compile_python_312_no_deps() -> Result<()> { + let context = TestContext::new("3.12"); + let requirements_in = context.temp_dir.child("requirements.in"); + requirements_in.write_str("black==23.10.1")?; + + puffin_snapshot!(context.compile() + .arg("requirements.in") + .arg("--no-deps") + .arg("--python-version") + .arg("3.12"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by Puffin v[VERSION] via the following command: + # puffin pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --no-deps --python-version 3.12 + black==23.10.1 + + ----- stderr ----- + Resolved 1 package in [TIME] + "### + ); + + Ok(()) +} + /// Resolve a specific version of Black at Python 3.7. #[test] fn compile_python_37() -> Result<()> {