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 5, 2023
1 parent 2bb7232 commit b36697a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 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
26 changes: 16 additions & 10 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 @@ -399,7 +400,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)?;
let output = run_python_script(&self.venv, &script, &self.source_tree)?;
drop(span);
if !output.status.success() {
return Err(Error::from_command_output(
Expand Down Expand Up @@ -511,7 +512,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)?;
let output = run_python_script(&self.venv, &script, &self.source_tree)?;
drop(span);
if !output.status.success() {
return Err(Error::from_command_output(
Expand Down Expand Up @@ -577,7 +578,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)?;
let output = run_python_script(venv, &script, source_tree)?;
drop(span);
if !output.status.success() {
return Err(Error::from_command_output(
Expand Down Expand Up @@ -678,16 +679,21 @@ fn extract_archive(sdist: &Path, extracted: &PathBuf) -> Result<PathBuf, Error>
}

/// It is the caller's responsibility to create an informative span.
fn run_python_script(
python_interpreter: &Path,
script: &str,
source_tree: &Path,
) -> Result<Output, Error> {
Command::new(python_interpreter)
fn run_python_script(venv: &Virtualenv, script: &str, source_tree: &Path) -> Result<Output, Error> {
// `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()
.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 b36697a

Please sign in to comment.