Skip to content

Commit

Permalink
Create temporary environments in dedicated cache bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 10, 2024
1 parent fd52fe7 commit a1a7a02
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/uv-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ distribution-types = { workspace = true }
pep440_rs = { workspace = true }
pep508_rs = { workspace = true }
pypi-types = { workspace = true }
uv-cache = { workspace = true }
uv-configuration = { workspace = true }
uv-fs = { workspace = true }
uv-toolchain = { workspace = true }
uv-types = { workspace = true }
uv-configuration = { workspace = true }
uv-virtualenv = { workspace = true }

anyhow = { workspace = true }
Expand Down
11 changes: 6 additions & 5 deletions crates/uv-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
//!
//! <https://packaging.python.org/en/latest/specifications/source-distribution-format/>

use std::{env, iter};
use std::ffi::OsString;
use std::fmt::{Display, Formatter};
use std::io;
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Output};
use std::rc::Rc;
use std::str::FromStr;
use std::{env, iter};

use fs_err as fs;
use indoc::formatdoc;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use rustc_hash::FxHashMap;
use serde::de::{value, SeqAccess, Visitor};
use serde::{de, Deserialize, Deserializer};
use tempfile::{tempdir_in, TempDir};
use serde::de::{SeqAccess, value, Visitor};
use tempfile::{TempDir, tempdir_in};
use thiserror::Error;
use tokio::process::Command;
use tokio::sync::{Mutex, Semaphore};
Expand All @@ -29,6 +29,7 @@ use distribution_types::Resolution;
use pep440_rs::Version;
use pep508_rs::PackageName;
use pypi_types::{Requirement, VerbatimParsedUrl};
use uv_cache::CacheBucket;
use uv_configuration::{BuildKind, ConfigSettings, SetupPyStrategy};
use uv_fs::{PythonExt, Simplified};
use uv_toolchain::{Interpreter, PythonEnvironment};
Expand Down Expand Up @@ -413,7 +414,7 @@ impl SourceBuild {
mut environment_variables: FxHashMap<OsString, OsString>,
concurrent_builds: usize,
) -> Result<Self, Error> {
let temp_dir = tempdir_in(build_context.cache().root())?;
let temp_dir = tempdir_in(build_context.cache().bucket(CacheBucket::Environments))?;

let source_tree = if let Some(subdir) = subdirectory {
source.join(subdir)
Expand All @@ -431,7 +432,7 @@ impl SourceBuild {
// Create a virtual environment, or install into the shared environment if requested.
let venv = match build_isolation {
BuildIsolation::Isolated => uv_virtualenv::create_venv(
&temp_dir.path().join(".venv"),
temp_dir.path(),
interpreter.clone(),
uv_virtualenv::Prompt::None,
false,
Expand Down
23 changes: 15 additions & 8 deletions crates/uv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ pub enum CacheBucket {
/// that cache entries can be atomically replaced and removed, as storing directories in the
/// other buckets directly would make atomic operations impossible.
Archive,
/// Ephemeral virtual environments used to execute PEP 517 builds and other operations.
Environments,
}

impl CacheBucket {
Expand All @@ -640,6 +642,7 @@ impl CacheBucket {
Self::Simple => "simple-v8",
Self::Wheels => "wheels-v1",
Self::Archive => "archive-v0",
Self::Environments => "environments-v0",
}
}

Expand Down Expand Up @@ -750,20 +753,24 @@ impl CacheBucket {
Self::Archive => {
// Nothing to do.
}
Self::Environments => {
// Nothing to do.
}
}
Ok(summary)
}

/// Return an iterator over all cache buckets.
pub fn iter() -> impl Iterator<Item = CacheBucket> {
pub fn iter() -> impl Iterator<Item = Self> {
[
CacheBucket::Wheels,
CacheBucket::BuiltWheels,
CacheBucket::FlatIndex,
CacheBucket::Git,
CacheBucket::Interpreter,
CacheBucket::Simple,
CacheBucket::Archive,
Self::Wheels,
Self::BuiltWheels,
Self::FlatIndex,
Self::Git,
Self::Interpreter,
Self::Simple,
Self::Archive,
Self::Environments,
]
.iter()
.copied()
Expand Down
11 changes: 4 additions & 7 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tempfile::tempdir_in;
use tokio::process::Command;
use tracing::debug;

use uv_cache::Cache;
use uv_cache::{Cache, CacheBucket};
use uv_client::{BaseClientBuilder, Connectivity};
use uv_configuration::{ExtrasSpecification, PreviewMode, Upgrade};
use uv_distribution::{ProjectWorkspace, Workspace};
Expand Down Expand Up @@ -107,7 +107,7 @@ pub(crate) async fn run(
};

// If necessary, create an environment for the ephemeral requirements.
let tmpdir;
let temp_dir;
let ephemeral_env = if requirements.is_empty() {
None
} else {
Expand Down Expand Up @@ -135,12 +135,9 @@ pub(crate) async fn run(
// environment.

// Create a virtual environment
// TODO(zanieb): Move this path derivation elsewhere
let uv_state_path = std::env::current_dir()?.join(".uv");
fs_err::create_dir_all(&uv_state_path)?;
tmpdir = tempdir_in(uv_state_path)?;
temp_dir = tempdir_in(cache.bucket(CacheBucket::Environments))?;
let venv = uv_virtualenv::create_venv(
tmpdir.path(),
temp_dir.path(),
interpreter,
uv_virtualenv::Prompt::None,
false,
Expand Down
11 changes: 4 additions & 7 deletions crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tempfile::tempdir_in;
use tokio::process::Command;
use tracing::debug;

use uv_cache::Cache;
use uv_cache::{Cache, CacheBucket};
use uv_client::Connectivity;
use uv_configuration::PreviewMode;
use uv_requirements::RequirementsSource;
Expand Down Expand Up @@ -61,13 +61,10 @@ pub(crate) async fn run(
)?
.into_interpreter();

// Create a virtual environment1
// TODO(zanieb): Move this path derivation elsewhere
let uv_state_path = std::env::current_dir()?.join(".uv");
fs_err::create_dir_all(&uv_state_path)?;
let tmpdir = tempdir_in(uv_state_path)?;
// Create a virtual environment.
let temp_dir = tempdir_in(cache.bucket(CacheBucket::Environments))?;
let venv = uv_virtualenv::create_venv(
tmpdir.path(),
temp_dir.path(),
interpreter,
uv_virtualenv::Prompt::None,
false,
Expand Down

0 comments on commit a1a7a02

Please sign in to comment.