Skip to content

Commit

Permalink
Clean up Refresh API
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 8, 2024
1 parent 54a85c0 commit ebeb9dd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 34 deletions.
36 changes: 36 additions & 0 deletions crates/uv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,40 @@ impl Refresh {
pub fn is_none(&self) -> bool {
matches!(self, Self::None(_))
}

/// Combine two [`Refresh`] policies, taking the "max" of the two policies.
pub fn combine(self, other: Refresh) -> Self {
/// Return the maximum of two timestamps.
fn max(a: Timestamp, b: Timestamp) -> Timestamp {
if a > b {
a
} else {
b
}
}

match (self, other) {
// If the policy is `None`, return the existing refresh policy.
// Take the `max` of the two timestamps.
(Self::None(t1), Refresh::None(t2)) => Refresh::None(max(t1, t2)),
(Self::None(t1), Refresh::All(t2)) => Refresh::All(max(t1, t2)),
(Self::None(t1), Refresh::Packages(packages, t2)) => {
Refresh::Packages(packages, max(t1, t2))
}

// If the policy is `All`, refresh all packages.
(Self::All(t1), Refresh::None(t2)) => Refresh::All(max(t1, t2)),
(Self::All(t1), Refresh::All(t2)) => Refresh::All(max(t1, t2)),
(Self::All(t1), Refresh::Packages(_packages, t2)) => Refresh::All(max(t1, t2)),

// If the policy is `Packages`, take the "max" of the two policies.
(Self::Packages(packages, t1), Refresh::None(t2)) => {
Refresh::Packages(packages, max(t1, t2))
}
(Self::Packages(_packages, t1), Refresh::All(t2)) => Refresh::All(max(t1, t2)),
(Self::Packages(packages1, t1), Refresh::Packages(packages2, t2)) => {
Refresh::Packages(packages1.into_iter().chain(packages2).collect(), max(t1, t2))
}
}
}
}
44 changes: 20 additions & 24 deletions crates/uv-configuration/src/package_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pep508_rs::PackageName;

use pypi_types::Requirement;
use rustc_hash::FxHashMap;
use uv_cache::Refresh;
use uv_cache::{Refresh, Timestamp};

/// Whether to reinstall packages.
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -44,30 +44,15 @@ impl Reinstall {
pub fn is_all(&self) -> bool {
matches!(self, Self::All)
}
}

/// Create a [`Refresh`] policy by integrating the [`Reinstall`] policy.
pub fn to_refresh(self, refresh: Refresh) -> Refresh {
match (self, refresh) {
// If the policy is `None`, return the existing refresh policy.
(Self::None, Refresh::None(timestamp)) => Refresh::None(timestamp),
(Self::None, Refresh::All(timestamp)) => Refresh::All(timestamp),
(Self::None, Refresh::Packages(packages, timestamp)) => {
Refresh::Packages(packages, timestamp)
}

// If the policy is `All`, refresh all packages.
(Self::All, Refresh::None(timestamp)) => Refresh::All(timestamp),
(Self::All, Refresh::All(timestamp)) => Refresh::All(timestamp),
(Self::All, Refresh::Packages(_packages, timestamp)) => Refresh::All(timestamp),

// If the policy is `Packages`, take the "max" of the two policies.
(Self::Packages(packages), Refresh::None(timestamp)) => {
Refresh::Packages(packages, timestamp)
}
(Self::Packages(_packages), Refresh::All(timestamp)) => Refresh::All(timestamp),
(Self::Packages(packages1), Refresh::Packages(packages2, timestamp)) => {
Refresh::Packages(packages1.into_iter().chain(packages2).collect(), timestamp)
}
/// 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) => Self::Packages(packages, Timestamp::now()),
}
}
}
Expand Down Expand Up @@ -144,3 +129,14 @@ impl Upgrade {
}
}
}

/// Create a [`Refresh`] policy by integrating the [`Upgrade`] policy.
impl From<Upgrade> for Refresh {
fn from(value: Upgrade) -> Self {
match value {
Upgrade::None => Self::None(Timestamp::now()),
Upgrade::All => Self::All(Timestamp::now()),
Upgrade::Packages(packages) => Self::Packages(packages.into_keys().collect::<Vec<_>>(), Timestamp::now()),
}
}
}
20 changes: 10 additions & 10 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use owo_colors::OwoColorize;
use tracing::{debug, instrument};

use settings::PipTreeSettings;
use uv_cache::Cache;
use uv_cache::{Cache, Refresh};
use uv_cli::{
compat::CompatArgs, CacheCommand, CacheNamespace, Cli, Commands, PipCommand, PipNamespace,
ProjectCommand,
Expand Down Expand Up @@ -226,7 +226,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

let requirements = args
.src_file
Expand Down Expand Up @@ -319,7 +319,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

let requirements = args
.src_file
Expand Down Expand Up @@ -391,7 +391,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

let requirements = args
.package
Expand Down Expand Up @@ -707,7 +707,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

let requirements = args
.with
Expand Down Expand Up @@ -750,7 +750,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

let requirements = args
.with
Expand Down Expand Up @@ -998,7 +998,7 @@ async fn run_project(
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

let requirements = args
.with
Expand Down Expand Up @@ -1043,7 +1043,7 @@ async fn run_project(
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

commands::sync(
args.locked,
Expand Down Expand Up @@ -1097,7 +1097,7 @@ async fn run_project(
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

commands::add(
args.locked,
Expand Down Expand Up @@ -1133,7 +1133,7 @@ async fn run_project(
// Initialize the cache.
let cache = cache
.init()?
.with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh));
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));

commands::remove(
args.locked,
Expand Down

0 comments on commit ebeb9dd

Please sign in to comment.