Skip to content

Commit

Permalink
Add upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 8, 2024
1 parent ebeb9dd commit db4994c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 80 deletions.
31 changes: 5 additions & 26 deletions crates/uv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,6 @@ impl Cache {
}
}

/// Returns `true` if a cache entry is up-to-date. Unlike [`Cache::freshness`], this method does
/// not take the [`Refresh`] policy into account.
///
/// A cache entry is considered up-to-date if it was created after the [`Cache`] instance itself
/// was initialized.
pub fn is_fresh(&self, entry: &CacheEntry) -> io::Result<bool> {
// Grab the cutoff timestamp.
let timestamp = match &self.refresh {
Refresh::None(timestamp) => timestamp,
Refresh::All(timestamp) => timestamp,
Refresh::Packages(_packages, timestamp) => timestamp,
};

match fs::metadata(entry.path()) {
Ok(metadata) => Ok(Timestamp::from_metadata(&metadata) >= *timestamp),
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(false),
Err(err) => Err(err),
}
}

/// Persist a temporary directory to the artifact store, returning its unique ID.
pub async fn persist(
&self,
Expand Down Expand Up @@ -1004,9 +984,6 @@ impl Freshness {
}

/// A refresh policy for cache entries.
///
/// Each policy stores a timestamp, even if no entries are refreshed, to enable out-of-policy
/// freshness checks via [`Cache::is_fresh`].
#[derive(Debug, Clone)]
pub enum Refresh {
/// Don't refresh any entries.
Expand Down Expand Up @@ -1040,6 +1017,7 @@ impl Refresh {
}

/// Combine two [`Refresh`] policies, taking the "max" of the two policies.
#[must_use]
pub fn combine(self, other: Refresh) -> Self {
/// Return the maximum of two timestamps.
fn max(a: Timestamp, b: Timestamp) -> Timestamp {
Expand Down Expand Up @@ -1069,9 +1047,10 @@ impl Refresh {
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))
}
(Self::Packages(packages1, t1), Refresh::Packages(packages2, t2)) => Refresh::Packages(
packages1.into_iter().chain(packages2).collect(),
max(t1, t2),
),
}
}
}
10 changes: 6 additions & 4 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3104,7 +3104,8 @@ pub struct ResolverArgs {
#[command(flatten)]
pub index_args: IndexArgs,

/// Allow package upgrades, ignoring pinned versions in any existing output file.
/// Allow package upgrades, ignoring pinned versions in any existing output file. Implies
/// `--refresh`.
#[arg(
long,
short = 'U',
Expand All @@ -3122,7 +3123,7 @@ pub struct ResolverArgs {
pub no_upgrade: bool,

/// Allow upgrades for a specific package, ignoring pinned versions in any existing output
/// file.
/// file. Implies `--refresh-package`.
#[arg(long, short = 'P', help_heading = "Resolver options")]
pub upgrade_package: Vec<Requirement<VerbatimParsedUrl>>,

Expand Down Expand Up @@ -3252,7 +3253,8 @@ pub struct ResolverInstallerArgs {
#[command(flatten)]
pub index_args: IndexArgs,

/// Allow package upgrades, ignoring pinned versions in any existing output file.
/// Allow package upgrades, ignoring pinned versions in any existing output file. Implies
/// `--refresh`.
#[arg(
long,
short = 'U',
Expand All @@ -3270,7 +3272,7 @@ pub struct ResolverInstallerArgs {
pub no_upgrade: bool,

/// Allow upgrades for a specific package, ignoring pinned versions in any existing output
/// file.
/// file. Implies `--refresh-package`.
#[arg(long, short = 'P', help_heading = "Resolver options")]
pub upgrade_package: Vec<Requirement<VerbatimParsedUrl>>,

Expand Down
4 changes: 3 additions & 1 deletion crates/uv-configuration/src/package_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ impl From<Upgrade> for Refresh {
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()),
Upgrade::Packages(packages) => {
Self::Packages(packages.into_keys().collect::<Vec<_>>(), Timestamp::now())
}
}
}
}
72 changes: 45 additions & 27 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
.expect("failed to initialize global rayon pool");

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

let requirements = args
.src_file
Expand Down Expand Up @@ -317,9 +319,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
.expect("failed to initialize global rayon pool");

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

let requirements = args
.src_file
Expand Down Expand Up @@ -389,9 +393,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
.expect("failed to initialize global rayon pool");

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

let requirements = args
.package
Expand Down Expand Up @@ -705,9 +711,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
show_settings!(args);

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

let requirements = args
.with
Expand Down Expand Up @@ -748,9 +756,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
show_settings!(args);

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

let requirements = args
.with
Expand Down Expand Up @@ -996,9 +1006,11 @@ async fn run_project(
show_settings!(args);

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

let requirements = args
.with
Expand Down Expand Up @@ -1041,9 +1053,11 @@ async fn run_project(
show_settings!(args);

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

commands::sync(
args.locked,
Expand Down Expand Up @@ -1095,9 +1109,11 @@ async fn run_project(
show_settings!(args);

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

commands::add(
args.locked,
Expand Down Expand Up @@ -1131,9 +1147,11 @@ async fn run_project(
show_settings!(args);

// Initialize the cache.
let cache = cache
.init()?
.with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh));
let cache = cache.init()?.with_refresh(
args.refresh
.combine(Refresh::from(args.settings.reinstall.clone()))
.combine(Refresh::from(args.settings.upgrade.clone())),
);

commands::remove(
args.locked,
Expand Down
Loading

0 comments on commit db4994c

Please sign in to comment.