From 812b9cf841d20342a101ed63bbe7ca45764d8ae0 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 24 Feb 2024 19:25:56 -0500 Subject: [PATCH] Expand scope of archive timestamping --- crates/uv-cache/src/lib.rs | 40 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/uv-cache/src/lib.rs b/crates/uv-cache/src/lib.rs index fb48a13a2304..6a371d5cdf88 100644 --- a/crates/uv-cache/src/lib.rs +++ b/crates/uv-cache/src/lib.rs @@ -1,3 +1,4 @@ +use std::cmp::max; use std::fmt::{Display, Formatter}; use std::io; use std::io::Write; @@ -638,33 +639,48 @@ impl ArchiveTimestamp { /// Return the modification timestamp for an archive, which could be a file (like a wheel or a zip /// archive) or a directory containing a Python package. /// - /// If the path is to a directory with no entrypoint (i.e., no `pyproject.toml` or `setup.py`), - /// returns `None`. + /// If the path is to a directory with no entrypoint (i.e., no `pyproject.toml`, `setup.py`, or + /// `setup.cfg`), returns `None`. pub fn from_path(path: impl AsRef) -> Result, io::Error> { let metadata = fs_err::metadata(path.as_ref())?; if metadata.is_file() { Ok(Some(Self::Exact(Timestamp::from_metadata(&metadata)))) } else { - // TODO(charlie): Take the maximum of `pyproject.toml`, `setup.py`, and `setup.cfg`. - if let Some(metadata) = path + // Compute the modification timestamp for the `pyproject.toml`, `setup.py`, and + // `setup.cfg` files, if they exist. + let pyproject_toml = path .as_ref() .join("pyproject.toml") .metadata() .ok() .filter(std::fs::Metadata::is_file) - { - Ok(Some(Self::Approximate(Timestamp::from_metadata(&metadata)))) - } else if let Some(metadata) = path + .as_ref() + .map(Timestamp::from_metadata); + + let setup_py = path .as_ref() .join("setup.py") .metadata() .ok() .filter(std::fs::Metadata::is_file) - { - Ok(Some(Self::Approximate(Timestamp::from_metadata(&metadata)))) - } else { - Ok(None) - } + .as_ref() + .map(Timestamp::from_metadata); + + let setup_cfg = path + .as_ref() + .join("setup.cfg") + .metadata() + .ok() + .filter(std::fs::Metadata::is_file) + .as_ref() + .map(Timestamp::from_metadata); + + // Take the most recent timestamp of the three files. + let Some(timestamp) = max(pyproject_toml, max(setup_py, setup_cfg)) else { + return Ok(None); + }; + + Ok(Some(Self::Approximate(timestamp))) } }