-
Notifications
You must be signed in to change notification settings - Fork 316
feat: switch to compressed mapping #1335
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
Changes from 14 commits
0b8203e
b19e3f5
1c70612
31789d9
e62a10d
29754a9
602c3b3
234f252
6097be3
562c897
be03a7a
37985e0
d4eb709
98cd76e
d20b86e
408025b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"jupyter-ros": "my-name-from-mapping" | ||
"jupyter-ros": "my-name-from-mapping", | ||
"jupyter-amphion": null | ||
} |
Large diffs are not rendered by default.
nichmor marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,17 @@ use async_once_cell::OnceCell; | |
use crate::pypi_mapping::MappingLocation; | ||
|
||
use super::{ | ||
prefix_pypi_name_mapping::{self}, | ||
build_pypi_purl_from_package_record, is_conda_forge_record, prefix_pypi_name_mapping, | ||
MappingMap, Reporter, | ||
}; | ||
|
||
pub async fn fetch_mapping_from_url( | ||
pub async fn fetch_mapping_from_url<T>( | ||
client: &ClientWithMiddleware, | ||
url: &Url, | ||
) -> miette::Result<HashMap<String, String>> { | ||
) -> miette::Result<T> | ||
where | ||
T: serde::de::DeserializeOwned, | ||
{ | ||
let response = client | ||
.get(url.clone()) | ||
.send() | ||
|
@@ -34,8 +37,7 @@ pub async fn fetch_mapping_from_url( | |
)); | ||
} | ||
|
||
let mapping_by_name: HashMap<String, String> = | ||
response.json().await.into_diagnostic().context(format!( | ||
let mapping_by_name: T = response.json().await.into_diagnostic().context(format!( | ||
"failed to parse pypi name mapping located at {}. Please make sure that it's a valid json", | ||
url | ||
))?; | ||
|
@@ -46,11 +48,11 @@ pub async fn fetch_mapping_from_url( | |
pub async fn fetch_custom_mapping( | ||
client: &ClientWithMiddleware, | ||
mapping_url: &MappingMap, | ||
) -> miette::Result<&'static HashMap<String, HashMap<String, String>>> { | ||
static MAPPING: OnceCell<HashMap<String, HashMap<String, String>>> = OnceCell::new(); | ||
) -> miette::Result<&'static HashMap<String, HashMap<String, Option<String>>>> { | ||
static MAPPING: OnceCell<HashMap<String, HashMap<String, Option<String>>>> = OnceCell::new(); | ||
MAPPING | ||
.get_or_try_init(async { | ||
let mut mapping_url_to_name: HashMap<String, HashMap<String, String>> = | ||
let mut mapping_url_to_name: HashMap<String, HashMap<String, Option<String>>> = | ||
Default::default(); | ||
|
||
for (name, url) in mapping_url.iter() { | ||
|
@@ -83,10 +85,9 @@ pub async fn fetch_custom_mapping( | |
let contents = std::fs::read_to_string(path) | ||
.into_diagnostic() | ||
.context(format!("mapping on {path:?} could not be loaded"))?; | ||
let data: HashMap<String, String> = serde_json::from_str(&contents) | ||
.unwrap_or_else(|_| { | ||
panic!("Failed to parse JSON mapping located at {path:?}") | ||
}); | ||
let data: HashMap<String, Option<String>> = serde_json::from_str(&contents) | ||
.into_diagnostic() | ||
.context(format!("Failed to parse JSON mapping located at {path:?}"))?; | ||
nichmor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mapping_url_to_name.insert(name.to_string(), data); | ||
} | ||
|
@@ -149,7 +150,7 @@ pub async fn amend_pypi_purls( | |
/// a conda-forge package. | ||
fn amend_pypi_purls_for_record( | ||
record: &mut RepoDataRecord, | ||
custom_mapping: &'static HashMap<String, HashMap<String, String>>, | ||
custom_mapping: &'static HashMap<String, HashMap<String, Option<String>>>, | ||
) -> miette::Result<()> { | ||
// If the package already has a pypi name we can stop here. | ||
if record | ||
|
@@ -161,27 +162,41 @@ fn amend_pypi_purls_for_record( | |
return Ok(()); | ||
} | ||
|
||
let mut not_a_pypi = false; | ||
|
||
// If this package is a conda-forge package or user specified a custom channel mapping | ||
// we can try to guess the pypi name from the conda name | ||
if custom_mapping.contains_key(&record.channel) { | ||
if let Some(mapped_channel) = custom_mapping.get(&record.channel) { | ||
if let Some(mapped_name) = | ||
mapped_channel.get(record.package_record.name.as_normalized()) | ||
{ | ||
record.package_record.purls.push( | ||
PackageUrl::new(String::from("pypi"), mapped_name) | ||
.expect("valid pypi package url"), | ||
); | ||
if let Some(mapped_channel) = custom_mapping.get(&record.channel) { | ||
if let Some(mapped_name) = mapped_channel.get(record.package_record.name.as_normalized()) { | ||
if let Some(name) = mapped_name { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It think you can use an if-let chain for this: rust-lang/rust#88642. Not sure, have not used it yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is saying that ``let |
||
let purl = PackageUrl::builder(String::from("pypi"), name.to_string()) | ||
.with_qualifier("source", "project-defined-mapping") | ||
.expect("valid qualifier"); | ||
|
||
record | ||
.package_record | ||
.purls | ||
.push(purl.build().expect("valid pypi package url")); | ||
} else { | ||
not_a_pypi = true; | ||
} | ||
} | ||
} | ||
|
||
if !not_a_pypi && record.package_record.purls.is_empty() && is_conda_forge_record(record) { | ||
// Convert the conda package names to pypi package names. If the conversion fails we | ||
// just assume that its not a valid python package. | ||
if let Some(purl) = build_pypi_purl_from_package_record(&record.package_record) { | ||
record.package_record.purls.push(purl); | ||
} | ||
} | ||
nichmor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(()) | ||
} | ||
|
||
pub fn _amend_only_custom_pypi_purls( | ||
conda_packages: &mut [RepoDataRecord], | ||
custom_mapping: &'static HashMap<String, HashMap<String, String>>, | ||
custom_mapping: &'static HashMap<String, HashMap<String, Option<String>>>, | ||
) -> miette::Result<()> { | ||
for record in conda_packages.iter_mut() { | ||
amend_pypi_purls_for_record(record, custom_mapping)?; | ||
|
Uh oh!
There was an error while loading. Please reload this page.