Skip to content

Commit

Permalink
Tweak methods
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 8, 2024
1 parent 6b5d76c commit f2e9b24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
30 changes: 7 additions & 23 deletions crates/uv-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,38 +437,22 @@ impl SourceBuild {
let package_name = project.clone().map(|p| p.name);

// Create a virtual environment, or install into the shared environment if requested.
let venv = match build_isolation {
BuildIsolation::Isolated => uv_virtualenv::create_venv(
let venv = if let Some(venv) = build_isolation.shared_environment(package_name.as_ref()) {
venv.clone()
} else {
uv_virtualenv::create_venv(
temp_dir.path(),
interpreter.clone(),
uv_virtualenv::Prompt::None,
false,
false,
false,
)?,
BuildIsolation::Shared(venv) => venv.clone(),
BuildIsolation::SharedPackage(venv, packages) => {
if package_name
.as_ref()
.is_some_and(|name| packages.iter().any(|p| p == name))
{
venv.clone()
} else {
uv_virtualenv::create_venv(
temp_dir.path(),
interpreter.clone(),
uv_virtualenv::Prompt::None,
false,
false,
false,
)?
}
}
)?
};

// Setup the build environment. If build isolation is disabled, we assume the build
// environment is already setup.
if build_isolation.is_isolated(&package_name) {
if build_isolation.is_isolated(package_name.as_ref()) {
let resolved_requirements = Self::get_resolved_requirements(
build_context,
source_build_context,
Expand Down Expand Up @@ -518,7 +502,7 @@ impl SourceBuild {
// Create the PEP 517 build environment. If build isolation is disabled, we assume the build
// environment is already setup.
let runner = PythonRunner::new(concurrent_builds);
if build_isolation.is_isolated(&package_name) {
if build_isolation.is_isolated(package_name.as_ref()) {
if let Some(pep517_backend) = &pep517_backend {
create_pep517_build_environment(
&runner,
Expand Down
24 changes: 19 additions & 5 deletions crates/uv-types/src/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@ pub enum BuildIsolation<'a> {
}

impl<'a> BuildIsolation<'a> {
/// Returns `true` if build isolation is enforced.
pub fn is_isolated(&self, package_name: &Option<PackageName>) -> bool {
/// Returns `true` if build isolation is enforced for the given package name.
pub fn is_isolated(&self, package: Option<&PackageName>) -> bool {
match self {
Self::Isolated => true,
Self::Shared(_) => false,
Self::SharedPackage(_, packages) => match package_name {
Some(package_name) => !packages.iter().any(|p| p == package_name),
None => true,
Self::SharedPackage(_, packages) => package.map_or(true, |package| {
!packages.iter().any(|p| p == package)
}),
}
}

/// Returns the shared environment for a given package, if build isolation is not enforced.
pub fn shared_environment(&self, package: Option<&PackageName>) -> Option<&PythonEnvironment> {
match self {
Self::Isolated => None,
Self::Shared(env) => Some(env),
Self::SharedPackage(env, packages) => if package.is_some_and(|package| {
packages.iter().any(|p| p == package)
}) {
Some(env)
} else {
None
},
}
}
Expand Down

0 comments on commit f2e9b24

Please sign in to comment.