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
220
221
222
223
224
225
use std::borrow::Cow;
use std::fmt::Display;
use std::path::Path;
use itertools::Itertools;
use uv_distribution_types::{
DistributionMetadata, Name, RequiresPython, ResolvedDist, SimplifiedMarkerTree, Verbatim,
VersionOrUrlRef,
};
use uv_normalize::{ExtraName, PackageName};
use uv_pep440::Version;
use uv_pep508::{MarkerTree, Scheme, split_scheme};
use uv_pypi_types::HashDigest;
use crate::resolution::AnnotatedDist;
#[derive(Debug, Clone)]
/// A pinned package with its resolved distribution and all the extras that were pinned for it.
pub(crate) struct RequirementsTxtDist<'dist> {
pub(crate) dist: &'dist ResolvedDist,
pub(crate) version: &'dist Version,
pub(crate) hashes: &'dist [HashDigest],
pub(crate) markers: MarkerTree,
pub(crate) extras: Vec<ExtraName>,
}
impl<'dist> RequirementsTxtDist<'dist> {
/// Convert the [`RequirementsTxtDist`] to a requirement that adheres to the `requirements.txt`
/// format.
///
/// This typically results in a PEP 508 representation of the requirement, but will write an
/// unnamed requirement for relative paths, which can't be represented with PEP 508 (but are
/// supported in `requirements.txt`).
pub(crate) fn to_requirements_txt(
&self,
requires_python: &RequiresPython,
include_markers: bool,
) -> Cow<'_, str> {
// If the URL is editable, write it as an editable requirement.
if self.dist.is_editable() {
if let VersionOrUrlRef::Url(url) = self.dist.version_or_url() {
let given = url.verbatim();
return Cow::Owned(format!("-e {given}"));
}
}
// If the URL is not _definitively_ a `file://` URL, write it as a relative path.
if self.dist.is_local() {
if let VersionOrUrlRef::Url(url) = self.dist.version_or_url() {
let given = url.verbatim();
let given = match split_scheme(&given) {
Some((scheme, path)) => {
match Scheme::parse(scheme) {
Some(Scheme::File) => {
if path
.strip_prefix("//localhost")
.filter(|path| path.starts_with('/'))
.is_some()
{
// Always absolute; nothing to do.
None
} else if let Some(path) = path.strip_prefix("//") {
// Strip the prefix, to convert, e.g., `file://flask-3.0.3-py3-none-any.whl` to `flask-3.0.3-py3-none-any.whl`.
//
// However, we should allow any of the following:
// - `file:///flask-3.0.3-py3-none-any.whl`
// - `file://C:\Users\user\flask-3.0.3-py3-none-any.whl`
// - `file:///C:\Users\user\flask-3.0.3-py3-none-any.whl`
if !path.starts_with("${PROJECT_ROOT}")
&& !Path::new(path).has_root()
{
Some(Cow::Owned(path.to_string()))
} else {
// Ex) `file:///flask-3.0.3-py3-none-any.whl`
None
}
} else {
// Ex) `file:./flask-3.0.3-py3-none-any.whl`
None
}
}
Some(_) => None,
None => {
// Ex) `flask @ C:\Users\user\flask-3.0.3-py3-none-any.whl`
Some(given)
}
}
}
None => {
// Ex) `flask @ flask-3.0.3-py3-none-any.whl`
Some(given)
}
};
if let Some(given) = given {
return if let Some(markers) =
SimplifiedMarkerTree::new(requires_python, self.markers)
.try_to_string()
.filter(|_| include_markers)
{
Cow::Owned(format!("{given} ; {markers}"))
} else {
given
};
}
}
}
if self.extras.is_empty() {
if let Some(markers) = SimplifiedMarkerTree::new(requires_python, self.markers)
.try_to_string()
.filter(|_| include_markers)
{
Cow::Owned(format!("{} ; {}", self.dist.verbatim(), markers))
} else {
self.dist.verbatim()
}
} else {
let mut extras = self.extras.clone();
extras.sort_unstable();
extras.dedup();
if let Some(markers) = SimplifiedMarkerTree::new(requires_python, self.markers)
.try_to_string()
.filter(|_| include_markers)
{
Cow::Owned(format!(
"{}[{}]{} ; {}",
self.name(),
extras.into_iter().join(", "),
self.version_or_url().verbatim(),
markers,
))
} else {
Cow::Owned(format!(
"{}[{}]{}",
self.name(),
extras.into_iter().join(", "),
self.version_or_url().verbatim()
))
}
}
}
/// Convert the [`RequirementsTxtDist`] to a comparator that can be used to sort the requirements
/// in a `requirements.txt` file.
pub(crate) fn to_comparator(&self) -> RequirementsTxtComparator<'_> {
if self.dist.is_editable() {
if let VersionOrUrlRef::Url(url) = self.dist.version_or_url() {
return RequirementsTxtComparator::Url(url.verbatim());
}
}
if let VersionOrUrlRef::Url(url) = self.version_or_url() {
RequirementsTxtComparator::Name {
name: self.name(),
version: self.version,
url: Some(url.verbatim()),
extras: &self.extras,
}
} else {
RequirementsTxtComparator::Name {
name: self.name(),
version: self.version,
url: None,
extras: &self.extras,
}
}
}
pub(crate) fn from_annotated_dist(annotated: &'dist AnnotatedDist) -> Self {
assert!(
annotated.marker.conflict().is_true(),
"found dist {annotated} with non-trivial conflicting marker {marker:?}, \
which cannot be represented in a `requirements.txt` format",
marker = annotated.marker,
);
Self {
dist: &annotated.dist,
version: &annotated.version,
hashes: annotated.hashes.as_slice(),
// OK because we've asserted above that this dist
// does not have a non-trivial conflicting marker
// that we would otherwise need to care about.
markers: annotated.marker.combined(),
extras: if let Some(extra) = annotated.extra.clone() {
vec![extra]
} else {
vec![]
},
}
}
}
/// A comparator for sorting requirements in a `requirements.txt` file.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum RequirementsTxtComparator<'a> {
/// Sort by URL for editable requirements.
Url(Cow<'a, str>),
/// In universal mode, we can have multiple versions for a package, so we track the version and
/// the URL (for non-index packages) to have a stable sort for those, too.
Name {
name: &'a PackageName,
version: &'a Version,
url: Option<Cow<'a, str>>,
extras: &'a [ExtraName],
},
}
impl Name for RequirementsTxtDist<'_> {
fn name(&self) -> &PackageName {
self.dist.name()
}
}
impl DistributionMetadata for RequirementsTxtDist<'_> {
fn version_or_url(&self) -> VersionOrUrlRef<'_> {
self.dist.version_or_url()
}
}
impl Display for RequirementsTxtDist<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.dist, f)
}
}