Skip to content

Commit

Permalink
Avoid erroneous version warning for .dist-info
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 16, 2024
1 parent d3e6765 commit 5c9bf8f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 54 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions crates/uv-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ license.workspace = true

[dependencies]
distribution-filename = { workspace = true }
pep440_rs = { workspace = true }
pypi-types = { workspace = true }
uv-normalize = { workspace = true }

Expand All @@ -21,7 +20,6 @@ futures = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tokio-util = { workspace = true }
tracing = { workspace = true }
zip = { workspace = true }

[lints]
Expand Down
62 changes: 12 additions & 50 deletions crates/uv-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
//! specification](https://packaging.python.org/en/latest/specifications/core-metadata/).

use distribution_filename::WheelFilename;
use pep440_rs::Version;
use pypi_types::Metadata23;
use std::io;
use std::io::{Read, Seek};
use std::path::Path;
use std::str::FromStr;
use thiserror::Error;
use tokio::io::AsyncReadExt;
use tokio_util::compat::{FuturesAsyncReadCompatExt, TokioAsyncReadCompatExt};
use tracing::warn;
use uv_normalize::{DistInfoName, InvalidNameError};
use zip::ZipArchive;

Expand All @@ -32,8 +29,6 @@ pub enum Error {
MissingDistInfoSegments(String),
#[error("The .dist-info directory {0} does not start with the normalized package name: {1}")]
MissingDistInfoPackageName(String, String),
#[error("The .dist-info directory {0} does not start with the normalized version: {1}")]
MissingDistInfoVersion(String, String),
#[error("The .dist-info directory name contains invalid characters")]
InvalidName(#[from] InvalidNameError),
#[error("The metadata at {0} is invalid")]
Expand Down Expand Up @@ -85,28 +80,17 @@ pub fn find_archive_dist_info<'a, T: Copy>(
};

// Like `pip`, validate that the `.dist-info` directory is prefixed with the canonical
// package name, but only warn if the version is not the normalized version.
// package name.
let normalized_prefix = DistInfoName::new(dist_info_prefix);
let Some(rest) = normalized_prefix
if !normalized_prefix
.as_ref()
.strip_prefix(filename.name.as_str())
else {
.starts_with(filename.name.as_str())
{
return Err(Error::MissingDistInfoPackageName(
dist_info_prefix.to_string(),
filename.name.to_string(),
));
};
if !rest.strip_prefix('-').is_some_and(|version| {
Version::from_str(version).is_ok_and(|version| version == filename.version)
}) {
warn!(
"{}",
Error::MissingDistInfoVersion(
dist_info_prefix.to_string(),
filename.version.to_string(),
)
);
}

Ok((payload, dist_info_prefix))
}
Expand All @@ -125,28 +109,17 @@ pub fn is_metadata_entry(path: &str, filename: &WheelFilename) -> Result<bool, E
};

// Like `pip`, validate that the `.dist-info` directory is prefixed with the canonical
// package name, but only warn if the version is not the normalized version.
// package name.
let normalized_prefix = DistInfoName::new(dist_info_prefix);
let Some(rest) = normalized_prefix
if !normalized_prefix
.as_ref()
.strip_prefix(filename.name.as_str())
else {
.starts_with(filename.name.as_str())
{
return Err(Error::MissingDistInfoPackageName(
dist_info_prefix.to_string(),
filename.name.to_string(),
));
};
if !rest.strip_prefix('-').is_some_and(|version| {
Version::from_str(version).is_ok_and(|version| version == filename.version)
}) {
warn!(
"{}",
Error::MissingDistInfoVersion(
dist_info_prefix.to_string(),
filename.version.to_string(),
)
);
}

Ok(true)
}
Expand Down Expand Up @@ -200,28 +173,17 @@ pub fn find_flat_dist_info(
};

// Like `pip`, validate that the `.dist-info` directory is prefixed with the canonical
// package name, but only warn if the version is not the normalized version.
// package name.
let normalized_prefix = DistInfoName::new(&dist_info_prefix);
let Some(rest) = normalized_prefix
if !normalized_prefix
.as_ref()
.strip_prefix(filename.name.as_str())
else {
.starts_with(filename.name.as_str())
{
return Err(Error::MissingDistInfoPackageName(
dist_info_prefix.to_string(),
filename.name.to_string(),
));
};
if !rest.strip_prefix('-').is_some_and(|version| {
Version::from_str(version).is_ok_and(|version| version == filename.version)
}) {
warn!(
"{}",
Error::MissingDistInfoVersion(
dist_info_prefix.to_string(),
filename.version.to_string(),
)
);
}

Ok(dist_info_prefix)
}
Expand Down

0 comments on commit 5c9bf8f

Please sign in to comment.