Add PackageName::as_dist_info_name (#305)

From
https://packaging.python.org/en/latest/specifications/recording-installed-packages/#recording-installed-packages

> This directory is named as {name}-{version}.dist-info, with name and
version fields corresponding to Core metadata specifications. Both
fields must be normalized (see Package name normalization and PEP 440
for the definition of normalization for each field respectively), and
replace dash (-) characters with underscore (_) characters, so the
.dist-info directory always has exactly one dash (-) character in its
stem, separating the name and version fields.

Follow up to #278
This commit is contained in:
konsti 2023-11-03 09:16:44 +01:00 committed by GitHub
parent e47d3f1f66
commit e008c43f29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View file

@ -1030,8 +1030,12 @@ pub fn find_dist_info(
filename: &WheelFilename, filename: &WheelFilename,
archive: &mut ZipArchive<impl Read + Seek + Sized>, archive: &mut ZipArchive<impl Read + Seek + Sized>,
) -> Result<String, Error> { ) -> Result<String, Error> {
let dist_info_matcher = let dist_info_matcher = format!(
format!("{}-{}", filename.distribution, filename.version).to_lowercase(); "{}-{}",
filename.distribution.as_dist_info_name(),
filename.version
)
.to_lowercase();
let dist_infos: Vec<_> = archive let dist_infos: Vec<_> = archive
.file_names() .file_names()
.filter_map(|name| name.split_once('/')) .filter_map(|name| name.split_once('/'))

View file

@ -36,6 +36,13 @@ impl PackageName {
normalized.make_ascii_lowercase(); normalized.make_ascii_lowercase();
Self(normalized) Self(normalized)
} }
/// Escape this name with underscores (`_`) instead of dashes (`-`)
///
/// See: <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#recording-installed-packages>
pub fn as_dist_info_name(&self) -> String {
self.0.replace('-', "_")
}
} }
impl AsRef<str> for PackageName { impl AsRef<str> for PackageName {