|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use futures::{stream::FuturesOrdered, TryStreamExt}; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +use uv_distribution::{DistributionDatabase, Reporter}; |
| 7 | +use uv_distribution_types::{BuiltDist, Dist, DistributionMetadata, SourceDist}; |
| 8 | +use uv_pypi_types::Requirement; |
| 9 | +use uv_resolver::{InMemoryIndex, MetadataResponse}; |
| 10 | +use uv_types::{BuildContext, HashStrategy}; |
| 11 | + |
| 12 | +use crate::required_dist; |
| 13 | + |
| 14 | +#[derive(Debug, Error)] |
| 15 | +pub enum ExtrasError { |
| 16 | + #[error("Failed to download: `{0}`")] |
| 17 | + Download(BuiltDist, #[source] uv_distribution::Error), |
| 18 | + #[error("Failed to download and build: `{0}`")] |
| 19 | + DownloadAndBuild(SourceDist, #[source] uv_distribution::Error), |
| 20 | + #[error("Failed to build: `{0}`")] |
| 21 | + Build(SourceDist, #[source] uv_distribution::Error), |
| 22 | + #[error(transparent)] |
| 23 | + UnsupportedUrl(#[from] uv_distribution_types::Error), |
| 24 | +} |
| 25 | + |
| 26 | +/// A resolver to expand the requested extras for a set of requirements to include all defined |
| 27 | +/// extras. |
| 28 | +pub struct ExtrasResolver<'a, Context: BuildContext> { |
| 29 | + /// Whether to check hashes for distributions. |
| 30 | + hasher: &'a HashStrategy, |
| 31 | + /// The in-memory index for resolving dependencies. |
| 32 | + index: &'a InMemoryIndex, |
| 33 | + /// The database for fetching and building distributions. |
| 34 | + database: DistributionDatabase<'a, Context>, |
| 35 | +} |
| 36 | + |
| 37 | +impl<'a, Context: BuildContext> ExtrasResolver<'a, Context> { |
| 38 | + /// Instantiate a new [`ExtrasResolver`] for a given set of requirements. |
| 39 | + pub fn new( |
| 40 | + hasher: &'a HashStrategy, |
| 41 | + index: &'a InMemoryIndex, |
| 42 | + database: DistributionDatabase<'a, Context>, |
| 43 | + ) -> Self { |
| 44 | + Self { |
| 45 | + hasher, |
| 46 | + index, |
| 47 | + database, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// Set the [`Reporter`] to use for this resolver. |
| 52 | + #[must_use] |
| 53 | + pub fn with_reporter(self, reporter: impl Reporter + 'static) -> Self { |
| 54 | + Self { |
| 55 | + database: self.database.with_reporter(reporter), |
| 56 | + ..self |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Expand the set of available extras for a given set of requirements. |
| 61 | + pub async fn resolve( |
| 62 | + self, |
| 63 | + requirements: impl Iterator<Item = Requirement>, |
| 64 | + ) -> Result<Vec<Requirement>, ExtrasError> { |
| 65 | + let Self { |
| 66 | + hasher, |
| 67 | + index, |
| 68 | + database, |
| 69 | + } = self; |
| 70 | + requirements |
| 71 | + .map(|requirement| async { |
| 72 | + Self::resolve_requirement(requirement, hasher, index, &database) |
| 73 | + .await |
| 74 | + .map(Requirement::from) |
| 75 | + }) |
| 76 | + .collect::<FuturesOrdered<_>>() |
| 77 | + .try_collect() |
| 78 | + .await |
| 79 | + } |
| 80 | + |
| 81 | + /// Expand the set of available extras for a given [`Requirement`]. |
| 82 | + async fn resolve_requirement( |
| 83 | + requirement: Requirement, |
| 84 | + hasher: &HashStrategy, |
| 85 | + index: &InMemoryIndex, |
| 86 | + database: &DistributionDatabase<'a, Context>, |
| 87 | + ) -> Result<Requirement, ExtrasError> { |
| 88 | + // Determine whether the requirement represents a local distribution and convert to a |
| 89 | + // buildable distribution. |
| 90 | + let Some(dist) = required_dist(&requirement)? else { |
| 91 | + return Ok(requirement); |
| 92 | + }; |
| 93 | + |
| 94 | + // Fetch the metadata for the distribution. |
| 95 | + let metadata = { |
| 96 | + let id = dist.version_id(); |
| 97 | + if let Some(archive) = index |
| 98 | + .distributions() |
| 99 | + .get(&id) |
| 100 | + .as_deref() |
| 101 | + .and_then(|response| { |
| 102 | + if let MetadataResponse::Found(archive, ..) = response { |
| 103 | + Some(archive) |
| 104 | + } else { |
| 105 | + None |
| 106 | + } |
| 107 | + }) |
| 108 | + { |
| 109 | + // If the metadata is already in the index, return it. |
| 110 | + archive.metadata.clone() |
| 111 | + } else { |
| 112 | + // Run the PEP 517 build process to extract metadata from the source distribution. |
| 113 | + let archive = database |
| 114 | + .get_or_build_wheel_metadata(&dist, hasher.get(&dist)) |
| 115 | + .await |
| 116 | + .map_err(|err| match &dist { |
| 117 | + Dist::Built(built) => ExtrasError::Download(built.clone(), err), |
| 118 | + Dist::Source(source) => { |
| 119 | + if source.is_local() { |
| 120 | + ExtrasError::Build(source.clone(), err) |
| 121 | + } else { |
| 122 | + ExtrasError::DownloadAndBuild(source.clone(), err) |
| 123 | + } |
| 124 | + } |
| 125 | + })?; |
| 126 | + |
| 127 | + let metadata = archive.metadata.clone(); |
| 128 | + |
| 129 | + // Insert the metadata into the index. |
| 130 | + index |
| 131 | + .distributions() |
| 132 | + .done(id, Arc::new(MetadataResponse::Found(archive))); |
| 133 | + |
| 134 | + metadata |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + // Sort extras for consistency. |
| 139 | + let extras = { |
| 140 | + let mut extras = metadata.provides_extras; |
| 141 | + extras.sort_unstable(); |
| 142 | + extras |
| 143 | + }; |
| 144 | + |
| 145 | + Ok(Requirement { |
| 146 | + extras, |
| 147 | + ..requirement |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments