-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathpackage_options.rs
More file actions
375 lines (332 loc) · 13.3 KB
/
package_options.rs
File metadata and controls
375 lines (332 loc) · 13.3 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use std::path::Path;
use rustc_hash::{FxHashMap, FxHashSet};
use uv_cache::Refresh;
use uv_cache_info::Timestamp;
use uv_distribution_types::{Requirement, RequirementSource};
use uv_normalize::{GroupName, PackageName};
/// Whether to reinstall packages.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum Reinstall {
/// Don't reinstall any packages; respect the existing installation.
#[default]
None,
/// Reinstall all packages in the plan.
All,
/// Reinstall only the specified packages.
Packages(Vec<PackageName>, Vec<Box<Path>>),
}
impl Reinstall {
/// Determine the reinstall strategy to use.
pub fn from_args(reinstall: Option<bool>, reinstall_package: Vec<PackageName>) -> Option<Self> {
match reinstall {
Some(true) => Some(Self::All),
Some(false) => Some(Self::None),
None if reinstall_package.is_empty() => None,
None => Some(Self::Packages(reinstall_package, Vec::new())),
}
}
/// Returns `true` if no packages should be reinstalled.
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}
/// Returns `true` if all packages should be reinstalled.
pub fn is_all(&self) -> bool {
matches!(self, Self::All)
}
/// Returns `true` if the specified package should be reinstalled.
pub fn contains_package(&self, package_name: &PackageName) -> bool {
match self {
Self::None => false,
Self::All => true,
Self::Packages(packages, ..) => packages.contains(package_name),
}
}
/// Returns `true` if the specified path should be reinstalled.
pub fn contains_path(&self, path: &Path) -> bool {
match self {
Self::None => false,
Self::All => true,
Self::Packages(.., paths) => paths
.iter()
.any(|target| same_file::is_same_file(path, target).unwrap_or(false)),
}
}
/// Combine a set of [`Reinstall`] values.
#[must_use]
pub fn combine(self, other: Self) -> Self {
match self {
// Setting `--reinstall` or `--no-reinstall` should clear previous `--reinstall-package` selections.
Self::All | Self::None => self,
Self::Packages(self_packages, self_paths) => match other {
// If `--reinstall` was enabled previously, `--reinstall-package` is subsumed by reinstalling all packages.
Self::All => other,
// If `--no-reinstall` was enabled previously, then `--reinstall-package` enables an explicit reinstall of those packages.
Self::None => Self::Packages(self_packages, self_paths),
// If `--reinstall-package` was included twice, combine the requirements.
Self::Packages(other_packages, other_paths) => {
let mut combined_packages = self_packages;
combined_packages.extend(other_packages);
let mut combined_paths = self_paths;
combined_paths.extend(other_paths);
Self::Packages(combined_packages, combined_paths)
}
},
}
}
/// Add a [`Box<Path>`] to the [`Reinstall`] policy.
#[must_use]
pub fn with_path(self, path: Box<Path>) -> Self {
match self {
Self::None => Self::Packages(vec![], vec![path]),
Self::All => Self::All,
Self::Packages(packages, mut paths) => {
paths.push(path);
Self::Packages(packages, paths)
}
}
}
/// Add a [`Package`] to the [`Reinstall`] policy.
#[must_use]
pub fn with_package(self, package_name: PackageName) -> Self {
match self {
Self::None => Self::Packages(vec![package_name], vec![]),
Self::All => Self::All,
Self::Packages(mut packages, paths) => {
packages.push(package_name);
Self::Packages(packages, paths)
}
}
}
/// Create a [`Reinstall`] strategy to reinstall a single package.
pub fn package(package_name: PackageName) -> Self {
Self::Packages(vec![package_name], vec![])
}
}
/// Create a [`Refresh`] policy by integrating the [`Reinstall`] policy.
impl From<Reinstall> for Refresh {
fn from(value: Reinstall) -> Self {
match value {
Reinstall::None => Self::None(Timestamp::now()),
Reinstall::All => Self::All(Timestamp::now()),
Reinstall::Packages(packages, paths) => {
Self::Packages(packages, paths, Timestamp::now())
}
}
}
}
/// Strategy for determining which packages to consider for upgrade.
#[derive(Debug, Default, Clone)]
pub enum UpgradeStrategy {
/// Prefer pinned versions from the existing lockfile, if possible.
#[default]
None,
/// Allow package upgrades for all packages, ignoring the existing lockfile.
All,
/// Allow package upgrades, but only for the specified packages.
Packages(FxHashSet<PackageName>),
}
/// Whether to allow package upgrades.
#[derive(Debug, Default, Clone)]
pub struct Upgrade {
/// Strategy for picking packages to consider for upgrade.
strategy: UpgradeStrategy,
/// Additional version constraints for specific packages.
constraints: FxHashMap<PackageName, Vec<Requirement>>,
/// Dependency groups whose packages should be upgraded.
groups: FxHashSet<GroupName>,
}
impl Upgrade {
/// Create a new [`Upgrade`] with no upgrades nor constraints.
pub fn none() -> Self {
Self {
strategy: UpgradeStrategy::None,
constraints: FxHashMap::default(),
groups: FxHashSet::default(),
}
}
/// Create a new [`Upgrade`] to consider all packages.
pub fn all() -> Self {
Self {
strategy: UpgradeStrategy::All,
constraints: FxHashMap::default(),
groups: FxHashSet::default(),
}
}
/// Determine the upgrade selection strategy from the command-line arguments.
pub fn from_args(
upgrade: Option<bool>,
upgrade_package: Vec<Requirement>,
upgrade_group: Vec<GroupName>,
) -> Option<Self> {
let groups: FxHashSet<GroupName> = upgrade_group.into_iter().collect();
let strategy = match upgrade {
Some(true) => UpgradeStrategy::All,
Some(false) => {
if upgrade_package.is_empty() && groups.is_empty() {
return Some(Self::none());
}
// `--no-upgrade` with `--upgrade-package` allows selecting the specified packages for upgrade.
let packages = upgrade_package.iter().map(|req| req.name.clone()).collect();
UpgradeStrategy::Packages(packages)
}
None => {
if upgrade_package.is_empty() && groups.is_empty() {
return None;
}
let packages = upgrade_package.iter().map(|req| req.name.clone()).collect();
UpgradeStrategy::Packages(packages)
}
};
let mut constraints: FxHashMap<PackageName, Vec<Requirement>> = FxHashMap::default();
for requirement in upgrade_package {
// Skip any "empty" constraints.
if let RequirementSource::Registry { specifier, .. } = &requirement.source {
if specifier.is_empty() {
continue;
}
}
constraints
.entry(requirement.name.clone())
.or_default()
.push(requirement);
}
Some(Self {
strategy,
constraints,
groups,
})
}
/// Create an [`Upgrade`] to upgrade a single package.
pub fn package(package_name: PackageName) -> Self {
let mut packages = FxHashSet::default();
packages.insert(package_name);
Self {
strategy: UpgradeStrategy::Packages(packages),
constraints: FxHashMap::default(),
groups: FxHashSet::default(),
}
}
/// Returns `true` if no packages should be upgraded.
pub fn is_none(&self) -> bool {
matches!(self.strategy, UpgradeStrategy::None) && self.groups.is_empty()
}
/// Returns `true` if all packages should be upgraded.
pub fn is_all(&self) -> bool {
matches!(self.strategy, UpgradeStrategy::All)
}
/// Returns `true` if the specified package should be upgraded.
pub fn contains(&self, package_name: &PackageName) -> bool {
match &self.strategy {
UpgradeStrategy::None => false,
UpgradeStrategy::All => true,
UpgradeStrategy::Packages(packages) => packages.contains(package_name),
}
}
/// Returns an iterator over the constraints.
///
/// When upgrading, users can provide bounds on the upgrade (e.g., `--upgrade-package flask<3`).
pub fn constraints(&self) -> impl Iterator<Item = &Requirement> {
self.constraints
.values()
.flat_map(|requirements| requirements.iter())
}
/// Returns the set of dependency groups whose packages should be upgraded.
pub fn groups(&self) -> &FxHashSet<GroupName> {
&self.groups
}
/// Combine a set of [`Upgrade`] values.
#[must_use]
pub fn combine(self, other: Self) -> Self {
// For `strategy`: `other` takes precedence for an explicit `All` or `None`; otherwise, merge.
let strategy = match (self.strategy, other.strategy) {
(_, UpgradeStrategy::All) => UpgradeStrategy::All,
(_, UpgradeStrategy::None) => UpgradeStrategy::None,
(
UpgradeStrategy::Packages(mut self_packages),
UpgradeStrategy::Packages(other_packages),
) => {
self_packages.extend(other_packages);
UpgradeStrategy::Packages(self_packages)
}
(_, UpgradeStrategy::Packages(packages)) => UpgradeStrategy::Packages(packages),
};
// For `constraints`: always merge the constraints of `self` and `other`.
let mut combined_constraints = self.constraints.clone();
for (package, requirements) in other.constraints {
combined_constraints
.entry(package)
.or_default()
.extend(requirements);
}
// For `groups`: always merge.
let mut combined_groups = self.groups;
combined_groups.extend(other.groups);
Self {
strategy,
constraints: combined_constraints,
groups: combined_groups,
}
}
}
/// Create a [`Refresh`] policy by integrating the [`Upgrade`] policy.
impl From<Upgrade> for Refresh {
fn from(value: Upgrade) -> Self {
match value.strategy {
UpgradeStrategy::None => Self::None(Timestamp::now()),
UpgradeStrategy::All => Self::All(Timestamp::now()),
UpgradeStrategy::Packages(packages) => Self::Packages(
packages.into_iter().collect::<Vec<_>>(),
Vec::new(),
Timestamp::now(),
),
}
}
}
/// Whether to isolate builds.
#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum BuildIsolation {
/// Isolate all builds.
#[default]
Isolate,
/// Do not isolate any builds.
Shared,
/// Do not isolate builds for the specified packages.
SharedPackage(Vec<PackageName>),
}
impl BuildIsolation {
/// Determine the build isolation strategy from the command-line arguments.
pub fn from_args(
no_build_isolation: Option<bool>,
no_build_isolation_package: Vec<PackageName>,
) -> Option<Self> {
match no_build_isolation {
Some(true) => Some(Self::Shared),
Some(false) => Some(Self::Isolate),
None if no_build_isolation_package.is_empty() => None,
None => Some(Self::SharedPackage(no_build_isolation_package)),
}
}
/// Combine a set of [`BuildIsolation`] values.
#[must_use]
pub fn combine(self, other: Self) -> Self {
match self {
// Setting `--build-isolation` or `--no-build-isolation` should clear previous `--no-build-isolation-package` selections.
Self::Isolate | Self::Shared => self,
Self::SharedPackage(self_packages) => match other {
// If `--no-build-isolation` was enabled previously, `--no-build-isolation-package` is subsumed by sharing all builds.
Self::Shared => other,
// If `--build-isolation` was enabled previously, then `--no-build-isolation-package` enables specific packages to be shared.
Self::Isolate => Self::SharedPackage(self_packages),
// If `--no-build-isolation-package` was included twice, combine the packages.
Self::SharedPackage(other_packages) => {
let mut combined = self_packages;
combined.extend(other_packages);
Self::SharedPackage(combined)
}
},
}
}
}