Skip to content

Commit 53abb81

Browse files
authored
Merge pull request #9 from matt-phylum/exts
Add missing sdist extensions
2 parents bf1d4e8 + dd5e39e commit 53abb81

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

src/distribution.rs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use std::str::FromStr;
77
use bzip2::read::BzDecoder;
88
use flate2::read::GzDecoder;
99
#[cfg(feature = "xz")]
10-
use xz::read::XzDecoder;
10+
use xz::bufread::XzDecoder;
11+
#[cfg(feature = "xz")]
12+
use xz::stream::Stream as XzStream;
1113
use zip::ZipArchive;
1214

1315
use crate::{Error, Metadata};
@@ -59,13 +61,13 @@ impl FromStr for SDistType {
5961
fn from_str(s: &str) -> Result<Self, Self::Err> {
6062
let dist_type = match s {
6163
"zip" => SDistType::Zip,
62-
"gz" => SDistType::GzTar,
64+
"gz" | "tgz" => SDistType::GzTar,
6365
#[cfg(feature = "deprecated-formats")]
6466
"tar" => SDistType::Tar,
6567
#[cfg(feature = "bzip2")]
66-
"bz2" => SDistType::BzTar,
68+
"bz2" | "tbz" => SDistType::BzTar,
6769
#[cfg(feature = "xz")]
68-
"xz" => SDistType::XzTar,
70+
"lz" | "lzma" | "tlz" | "txz" | "xz" => SDistType::XzTar,
6971
_ => return Err(Error::UnknownDistributionType),
7072
};
7173
Ok(dist_type)
@@ -76,25 +78,20 @@ impl Distribution {
7678
/// Open and parse a distribution from `path`
7779
pub fn new(path: impl AsRef<Path>) -> Result<Self, Error> {
7880
let path = path.as_ref();
79-
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
80-
let dist_type = match ext {
81-
"zip" | "gz" => DistributionType::SDist,
82-
"egg" => DistributionType::Egg,
83-
"whl" => DistributionType::Wheel,
84-
#[cfg(feature = "deprecated-formats")]
85-
"tar" => DistributionType::SDist,
86-
#[cfg(feature = "bzip2")]
87-
"bz2" => DistributionType::SDist,
88-
#[cfg(feature = "xz")]
89-
"xz" => DistributionType::SDist,
90-
_ => return Err(Error::UnknownDistributionType),
91-
};
92-
let (metadata, python_version) = match dist_type {
93-
DistributionType::SDist => {
94-
let sdist_type: SDistType = ext.parse()?;
95-
(Self::parse_sdist(path, sdist_type)?, "source".to_string())
96-
}
97-
DistributionType::Egg => {
81+
let ext = path
82+
.extension()
83+
.and_then(|ext| ext.to_str())
84+
.ok_or(Error::UnknownDistributionType)?;
85+
86+
Ok(if let Ok(sdist_type) = ext.parse() {
87+
Self {
88+
dist_type: DistributionType::SDist,
89+
metadata: Self::parse_sdist(path, sdist_type)?,
90+
python_version: "source".to_string(),
91+
}
92+
} else {
93+
match ext {
94+
"egg" => {
9895
let parts: Vec<&str> = path
9996
.file_stem()
10097
.unwrap()
@@ -106,9 +103,13 @@ impl Distribution {
106103
[_name, _version, py_ver] => py_ver,
107104
_ => "any",
108105
};
109-
(Self::parse_egg(path)?, python_version.to_string())
106+
Self {
107+
dist_type: DistributionType::Egg,
108+
metadata: Self::parse_egg(path)?,
109+
python_version: python_version.to_string(),
110+
}
110111
}
111-
DistributionType::Wheel => {
112+
"whl" => {
112113
let parts: Vec<&str> = path
113114
.file_stem()
114115
.unwrap()
@@ -120,16 +121,15 @@ impl Distribution {
120121
[_name, _version, py_ver, _abi_tag, _plat_tag] => py_ver,
121122
_ => "any",
122123
};
123-
(Self::parse_wheel(path)?, python_version.to_string())
124+
Self {
125+
dist_type: DistributionType::Wheel,
126+
metadata: Self::parse_wheel(path)?,
127+
python_version: python_version.to_string(),
128+
}
124129
}
125-
};
126-
return Ok(Self {
127-
dist_type,
128-
metadata,
129-
python_version,
130-
});
131-
}
132-
Err(Error::UnknownDistributionType)
130+
_ => return Err(Error::UnknownDistributionType),
131+
}
132+
})
133133
}
134134

135135
/// Returns distribution type
@@ -162,9 +162,10 @@ impl Distribution {
162162
Self::parse_tar(BzDecoder::new(BufReader::new(fs_err::File::open(path)?)))
163163
}
164164
#[cfg(feature = "xz")]
165-
SDistType::XzTar => {
166-
Self::parse_tar(XzDecoder::new(BufReader::new(fs_err::File::open(path)?)))
167-
}
165+
SDistType::XzTar => Self::parse_tar(XzDecoder::new_stream(
166+
BufReader::new(fs_err::File::open(path)?),
167+
XzStream::new_auto_decoder(u64::max_value(), 0).unwrap(),
168+
)),
168169
}
169170
}
170171

tests/fixtures/build-0.4.0.tar.lz

11.6 KB
Binary file not shown.

tests/test_distribution.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ fn test_parse_sdist_tar_bz2() {
8484
assert_eq!(dist.python_version(), "source");
8585
}
8686

87+
#[cfg(feature = "xz")]
88+
#[test]
89+
fn test_parse_sdist_tar_lz() {
90+
let dist = Distribution::new("tests/fixtures/build-0.4.0.tar.lz").unwrap();
91+
assert_eq!(dist.r#type(), DistributionType::SDist);
92+
let metadata = dist.metadata();
93+
assert_eq!(metadata.metadata_version, "2.1");
94+
assert_eq!(metadata.name, "build");
95+
assert!(metadata.home_page.is_none());
96+
assert!(metadata.download_url.is_none());
97+
}
98+
8799
#[cfg(feature = "xz")]
88100
#[test]
89101
fn test_parse_sdist_tar_xz() {

0 commit comments

Comments
 (0)