From bb6ac67df3e16fe5b9731c89a932ec421f0cf593 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 4 Aug 2024 22:32:24 -0400 Subject: [PATCH] Use cache for Python install temporary directories (#5787) ## Summary It's fine for this to be in the cache, I think, since we don't necessarily need to colocate it with the Python directory. Closes https://github.com/astral-sh/uv/issues/5747. --- crates/uv-python/src/downloads.rs | 6 ++++-- crates/uv-python/src/installation.rs | 4 +++- crates/uv/src/commands/python/install.rs | 4 +++- crates/uv/src/lib.rs | 4 ++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/uv-python/src/downloads.rs b/crates/uv-python/src/downloads.rs index 86dd495e3..ff4287a26 100644 --- a/crates/uv-python/src/downloads.rs +++ b/crates/uv-python/src/downloads.rs @@ -14,6 +14,7 @@ use tracing::{debug, instrument}; use url::Url; use pypi_types::{HashAlgorithm, HashDigest}; +use uv_cache::Cache; use uv_client::WrappedReqwestError; use uv_extract::hash::Hasher; use uv_fs::{rename_with_retry, Simplified}; @@ -401,11 +402,12 @@ impl ManagedPythonDownload { } /// Download and extract - #[instrument(skip(client, parent_path, reporter), fields(download = % self.key()))] + #[instrument(skip(client, parent_path, cache, reporter), fields(download = % self.key()))] pub async fn fetch( &self, client: &uv_client::BaseClient, parent_path: &Path, + cache: &Cache, reporter: Option<&dyn Reporter>, ) -> Result { let url = Url::parse(self.url)?; @@ -428,7 +430,7 @@ impl ManagedPythonDownload { .map(|reporter| (reporter, reporter.on_download_start(&self.key, size))); // Download and extract into a temporary directory. - let temp_dir = tempfile::tempdir_in(parent_path).map_err(Error::DownloadDirError)?; + let temp_dir = tempfile::tempdir_in(cache.root()).map_err(Error::DownloadDirError)?; debug!( "Downloading {url} to temporary location {}", diff --git a/crates/uv-python/src/installation.rs b/crates/uv-python/src/installation.rs index ef525d3c8..5885c06c0 100644 --- a/crates/uv-python/src/installation.rs +++ b/crates/uv-python/src/installation.rs @@ -129,7 +129,9 @@ impl PythonInstallation { let client = client_builder.build(); info!("Fetching requested Python..."); - let result = download.fetch(&client, installations_dir, reporter).await?; + let result = download + .fetch(&client, installations_dir, cache, reporter) + .await?; let path = match result { DownloadResult::AlreadyAvailable(path) => path, diff --git a/crates/uv/src/commands/python/install.rs b/crates/uv/src/commands/python/install.rs index 12b0ac61d..dcbd15c57 100644 --- a/crates/uv/src/commands/python/install.rs +++ b/crates/uv/src/commands/python/install.rs @@ -8,6 +8,7 @@ use std::collections::BTreeSet; use std::fmt::Write; use std::path::PathBuf; use tracing::debug; +use uv_cache::Cache; use uv_client::Connectivity; use uv_configuration::PreviewMode; use uv_fs::CWD; @@ -31,6 +32,7 @@ pub(crate) async fn install( connectivity: Connectivity, preview: PreviewMode, no_config: bool, + cache: &Cache, printer: Printer, ) -> Result { if preview.is_disabled() { @@ -149,7 +151,7 @@ pub(crate) async fn install( ( download.key(), download - .fetch(&client, installations_dir, Some(&reporter)) + .fetch(&client, installations_dir, cache, Some(&reporter)) .await, ) }); diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index f9f4c7776..14a568616 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -827,6 +827,9 @@ async fn run(cli: Cli) -> Result { let args = settings::PythonInstallSettings::resolve(args, filesystem); show_settings!(args); + // Initialize the cache. + let cache = cache.init()?; + commands::python_install( args.targets, args.reinstall, @@ -834,6 +837,7 @@ async fn run(cli: Cli) -> Result { globals.connectivity, globals.preview, cli.no_config, + &cache, printer, ) .await