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

Replace conda-meta-entries with rattler's PrefixRecord #15

Open
pavelzw opened this issue Nov 12, 2024 · 0 comments
Open

Replace conda-meta-entries with rattler's PrefixRecord #15

pavelzw opened this issue Nov 12, 2024 · 0 comments
Labels

Comments

@pavelzw
Copy link
Member

pavelzw commented Nov 12, 2024

pub struct CondaMetaEntry {
pub name: String,
pub version: String,
pub license: LicenseState,
#[allow(dead_code)]
pub sha256: String,
pub timestamp: u64,
#[allow(dead_code)]
pub platform: String,
pub build: String,
}
impl CondaMetaEntry {
pub fn from_filepath(filepath: &str) -> Result<Self> {
let contents = Self::read_file(filepath)
.with_context(|| format!("Failed to read file: {}", filepath))?;
let v: Value = serde_json::from_str(&contents)
.with_context(|| format!("Failed to parse JSON from file: {}", filepath))?;
let license_str = v["license"].as_str().unwrap_or_default();
let license = match parse_expression(license_str) {
Ok(parsed_license) => LicenseState::Valid(parsed_license),
Err(_) => LicenseState::Invalid(license_str.to_owned()),
};
Ok(CondaMetaEntry {
name: v["name"].as_str().unwrap_or_default().to_owned(),
version: v["version"].as_str().unwrap_or_default().to_owned(),
license,
sha256: v["sha256"].as_str().unwrap_or_default().to_owned(),
timestamp: v["timestamp"].as_u64().unwrap_or_default(),
platform: v["subdir"].as_str().unwrap_or_default().to_owned(),
build: v["build"].as_str().unwrap_or_default().to_owned(),
})
}
fn read_file(filepath: &str) -> Result<String> {
let path = Path::new(filepath);
if !path.exists() {
anyhow::bail!("Error: The file {} does not exist.", filepath);
}
let mut file = File::open(filepath)
.with_context(|| format!("Failed to open the file: {}", filepath))?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.with_context(|| format!("Failed to read the file: {}", filepath))?;
Ok(contents)
}
}
pub struct CondaMetaEntries {
pub entries: Vec<CondaMetaEntry>,
}
impl CondaMetaEntries {
pub fn from_dir(conda_meta_path: &str) -> Result<Self> {
let mut conda_meta_entries: Vec<CondaMetaEntry> = Vec::new();
for entry in fs::read_dir(conda_meta_path)? {
let entry = entry?;
let file_name = entry
.path()
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned();
// Skip file if it is not a .json file
if !file_name.ends_with(".json") {
continue;
}
let conda_meta_entry =
CondaMetaEntry::from_filepath(entry.path().to_str().unwrap_or_else(|| {
panic!("Failed to convert path to string: {:?}", entry.path())
}))?;
conda_meta_entries.push(conda_meta_entry);
}
Ok(CondaMetaEntries {
entries: conda_meta_entries,
})
}
}
can be refactored by just using https://docs.rs/rattler_conda_types/latest/rattler_conda_types/prefix_record/struct.PrefixRecord.html#method.collect_from_prefix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant