Skip to content

Commit 0a8223c

Browse files
committed
Implement egg metadata parsing
1 parent de2cb07 commit 0a8223c

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/distribution.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{Error, Metadata};
88
#[derive(Debug, Clone, Copy, PartialEq)]
99
pub enum DistributionType {
1010
SDist,
11-
BDist,
11+
Egg,
1212
Wheel,
1313
}
1414

@@ -31,7 +31,7 @@ impl Distribution {
3131
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
3232
let dist_type = match ext {
3333
"zip" | "gz" => DistributionType::SDist,
34-
"egg" => DistributionType::BDist,
34+
"egg" => DistributionType::Egg,
3535
"whl" => DistributionType::Wheel,
3636
_ => return Err(Error::UnknownDistributionType),
3737
};
@@ -44,7 +44,7 @@ impl Distribution {
4444
};
4545
Self::parse_sdist(path, sdist_type)
4646
}
47-
DistributionType::BDist => Self::parse_bdist(path),
47+
DistributionType::Egg => Self::parse_egg(path),
4848
DistributionType::Wheel => Self::parse_wheel(path),
4949
}?;
5050
return Ok(Self {
@@ -69,16 +69,20 @@ impl Distribution {
6969
todo!()
7070
}
7171

72-
fn parse_bdist(path: &Path) -> Result<Metadata, Error> {
73-
todo!()
72+
fn parse_egg(path: &Path) -> Result<Metadata, Error> {
73+
Self::parse_zip(path, "EGG-INFO/PKG-INFO")
7474
}
7575

7676
fn parse_wheel(path: &Path) -> Result<Metadata, Error> {
77+
Self::parse_zip(path, ".dist-info/METADATA")
78+
}
79+
80+
fn parse_zip(path: &Path, metadata_file_suffix: &str) -> Result<Metadata, Error> {
7781
let reader = BufReader::new(fs_err::File::open(path)?);
7882
let mut archive = ZipArchive::new(reader)?;
7983
let metadata_files: Vec<_> = archive
8084
.file_names()
81-
.filter(|name| name.ends_with(".dist-info/METADATA"))
85+
.filter(|name| name.ends_with(metadata_file_suffix))
8286
.map(ToString::to_string)
8387
.collect();
8488
match metadata_files.as_slice() {
26.8 KB
Binary file not shown.

tests/test_distribution.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,14 @@ fn test_parse_wheel() {
1010
assert!(metadata.home_page.is_none());
1111
assert!(metadata.download_url.is_none());
1212
}
13+
14+
#[test]
15+
fn test_parse_egg() {
16+
let dist = Distribution::new("tests/fixtures/build-0.4.0-py3.9.egg").unwrap();
17+
assert_eq!(dist.r#type(), DistributionType::Egg);
18+
let metadata = dist.metadata();
19+
assert_eq!(metadata.metadata_version, "2.1");
20+
assert_eq!(metadata.name, "build");
21+
assert!(metadata.home_page.is_none());
22+
assert!(metadata.download_url.is_none());
23+
}

0 commit comments

Comments
 (0)