Skip to content

Commit

Permalink
get rid of system python
Browse files Browse the repository at this point in the history
  • Loading branch information
jpopesculian committed Feb 14, 2024
1 parent 0c0709d commit 289e101
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions runner/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use futures::prelude::*;
use pyo3::types::PyDict;
use pyo3::{prelude::*, pyclass::IterANextOutput, types::PyType};
use std::collections::HashSet;
use std::ffi::OsStr;
Expand All @@ -7,15 +8,6 @@ use thiserror::Error;
use tokio::{process::Command, sync::RwLock};

lazy_static::lazy_static! {
static ref SYSTEM_PYTHON_PATH: PathBuf = {
Python::with_gil(|py| {
let sys = py.import("sys").unwrap();
let executable: String = sys
.getattr(pyo3::intern!(py, "executable")).unwrap()
.extract().unwrap();
PathBuf::from(executable)
})
};
static ref INITIALIZED_ENVS: RwLock<HashSet<PathBuf>> = RwLock::new(HashSet::new());
}

Expand All @@ -33,8 +25,6 @@ pub enum EnvError {
Io(#[from] tokio::io::Error),
#[error(transparent)]
Python(#[from] PyErr),
#[error("Failed to setup virtualenv: {0}")]
VenvFailed(String),
}

impl PyEnv {
Expand Down Expand Up @@ -73,19 +63,20 @@ impl PyEnv {
if path.join("pyvenv.cfg").exists() {
return Ok(());
}
let output = Command::new(SYSTEM_PYTHON_PATH.as_os_str())
.arg("-m")
.arg("venv")
.arg(path)
.output()
.await?;
if output.status.success() {
tokio::fs::create_dir_all(path).await?;
Python::with_gil(|py| -> PyResult<()> {
let config = PyDict::new(py);
config.set_item("with_pip", true)?;
let builder = py
.import("venv")?
.getattr(pyo3::intern!(py, "EnvBuilder"))?
.call((), Some(config))?;
builder
.getattr(pyo3::intern!(py, "create"))?
.call1((path,))?;
Ok(())
} else {
Err(EnvError::VenvFailed(
String::from_utf8_lossy(&output.stderr).to_string(),
))
}
})?;
Ok(())
}

pub fn python_path(&self) -> PathBuf {
Expand Down

0 comments on commit 289e101

Please sign in to comment.