mirror of
https://github.com/astral-sh/uv.git
synced 2025-11-25 21:37:51 +00:00
Move ExcludeNewer into its own type (#3041)
## Summary This makes it easier to add (e.g.) JSON Schema derivations to the type. If we have support for other dates in the future, we can generalize it to a `UserDate` or similar.
This commit is contained in:
parent
37a43f4b48
commit
1f626bfc73
12 changed files with 96 additions and 61 deletions
53
crates/uv-resolver/src/exclude_newer.rs
Normal file
53
crates/uv-resolver/src/exclude_newer.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use chrono::{DateTime, Days, NaiveDate, NaiveTime, Utc};
|
||||
|
||||
/// A timestamp that excludes files newer than it.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct ExcludeNewer(DateTime<Utc>);
|
||||
|
||||
impl ExcludeNewer {
|
||||
/// Returns the timestamp in milliseconds.
|
||||
pub fn timestamp_millis(&self) -> i64 {
|
||||
self.0.timestamp_millis()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DateTime<Utc>> for ExcludeNewer {
|
||||
fn from(datetime: DateTime<Utc>) -> Self {
|
||||
Self(datetime)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ExcludeNewer {
|
||||
type Err = String;
|
||||
|
||||
/// Parse an [`ExcludeNewer`] from a string.
|
||||
///
|
||||
/// Accepts both RFC 3339 timestamps (e.g., `2006-12-02T02:07:43Z`) and UTC dates in the same
|
||||
/// format (e.g., `2006-12-02`).
|
||||
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
||||
let date_err = match NaiveDate::from_str(input) {
|
||||
Ok(date) => {
|
||||
// Midnight that day is 00:00:00 the next day
|
||||
return Ok(Self(
|
||||
(date + Days::new(1)).and_time(NaiveTime::MIN).and_utc(),
|
||||
));
|
||||
}
|
||||
Err(err) => err,
|
||||
};
|
||||
let datetime_err = match DateTime::parse_from_rfc3339(input) {
|
||||
Ok(datetime) => return Ok(Self(datetime.with_timezone(&Utc))),
|
||||
Err(err) => err,
|
||||
};
|
||||
Err(format!(
|
||||
"`{input}` is neither a valid date ({date_err}) nor a valid datetime ({datetime_err})"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ExcludeNewer {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
pub use dependency_mode::DependencyMode;
|
||||
pub use error::ResolveError;
|
||||
pub use exclude_newer::ExcludeNewer;
|
||||
pub use exclusions::Exclusions;
|
||||
pub use flat_index::FlatIndex;
|
||||
pub use manifest::Manifest;
|
||||
|
|
@ -24,6 +25,7 @@ mod dependency_mode;
|
|||
mod dependency_provider;
|
||||
mod editables;
|
||||
mod error;
|
||||
mod exclude_newer;
|
||||
mod exclusions;
|
||||
mod flat_index;
|
||||
mod manifest;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::{DependencyMode, PreReleaseMode, ResolutionMode};
|
||||
use crate::{DependencyMode, ExcludeNewer, PreReleaseMode, ResolutionMode};
|
||||
|
||||
/// Options for resolving a manifest.
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
|
|
@ -8,7 +6,7 @@ pub struct Options {
|
|||
pub resolution_mode: ResolutionMode,
|
||||
pub prerelease_mode: PreReleaseMode,
|
||||
pub dependency_mode: DependencyMode,
|
||||
pub exclude_newer: Option<DateTime<Utc>>,
|
||||
pub exclude_newer: Option<ExcludeNewer>,
|
||||
}
|
||||
|
||||
/// Builder for [`Options`].
|
||||
|
|
@ -17,7 +15,7 @@ pub struct OptionsBuilder {
|
|||
resolution_mode: ResolutionMode,
|
||||
prerelease_mode: PreReleaseMode,
|
||||
dependency_mode: DependencyMode,
|
||||
exclude_newer: Option<DateTime<Utc>>,
|
||||
exclude_newer: Option<ExcludeNewer>,
|
||||
}
|
||||
|
||||
impl OptionsBuilder {
|
||||
|
|
@ -49,7 +47,7 @@ impl OptionsBuilder {
|
|||
|
||||
/// Sets the exclusion date.
|
||||
#[must_use]
|
||||
pub fn exclude_newer(mut self, exclude_newer: Option<DateTime<Utc>>) -> Self {
|
||||
pub fn exclude_newer(mut self, exclude_newer: Option<ExcludeNewer>) -> Self {
|
||||
self.exclude_newer = exclude_newer;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
use std::future::Future;
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use distribution_types::{Dist, IndexLocations};
|
||||
use platform_tags::Tags;
|
||||
|
||||
use uv_client::RegistryClient;
|
||||
use uv_configuration::{NoBinary, NoBuild};
|
||||
use uv_distribution::{ArchiveMetadata, DistributionDatabase};
|
||||
|
|
@ -16,6 +14,7 @@ use crate::flat_index::FlatIndex;
|
|||
use crate::python_requirement::PythonRequirement;
|
||||
use crate::version_map::VersionMap;
|
||||
use crate::yanks::AllowedYanks;
|
||||
use crate::ExcludeNewer;
|
||||
|
||||
pub type PackageVersionsResult = Result<VersionsResponse, uv_client::Error>;
|
||||
pub type WheelMetadataResult = Result<MetadataResponse, uv_distribution::Error>;
|
||||
|
|
@ -84,7 +83,7 @@ pub struct DefaultResolverProvider<'a, Context: BuildContext + Send + Sync> {
|
|||
python_requirement: PythonRequirement,
|
||||
allowed_yanks: AllowedYanks,
|
||||
hasher: HashStrategy,
|
||||
exclude_newer: Option<DateTime<Utc>>,
|
||||
exclude_newer: Option<ExcludeNewer>,
|
||||
no_binary: NoBinary,
|
||||
no_build: NoBuild,
|
||||
}
|
||||
|
|
@ -100,7 +99,7 @@ impl<'a, Context: BuildContext + Send + Sync> DefaultResolverProvider<'a, Contex
|
|||
python_requirement: PythonRequirement,
|
||||
allowed_yanks: AllowedYanks,
|
||||
hasher: &'a HashStrategy,
|
||||
exclude_newer: Option<DateTime<Utc>>,
|
||||
exclude_newer: Option<ExcludeNewer>,
|
||||
no_binary: &'a NoBinary,
|
||||
no_build: &'a NoBuild,
|
||||
) -> Self {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use std::collections::btree_map::{BTreeMap, Entry};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use rkyv::{de::deserializers::SharedDeserializeMap, Deserialize};
|
||||
use rustc_hash::FxHashSet;
|
||||
use tracing::instrument;
|
||||
|
|
@ -21,7 +20,7 @@ use uv_types::HashStrategy;
|
|||
use uv_warnings::warn_user_once;
|
||||
|
||||
use crate::flat_index::FlatDistributions;
|
||||
use crate::{python_requirement::PythonRequirement, yanks::AllowedYanks};
|
||||
use crate::{python_requirement::PythonRequirement, yanks::AllowedYanks, ExcludeNewer};
|
||||
|
||||
/// A map from versions to distributions.
|
||||
#[derive(Debug)]
|
||||
|
|
@ -49,7 +48,7 @@ impl VersionMap {
|
|||
python_requirement: &PythonRequirement,
|
||||
allowed_yanks: &AllowedYanks,
|
||||
hasher: &HashStrategy,
|
||||
exclude_newer: Option<&DateTime<Utc>>,
|
||||
exclude_newer: Option<&ExcludeNewer>,
|
||||
flat_index: Option<FlatDistributions>,
|
||||
no_binary: &NoBinary,
|
||||
no_build: &NoBuild,
|
||||
|
|
@ -304,7 +303,7 @@ struct VersionMapLazy {
|
|||
/// exists) is satisfied or not.
|
||||
python_requirement: PythonRequirement,
|
||||
/// Whether files newer than this timestamp should be excluded or not.
|
||||
exclude_newer: Option<DateTime<Utc>>,
|
||||
exclude_newer: Option<ExcludeNewer>,
|
||||
/// Which yanked versions are allowed
|
||||
allowed_yanks: FxHashSet<Version>,
|
||||
/// The hashes of allowed distributions.
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ use uv_client::RegistryClientBuilder;
|
|||
use uv_configuration::{BuildKind, Constraints, NoBinary, NoBuild, Overrides, SetupPyStrategy};
|
||||
use uv_interpreter::{find_default_python, Interpreter, PythonEnvironment};
|
||||
use uv_resolver::{
|
||||
DisplayResolutionGraph, Exclusions, FlatIndex, InMemoryIndex, Manifest, Options,
|
||||
DisplayResolutionGraph, ExcludeNewer, Exclusions, FlatIndex, InMemoryIndex, Manifest, Options,
|
||||
OptionsBuilder, PreReleaseMode, Preference, ResolutionGraph, ResolutionMode, Resolver,
|
||||
};
|
||||
use uv_types::{
|
||||
|
|
@ -26,10 +26,12 @@ use uv_types::{
|
|||
};
|
||||
|
||||
// Exclude any packages uploaded after this date.
|
||||
static EXCLUDE_NEWER: Lazy<DateTime<Utc>> = Lazy::new(|| {
|
||||
DateTime::parse_from_rfc3339("2023-11-18T12:00:00Z")
|
||||
.unwrap()
|
||||
.with_timezone(&Utc)
|
||||
static EXCLUDE_NEWER: Lazy<ExcludeNewer> = Lazy::new(|| {
|
||||
ExcludeNewer::from(
|
||||
DateTime::parse_from_rfc3339("2023-11-18T12:00:00Z")
|
||||
.unwrap()
|
||||
.with_timezone(&Utc),
|
||||
)
|
||||
});
|
||||
|
||||
struct DummyContext {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue