Skip to content

Commit 6832672

Browse files
authored
Add Distribution::python_version() method
1 parent 643bf04 commit 6832672

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/distribution.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum SDistType {
3131
pub struct Distribution {
3232
dist_type: DistributionType,
3333
metadata: Metadata,
34+
python_version: String,
3435
}
3536

3637
impl FromStr for SDistType {
@@ -58,17 +59,44 @@ impl Distribution {
5859
"whl" => DistributionType::Wheel,
5960
_ => return Err(Error::UnknownDistributionType),
6061
};
61-
let metadata = match dist_type {
62+
let (metadata, python_version) = match dist_type {
6263
DistributionType::SDist => {
6364
let sdist_type: SDistType = ext.parse()?;
64-
Self::parse_sdist(path, sdist_type)
65+
(Self::parse_sdist(path, sdist_type)?, "source".to_string())
6566
}
66-
DistributionType::Egg => Self::parse_egg(path),
67-
DistributionType::Wheel => Self::parse_wheel(path),
68-
}?;
67+
DistributionType::Egg => {
68+
let parts: Vec<&str> = path
69+
.file_stem()
70+
.unwrap()
71+
.to_str()
72+
.unwrap()
73+
.split('-')
74+
.collect();
75+
let python_version = match parts.as_slice() {
76+
[_name, _version, py_ver] => py_ver,
77+
_ => "any",
78+
};
79+
(Self::parse_egg(path)?, python_version.to_string())
80+
}
81+
DistributionType::Wheel => {
82+
let parts: Vec<&str> = path
83+
.file_stem()
84+
.unwrap()
85+
.to_str()
86+
.unwrap()
87+
.split('-')
88+
.collect();
89+
let python_version = match parts.as_slice() {
90+
[_name, _version, py_ver, _abi_tag, _plat_tag] => py_ver,
91+
_ => "any",
92+
};
93+
(Self::parse_wheel(path)?, python_version.to_string())
94+
}
95+
};
6996
return Ok(Self {
7097
dist_type,
7198
metadata,
99+
python_version,
72100
});
73101
}
74102
Err(Error::UnknownDistributionType)
@@ -84,6 +112,13 @@ impl Distribution {
84112
&self.metadata
85113
}
86114

115+
/// Returns the supported Python version tag
116+
///
117+
/// For source distributions the version tag is always `source`
118+
pub fn python_version(&self) -> &str {
119+
&self.python_version
120+
}
121+
87122
fn parse_sdist(path: &Path, sdist_type: SDistType) -> Result<Metadata, Error> {
88123
match sdist_type {
89124
SDistType::Zip => Self::parse_zip(path, "PKG-INFO"),

tests/test_distribution.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn test_parse_wheel() {
99
assert_eq!(metadata.name, "build");
1010
assert!(metadata.home_page.is_none());
1111
assert!(metadata.download_url.is_none());
12+
assert_eq!(dist.python_version(), "py2.py3");
1213
}
1314

1415
#[test]
@@ -20,6 +21,7 @@ fn test_parse_egg() {
2021
assert_eq!(metadata.name, "build");
2122
assert!(metadata.home_page.is_none());
2223
assert!(metadata.download_url.is_none());
24+
assert_eq!(dist.python_version(), "py3.9");
2325
}
2426

2527
#[test]
@@ -31,6 +33,7 @@ fn test_parse_sdist_zip() {
3133
assert_eq!(metadata.name, "build");
3234
assert!(metadata.home_page.is_none());
3335
assert!(metadata.download_url.is_none());
36+
assert_eq!(dist.python_version(), "source");
3437
}
3538

3639
#[test]
@@ -42,6 +45,7 @@ fn test_parse_sdist_tar_gz() {
4245
assert_eq!(metadata.name, "build");
4346
assert!(metadata.home_page.is_none());
4447
assert!(metadata.download_url.is_none());
48+
assert_eq!(dist.python_version(), "source");
4549
}
4650

4751
#[test]
@@ -53,4 +57,5 @@ fn test_parse_sdist_tar_bz2() {
5357
assert_eq!(metadata.name, "build");
5458
assert!(metadata.home_page.is_none());
5559
assert!(metadata.download_url.is_none());
60+
assert_eq!(dist.python_version(), "source");
5661
}

0 commit comments

Comments
 (0)