Skip to content

Commit

Permalink
Disable installation
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 7, 2024
1 parent 53239a6 commit 7afd06c
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 101 deletions.
63 changes: 34 additions & 29 deletions crates/uv-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,19 +417,24 @@ impl SourceBuild {
BuildIsolation::Shared(venv) => venv.clone(),
};

// Setup the build environment.
let resolved_requirements = Self::get_resolved_requirements(
build_context,
source_build_context,
&default_backend,
pep517_backend.as_ref(),
)
.await?;
// Setup the build environment. If build isolation is disabled, we assume the build
// environment is already setup.
if build_isolation.is_isolated() {
let resolved_requirements = Self::get_resolved_requirements(
build_context,
source_build_context,
&default_backend,
pep517_backend.as_ref(),
)
.await?;

build_context
.install(&resolved_requirements, &venv)
.await
.map_err(|err| Error::RequirementsInstall("build-system.requires (install)", err))?;
build_context
.install(&resolved_requirements, &venv)
.await
.map_err(|err| {
Error::RequirementsInstall("build-system.requires (install)", err)
})?;
}

// Figure out what the modified path should be
// Remove the PATH variable from the environment variables if it's there
Expand Down Expand Up @@ -461,19 +466,23 @@ impl SourceBuild {
OsString::from(venv.scripts())
};

