Extract a single-index fetch in flat index client (#12394)

This commit is contained in:
Charlie Marsh 2025-03-23 06:07:19 -07:00 committed by GitHub
parent 2250ddedbf
commit 9af989e30c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 30 additions and 26 deletions

View file

@ -107,26 +107,13 @@ impl<'a> FlatIndexClient<'a> {
}
/// Read the directories and flat remote indexes from `--find-links`.
#[allow(clippy::result_large_err)]
pub async fn fetch(
pub async fn fetch_all(
&self,
indexes: impl Iterator<Item = &IndexUrl>,
) -> Result<FlatIndexEntries, FlatIndexError> {
let mut fetches = futures::stream::iter(indexes)
.map(|index| async move {
let entries = match index {
IndexUrl::Path(url) => {
let path = url
.to_file_path()
.map_err(|()| FlatIndexError::NonFileUrl(url.to_url()))?;
Self::read_from_directory(&path, index)
.map_err(|err| FlatIndexError::FindLinksDirectory(path.clone(), err))?
}
IndexUrl::Pypi(url) | IndexUrl::Url(url) => self
.read_from_url(url, index)
.await
.map_err(|err| FlatIndexError::FindLinksUrl(url.to_url(), err))?,
};
let entries = self.fetch_index(index).await?;
if entries.is_empty() {
warn!("No packages found in `--find-links` entry: {}", index);
} else {
@ -151,6 +138,23 @@ impl<'a> FlatIndexClient<'a> {
Ok(results)
}
/// Fetch a flat remote index from a `--find-links` URL.
pub async fn fetch_index(&self, index: &IndexUrl) -> Result<FlatIndexEntries, FlatIndexError> {
match index {
IndexUrl::Path(url) => {
let path = url
.to_file_path()
.map_err(|()| FlatIndexError::NonFileUrl(url.to_url()))?;
Self::read_from_directory(&path, index)
.map_err(|err| FlatIndexError::FindLinksDirectory(path.clone(), err))
}
IndexUrl::Pypi(url) | IndexUrl::Url(url) => self
.read_from_url(url, index)
.await
.map_err(|err| FlatIndexError::FindLinksUrl(url.to_url(), err)),
}
}
/// Read a flat remote index from a `--find-links` URL.
async fn read_from_url(
&self,

View file

@ -552,7 +552,7 @@ async fn build_package(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, None, &hasher, build_options)
};

View file

@ -375,7 +375,7 @@ pub(crate) async fn pip_compile(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), &cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, tags.as_deref(), &hasher, &build_options)
};

View file

@ -368,7 +368,7 @@ pub(crate) async fn pip_install(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), &cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, Some(&tags), &hasher, &build_options)
};

View file

@ -296,7 +296,7 @@ pub(crate) async fn pip_sync(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), &cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, Some(&tags), &hasher, &build_options)
};

View file

@ -348,7 +348,7 @@ pub(crate) async fn add(
let client =
FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(
.fetch_all(
settings
.resolver
.index_locations

View file

@ -636,7 +636,7 @@ async fn do_lock(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, None, &hasher, build_options)
};

View file

@ -1754,7 +1754,7 @@ pub(crate) async fn resolve_environment(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, Some(tags), &hasher, build_options)
};
@ -1897,7 +1897,7 @@ pub(crate) async fn sync_environment(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, Some(tags), &hasher, build_options)
};
@ -2124,7 +2124,7 @@ pub(crate) async fn update_environment(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, Some(tags), &hasher, build_options)
};

View file

@ -656,7 +656,7 @@ pub(super) async fn do_sync(
let flat_index = {
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await?;
FlatIndex::from_entries(entries, Some(tags), &hasher, build_options)
};

View file

@ -308,7 +308,7 @@ async fn venv_impl(
let tags = interpreter.tags().map_err(VenvError::Tags)?;
let client = FlatIndexClient::new(client.cached_client(), client.connectivity(), cache);
let entries = client
.fetch(index_locations.flat_indexes().map(Index::url))
.fetch_all(index_locations.flat_indexes().map(Index::url))
.await
.map_err(VenvError::FlatIndex)?;
FlatIndex::from_entries(