Skip to content

Commit

Permalink
Create virtual env wrapper env
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Jun 6, 2024
1 parent 2288b94 commit 27412f7
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 594 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ jobs:
run: |
pip install virtualenvwrapper
echo "WORKON_HOME=$HOME/.virtualenvs" >> $GITHUB_ENV
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv venv_wrapper_env1
shell: bash

- name: Install virtualenvwrapper-win
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ target/

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Directory with generated environments (generally created on CI)
tmp
12 changes: 9 additions & 3 deletions crates/pet-conda/src/conda_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
use crate::env_variables::EnvVariables;
use log::trace;
use pet_utils::path::normalize;
use std::{fs, path::PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};

#[derive(Debug)]
pub struct Condarc {
Expand All @@ -15,6 +18,9 @@ impl Condarc {
pub fn from(env_vars: &EnvVariables) -> Option<Condarc> {
get_conda_conda_rc(env_vars)
}
pub fn from_path(path: &Path) -> Option<Condarc> {
parse_conda_rc(&path.join(".condarc"))
}
}

#[cfg(windows)]
Expand Down Expand Up @@ -141,10 +147,10 @@ fn get_conda_conda_rc(env_vars: &EnvVariables) -> Option<Condarc> {
parse_conda_rc(&conda_rc)
}

fn parse_conda_rc(conda_rc: &PathBuf) -> Option<Condarc> {
fn parse_conda_rc(conda_rc: &Path) -> Option<Condarc> {
let mut start_consuming_values = false;
trace!("conda_rc: {:?}", conda_rc);
let reader = fs::read_to_string(conda_rc).ok()?;
trace!("conda_rc: {:?}", conda_rc);
let mut env_dirs = vec![];
for line in reader.lines() {
if line.starts_with("envs_dirs:") && !start_consuming_values {
Expand Down
48 changes: 4 additions & 44 deletions crates/pet-conda/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub fn get_environments(conda_dir: &Path) -> Vec<PathBuf> {
.collect(),
);
}
// Then read the .condarc in the conda install folder as well.
if let Some(mut conda_rc) = Condarc::from_path(&conda_dir) {
envs.append(&mut conda_rc.env_dirs);
}
} else if is_conda_env(conda_dir) {
envs.push(conda_dir.to_path_buf());
} else if fs::metadata(conda_dir.join("envs")).is_ok() {
Expand Down Expand Up @@ -221,7 +225,6 @@ pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf
pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf> {
let mut known_paths = vec![];
let directories_to_look_in = [
"/opt",
"/opt",
"/usr/share",
"/usr/local",
Expand Down Expand Up @@ -254,51 +257,8 @@ pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf
known_paths.push(home.clone().join("miniforge3"));
known_paths.push(home.join(".conda"));
}
known_paths.append(get_known_conda_locations(env_vars).as_mut());
known_paths.sort();
known_paths.dedup();

known_paths
}

#[cfg(windows)]
pub fn get_known_conda_locations(env_vars: &EnvVariables) -> Vec<PathBuf> {
let user_profile = env_vars.userprofile.clone().unwrap_or_default();
let program_data = env_vars.programdata.clone().unwrap_or_default();
let all_user_profile = env_vars.allusersprofile.clone().unwrap_or_default();
let home_drive = env_vars.homedrive.clone().unwrap_or_default();
let mut known_paths = vec![
Path::new(&user_profile).join("Anaconda3\\Scripts"),
Path::new(&program_data).join("Anaconda3\\Scripts"),
Path::new(&all_user_profile).join("Anaconda3\\Scripts"),
Path::new(&home_drive).join("Anaconda3\\Scripts"),
Path::new(&user_profile).join("Miniconda3\\Scripts"),
Path::new(&program_data).join("Miniconda3\\Scripts"),
Path::new(&all_user_profile).join("Miniconda3\\Scripts"),
Path::new(&home_drive).join("Miniconda3\\Scripts"),
];
known_paths.append(&mut env_vars.known_global_search_locations.clone());
known_paths
}

#[cfg(unix)]
pub fn get_known_conda_locations(env_vars: &EnvVariables) -> Vec<PathBuf> {
let mut known_paths = vec![
PathBuf::from("/opt/anaconda3/bin"),
PathBuf::from("/opt/miniconda3/bin"),
PathBuf::from("/usr/local/anaconda3/bin"),
PathBuf::from("/usr/local/miniconda3/bin"),
PathBuf::from("/usr/anaconda3/bin"),
PathBuf::from("/usr/miniconda3/bin"),
PathBuf::from("/home/anaconda3/bin"),
PathBuf::from("/home/miniconda3/bin"),
PathBuf::from("/anaconda3/bin"),
PathBuf::from("/miniconda3/bin"),
];
if let Some(ref home) = env_vars.home {
known_paths.push(home.clone().join("anaconda3/bin"));
known_paths.push(home.join("miniconda3/bin"));
}
known_paths.append(&mut env_vars.known_global_search_locations.clone());
known_paths
}
33 changes: 3 additions & 30 deletions crates/pet-conda/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// Licensed under the MIT License.

use crate::{
env_variables::EnvVariables, environment_locations::get_known_conda_locations,
environments::get_conda_installation_used_to_create_conda_env, package::CondaPackageInfo,
utils::is_conda_env,
env_variables::EnvVariables, environments::get_conda_installation_used_to_create_conda_env,
package::CondaPackageInfo, utils::is_conda_env,
};
use pet_core::{manager::EnvManager, manager::EnvManagerType};
use std::{
Expand Down Expand Up @@ -44,7 +43,7 @@ fn get_conda_bin_names() -> Vec<&'static str> {
}

/// Find the conda binary on the PATH environment variable
pub fn find_conda_binary_on_path(env_vars: &EnvVariables) -> Option<PathBuf> {
pub fn find_conda_binary(env_vars: &EnvVariables) -> Option<PathBuf> {
let paths = env_vars.path.clone()?;
for path in env::split_paths(&paths) {
for bin in get_conda_bin_names() {
Expand All @@ -59,32 +58,6 @@ pub fn find_conda_binary_on_path(env_vars: &EnvVariables) -> Option<PathBuf> {
None
}

/// Find conda binary in known locations
fn find_conda_binary_in_known_locations(env_vars: &EnvVariables) -> Option<PathBuf> {
let conda_bin_names = get_conda_bin_names();
let known_locations = get_known_conda_locations(env_vars);
for location in known_locations {
for bin in &conda_bin_names {
let conda_path = location.join(bin);
if let Ok(metadata) = std::fs::metadata(&conda_path) {
if metadata.is_file() || metadata.is_symlink() {
return Some(conda_path);
}
}
}
}
None
}

/// Find the conda binary on the system
pub fn find_conda_binary(env_vars: &EnvVariables) -> Option<PathBuf> {
let conda_binary_on_path = find_conda_binary_on_path(env_vars);
match conda_binary_on_path {
Some(conda_binary_on_path) => Some(conda_binary_on_path),
None => find_conda_binary_in_known_locations(env_vars),
}
}

#[derive(Debug, Clone)]
pub struct CondaManager {
pub executable: PathBuf,
Expand Down
41 changes: 0 additions & 41 deletions crates/pet-conda/tests/environment_locations_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,6 @@ fn non_existent_envrionments_txt() {
assert!(environments.is_empty());
}

#[cfg(unix)]
#[test]
fn known_install_locations() {
use common::{create_env_variables, resolve_test_path};
use pet_conda::environment_locations::get_known_conda_locations;
use std::path::PathBuf;

let root = resolve_test_path(&["unix", "root_empty"]).into();
let home = resolve_test_path(&["unix", "user_home"]).into();
let env = create_env_variables(home, root);

let mut locations = get_known_conda_locations(&env);
locations.sort();

let mut expected = [
vec![
"/opt/anaconda3/bin",
"/opt/miniconda3/bin",
"/usr/local/anaconda3/bin",
"/usr/local/miniconda3/bin",
"/usr/anaconda3/bin",
"/usr/miniconda3/bin",
"/home/anaconda3/bin",
"/home/miniconda3/bin",
"/anaconda3/bin",
"/miniconda3/bin",
]
.iter()
.map(PathBuf::from)
.collect::<Vec<PathBuf>>(),
vec![
resolve_test_path(&["unix", "user_home", "anaconda3", "bin"]),
resolve_test_path(&["unix", "user_home", "miniconda3", "bin"]),
],
]
.concat();
expected.sort();

assert_eq!(locations, expected);
}

#[cfg(unix)]
#[test]
fn list_conda_envs_in_install_location() {
Expand Down
1 change: 1 addition & 0 deletions crates/pet-core/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ use crate::{manager::EnvManager, python_environment::PythonEnvironment};
pub trait Reporter: Send + Sync {
fn report_manager(&self, manager: &EnvManager);
fn report_environment(&self, env: &PythonEnvironment);
fn report_completion(&self, duration: std::time::Duration);
}
5 changes: 1 addition & 4 deletions crates/pet-env-var-path/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use pet_core::{
Locator, LocatorResult,
};
use pet_utils::{
env::PythonEnv,
executable::{find_executables, get_shortest_executable, resolve_symlink},
headers::Headers,
pyvenv_cfg::PyVenvCfg,
env::PythonEnv, executable::{find_executables, get_shortest_executable}, headers::Headers, path::resolve_symlink, pyvenv_cfg::PyVenvCfg
};
use regex::Regex;

Expand Down
3 changes: 3 additions & 0 deletions crates/pet-global-virtualenvs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ pub fn list_global_virtual_envs_paths(
}
}

python_envs.sort();
python_envs.dedup();

python_envs
}
5 changes: 1 addition & 4 deletions crates/pet-homebrew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use environments::get_python_info;
use pet_core::{
os_environment::Environment, python_environment::PythonEnvironment, Locator, LocatorResult,
};
use pet_utils::{
env::PythonEnv,
executable::{find_executables, resolve_symlink},
};
use pet_utils::{env::PythonEnv, executable::find_executables, path::resolve_symlink};
use std::{collections::HashSet, path::PathBuf};

mod env_variables;
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-homebrew/src/sym_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

use lazy_static::lazy_static;
use pet_utils::executable::resolve_symlink;
use pet_utils::path::resolve_symlink;
use regex::Regex;
use std::path::{Path, PathBuf};

Expand Down
Loading

0 comments on commit 27412f7

Please sign in to comment.