Skip to content

Commit

Permalink
fix: more unwraps and cleanup (prefix-dev#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
swarnimarun authored Nov 24, 2023
1 parent 0ffad50 commit 4bd2ccf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 42 deletions.
55 changes: 17 additions & 38 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ fn package_record_from_index_json<T: Read>(
fn package_record_from_tar_bz2(file: &Path) -> Result<PackageRecord, std::io::Error> {
let reader = std::fs::File::open(file)?;
let mut archive = read::stream_tar_bz2(reader);

for entry in archive.entries()? {
let mut entry = entry?;
for entry in archive.entries()?.flatten() {
let mut entry = entry;
let path = entry.path()?;
let path = path.to_string_lossy();
if path == "info/index.json" {
if path.as_os_str().eq("info/index.json") {
return package_record_from_index_json(file, &mut entry);
}
}
Expand All @@ -74,11 +72,10 @@ fn package_record_from_conda(file: &Path) -> Result<PackageRecord, std::io::Erro
let reader = std::fs::File::open(file)?;
let mut archive = seek::stream_conda_info(reader).expect("Could not open conda file");

for entry in archive.entries()? {
let mut entry = entry?;
for entry in archive.entries()?.flatten() {
let mut entry = entry;
let path = entry.path()?;
let path = path.to_string_lossy();
if path == "info/index.json" {
if path.as_os_str().eq("info/index.json") {
return package_record_from_index_json(file, &mut entry);
}
}
Expand Down Expand Up @@ -175,26 +172,17 @@ pub fn index(
})
})
}) {
match t {
ArchiveType::TarBz2 => {
if let Ok(record) = package_record_from_tar_bz2(p) {
repodata
.packages
.insert(p.file_name().unwrap().to_string_lossy().into(), record);
} else {
tracing::info!("Could not read package record from {:?}", p);
}
}
ArchiveType::Conda => {
if let Ok(record) = package_record_from_conda(p) {
repodata
.conda_packages
.insert(p.file_name().unwrap().to_string_lossy().to_string(), record);
} else {
tracing::info!("Could not read package record from {:?}", p);
}
}
let record = match t {
ArchiveType::TarBz2 => package_record_from_tar_bz2(p),
ArchiveType::Conda => package_record_from_conda(p),
};
let (Ok(record), Some(file_name)) = (record, p.file_name()) else {
tracing::info!("Could not read package record from {:?}", p);
continue;
};
repodata
.conda_packages
.insert(file_name.to_string_lossy().to_string(), record);
}
let out_file = output_folder.join(platform).join("repodata.json");
File::create(&out_file)?.write_all(serde_json::to_string_pretty(&repodata)?.as_bytes())?;
Expand All @@ -203,13 +191,4 @@ pub fn index(
Ok(())
}

#[cfg(test)]
mod test {

#[test]
fn test_index() {
// let output_folder = PathBuf::from("/Users/wolfv/Programs/roar/output");
// let target_platform = "osx-64";
// index(&output_folder, Some(target_platform)).unwrap();
}
}
// TODO: write proper unit tests for above functions
4 changes: 2 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ mod tests {
// with build_id (aka timestamp)
let timestamp = &Utc::now();
let p2 = setup_build_dir(dir.path(), "name", false, timestamp).unwrap();
let f2 = p2.file_name().unwrap().to_string_lossy();
let f2 = p2.file_name().unwrap();
let epoch = timestamp.timestamp();
assert!(f2.eq(&format!("rattler-build_name_{epoch}")));
assert!(f2.eq(format!("rattler-build_name_{epoch}").as_str()));
_ = std::fs::remove_dir_all(p2);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ fn extract(
) -> Result<std::process::Output, std::io::Error> {
let output = Command::new("tar")
.arg("-xf")
.arg(String::from(archive.to_string_lossy()))
.arg(archive.as_os_str())
.arg("--preserve-permissions")
.arg("--strip-components=1")
.arg("-C")
.arg(String::from(target_directory.to_string_lossy()))
.arg(target_directory.as_os_str())
.output();

output
Expand Down

0 comments on commit 4bd2ccf

Please sign in to comment.