Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: now determines subdir #145

Merged
merged 6 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 79 additions & 5 deletions crates/rattler_conda_types/src/repo_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use fxhash::{FxHashMap, FxHashSet};

use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none, DisplayFromStr, OneOrMany};
use thiserror::Error;

use rattler_macros::sorted;

use crate::package::IndexJson;
use crate::{Channel, NoArchType, RepoDataRecord, Version};
use crate::{Channel, NoArchType, Platform, RepoDataRecord, Version};

/// [`RepoData`] is an index of package binaries available on in a subdirectory of a Conda channel.
// Note: we cannot use the sorted macro here, because the `packages` and `conda_packages` fields are
Expand Down Expand Up @@ -176,15 +177,75 @@ impl RepoData {
}
}

/// An error that can occur when parsing a platform from a string.
#[derive(Debug, Error, Clone, Eq, PartialEq)]
pub enum ConvertSubdirError {
#[error("platform: {platform}, arch: {arch} is not a known combination")]
NoKnownCombination {
/// The platform string that could not be parsed.
platform: String,
/// The architecture.
arch: String,
},
#[error("platform key is empty in index.json")]
PlatformEmpty,
#[error("arch key is empty in index.json")]
ArchEmpty,
}

/// Determine the subdir based on result taken from the prefix.dev
/// database
/// These were the combinations that have been found in the database.
/// and have been represented in the function.
///
/// # Why can we not use Platform::FromStr?
///
/// We cannot use the Platform FromStr directly because x86 and x86_64
/// are different architecture strings. Also some combinations have been removed,
/// because they have not been found.
fn determine_subdir(
platform: Option<String>,
arch: Option<String>,
) -> Result<String, ConvertSubdirError> {
let platform = platform.ok_or(ConvertSubdirError::PlatformEmpty)?;
let arch = arch.ok_or(ConvertSubdirError::ArchEmpty)?;
let canonical = format!("{platform}-{arch}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of first converting to string I would just match on (platform, arch).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it!

// Convert to Platform first
let plat = match canonical.as_ref() {
"linux-x86" => Platform::Linux32,
"linux-x86_64" => Platform::Linux64,
"linux-aarch64" => Platform::LinuxAarch64,
"linux-armv6l" => Platform::LinuxArmV6l,
"linux-armv7l" => Platform::LinuxArmV7l,
"linux-ppc64le" => Platform::LinuxPpc64le,
"linux-ppc64" => Platform::LinuxPpc64,
"linux-s390x" => Platform::LinuxS390X,
"osx-x86_64" => Platform::Osx64,
"osx-arm64" => Platform::OsxArm64,
"win-32" => Platform::Win32,
"win-64" => Platform::Win64,
"win-arm64" => Platform::WinArm64,
_ => return Err(ConvertSubdirError::NoKnownCombination { platform, arch }),
};
// Convert back to Platform string which should correspond to known subdirs
Ok(plat.to_string())
}

impl PackageRecord {
/// Builds a [`PackageRecord`] from a [`IndexJson`] and optionally a size, sha256 and md5 hash.
pub fn from_index_json(
index: IndexJson,
size: Option<u64>,
sha256: Option<String>,
md5: Option<String>,
) -> Self {
PackageRecord {
) -> Result<PackageRecord, ConvertSubdirError> {
// Determine the subdir if it can't be found
let subdir = match index.subdir {
None => determine_subdir(index.platform.clone(), index.arch.clone())?,
Some(s) => s,
};

Ok(PackageRecord {
arch: index.arch,
build: index.build,
build_number: index.build_number,
Expand All @@ -201,11 +262,11 @@ impl PackageRecord {
platform: index.platform,
sha256,
size,
subdir: index.subdir.unwrap(),
subdir,
timestamp: index.timestamp,
track_features: index.track_features,
version: index.version,
}
})
}
}

Expand All @@ -225,10 +286,23 @@ fn sort_set_alphabetically<S: serde::Serializer>(

#[cfg(test)]
mod test {
use crate::repo_data::determine_subdir;
use fxhash::FxHashSet;

use crate::RepoData;

// isl-0.12.2-1.tar.bz2
// gmp-5.1.2-6.tar.bz2
// Are both package variants in the osx-64 subdir
// Will just test for this case
#[test]
fn test_determine_subdir() {
assert_eq!(
determine_subdir(Some("osx".to_string()), Some("x86_64".to_string())).unwrap(),
"osx-64"
);
}

#[test]
fn test_serialize() {
let repodata = RepoData {
Expand Down
6 changes: 3 additions & 3 deletions crates/rattler_package_streaming/tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn find_all_archives() -> impl Iterator<Item = PathBuf> {
}

fn find_all_package_files(path: &Path) -> Vec<PathBuf> {
WalkDir::new(&path)
WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.map(|e| e.into_path())
Expand All @@ -35,11 +35,11 @@ impl Decoder {
match self {
Decoder::TarBz2 => {
let d = bzip2::read::BzDecoder::new(f);
return tar::Archive::new(Box::new(d));
tar::Archive::new(Box::new(d))
}
Decoder::Zst => {
let d = zstd::stream::read::Decoder::new(f).unwrap();
return tar::Archive::new(Box::new(d));
tar::Archive::new(Box::new(d))
}
}
}
Expand Down