Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colocate Python install cache with destination directory #6043

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions crates/uv-python/src/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use tokio::io::{AsyncRead, ReadBuf};
use tokio_util::compat::FuturesAsyncReadCompatExt;
use tracing::{debug, instrument};
use url::Url;
use uv_cache::Cache;
use uv_client::WrappedReqwestError;
use uv_extract::hash::Hasher;
use uv_fs::{rename_with_retry, Simplified};
Expand Down Expand Up @@ -408,16 +407,16 @@ impl ManagedPythonDownload {
}

/// Download and extract
#[instrument(skip(client, parent_path, cache, reporter), fields(download = % self.key()))]
#[instrument(skip(client, installation_dir, cache_dir, reporter), fields(download = % self.key()))]
pub async fn fetch(
&self,
client: &uv_client::BaseClient,
parent_path: &Path,
cache: &Cache,
installation_dir: &Path,
cache_dir: &Path,
reporter: Option<&dyn Reporter>,
) -> Result<DownloadResult, Error> {
let url = self.download_url()?;
let path = parent_path.join(self.key().to_string());
let path = installation_dir.join(self.key().to_string());

// If it already exists, return it
if path.is_dir() {
Expand All @@ -438,7 +437,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(cache.root()).map_err(Error::DownloadDirError)?;
let temp_dir = tempfile::tempdir_in(cache_dir).map_err(Error::DownloadDirError)?;

debug!(
"Downloading {url} to temporary location: {}",
Expand Down
7 changes: 4 additions & 3 deletions crates/uv-python/src/installation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fmt;
use std::str::FromStr;

use pep440_rs::Version;
use tracing::{debug, info};
use uv_client::BaseClientBuilder;

use pep440_rs::Version;
use uv_cache::Cache;
use uv_client::BaseClientBuilder;

use crate::discovery::{
find_best_python_installation, find_python_installation, EnvironmentPreference, PythonRequest,
Expand Down Expand Up @@ -123,14 +123,15 @@ impl PythonInstallation {
) -> Result<Self, Error> {
let installations = ManagedPythonInstallations::from_settings()?.init()?;
let installations_dir = installations.root();
let cache_dir = installations.cache();
let _lock = installations.acquire_lock()?;

let download = ManagedPythonDownload::from_request(&request)?;
let client = client_builder.build();

info!("Fetching requested Python...");
let result = download
.fetch(&client, installations_dir, cache, reporter)
.fetch(&client, installations_dir, &cache_dir, reporter)
.await?;

let path = match result {
Expand Down
11 changes: 11 additions & 0 deletions crates/uv-python/src/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ impl ManagedPythonInstallations {
))
}

/// Return the location of the cache directory for managed Python installations.
pub fn cache(&self) -> PathBuf {
self.root.join(".cache")
}

/// Initialize the Python installation directory.
///
/// Ensures the directory is created.
Expand All @@ -119,6 +124,10 @@ impl ManagedPythonInstallations {
// Create the directory, if it doesn't exist.
fs::create_dir_all(root)?;

// Create the cache directory, if it doesn't exist.
let cache = self.cache();
fs::create_dir_all(&cache)?;

// Add a .gitignore.
match fs::OpenOptions::new()
.write(true)
Expand Down Expand Up @@ -166,8 +175,10 @@ impl ManagedPythonInstallations {
})
}
};
let cache = self.cache();
Ok(dirs
.into_iter()
.filter(|path| *path != cache)
.filter_map(|path| {
ManagedPythonInstallation::new(path)
.inspect_err(|err| {
Expand Down
13 changes: 7 additions & 6 deletions crates/uv/src/commands/python/install.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::collections::BTreeSet;
use std::fmt::Write;
use std::path::PathBuf;

use anyhow::Result;
use fs_err as fs;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use itertools::Itertools;
use owo_colors::OwoColorize;
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;
Expand All @@ -34,7 +35,6 @@ pub(crate) async fn install(
connectivity: Connectivity,
preview: PreviewMode,
no_config: bool,
cache: &Cache,
printer: Printer,
) -> Result<ExitStatus> {
if preview.is_disabled() {
Expand All @@ -45,6 +45,7 @@ pub(crate) async fn install(

let installations = ManagedPythonInstallations::from_settings()?.init()?;
let installations_dir = installations.root();
let cache_dir = installations.cache();
let _lock = installations.acquire_lock()?;

let targets = targets.into_iter().collect::<BTreeSet<_>>();
Expand Down Expand Up @@ -161,7 +162,7 @@ pub(crate) async fn install(
(
download.key(),
download
.fetch(&client, installations_dir, cache, Some(&reporter))
.fetch(&client, installations_dir, &cache_dir, Some(&reporter))
.await,
)
});
Expand Down
4 changes: 0 additions & 4 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,6 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
let args = settings::PythonInstallSettings::resolve(args, filesystem);
show_settings!(args);

// Initialize the cache.
let cache = cache.init()?;

commands::python_install(
args.targets,
args.reinstall,
Expand All @@ -921,7 +918,6 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
globals.connectivity,
globals.preview,
cli.no_config,
&cache,
printer,
)
.await
Expand Down
Loading