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 support for .tar.bz2 source distributions #3069

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion crates/distribution-filename/src/source_dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use uv_normalize::{InvalidNameError, PackageName};
pub enum SourceDistExtension {
Zip,
TarGz,
TarBz2,
}

impl FromStr for SourceDistExtension {
Expand All @@ -28,6 +29,7 @@ impl FromStr for SourceDistExtension {
Ok(match s {
"zip" => Self::Zip,
"tar.gz" => Self::TarGz,
"tar.bz2" => Self::TarBz2,
other => return Err(other.to_string()),
})
}
Expand All @@ -38,6 +40,7 @@ impl Display for SourceDistExtension {
match self {
Self::Zip => f.write_str("zip"),
Self::TarGz => f.write_str("tar.gz"),
Self::TarBz2 => f.write_str("tar.bz2"),
}
}
}
Expand All @@ -50,6 +53,9 @@ impl SourceDistExtension {
if let Some(stem) = filename.strip_suffix(".tar.gz") {
return Some((stem, Self::TarGz));
}
if let Some(stem) = filename.strip_suffix(".tar.bz2") {
return Some((stem, Self::TarBz2));
}
None
}
}
Expand Down Expand Up @@ -182,7 +188,7 @@ impl Display for SourceDistFilenameError {
enum SourceDistFilenameErrorKind {
#[error("Name doesn't start with package name {0}")]
Filename(PackageName),
#[error("Source distributions filenames must end with .zip or .tar.gz")]
#[error("Source distributions filenames must end with .zip, .tar.gz, or .tar.bz2")]
Extension,
#[error("Version section is invalid")]
Version(#[from] VersionParseError),
Expand All @@ -207,6 +213,7 @@ mod tests {
"foo-lib-1.2.3.zip",
"foo-lib-1.2.3a3.zip",
"foo-lib-1.2.3.tar.gz",
"foo-lib-1.2.3.tar.bz2",
] {
assert_eq!(
SourceDistFilename::parse(normalized, &PackageName::from_str("foo_lib").unwrap())
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-extract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ workspace = true
[dependencies]
pypi-types = { workspace = true }

async-compression = { workspace = true, features = ["gzip", "zstd"] }
async-compression = { workspace = true, features = ["bzip2", "gzip", "zstd"] }
async_zip = { workspace = true, features = ["tokio"] }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
Expand Down
17 changes: 16 additions & 1 deletion crates/uv-extract/src/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub async fn unzip<R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin>(
Ok(())
}

/// Unzip a `.zip` or `.tar.gz` archive into the target directory, requiring `Seek`.
/// Unzip a `.zip`, `.tar.gz`, or `.tar.bz2` archive into the target directory, requiring `Seek`.
pub async fn archive<R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin>(
reader: R,
source: impl AsRef<Path>,
Expand Down Expand Up @@ -111,6 +111,21 @@ pub async fn archive<R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin>(
return Ok(());
}

// `.tar.bz2`
if source
.as_ref()
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("bz2"))
&& source.as_ref().file_stem().is_some_and(|stem| {
Path::new(stem)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("tar"))
})
{
crate::stream::untar_bz2(reader, target).await?;
return Ok(());
}

// `.tar.zst`
if source
.as_ref()
Expand Down
33 changes: 32 additions & 1 deletion crates/uv-extract/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ pub async fn untar_gz<R: tokio::io::AsyncRead + Unpin>(
Ok(())
}

/// Unzip a `.tar.bz2` archive into the target directory, without requiring `Seek`.
///
/// This is useful for unpacking files as they're being downloaded.
pub async fn untar_bz2<R: tokio::io::AsyncRead + Unpin>(
reader: R,
target: impl AsRef<Path>,
) -> Result<(), Error> {
let reader = tokio::io::BufReader::new(reader);
let decompressed_bytes = async_compression::tokio::bufread::BzDecoder::new(reader);

let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
.set_preserve_mtime(false)
.build();
untar_in(&mut archive, target.as_ref()).await?;
Ok(())
}

/// Unzip a `.tar.zst` archive into the target directory, without requiring `Seek`.
///
/// This is useful for unpacking files as they're being downloaded.
Expand All @@ -181,7 +198,7 @@ pub async fn untar_zst<R: tokio::io::AsyncRead + Unpin>(
Ok(untar_in(&mut archive, target.as_ref()).await?)
}

/// Unzip a `.zip` or `.tar.gz` archive into the target directory, without requiring `Seek`.
/// Unzip a `.zip`, `.tar.gz`, or `.tar.bz2` archive into the target directory, without requiring `Seek`.
pub async fn archive<R: tokio::io::AsyncRead + Unpin>(
reader: R,
source: impl AsRef<Path>,
Expand Down Expand Up @@ -212,6 +229,20 @@ pub async fn archive<R: tokio::io::AsyncRead + Unpin>(
return Ok(());
}

// `.tar.bz2`
if source
.as_ref()
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("bz2"))
&& source.as_ref().file_stem().is_some_and(|stem| {
Path::new(stem)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("tar"))
})
{
untar_bz2(reader, target).await?;
return Ok(());
}
// `.tar.zst`
if source
.as_ref()
Expand Down
35 changes: 35 additions & 0 deletions crates/uv/tests/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,41 @@ fn install_package() {
context.assert_command("import flask").success();
}

/// Install a package with source archive format `.tar.bz2`.
#[test]
fn archive_type_bz2() {
let context = TestContext::new("3.8");

// Install a version of Twisted that uses `.tar.bz2` (and provides no wheel
// for the given version of Python).
uv_snapshot!(context.install()
.arg("Twisted==20.3.0")
.arg("--strict"), @r###"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to use --no-binary twisted, just to be sure that we aren't using the wheel? Also, can you make this a pip sync test instead to avoid the extra work downloading those other packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, working on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed the changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 11 packages in [TIME]
Downloaded 11 packages in [TIME]
Installed 11 packages in [TIME]
+ attrs==23.2.0
+ automat==22.10.0
+ constantly==23.10.4
+ hyperlink==21.0.0
+ idna==3.6
+ incremental==22.10.0
+ pyhamcrest==2.1.0
+ setuptools==69.2.0
+ six==1.16.0
+ twisted==20.3.0
+ zope-interface==6.2
"###
);

context.assert_command("import twisted").success();
}

/// Install a package from a `requirements.txt` into a virtual environment.
#[test]
fn install_requirements_txt() -> Result<()> {
Expand Down
Loading