Skip to content

Commit

Permalink
Activate venv before source dist build
Browse files Browse the repository at this point in the history
Fixes #552
  • Loading branch information
konstin committed Dec 12, 2023
1 parent 923697c commit 97a0452
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/gourgeist/src/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn create_bare_venv(location: &Utf8Path, interpreter: &Interpreter) -> io::R
}
#[cfg(windows)]
{
location.join("Bin")
location.join("Scripts")
}
#[cfg(not(any(unix, windows)))]
{
Expand Down
24 changes: 16 additions & 8 deletions crates/puffin-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! <https://packaging.python.org/en/latest/specifications/source-distribution-format/>

use std::env;
use std::fmt::{Display, Formatter};
use std::io;
use std::io::BufRead;
Expand Down Expand Up @@ -401,8 +402,7 @@ impl SourceBuild {
name="prepare_metadata_for_build_wheel",
python_version = %self.venv.interpreter().version()
);
let output =
run_python_script(&self.venv.python_executable(), &script, &self.source_tree).await?;
let output = run_python_script(&self.venv, &script, &self.source_tree).await?;
drop(span);
if !output.status.success() {
return Err(Error::from_command_output(
Expand Down Expand Up @@ -519,8 +519,7 @@ impl SourceBuild {
name=format!("build_{}", self.build_kind),
python_version = %self.venv.interpreter().version()
);
let output =
run_python_script(&self.venv.python_executable(), &script, &self.source_tree).await?;
let output = run_python_script(&self.venv, &script, &self.source_tree).await?;
drop(span);
if !output.status.success() {
return Err(Error::from_command_output(
Expand Down Expand Up @@ -587,7 +586,7 @@ async fn create_pep517_build_environment(
name="build_wheel",
python_version = %venv.interpreter().version()
);
let output = run_python_script(&venv.python_executable(), &script, source_tree).await?;
let output = run_python_script(venv, &script, source_tree).await?;
drop(span);
if !output.status.success() {
return Err(Error::from_command_output(
Expand Down Expand Up @@ -688,16 +687,25 @@ fn extract_archive(sdist: &Path, extracted: &PathBuf) -> Result<PathBuf, Error>

/// It is the caller's responsibility to create an informative span.
async fn run_python_script(
python_interpreter: &Path,
venv: &Virtualenv,
script: &str,
source_tree: &Path,
) -> Result<Output, Error> {
Command::new(python_interpreter)
// `OsString` doesn't impl `Add`
let mut new_path = venv.bin_dir().into_os_string();
if let Some(path) = env::var_os("PATH") {
new_path.push(":");
new_path.push(path);
}
Command::new(venv.python_executable())
.args(["-c", script])
.current_dir(source_tree)
// Activate the venv
.env("VIRTUAL_ENV", venv.root())
.env("PATH", new_path)
.output()
.await
.map_err(|err| Error::CommandFailed(python_interpreter.to_path_buf(), err))
.map_err(|err| Error::CommandFailed(venv.python_executable(), err))
}

#[cfg(test)]
Expand Down
15 changes: 15 additions & 0 deletions crates/puffin-interpreter/src/virtual_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ impl Virtualenv {
.platform
.venv_site_packages(&self.root, self.interpreter().simple_version())
}

pub fn bin_dir(&self) -> PathBuf {
#[cfg(unix)]
{
self.root().join("bin")
}
#[cfg(windows)]
{
self.root().join("Scripts")
}
#[cfg(not(any(unix, windows)))]
{
compile_error!("only unix (like mac and linux) and windows are supported")
}
}
}

/// Locate the current virtual environment.
Expand Down

0 comments on commit 97a0452

Please sign in to comment.