From 669384d569446cefc880f5f2bb9567bea6eb0fa3 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 10 Apr 2024 14:53:29 -0400 Subject: [PATCH] Rebase --- crates/uv-client/src/flat_index.rs | 4 +- crates/uv-dispatch/src/lib.rs | 4 +- crates/uv-resolver/src/flat_index.rs | 51 +++++++++++++++++++++++++- crates/uv-resolver/src/resolver/mod.rs | 2 +- crates/uv-resolver/tests/resolver.rs | 6 ++- crates/uv/src/commands/pip_install.rs | 5 ++- crates/uv/src/commands/pip_sync.rs | 4 +- 7 files changed, 65 insertions(+), 11 deletions(-) diff --git a/crates/uv-client/src/flat_index.rs b/crates/uv-client/src/flat_index.rs index 8a6e1514d4fd5..623e9b26ec99b 100644 --- a/crates/uv-client/src/flat_index.rs +++ b/crates/uv-client/src/flat_index.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use futures::{FutureExt, StreamExt}; use reqwest::Response; -use tracing::{debug, info_span, Instrument, warn}; +use tracing::{debug, info_span, warn, Instrument}; use url::Url; use distribution_filename::DistFilename; @@ -10,9 +10,9 @@ use distribution_types::{File, FileLocation, FlatIndexLocation, IndexUrl}; use pep508_rs::VerbatimUrl; use uv_cache::{Cache, CacheBucket}; -use crate::{Connectivity, Error, ErrorKind, RegistryClient}; use crate::cached_client::{CacheControl, CachedClientError}; use crate::html::SimpleHtml; +use crate::{Connectivity, Error, ErrorKind, RegistryClient}; #[derive(Debug, thiserror::Error)] pub enum FlatIndexError { diff --git a/crates/uv-dispatch/src/lib.rs b/crates/uv-dispatch/src/lib.rs index 9d0ff08886752..fa8ea31b47c55 100644 --- a/crates/uv-dispatch/src/lib.rs +++ b/crates/uv-dispatch/src/lib.rs @@ -3,8 +3,8 @@ //! implementing [`BuildContext`]. use std::ffi::OsStr; +use std::ffi::OsString; use std::path::Path; -use std::{ffi::OsString, future::Future}; use anyhow::{bail, Context, Result}; use futures::FutureExt; @@ -164,7 +164,7 @@ impl<'a> BuildContext for BuildDispatch<'a> { venv = ?venv.root() ) )] - fn install<'data>( + async fn install<'data>( &'data self, resolution: &'data Resolution, venv: &'data PythonEnvironment, diff --git a/crates/uv-resolver/src/flat_index.rs b/crates/uv-resolver/src/flat_index.rs index 820ad310a1537..3e09689bcde24 100644 --- a/crates/uv-resolver/src/flat_index.rs +++ b/crates/uv-resolver/src/flat_index.rs @@ -11,9 +11,11 @@ use distribution_types::{ }; use pep440_rs::Version; use platform_tags::Tags; +use pypi_types::HashDigest; use uv_client::FlatIndexEntries; use uv_configuration::{NoBinary, NoBuild}; use uv_normalize::PackageName; +use uv_types::RequiredHashes; /// A set of [`PrioritizedDist`] from a `--find-links` entry, indexed by [`PackageName`] /// and [`Version`]. @@ -32,6 +34,7 @@ impl FlatIndex { pub fn from_entries( entries: FlatIndexEntries, tags: &Tags, + required_hashes: &RequiredHashes, no_build: &NoBuild, no_binary: &NoBinary, ) -> Self { @@ -44,6 +47,7 @@ impl FlatIndex { file, filename, tags, + required_hashes, no_build, no_binary, url, @@ -56,11 +60,13 @@ impl FlatIndex { Self { index, offline } } + #[allow(clippy::too_many_arguments)] fn add_file( distributions: &mut FlatDistributions, file: File, filename: DistFilename, tags: &Tags, + required_hashes: &RequiredHashes, no_build: &NoBuild, no_binary: &NoBinary, index: IndexUrl, @@ -71,7 +77,13 @@ impl FlatIndex { DistFilename::WheelFilename(filename) => { let version = filename.version.clone(); - let compatibility = Self::wheel_compatibility(&filename, tags, no_binary); + let compatibility = Self::wheel_compatibility( + &filename, + &file.hashes, + tags, + required_hashes, + no_binary, + ); let dist = Dist::Built(BuiltDist::Registry(RegistryBuiltDist { filename, file: Box::new(file), @@ -87,7 +99,12 @@ impl FlatIndex { } } DistFilename::SourceDistFilename(filename) => { - let compatibility = Self::source_dist_compatibility(&filename, no_build); + let compatibility = Self::source_dist_compatibility( + &filename, + &file.hashes, + required_hashes, + no_build, + ); let dist = Dist::Source(SourceDist::Registry(RegistrySourceDist { filename: filename.clone(), file: Box::new(file), @@ -107,6 +124,8 @@ impl FlatIndex { fn source_dist_compatibility( filename: &SourceDistFilename, + hashes: &[HashDigest], + required_hashes: &RequiredHashes, no_build: &NoBuild, ) -> SourceDistCompatibility { // Check if source distributions are allowed for this package. @@ -120,12 +139,28 @@ impl FlatIndex { return SourceDistCompatibility::Incompatible(IncompatibleSource::NoBuild); } + // Check if hashes line up + if let Some(required_hashes) = required_hashes.get(&filename.name) { + if !required_hashes.is_empty() { + if hashes.is_empty() { + return SourceDistCompatibility::Incompatible(IncompatibleSource::MissingHash); + } + if !hashes.iter().any(|hash| required_hashes.contains(hash)) { + return SourceDistCompatibility::Incompatible( + IncompatibleSource::MismatchedHash, + ); + } + } + } + SourceDistCompatibility::Compatible } fn wheel_compatibility( filename: &WheelFilename, + hashes: &[HashDigest], tags: &Tags, + required_hashes: &RequiredHashes, no_binary: &NoBinary, ) -> WheelCompatibility { // Check if binaries are allowed for this package. @@ -139,6 +174,18 @@ impl FlatIndex { return WheelCompatibility::Incompatible(IncompatibleWheel::NoBinary); } + // Check if hashes line up + if let Some(required_hashes) = required_hashes.get(&filename.name) { + if !required_hashes.is_empty() { + if hashes.is_empty() { + return WheelCompatibility::Incompatible(IncompatibleWheel::MissingHash); + } + if !hashes.iter().any(|hash| required_hashes.contains(hash)) { + return WheelCompatibility::Incompatible(IncompatibleWheel::MismatchedHash); + } + } + } + // Determine a compatibility for the wheel based on tags. WheelCompatibility::from(filename.compatibility(tags)) } diff --git a/crates/uv-resolver/src/resolver/mod.rs b/crates/uv-resolver/src/resolver/mod.rs index 45d89877a7bad..732cdf867de7d 100644 --- a/crates/uv-resolver/src/resolver/mod.rs +++ b/crates/uv-resolver/src/resolver/mod.rs @@ -55,7 +55,7 @@ pub use crate::resolver::provider::{ use crate::resolver::reporter::Facade; pub use crate::resolver::reporter::{BuildId, Reporter}; use crate::yanks::AllowedYanks; -use crate::{DependencyMode, Exclusions, Options}; +use crate::{DependencyMode, Exclusions, FlatIndex, Options}; mod batch_prefetch; mod index; diff --git a/crates/uv-resolver/tests/resolver.rs b/crates/uv-resolver/tests/resolver.rs index 975d474668134..93ca66436f704 100644 --- a/crates/uv-resolver/tests/resolver.rs +++ b/crates/uv-resolver/tests/resolver.rs @@ -19,9 +19,11 @@ use uv_configuration::{BuildKind, Constraints, NoBinary, NoBuild, Overrides, Set use uv_interpreter::{find_default_python, Interpreter, PythonEnvironment}; use uv_resolver::{ DisplayResolutionGraph, Exclusions, FlatIndex, InMemoryIndex, Manifest, Options, - OptionsBuilder, Preference, PreReleaseMode, ResolutionGraph, ResolutionMode, Resolver, + OptionsBuilder, PreReleaseMode, Preference, ResolutionGraph, ResolutionMode, Resolver, +}; +use uv_types::{ + BuildContext, BuildIsolation, EmptyInstalledPackages, RequiredHashes, SourceBuildTrait, }; -use uv_types::{BuildContext, BuildIsolation, EmptyInstalledPackages, RequiredHashes, SourceBuildTrait}; // Exclude any packages uploaded after this date. static EXCLUDE_NEWER: Lazy> = Lazy::new(|| { diff --git a/crates/uv/src/commands/pip_install.rs b/crates/uv/src/commands/pip_install.rs index 508db2c407c38..040f2ba874586 100644 --- a/crates/uv/src/commands/pip_install.rs +++ b/crates/uv/src/commands/pip_install.rs @@ -36,7 +36,10 @@ use uv_requirements::{ ExtrasSpecification, LookaheadResolver, NamedRequirementsResolver, RequirementsSource, RequirementsSpecification, SourceTreeResolver, }; -use uv_resolver::{DependencyMode, Exclusions, FlatIndex, InMemoryIndex, Manifest, Options, OptionsBuilder, PreReleaseMode, Preference, ResolutionGraph, ResolutionMode, Resolver, HashCheckingMode}; +use uv_resolver::{ + DependencyMode, Exclusions, FlatIndex, HashCheckingMode, InMemoryIndex, Manifest, Options, + OptionsBuilder, PreReleaseMode, Preference, ResolutionGraph, ResolutionMode, Resolver, +}; use uv_types::{BuildIsolation, InFlight, RequiredHashes}; use uv_warnings::warn_user; diff --git a/crates/uv/src/commands/pip_sync.rs b/crates/uv/src/commands/pip_sync.rs index d14d4e0762032..093954682da34 100644 --- a/crates/uv/src/commands/pip_sync.rs +++ b/crates/uv/src/commands/pip_sync.rs @@ -30,7 +30,9 @@ use uv_requirements::{ ExtrasSpecification, NamedRequirementsResolver, RequirementsSource, RequirementsSpecification, SourceTreeResolver, }; -use uv_resolver::{DependencyMode, FlatIndex, HashCheckingMode, InMemoryIndex, Manifest, OptionsBuilder, Resolver}; +use uv_resolver::{ + DependencyMode, FlatIndex, HashCheckingMode, InMemoryIndex, Manifest, OptionsBuilder, Resolver, +}; use uv_types::{BuildIsolation, EmptyInstalledPackages, InFlight, RequiredHashes}; use uv_warnings::warn_user;