Skip to content

Commit

Permalink
Implement pip list —verbose
Browse files Browse the repository at this point in the history
Handle the `-v/—verbose` flag for `pip list`.

Fixes astral-sh#9838
  • Loading branch information
julienp committed Dec 12, 2024
1 parent c0f8e20 commit e99bc80
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 15 deletions.
79 changes: 64 additions & 15 deletions crates/uv/src/commands/pip/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,48 @@ pub(crate) async fn pip_list(
results
};

let verbose = true; // TODO: this should be a flag

match format {
ListFormat::Json => {
let rows = results
.iter()
.copied()
.map(|dist| Entry {
name: dist.name().clone(),
version: dist.version().clone(),
latest_version: latest
.get(dist.name())
.and_then(|filename| filename.as_ref())
.map(DistFilename::version)
.cloned(),
latest_filetype: latest
.get(dist.name())
.and_then(|filename| filename.as_ref())
.map(FileType::from),
editable_project_location: dist
.as_editable()
.map(|url| url.to_file_path().unwrap().simplified_display().to_string()),
.map(|dist| {
let location = if verbose && dist.path().parent().is_some() {
Some(dist.path().parent().unwrap().simplified_display().to_string())
} else {
None
};
let installer = if verbose {
match dist.installer() {
Ok(installer) => installer,
Err(err) => {
println!("Error: {}", err);
None
}
}
} else {
None
};
Entry {
name: dist.name().clone(),
version: dist.version().clone(),
location,
installer,
latest_version: latest
.get(dist.name())
.and_then(|filename| filename.as_ref())
.map(DistFilename::version)
.cloned(),
latest_filetype: latest
.get(dist.name())
.and_then(|filename| filename.as_ref())
.map(FileType::from),
editable_project_location: dist
.as_editable()
.map(|url| url.to_file_path().unwrap().simplified_display().to_string()),
}
})
.collect_vec();
let output = serde_json::to_string(&rows)?;
Expand Down Expand Up @@ -246,6 +268,29 @@ pub(crate) async fn pip_list(
});
}

if true {
columns.push(Column {
header: String::from("Location"),
rows: results
.iter()
.map(|dist| {
if dist.path().parent().is_some() {
dist.path().parent().unwrap().simplified_display().to_string()
} else {
String::new()
}
})
.collect_vec(),
});
columns.push(Column {
header: String::from("Installer"),
rows: results
.iter()
.map(|_| "uv".to_string())
.collect_vec(),
});
}

for elems in MultiZip(columns.iter().map(Column::fmt).collect_vec()) {
println!("{}", elems.join(" ").trim_end());
}
Expand Down Expand Up @@ -326,6 +371,10 @@ struct Entry {
latest_filetype: Option<FileType>,
#[serde(skip_serializing_if = "Option::is_none")]
editable_project_location: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
location: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
installer: Option<String>,
}

/// A column in a table.
Expand Down
39 changes: 39 additions & 0 deletions crates/uv/tests/it/pip_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,3 +777,42 @@ fn list_ignores_quiet_flag_format_freeze() {
"###
);
}

#[test]
fn list_verbose() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("MarkupSafe==2.1.3")?;

uv_snapshot!(context.pip_install()
.arg("-r")
.arg("requirements.txt")
.arg("--strict"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 1 package in [TIME]
Prepared 1 package in [TIME]
Installed 1 package in [TIME]
+ markupsafe==2.1.3
"###
);

context.assert_command("import markupsafe").success();

uv_snapshot!(context.filters(), context.pip_list()
.arg("--format=json").arg("--verbose"), @r###"
success: true
exit_code: 0
----- stdout -----
[{"name":"markupsafe","version":"2.1.3","location":"[SITE_PACKAGES]","installer":"uv"}]
----- stderr -----
"###
);

Ok(())
}

0 comments on commit e99bc80

Please sign in to comment.