-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9159731
commit 356fa5f
Showing
7 changed files
with
189 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::ops::Deref; | ||
|
||
use rustc_hash::FxHashMap; | ||
|
||
use pep440_rs::Version; | ||
use pep508_rs::{MarkerEnvironment, VerbatimUrl, VersionOrUrl}; | ||
use uv_normalize::PackageName; | ||
|
||
use crate::{Manifest, ResolveError}; | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct Locals { | ||
/// A map of package names to their associated, required URLs. | ||
required: FxHashMap<PackageName, Version>, | ||
} | ||
|
||
impl Locals { | ||
pub(crate) fn from_manifest( | ||
manifest: &Manifest, | ||
markers: &MarkerEnvironment, | ||
) -> Result<Self, ResolveError> { | ||
let mut required: FxHashMap<PackageName, Version> = FxHashMap::default(); | ||
|
||
// Add all direct requirements and constraints. If there are any conflicts, return an error. | ||
for requirement in manifest | ||
.requirements | ||
.iter() | ||
.filter(|requirement| requirement.evaluate_markers(markers, &[])) | ||
.chain( | ||
manifest | ||
.constraints | ||
.iter() | ||
.filter(|requirement| requirement.evaluate_markers(markers, &[])), | ||
) | ||
.chain(manifest.editables.iter().flat_map(|(editable, metadata)| { | ||
metadata | ||
.requires_dist | ||
.iter() | ||
.filter(|requirement| requirement.evaluate_markers(markers, &editable.extras)) | ||
})) | ||
.chain( | ||
manifest | ||
.overrides | ||
.iter() | ||
.filter(|requirement| requirement.evaluate_markers(markers, &[])), | ||
) | ||
{ | ||
if let Some(version) = to_local(requirement.version_or_url.as_ref()) { | ||
required.insert(requirement.name.clone(), version.clone()); | ||
} | ||
} | ||
|
||
Ok(Self { required }) | ||
} | ||
|
||
/// Return the [`VerbatimUrl`] associated with the given package name, if any. | ||
pub(crate) fn get(&self, package: &PackageName) -> Option<&Version> { | ||
self.required.get(package) | ||
} | ||
|
||
/// Returns `true` if a package is allowed to have a local version. | ||
pub(crate) fn is_allowed(&self, expected: &Version, provided: &Version) -> bool { | ||
// The requirements should be the same, ignoring local segments. | ||
if expected.clone().without_local() != provided.clone().without_local() { | ||
return false; | ||
} | ||
|
||
// If the provided version has a local segment, it should be the same as the expected | ||
// version. | ||
if provided.local().is_empty() { | ||
true | ||
} else { | ||
expected.local() == provided.local() | ||
} | ||
} | ||
} | ||
|
||
fn to_local(version_or_url: Option<&VersionOrUrl>) -> Option<&Version> { | ||
let Some(VersionOrUrl::VersionSpecifier(specifier)) = version_or_url else { | ||
return None; | ||
}; | ||
|
||
let [specifier] = specifier.deref() else { | ||
return None; | ||
}; | ||
|
||
if *specifier.operator() != pep440_rs::Operator::Equal { | ||
return None; | ||
}; | ||
|
||
if specifier.version().local().is_empty() { | ||
return None; | ||
} | ||
|
||
Some(specifier.version()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
torchvision==0.15.1+cu118 | ||
torch==2.0.0+cu118 |