|
| 1 | +use rustc_hash::FxHashMap; |
| 2 | + |
| 3 | +use pep440_rs::{Operator, Version, VersionSpecifier}; |
| 4 | +use pep508_rs::{MarkerEnvironment, VersionOrUrl}; |
| 5 | +use uv_normalize::PackageName; |
| 6 | + |
| 7 | +use crate::Manifest; |
| 8 | + |
| 9 | +#[derive(Debug, Default)] |
| 10 | +pub(crate) struct Locals { |
| 11 | + /// A map of package names to their associated, required local versions. |
| 12 | + required: FxHashMap<PackageName, Version>, |
| 13 | +} |
| 14 | + |
| 15 | +impl Locals { |
| 16 | + /// Determine the set of permitted local versions in the [`Manifest`]. |
| 17 | + pub(crate) fn from_manifest(manifest: &Manifest, markers: &MarkerEnvironment) -> Self { |
| 18 | + let mut required: FxHashMap<PackageName, Version> = FxHashMap::default(); |
| 19 | + |
| 20 | + // Add all direct requirements and constraints. There's no need to look for conflicts, |
| 21 | + // since conflicting versions will be tracked upstream. |
| 22 | + for requirement in manifest |
| 23 | + .requirements |
| 24 | + .iter() |
| 25 | + .filter(|requirement| requirement.evaluate_markers(markers, &[])) |
| 26 | + .chain( |
| 27 | + manifest |
| 28 | + .constraints |
| 29 | + .iter() |
| 30 | + .filter(|requirement| requirement.evaluate_markers(markers, &[])), |
| 31 | + ) |
| 32 | + .chain(manifest.editables.iter().flat_map(|(editable, metadata)| { |
| 33 | + metadata |
| 34 | + .requires_dist |
| 35 | + .iter() |
| 36 | + .filter(|requirement| requirement.evaluate_markers(markers, &editable.extras)) |
| 37 | + })) |
| 38 | + .chain( |
| 39 | + manifest |
| 40 | + .overrides |
| 41 | + .iter() |
| 42 | + .filter(|requirement| requirement.evaluate_markers(markers, &[])), |
| 43 | + ) |
| 44 | + { |
| 45 | + if let Some(VersionOrUrl::VersionSpecifier(specifiers)) = |
| 46 | + requirement.version_or_url.as_ref() |
| 47 | + { |
| 48 | + for specifier in specifiers.iter() { |
| 49 | + if let Some(version) = to_local(specifier) { |
| 50 | + required.insert(requirement.name.clone(), version.clone()); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + Self { required } |
| 57 | + } |
| 58 | + |
| 59 | + /// Return the local [`Version`] to which a package is pinned, if any. |
| 60 | + pub(crate) fn get(&self, package: &PackageName) -> Option<&Version> { |
| 61 | + self.required.get(package) |
| 62 | + } |
| 63 | + |
| 64 | + /// Given a specifier that may include the version _without_ a local segment, return a specifier |
| 65 | + /// that includes the local segment from the expected version. |
| 66 | + pub(crate) fn map(local: &Version, specifier: &VersionSpecifier) -> VersionSpecifier { |
| 67 | + match specifier.operator() { |
| 68 | + Operator::Equal | Operator::EqualStar => { |
| 69 | + // Given `foo==1.0.0`, if the local version is `1.0.0+local`, map to |
| 70 | + // `foo==1.0.0+local`. This has the intended effect of allowing `1.0.0+local`. |
| 71 | + if is_compatible(local, specifier.version()) { |
| 72 | + VersionSpecifier::new(Operator::Equal, local.clone()) |
| 73 | + } else { |
| 74 | + specifier.clone() |
| 75 | + } |
| 76 | + } |
| 77 | + Operator::NotEqual | Operator::NotEqualStar => { |
| 78 | + // Given `foo!=1.0.0`, if the local version is `1.0.0+local`, map to |
| 79 | + // `foo!=1.0.0+local`. This has the intended effect of disallowing `1.0.0+local`. |
| 80 | + // There's no risk of including `foo @ 1.0.0` in the resolution, since we _know_ |
| 81 | + // `foo @ 1.0.0+local` is required and would conflict. |
| 82 | + if is_compatible(local, specifier.version()) { |
| 83 | + VersionSpecifier::new(Operator::NotEqual, local.clone()) |
| 84 | + } else { |
| 85 | + specifier.clone() |
| 86 | + } |
| 87 | + } |
| 88 | + Operator::LessThanEqual => { |
| 89 | + // Given `foo<=1.0.0`, if the local version is `1.0.0+local`, map to |
| 90 | + // `foo<=1.0.0+local`. This has the intended effect of allowing `1.0.0+local`. |
| 91 | + // There's no risk of including `foo @ 1.0.0.post1` in the resolution, since we |
| 92 | + // _know_ `foo @ 1.0.0+local` is required and would conflict. |
| 93 | + if is_compatible(local, specifier.version()) { |
| 94 | + VersionSpecifier::new(Operator::LessThanEqual, local.clone()) |
| 95 | + } else { |
| 96 | + specifier.clone() |
| 97 | + } |
| 98 | + } |
| 99 | + Operator::GreaterThan => { |
| 100 | + // Given `foo>1.0.0`, if the local version is `1.0.0+local`, map to |
| 101 | + // `foo>1.0.0+local`. This has the intended effect of disallowing `1.0.0+local`. |
| 102 | + if is_compatible(local, specifier.version()) { |
| 103 | + VersionSpecifier::new(Operator::GreaterThan, local.clone()) |
| 104 | + } else { |
| 105 | + specifier.clone() |
| 106 | + } |
| 107 | + } |
| 108 | + Operator::ExactEqual => { |
| 109 | + // Given `foo===1.0.0`, `1.0.0+local` is already disallowed. |
| 110 | + specifier.clone() |
| 111 | + } |
| 112 | + Operator::TildeEqual => { |
| 113 | + // Given `foo~=1.0.0`, `foo~=1.0.0+local` is already allowed. |
| 114 | + specifier.clone() |
| 115 | + } |
| 116 | + Operator::LessThan => { |
| 117 | + // Given `foo<1.0.0`, `1.0.0+local` is already disallowed. |
| 118 | + specifier.clone() |
| 119 | + } |
| 120 | + Operator::GreaterThanEqual => { |
| 121 | + // Given `foo>=1.0.0`, `foo>1.0.0+local` is already allowed. |
| 122 | + specifier.clone() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Returns `true` if a provided version is compatible with the expected local version. |
| 129 | +/// |
| 130 | +/// The versions are compatible if they are the same including their local segment, or the |
| 131 | +/// same except for the local segment, which is empty in the provided version. |
| 132 | +/// |
| 133 | +/// For example, if the expected version is `1.0.0+local` and the provided version is `1.0.0+other`, |
| 134 | +/// this function will return `false`. |
| 135 | +/// |
| 136 | +/// If the expected version is `1.0.0+local` and the provided version is `1.0.0`, the function will |
| 137 | +/// return `true`. |
| 138 | +fn is_compatible(expected: &Version, provided: &Version) -> bool { |
| 139 | + // The requirements should be the same, ignoring local segments. |
| 140 | + if expected.clone().without_local() != provided.clone().without_local() { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + // If the provided version has a local segment, it should be the same as the expected |
| 145 | + // version. |
| 146 | + if provided.local().is_empty() { |
| 147 | + true |
| 148 | + } else { |
| 149 | + expected.local() == provided.local() |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// If a [`VersionSpecifier`] represents exact equality against a local version, return the local |
| 154 | +/// version. |
| 155 | +fn to_local(specifier: &VersionSpecifier) -> Option<&Version> { |
| 156 | + if !matches!(specifier.operator(), Operator::Equal | Operator::ExactEqual) { |
| 157 | + return None; |
| 158 | + }; |
| 159 | + |
| 160 | + if specifier.version().local().is_empty() { |
| 161 | + return None; |
| 162 | + } |
| 163 | + |
| 164 | + Some(specifier.version()) |
| 165 | +} |
0 commit comments