1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use uv_git_types::{GitLfs, GitReference};
use uv_normalize::ExtraName;
use uv_pep508::{MarkerEnvironment, MarkerTree, UnnamedRequirement};
use uv_pypi_types::{Hashes, ParsedUrl};
use crate::{Requirement, RequirementSource, VerbatimParsedUrl};
/// An [`UnresolvedRequirement`] with additional metadata from `requirements.txt`, currently only
/// hashes but in the future also editable and similar information.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct NameRequirementSpecification {
/// The actual requirement.
pub requirement: Requirement,
/// Hashes of the downloadable packages.
pub hashes: Vec<String>,
}
/// An [`UnresolvedRequirement`] with additional metadata from `requirements.txt`, currently only
/// hashes but in the future also editable and similar information.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct UnresolvedRequirementSpecification {
/// The actual requirement.
pub requirement: UnresolvedRequirement,
/// Hashes of the downloadable packages.
pub hashes: Vec<String>,
}
/// A requirement read from a `requirements.txt` or `pyproject.toml` file.
///
/// It is considered unresolved as we still need to query the URL for the `Unnamed` variant to
/// resolve the requirement name.
///
/// Analog to `RequirementsTxtRequirement` but with `distribution_types::Requirement` instead of
/// `uv_pep508::Requirement`.
#[derive(Hash, Debug, Clone, Eq, PartialEq)]
pub enum UnresolvedRequirement {
/// The uv-specific superset over PEP 508 requirements specifier incorporating
/// `tool.uv.sources`.
Named(Requirement),
/// A PEP 508-like, direct URL dependency specifier.
Unnamed(UnnamedRequirement<VerbatimParsedUrl>),
}
impl Display for UnresolvedRequirement {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Named(requirement) => write!(f, "{requirement}"),
Self::Unnamed(requirement) => write!(f, "{requirement}"),
}
}
}
impl UnresolvedRequirement {
/// Returns whether the markers apply for the given environment.
///
/// When the environment is not given, this treats all marker expressions
/// that reference the environment as true. In other words, it does
/// environment independent expression evaluation. (Which in turn devolves
/// to "only evaluate marker expressions that reference an extra name.")
pub fn evaluate_markers(&self, env: Option<&MarkerEnvironment>, extras: &[ExtraName]) -> bool {
match self {
Self::Named(requirement) => requirement.evaluate_markers(env, extras),
Self::Unnamed(requirement) => requirement.evaluate_optional_environment(env, extras),
}
}
/// Augment a user-provided requirement by attaching any specification data that was provided
/// separately from the requirement itself (e.g., `--branch main`).
#[must_use]
pub fn augment_requirement(
self,
rev: Option<&str>,
tag: Option<&str>,
branch: Option<&str>,
lfs: Option<bool>,
marker: Option<MarkerTree>,
) -> Self {
#[allow(clippy::manual_map)]
let git_reference = if let Some(rev) = rev {
Some(GitReference::from_rev(rev.to_string()))
} else if let Some(tag) = tag {
Some(GitReference::Tag(tag.to_string()))
} else if let Some(branch) = branch {
Some(GitReference::Branch(branch.to_string()))
} else {
None
};
match self {
Self::Named(mut requirement) => Self::Named(Requirement {
marker: marker
.map(|marker| {
requirement.marker.and(marker);
requirement.marker
})
.unwrap_or(requirement.marker),
source: match requirement.source {
RequirementSource::Git {
git,
subdirectory,
url,
} => {
let git = if let Some(git_reference) = git_reference {
git.with_reference(git_reference)
} else {
git
};
let git = if let Some(lfs) = lfs {
git.with_lfs(GitLfs::from(lfs))
} else {
git
};
RequirementSource::Git {
git,
subdirectory,
url,
}
}
_ => requirement.source,
},
..requirement
}),
Self::Unnamed(mut requirement) => Self::Unnamed(UnnamedRequirement {
marker: marker
.map(|marker| {
requirement.marker.and(marker);
requirement.marker
})
.unwrap_or(requirement.marker),
url: match requirement.url.parsed_url {
ParsedUrl::Git(mut git) => {
if let Some(git_reference) = git_reference {
git.url = git.url.with_reference(git_reference);
}
if let Some(lfs) = lfs {
git.url = git.url.with_lfs(GitLfs::from(lfs));
}
VerbatimParsedUrl {
parsed_url: ParsedUrl::Git(git),
verbatim: requirement.url.verbatim,
}
}
_ => requirement.url,
},
..requirement
}),
}
}
/// Returns the extras for the requirement.
pub fn extras(&self) -> &[ExtraName] {
match self {
Self::Named(requirement) => &requirement.extras,
Self::Unnamed(requirement) => &requirement.extras,
}
}
/// Return the version specifier or URL for the requirement.
pub fn source(&self) -> Cow<'_, RequirementSource> {
match self {
Self::Named(requirement) => Cow::Borrowed(&requirement.source),
Self::Unnamed(requirement) => Cow::Owned(RequirementSource::from_parsed_url(
requirement.url.parsed_url.clone(),
requirement.url.verbatim.clone(),
)),
}
}
/// Returns `true` if the requirement is editable.
pub fn is_editable(&self) -> bool {
match self {
Self::Named(requirement) => requirement.is_editable(),
Self::Unnamed(requirement) => requirement.url.is_editable(),
}
}
/// Return the hashes of the requirement, as specified in the URL fragment.
pub fn hashes(&self) -> Option<Hashes> {
match self {
Self::Named(requirement) => requirement.hashes(),
Self::Unnamed(requirement) => {
let fragment = requirement.url.verbatim.fragment()?;
Hashes::parse_fragment(fragment).ok()
}
}
}
}
impl NameRequirementSpecification {
/// Return the hashes of the requirement, as specified in the URL fragment.
pub fn hashes(&self) -> Option<Hashes> {
let RequirementSource::Url { ref url, .. } = self.requirement.source else {
return None;
};
let fragment = url.fragment()?;
Hashes::parse_fragment(fragment).ok()
}
}
impl From<Requirement> for UnresolvedRequirementSpecification {
fn from(requirement: Requirement) -> Self {
Self {
requirement: UnresolvedRequirement::Named(requirement),
hashes: Vec::new(),
}
}
}
impl From<Requirement> for NameRequirementSpecification {
fn from(requirement: Requirement) -> Self {
Self {
requirement,
hashes: Vec::new(),
}
}
}