-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
.egg-info
filename parsing spec compliant (#4533)
## Summary It turns out that `.egg-info` files and directories can _both_ have up to four segments in the filename: https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#filename-embedded-metadata. This PR upgrades the parsing and now uses the same parsing for files and directories. Closes #4532.
- Loading branch information
1 parent
41f051d
commit ca92b55
Showing
4 changed files
with
228 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use std::str::FromStr; | ||
|
||
use thiserror::Error; | ||
|
||
use pep440_rs::{Version, VersionParseError}; | ||
use uv_normalize::{InvalidNameError, PackageName}; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum EggInfoFilenameError { | ||
#[error("The filename \"{0}\" does not end in `.egg-info`")] | ||
InvalidExtension(String), | ||
#[error("The `.egg-info` filename \"{0}\" is missing a package name")] | ||
MissingPackageName(String), | ||
#[error("The `.egg-info` filename \"{0}\" is missing a version")] | ||
MissingVersion(String), | ||
#[error("The `.egg-info` filename \"{0}\" has an invalid package name")] | ||
InvalidPackageName(String, InvalidNameError), | ||
#[error("The `.egg-info` filename \"{0}\" has an invalid version: {1}")] | ||
InvalidVersion(String, VersionParseError), | ||
} | ||
|
||
/// A filename parsed from an `.egg-info` file or directory (e.g., `zstandard-0.22.0-py3.12.egg-info`). | ||
/// | ||
/// An `.egg-info` filename can contain up to four components, as in: | ||
/// | ||
/// ```text | ||
/// name ["-" version ["-py" pyver ["-" required_platform]]] "." ext | ||
/// ``` | ||
/// | ||
/// See: <https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#filename-embedded-metadata> | ||
#[derive(Debug, Clone)] | ||
pub struct EggInfoFilename { | ||
pub name: PackageName, | ||
pub version: Version, | ||
} | ||
|
||
impl EggInfoFilename { | ||
/// Parse an `.egg-info` filename, requiring at least a name and version. | ||
pub fn parse(stem: &str) -> Result<Self, EggInfoFilenameError> { | ||
// pip uses the following regex: | ||
// ```python | ||
// EGG_NAME = re.compile( | ||
// r""" | ||
// (?P<name>[^-]+) ( | ||
// -(?P<ver>[^-]+) ( | ||
// -py(?P<pyver>[^-]+) ( | ||
// -(?P<plat>.+) | ||
// )? | ||
// )? | ||
// )? | ||
// """, | ||
// re.VERBOSE | re.IGNORECASE, | ||
// ).match | ||
// ``` | ||
let mut parts = stem.split('-'); | ||
let name = parts | ||
.next() | ||
.ok_or_else(|| EggInfoFilenameError::MissingPackageName(format!("{stem}.egg-info")))?; | ||
let version = parts | ||
.next() | ||
.ok_or_else(|| EggInfoFilenameError::MissingVersion(format!("{stem}.egg-info")))?; | ||
let name = PackageName::from_str(name) | ||
.map_err(|e| EggInfoFilenameError::InvalidPackageName(format!("{stem}.egg-info"), e))?; | ||
let version = Version::from_str(version) | ||
.map_err(|e| EggInfoFilenameError::InvalidVersion(format!("{stem}.egg-info"), e))?; | ||
Ok(Self { name, version }) | ||
} | ||
} | ||
|
||
impl FromStr for EggInfoFilename { | ||
type Err = EggInfoFilenameError; | ||
|
||
fn from_str(filename: &str) -> Result<Self, Self::Err> { | ||
let stem = filename | ||
.strip_suffix(".egg-info") | ||
.ok_or_else(|| EggInfoFilenameError::InvalidExtension(filename.to_string()))?; | ||
Self::parse(stem) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn egg_info_filename() { | ||
let filename = "zstandard-0.22.0-py3.12-darwin.egg-info"; | ||
let parsed = EggInfoFilename::from_str(filename).unwrap(); | ||
assert_eq!(parsed.name.as_ref(), "zstandard"); | ||
assert_eq!(parsed.version.to_string(), "0.22.0"); | ||
|
||
let filename = "zstandard-0.22.0-py3.12.egg-info"; | ||
let parsed = EggInfoFilename::from_str(filename).unwrap(); | ||
assert_eq!(parsed.name.as_ref(), "zstandard"); | ||
assert_eq!(parsed.version.to_string(), "0.22.0"); | ||
|
||
let filename = "zstandard-0.22.0.egg-info"; | ||
let parsed = EggInfoFilename::from_str(filename).unwrap(); | ||
assert_eq!(parsed.name.as_ref(), "zstandard"); | ||
assert_eq!(parsed.version.to_string(), "0.22.0"); | ||
} | ||
|
||
#[test] | ||
fn egg_info_filename_missing_version() { | ||
let filename = "zstandard.egg-info"; | ||
let err = EggInfoFilename::from_str(filename).unwrap_err(); | ||
assert_eq!( | ||
err.to_string(), | ||
"The `.egg-info` filename \"zstandard.egg-info\" is missing a version" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters