Skip to content
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
4 changes: 4 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,10 @@ pub struct PipFreezeArgs {
#[arg(long)]
pub exclude_editable: bool,

/// Exclude the specified package(s) from the output.
#[arg(long)]
pub r#exclude: Vec<PackageName>,

/// Validate the Python environment, to detect packages with missing dependencies and other
/// issues.
#[arg(long, overrides_with("no_strict"))]
Expand Down
13 changes: 12 additions & 1 deletion crates/uv/src/commands/pip/freeze.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::fmt::Write;
use std::path::PathBuf;

Expand All @@ -10,6 +11,7 @@ use uv_cache::Cache;
use uv_distribution_types::{Diagnostic, InstalledDistKind, Name};
use uv_fs::Simplified;
use uv_installer::SitePackages;
use uv_normalize::PackageName;
use uv_preview::Preview;
use uv_python::PythonPreference;
use uv_python::{EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, Target};
Expand All @@ -21,6 +23,7 @@ use crate::printer::Printer;
/// Enumerate the installed packages in the current environment.
pub(crate) fn pip_freeze(
exclude_editable: bool,
exclude: &HashSet<PackageName>,
strict: bool,
python: Option<&str>,
system: bool,
Expand Down Expand Up @@ -80,7 +83,15 @@ pub(crate) fn pip_freeze(
site_packages
.iter()
.flat_map(uv_installer::SitePackages::iter)
.filter(|dist| !(exclude_editable && dist.is_editable()))
.filter(|dist| {
if exclude_editable && dist.is_editable() {
return false;
}
if exclude.contains(dist.name()) {
return false;
}
true
})
.sorted_unstable_by(|a, b| a.name().cmp(b.name()).then(a.version().cmp(b.version())))
.map(|dist| match &dist.kind {
InstalledDistKind::Registry(dist) => {
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {

commands::pip_freeze(
args.exclude_editable,
&args.exclude,
args.settings.strict,
args.settings.python.as_deref(),
args.settings.system,
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::env::VarError;
use std::num::NonZeroUsize;
use std::path::PathBuf;
Expand Down Expand Up @@ -3024,6 +3025,7 @@ impl PipUninstallSettings {
#[derive(Debug, Clone)]
pub(crate) struct PipFreezeSettings {
pub(crate) exclude_editable: bool,
pub(crate) exclude: HashSet<PackageName>,
pub(crate) paths: Option<Vec<PathBuf>>,
pub(crate) settings: PipSettings,
}
Expand All @@ -3037,6 +3039,7 @@ impl PipFreezeSettings {
) -> Self {
let PipFreezeArgs {
exclude_editable,
exclude,
strict,
no_strict,
python,
Expand All @@ -3050,6 +3053,7 @@ impl PipFreezeSettings {

Self {
exclude_editable,
exclude: exclude.into_iter().collect(),
paths,
settings: PipSettings::combine(
PipOptions {
Expand Down
38 changes: 38 additions & 0 deletions crates/uv/tests/it/pip_freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,41 @@ fn freeze_prefix() -> Result<()> {

Ok(())
}

#[test]
fn freeze_exclude() {
let context = TestContext::new("3.12");

let prefix = context.temp_dir.child("prefix");

// Install packages to a prefix directory.
context
.pip_install()
.arg("MarkupSafe")
.arg("tomli")
.arg("--prefix")
.arg(prefix.path())
.assert()
.success();

// Run `pip freeze --exclude MarkupSafe`.
uv_snapshot!(context.filters(), context.pip_freeze().arg("--exclude").arg("MarkupSafe").arg("--prefix").arg(prefix.path()), @r###"
success: true
exit_code: 0
----- stdout -----
tomli==2.0.1

----- stderr -----
"###
);

// Run `pip freeze --exclude MarkupSafe --exclude tomli`.
uv_snapshot!(context.filters(), context.pip_freeze().arg("--exclude").arg("MarkupSafe").arg("--exclude").arg("tomli").arg("--prefix").arg(prefix.path()), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
"###
);
}
Loading