if let Some(pep517_backend) = &pep517_backend {
create_pep517_build_environment(
&source_tree,
&venv,
pep517_backend,
build_context,
&package_id,
build_kind,
&config_settings,
&environment_variables,
&modified_path,
)
.await?;
// Create the PEP 517 build environment. If build isolation is disabled, we assume the build
// environment is already setup.
if build_isolation.is_isolated() {
if let Some(pep517_backend) = &pep517_backend {
create_pep517_build_environment(
&source_tree,
&venv,
pep517_backend,
build_context,
&package_id,
build_kind,
&config_settings,
&environment_variables,
&modified_path,
)
.await?;
}
}

Ok(Self {
Expand Down Expand Up @@ -925,10 +934,6 @@ async fn run_python_script(
environment_variables: &FxHashMap<OsString, OsString>,
modified_path: &OsString,
) -> Result<Output, Error> {
println!(
"Running python script: {}",
venv.python_executable().display()
);
Command::new(venv.python_executable())
.args(["-c", script])
.current_dir(source_tree.simplified())
Expand Down
25 changes: 2 additions & 23 deletions crates/uv-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use std::ffi::OsStr;
use std::path::Path;
use std::sync::Arc;
use std::{ffi::OsString, future::Future};

use anyhow::{bail, Context, Result};
use futures::FutureExt;
use itertools::Itertools;
use rustc_hash::FxHashMap;
use tokio::sync::{Mutex, MutexGuard};
use tracing::{debug, instrument};

use distribution_types::{IndexLocations, Name, Resolution, SourceDist};
Expand Down Expand Up @@ -44,7 +42,6 @@ pub struct BuildDispatch<'a> {
source_build_context: SourceBuildContext,
options: Options,
build_extra_env_vars: FxHashMap<OsString, OsString>,
mutex: Arc<Mutex<()>>,
}

impl<'a> BuildDispatch<'a> {
Expand Down Expand Up @@ -79,7 +76,6 @@ impl<'a> BuildDispatch<'a> {
source_build_context: SourceBuildContext::default(),
options: Options::default(),
build_extra_env_vars: FxHashMap::default(),
mutex: Arc::new(Mutex::new(())),
}
}

Expand All @@ -89,12 +85,6 @@ impl<'a> BuildDispatch<'a> {
self
}

#[must_use]
pub fn with_build_isolation(mut self, build_isolation: BuildIsolation<'a>) -> Self {
self.build_isolation = build_isolation;
self
}

/// Set the environment variables to be used when building a source distribution.
#[must_use]
pub fn with_build_extra_env_vars<I, K, V>(mut self, sdist_build_env_variables: I) -> Self
Expand All @@ -114,17 +104,6 @@ impl<'a> BuildDispatch<'a> {
impl<'a> BuildContext for BuildDispatch<'a> {
type SourceDistBuilder = SourceBuild;

fn mutex(&self) -> Arc<Mutex<()>> {
self.mutex.clone()
}

fn lock(&self) -> impl Future<Output = Option<MutexGuard<'_, ()>>> + Send {
async move {
let guard = self.mutex.lock().await;
Some(guard)
}
}

fn cache(&self) -> &Cache {
self.cache
}
Expand Down Expand Up @@ -165,7 +144,7 @@ impl<'a> BuildContext for BuildDispatch<'a> {
self.client,
self.flat_index,
self.index,
self
self,
)?;
let graph = resolver.resolve().await.with_context(|| {
format!(
Expand Down Expand Up @@ -258,7 +237,7 @@ impl<'a> BuildContext for BuildDispatch<'a> {

// Remove any unnecessary packages.
if !reinstalls.is_empty() {
for dist_info in reinstalls.iter() {
for dist_info in &reinstalls {
let summary = uv_installer::uninstall(dist_info)
.await
.context("Failed to uninstall build dependencies")?;
Expand Down
17 changes: 0 additions & 17 deletions crates/uv-distribution/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,11 +857,6 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> {
return Err(Error::NoBuild);
}

// Lock the build context.
println!("Locking build context");
let mutex = self.build_context.mutex();
let lock = mutex.lock().await;

// Build the wheel.
fs::create_dir_all(&cache_shard)
.await
Expand All @@ -881,8 +876,6 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> {
.await
.map_err(|err| Error::Build(dist.to_string(), err))?;

drop(lock);

// Read the metadata from the wheel.
let filename = WheelFilename::from_str(&disk_filename)?;
let metadata = read_wheel_metadata(&filename, cache_shard.join(&disk_filename))?;
Expand All @@ -909,16 +902,6 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> {
) -> Result<Option<Metadata21>, Error> {
debug!("Preparing metadata for: {dist}");

// Lock the build context.
// let mutex;
// let _lock;
// if self.build_context.build_isolation().is_shared() {
// mutex = self.build_context.mutex();
// _lock = mutex.lock().await;
// }

// let _lock = self.build_context.lock().await;

// Setup the builder.
let mut builder = self
.build_context
Expand Down
15 changes: 2 additions & 13 deletions crates/uv-interpreter/src/python_environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::env;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use tracing::debug;

Expand All @@ -16,7 +15,6 @@ use crate::{find_default_python, find_requested_python, Error, Interpreter};
pub struct PythonEnvironment {
root: PathBuf,
interpreter: Interpreter,
lock: Arc<Mutex<()>>,
}

impl PythonEnvironment {
Expand All @@ -39,7 +37,6 @@ impl PythonEnvironment {
Ok(Self {
root: venv,
interpreter,
lock: Arc::new(Mutex::default()),
})
}

Expand All @@ -55,7 +52,6 @@ impl PythonEnvironment {
Ok(Self {
root: interpreter.prefix().to_path_buf(),
interpreter,
lock: Arc::new(Mutex::default()),
})
}

Expand All @@ -65,16 +61,14 @@ impl PythonEnvironment {
Ok(Self {
root: interpreter.prefix().to_path_buf(),
interpreter,
lock: Arc::new(Mutex::default()),
})
}

/// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and root directory.
pub fn from_interpreter(interpreter: Interpreter, root: PathBuf) -> Self {
pub fn from_interpreter(interpreter: Interpreter) -> Self {
Self {
root,
root: interpreter.prefix().to_path_buf(),
interpreter,
lock: Arc::new(Mutex::default()),
}
}

Expand Down Expand Up @@ -109,11 +103,6 @@ impl PythonEnvironment {
self.interpreter.scripts()
}

/// Grab an in-memory lock for the virtual environment to prevent concurrent writes across threads.
pub fn acquire(&self) -> Arc<Mutex<()>> {
self.lock.clone()
}

/// Grab a file lock for the virtual environment to prevent concurrent writes across processes.
pub fn lock(&self) -> Result<LockedFile, std::io::Error> {
if self.interpreter.is_virtualenv() {
Expand Down
8 changes: 7 additions & 1 deletion crates/uv-resolver/tests/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use uv_resolver::{
DisplayResolutionGraph, InMemoryIndex, Manifest, Options, OptionsBuilder, PreReleaseMode,
ResolutionGraph, ResolutionMode, Resolver,
};
use uv_traits::{BuildContext, BuildKind, NoBinary, NoBuild, SetupPyStrategy, SourceBuildTrait};
use uv_traits::{
BuildContext, BuildIsolation, BuildKind, NoBinary, NoBuild, SetupPyStrategy, SourceBuildTrait,
};

// Exclude any packages uploaded after this date.
static EXCLUDE_NEWER: Lazy<DateTime<Utc>> = Lazy::new(|| {
Expand Down Expand Up @@ -57,6 +59,10 @@ impl BuildContext for DummyContext {
&self.interpreter
}

fn build_isolation(&self) -> BuildIsolation {
BuildIsolation::Isolated
}

fn no_build(&self) -> &NoBuild {
&NoBuild::None
}
Expand Down
14 changes: 3 additions & 11 deletions crates/uv-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use std::fmt::{Display, Formatter};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

use anyhow::Result;
use tokio::sync::{Mutex, MutexGuard};

use distribution_types::{CachedDist, DistributionId, IndexLocations, Resolution, SourceDist};
use once_map::OnceMap;
Expand Down Expand Up @@ -56,15 +54,9 @@ use uv_normalize::PackageName;
/// `uv-build` to depend on `uv-resolver` which having actual crate dependencies between
/// them.

// TODO(konstin): Proper error types
pub trait BuildContext: Sync {
type SourceDistBuilder: SourceBuildTrait + Send + Sync;

/// Return a mutex that can be used to lock the build context.
fn mutex(&self) -> Arc<Mutex<()>>;

fn lock(&self) -> impl Future<Output = Option<MutexGuard<'_, ()>>> + Send;

/// Return a reference to the cache.
fn cache(&self) -> &Cache;

Expand Down Expand Up @@ -156,9 +148,9 @@ pub enum BuildIsolation<'a> {
}

impl<'a> BuildIsolation<'a> {
/// Returns `true` if build isolation is not enforced.
pub fn is_shared(&self) -> bool {
matches!(self, Self::Shared(_))
/// Returns `true` if build isolation is enforced.
pub fn is_isolated(&self) -> bool {
matches!(self, Self::Isolated)
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/uv-virtualenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,5 @@ pub fn create_venv(

// Create the corresponding `PythonEnvironment`.
let interpreter = interpreter.with_virtualenv(virtualenv);
let root = interpreter.prefix().to_path_buf();
Ok(PythonEnvironment::from_interpreter(interpreter, root))
Ok(PythonEnvironment::from_interpreter(interpreter))
}
15 changes: 13 additions & 2 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use uv_client::{Connectivity, FlatIndex, FlatIndexClient, RegistryClientBuilder}
use uv_dispatch::BuildDispatch;
use uv_fs::Simplified;
use uv_installer::{Downloader, NoBinary};
use uv_interpreter::{Interpreter, PythonVersion};
use uv_interpreter::{Interpreter, PythonEnvironment, PythonVersion};
use uv_normalize::{ExtraName, PackageName};
use uv_resolver::{
AnnotationStyle, DependencyMode, DisplayResolutionGraph, InMemoryIndex, Manifest,
Expand Down Expand Up @@ -62,6 +62,7 @@ pub(crate) async fn pip_compile(
setup_py: SetupPyStrategy,
config_settings: ConfigSettings,
connectivity: Connectivity,
no_build_isolation: bool,
no_build: &NoBuild,
python_version: Option<PythonVersion>,
exclude_newer: Option<DateTime<Utc>>,
Expand Down Expand Up @@ -137,6 +138,7 @@ pub(crate) async fn pip_compile(
interpreter.python_version(),
interpreter.sys_executable().simplified_display().cyan()
);

if let Some(python_version) = python_version.as_ref() {
// If the requested version does not match the version we're using warn the user
// _unless_ they have not specified a patch version and that is the only difference
Expand Down Expand Up @@ -203,6 +205,15 @@ pub(crate) async fn pip_compile(
// Track in-flight downloads, builds, etc., across resolutions.
let in_flight = InFlight::default();

// Determine whether to enable build isolation.
let venv;
let build_isolation = if no_build_isolation {
venv = PythonEnvironment::from_interpreter(interpreter.clone());
BuildIsolation::Shared(&venv)
} else {
BuildIsolation::Isolated
};

let build_dispatch = BuildDispatch::new(
&client,
&cache,
Expand All @@ -213,7 +224,7 @@ pub(crate) async fn pip_compile(
&in_flight,
setup_py,
&config_settings,
BuildIsolation::Isolated,
build_isolation,
no_build,
&NoBinary::None,
)
Expand Down
Loading

0 comments on commit 7afd06c

Please sign in to comment.