From 03d39bf7cd852049346ff84a93ccc9a009495f0a Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:01:37 +1000 Subject: [PATCH 01/16] Ensure we parse env variables once --- crates/pet-conda/src/env_variables.rs | 4 +- crates/pet-conda/src/lib.rs | 2 +- crates/pet-core/src/os_environment.rs | 2 +- crates/pet-env-var-path/src/lib.rs | 4 +- crates/pet-homebrew/src/env_variables.rs | 4 +- crates/pet-homebrew/src/lib.rs | 4 +- crates/pet-pipenv/src/env_variables.rs | 4 +- crates/pet-pipenv/src/lib.rs | 3 +- crates/pet-poetry/src/env_variables.rs | 4 +- crates/pet-poetry/src/lib.rs | 4 +- crates/pet-pyenv/src/env_variables.rs | 4 +- crates/pet-pyenv/src/lib.rs | 2 +- .../src/env_variables.rs | 4 +- crates/pet-virtualenvwrapper/src/lib.rs | 4 +- crates/pet/src/find.rs | 23 +++++---- crates/pet/src/jsonrpc.rs | 18 +++++-- crates/pet/src/lib.rs | 19 +++++-- crates/pet/src/locators.rs | 29 +++++------ crates/pet/src/resolve.rs | 6 +-- crates/pet/tests/ci_homebrew_container.rs | 7 +-- crates/pet/tests/ci_jupyter_container.rs | 7 +-- crates/pet/tests/ci_poetry.rs | 28 ++++++++--- crates/pet/tests/ci_test.rs | 49 ++++++++++++------- 23 files changed, 141 insertions(+), 94 deletions(-) diff --git a/crates/pet-conda/src/env_variables.rs b/crates/pet-conda/src/env_variables.rs index aa1bebc8..5403a5b6 100644 --- a/crates/pet-conda/src/env_variables.rs +++ b/crates/pet-conda/src/env_variables.rs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; use pet_core::os_environment::Environment; @@ -26,7 +26,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { EnvVariables { home: env.get_user_home(), root: env.get_root(), diff --git a/crates/pet-conda/src/lib.rs b/crates/pet-conda/src/lib.rs index 8ee17ef9..034a71a8 100644 --- a/crates/pet-conda/src/lib.rs +++ b/crates/pet-conda/src/lib.rs @@ -44,7 +44,7 @@ pub struct Conda { } impl Conda { - pub fn from(env: &dyn Environment) -> Conda { + pub fn from(env: Arc) -> Conda { Conda { env_dirs: Arc::new(Mutex::new(vec![])), environments: Arc::new(Mutex::new(HashMap::new())), diff --git a/crates/pet-core/src/os_environment.rs b/crates/pet-core/src/os_environment.rs index 7a8b6ef7..6fa0b44f 100644 --- a/crates/pet-core/src/os_environment.rs +++ b/crates/pet-core/src/os_environment.rs @@ -10,7 +10,7 @@ use std::{ use log::trace; use pet_fs::path::norm_case; -pub trait Environment { +pub trait Environment: Send + Sync { fn get_user_home(&self) -> Option; /// Only used in tests, None in production. #[allow(dead_code)] diff --git a/crates/pet-env-var-path/src/lib.rs b/crates/pet-env-var-path/src/lib.rs index 95740859..04dc6a9b 100644 --- a/crates/pet-env-var-path/src/lib.rs +++ b/crates/pet-env-var-path/src/lib.rs @@ -2,9 +2,9 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; -pub fn get_search_paths_from_env_variables(environment: &dyn Environment) -> Vec { +pub fn get_search_paths_from_env_variables(environment: Arc) -> Vec { // Exclude files from this folder, as they would have been discovered elsewhere (widows_store) // Also the exe is merely a pointer to another file. if let Some(home) = environment.get_user_home() { diff --git a/crates/pet-homebrew/src/env_variables.rs b/crates/pet-homebrew/src/env_variables.rs index 860b7cff..ae6b90fd 100644 --- a/crates/pet-homebrew/src/env_variables.rs +++ b/crates/pet-homebrew/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -20,7 +20,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { EnvVariables { home: env.get_user_home(), root: env.get_root(), diff --git a/crates/pet-homebrew/src/lib.rs b/crates/pet-homebrew/src/lib.rs index 29b2419e..f1d1c999 100644 --- a/crates/pet-homebrew/src/lib.rs +++ b/crates/pet-homebrew/src/lib.rs @@ -13,7 +13,7 @@ use pet_core::{ use pet_fs::path::resolve_symlink; use pet_python_utils::{env::PythonEnv, executable::find_executables}; use pet_virtualenv::is_virtualenv; -use std::{fs, path::PathBuf, thread}; +use std::{fs, path::PathBuf, sync::Arc, thread}; use sym_links::is_homebrew_python; mod env_variables; @@ -26,7 +26,7 @@ pub struct Homebrew { } impl Homebrew { - pub fn from(environment: &dyn Environment) -> Homebrew { + pub fn from(environment: Arc) -> Homebrew { Homebrew { environment: EnvVariables::from(environment), } diff --git a/crates/pet-pipenv/src/env_variables.rs b/crates/pet-pipenv/src/env_variables.rs index cd0e7ab7..ad615aab 100644 --- a/crates/pet-pipenv/src/env_variables.rs +++ b/crates/pet-pipenv/src/env_variables.rs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +use std::sync::Arc; + use pet_core::os_environment::Environment; #[derive(Debug, Clone)] @@ -13,7 +15,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { EnvVariables { pipenv_max_depth: env .get_env_var("PIPENV_MAX_DEPTH".to_string()) diff --git a/crates/pet-pipenv/src/lib.rs b/crates/pet-pipenv/src/lib.rs index 53e354db..34214226 100644 --- a/crates/pet-pipenv/src/lib.rs +++ b/crates/pet-pipenv/src/lib.rs @@ -13,6 +13,7 @@ use pet_python_utils::env::PythonEnv; use pet_python_utils::executable::find_executables; use pet_python_utils::version; use std::path::Path; +use std::sync::Arc; use std::{fs, path::PathBuf}; mod env_variables; @@ -64,7 +65,7 @@ pub struct PipEnv { } impl PipEnv { - pub fn from(environment: &dyn Environment) -> PipEnv { + pub fn from(environment: Arc) -> PipEnv { PipEnv { env_vars: EnvVariables::from(environment), } diff --git a/crates/pet-poetry/src/env_variables.rs b/crates/pet-poetry/src/env_variables.rs index c76c572f..e3516bec 100644 --- a/crates/pet-poetry/src/env_variables.rs +++ b/crates/pet-poetry/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -26,7 +26,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { let mut poetry_home = None; let home = env.get_user_home(); if let (Some(home), Some(poetry_home_value)) = diff --git a/crates/pet-poetry/src/lib.rs b/crates/pet-poetry/src/lib.rs index 66f20c61..dd716232 100644 --- a/crates/pet-poetry/src/lib.rs +++ b/crates/pet-poetry/src/lib.rs @@ -37,7 +37,7 @@ pub struct Poetry { } impl Poetry { - pub fn new(environment: &dyn Environment) -> Self { + pub fn new(environment: Arc) -> Self { Poetry { searched: AtomicBool::new(false), search_result: Arc::new(Mutex::new(None)), @@ -46,7 +46,7 @@ impl Poetry { poetry_executable: Arc::new(Mutex::new(None)), } } - pub fn from(environment: &dyn Environment) -> impl Locator { + pub fn from(environment: Arc) -> impl Locator { Poetry::new(environment) } pub fn find_with_executable(&self) -> Option<()> { diff --git a/crates/pet-pyenv/src/env_variables.rs b/crates/pet-pyenv/src/env_variables.rs index c12ec853..81e2586e 100644 --- a/crates/pet-pyenv/src/env_variables.rs +++ b/crates/pet-pyenv/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -17,7 +17,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { EnvVariables { home: env.get_user_home(), root: env.get_root(), diff --git a/crates/pet-pyenv/src/lib.rs b/crates/pet-pyenv/src/lib.rs index e460c616..bcbad900 100644 --- a/crates/pet-pyenv/src/lib.rs +++ b/crates/pet-pyenv/src/lib.rs @@ -35,7 +35,7 @@ pub struct PyEnv { impl PyEnv { pub fn from( - environment: &dyn Environment, + environment: Arc, conda_locator: Arc, ) -> impl Locator { PyEnv { diff --git a/crates/pet-virtualenvwrapper/src/env_variables.rs b/crates/pet-virtualenvwrapper/src/env_variables.rs index 1a8f2e28..283e674c 100644 --- a/crates/pet-virtualenvwrapper/src/env_variables.rs +++ b/crates/pet-virtualenvwrapper/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -13,7 +13,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { EnvVariables { home: env.get_user_home(), workon_home: env.get_env_var("WORKON_HOME".to_string()), diff --git a/crates/pet-virtualenvwrapper/src/lib.rs b/crates/pet-virtualenvwrapper/src/lib.rs index a52ba8d1..2e9cbd87 100644 --- a/crates/pet-virtualenvwrapper/src/lib.rs +++ b/crates/pet-virtualenvwrapper/src/lib.rs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +use std::sync::Arc; + use env_variables::EnvVariables; use environments::{get_project, is_virtualenvwrapper}; use pet_core::{ @@ -21,7 +23,7 @@ pub struct VirtualEnvWrapper { } impl VirtualEnvWrapper { - pub fn from(environment: &dyn Environment) -> VirtualEnvWrapper { + pub fn from(environment: Arc) -> VirtualEnvWrapper { VirtualEnvWrapper { env_vars: EnvVariables::from(environment), } diff --git a/crates/pet/src/find.rs b/crates/pet/src/find.rs index 70516290..62f9059b 100644 --- a/crates/pet/src/find.rs +++ b/crates/pet/src/find.rs @@ -3,7 +3,7 @@ use log::{trace, warn}; use pet_conda::CondaLocator; -use pet_core::os_environment::{Environment, EnvironmentApi}; +use pet_core::os_environment::Environment; use pet_core::reporter::Reporter; use pet_core::{Configuration, Locator}; use pet_env_var_path::get_search_paths_from_env_variables; @@ -36,6 +36,7 @@ pub fn find_and_report_envs( configuration: Configuration, locators: &Arc>>, conda_locator: Arc, + os_environment: Arc, ) -> Arc> { let summary = Arc::new(Mutex::new(Summary { time: Duration::from_secs(0), @@ -53,6 +54,7 @@ pub fn find_and_report_envs( let conda_executable = configuration.conda_executable; thread::scope(|s| { // 1. Find using known global locators. + let os_env = os_environment.clone(); s.spawn(|| { // Find in all the finders let start = std::time::Instant::now(); @@ -86,17 +88,15 @@ pub fn find_and_report_envs( // & see if we can find more environments by spawning poetry. // But we will not wait for this to complete. thread::spawn(move || { - let env = EnvironmentApi::new(); - Poetry::new(&env).find_with_executable(); + Poetry::new(os_env).find_with_executable(); Some(()) }); }); // Step 2: Search in PATH variable + let os_env = os_environment.clone(); s.spawn(|| { let start = std::time::Instant::now(); - let environment = EnvironmentApi::new(); - let global_env_search_paths: Vec = - get_search_paths_from_env_variables(&environment); + let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_env); trace!( "Searching for environments in global folders: {:?}", @@ -113,20 +113,19 @@ pub fn find_and_report_envs( summary.lock().unwrap().find_path_time = start.elapsed(); }); // Step 3: Search in some global locations for virtual envs. + let os_env = os_environment.clone(); s.spawn(|| { let start = std::time::Instant::now(); - let environment = EnvironmentApi::new(); let search_paths: Vec = [ list_global_virtual_envs_paths( - environment.get_env_var("WORKON_HOME".into()), - environment.get_env_var("XDG_DATA_HOME".into()), - environment.get_user_home(), + os_env.get_env_var("WORKON_HOME".into()), + os_env.get_env_var("XDG_DATA_HOME".into()), + os_env.get_user_home(), ), environment_directories, ] .concat(); - let global_env_search_paths: Vec = - get_search_paths_from_env_variables(&environment); + let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_env); trace!( "Searching for environments in global venv folders: {:?}", diff --git a/crates/pet/src/jsonrpc.rs b/crates/pet/src/jsonrpc.rs index 0293fc1d..46a73730 100644 --- a/crates/pet/src/jsonrpc.rs +++ b/crates/pet/src/jsonrpc.rs @@ -4,7 +4,11 @@ use log::{error, info, trace}; use pet::resolve::resolve_environment; use pet_conda::Conda; -use pet_core::{os_environment::EnvironmentApi, reporter::Reporter, Configuration, Locator}; +use pet_core::{ + os_environment::{Environment, EnvironmentApi}, + reporter::Reporter, + Configuration, Locator, +}; use pet_jsonrpc::{ send_error, send_reply, server::{start_server, HandlersKeyedByMethodName}, @@ -27,6 +31,7 @@ pub struct Context { configuration: RwLock, locators: Arc>>, conda_locator: Arc, + os_environment: Arc, } pub fn start_jsonrpc_server() { @@ -34,15 +39,16 @@ pub fn start_jsonrpc_server() { // These are globals for the the lifetime of the server. // Hence passed around as Arcs via the context. - let environment = EnvironmentApi::new(); + let environment = Arc::new(EnvironmentApi::new()); let jsonrpc_reporter = Arc::new(jsonrpc::create_reporter()); let reporter = Arc::new(CacheReporter::new(jsonrpc_reporter.clone())); - let conda_locator = Arc::new(Conda::from(&environment)); + let conda_locator = Arc::new(Conda::from(environment.clone())); let context = Context { reporter, - locators: create_locators(conda_locator.clone()), + locators: create_locators(conda_locator.clone(), environment.clone()), conda_locator, configuration: RwLock::new(Configuration::default()), + os_environment: environment, }; let mut handlers = HandlersKeyedByMethodName::new(Arc::new(context)); @@ -97,6 +103,7 @@ pub fn handle_refresh(context: Arc, id: u32, params: Value) { config, &context.locators, context.conda_locator.clone(), + context.os_environment.clone(), ); let summary = summary.lock().unwrap(); for locator in summary.find_locators_times.iter() { @@ -143,11 +150,12 @@ pub fn handle_resolve(context: Arc, id: u32, params: Value) { .project_directories; let search_paths = search_paths.unwrap_or_default(); // Start in a new thread, we can have multiple resolve requests. + let environment = context.os_environment.clone(); thread::spawn(move || { let now = SystemTime::now(); trace!("Resolving env {:?}", executable); if let Some(result) = - resolve_environment(&executable, &context.locators, search_paths) + resolve_environment(&executable, &context.locators, search_paths, environment) { if let Some(resolved) = result.resolved { // Gather telemetry of this resolved env and see what we got wrong. diff --git a/crates/pet/src/lib.rs b/crates/pet/src/lib.rs index 2039d266..bec279d2 100644 --- a/crates/pet/src/lib.rs +++ b/crates/pet/src/lib.rs @@ -22,19 +22,19 @@ pub fn find_and_report_envs_stdio(print_list: bool, print_summary: bool, verbose let stdio_reporter = Arc::new(stdio::create_reporter(print_list)); let reporter = CacheReporter::new(stdio_reporter.clone()); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); let mut config = Configuration::default(); if let Ok(cwd) = env::current_dir() { config.project_directories = Some(vec![cwd]); } - let locators = create_locators(conda_locator.clone()); + let locators = create_locators(conda_locator.clone(), environment.clone()); for locator in locators.iter() { locator.configure(&config); } - let summary = find_and_report_envs(&reporter, config, &locators, conda_locator); + let summary = find_and_report_envs(&reporter, config, &locators, conda_locator, environment); if print_summary { let summary = summary.lock().unwrap(); @@ -89,7 +89,16 @@ pub fn find_and_report_envs_stdio(print_list: bool, print_summary: bool, verbose .environments .clone() .into_iter() - .map(|(k, v)| (format!("{k:?}"), v)) + .map(|(k, v)| { + ( + format!( + "{}", + k.map(|v| format!("{:?}", v)) + .unwrap_or("Unknown".to_string()) + ), + v, + ) + }) .collect::>() { println!("{k:<20} : {v:?}"); diff --git a/crates/pet/src/locators.rs b/crates/pet/src/locators.rs index 5cffdfe4..e0b3a336 100644 --- a/crates/pet/src/locators.rs +++ b/crates/pet/src/locators.rs @@ -4,7 +4,7 @@ use log::{info, trace}; use pet_conda::Conda; use pet_core::arch::Architecture; -use pet_core::os_environment::EnvironmentApi; +use pet_core::os_environment::Environment; use pet_core::python_environment::{ PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind, }; @@ -23,9 +23,11 @@ use pet_virtualenvwrapper::VirtualEnvWrapper; use std::path::PathBuf; use std::sync::Arc; -pub fn create_locators(conda_locator: Arc) -> Arc>> { +pub fn create_locators( + conda_locator: Arc, + os_environment: Arc, +) -> Arc>> { // NOTE: The order of the items matter. - let environment = EnvironmentApi::new(); let mut locators: Vec> = vec![]; @@ -42,13 +44,16 @@ pub fn create_locators(conda_locator: Arc) -> Arc>> locators.push(Arc::new(WindowsRegistry::from(conda_locator.clone()))) } // 3. Pyenv Python - locators.push(Arc::new(PyEnv::from(&environment, conda_locator.clone()))); + locators.push(Arc::new(PyEnv::from( + os_environment.clone(), + conda_locator.clone(), + ))); // 4. Homebrew Python if cfg!(unix) { #[cfg(unix)] use pet_homebrew::Homebrew; #[cfg(unix)] - let homebrew_locator = Homebrew::from(&environment); + let homebrew_locator = Homebrew::from(os_environment.clone()); #[cfg(unix)] locators.push(Arc::new(homebrew_locator)); } @@ -57,9 +62,9 @@ pub fn create_locators(conda_locator: Arc) -> Arc>> // 6. Support for Virtual Envs // The order of these matter. // Basically PipEnv is a superset of VirtualEnvWrapper, which is a superset of Venv, which is a superset of VirtualEnv. - locators.push(Arc::new(Poetry::from(&environment))); - locators.push(Arc::new(PipEnv::from(&environment))); - locators.push(Arc::new(VirtualEnvWrapper::from(&environment))); + locators.push(Arc::new(Poetry::from(os_environment.clone()))); + locators.push(Arc::new(PipEnv::from(os_environment.clone()))); + locators.push(Arc::new(VirtualEnvWrapper::from(os_environment.clone()))); locators.push(Arc::new(Venv::new())); // VirtualEnv is the most generic, hence should be the last. locators.push(Arc::new(VirtualEnv::new())); @@ -115,11 +120,7 @@ pub fn identify_python_environment_using_locators( |e, loc| if e.is_some() { e } else { loc.try_from(&env) }, ) { - trace!( - "Unknown Env ({:?}) in Path resolved as {:?}", - executable, - env.kind - ); + trace!("Env ({:?}) in Path resolved as {:?}", executable, env.kind); identify_and_set_search_path(&mut env, &search_paths); // TODO: Telemetry point. // As we had to spawn earlier. @@ -145,7 +146,7 @@ pub fn identify_python_environment_using_locators( } } info!( - "Unknown Env ({:?}) in Path resolved as {:?} and reported as {:?}", + "Env ({:?}) in Path resolved as {:?} and reported as {:?}", executable, resolved_env, fallback_kind ); let mut env = create_unknown_env(resolved_env, fallback_kind); diff --git a/crates/pet/src/resolve.rs b/crates/pet/src/resolve.rs index f71881f1..a1e9864a 100644 --- a/crates/pet/src/resolve.rs +++ b/crates/pet/src/resolve.rs @@ -6,7 +6,7 @@ use std::{path::PathBuf, sync::Arc}; use log::{trace, warn}; use pet_core::{ arch::Architecture, - os_environment::EnvironmentApi, + os_environment::Environment, python_environment::{PythonEnvironment, PythonEnvironmentBuilder}, Locator, }; @@ -25,11 +25,11 @@ pub fn resolve_environment( executable: &PathBuf, locators: &Arc>>, search_paths: Vec, + os_environment: Arc, ) -> Option { // First check if this is a known environment let env = PythonEnv::new(executable.to_owned(), None, None); - let environment = EnvironmentApi::new(); - let global_env_search_paths: Vec = get_search_paths_from_env_variables(&environment); + let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_environment); if let Some(mut env) = identify_python_environment_using_locators(&env, locators, &global_env_search_paths, None) diff --git a/crates/pet/tests/ci_homebrew_container.rs b/crates/pet/tests/ci_homebrew_container.rs index 2bc78b26..d104981a 100644 --- a/crates/pet/tests/ci_homebrew_container.rs +++ b/crates/pet/tests/ci_homebrew_container.rs @@ -17,14 +17,15 @@ fn verify_python_in_homebrew_contaner() { use std::{path::PathBuf, sync::Arc}; let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone()), + &create_locators(conda_locator.clone(), environment.clone()), conda_locator, + environment, ); let result = reporter.get_result(); diff --git a/crates/pet/tests/ci_jupyter_container.rs b/crates/pet/tests/ci_jupyter_container.rs index 22e57a09..d6398b03 100644 --- a/crates/pet/tests/ci_jupyter_container.rs +++ b/crates/pet/tests/ci_jupyter_container.rs @@ -35,14 +35,15 @@ fn verify_python_in_jupyter_contaner() { setup(); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone()), + &create_locators(conda_locator.clone(), environment.clone()), conda_locator, + environment, ); let result = reporter.get_result(); diff --git a/crates/pet/tests/ci_poetry.rs b/crates/pet/tests/ci_poetry.rs index 033b5e95..418e880e 100644 --- a/crates/pet/tests/ci_poetry.rs +++ b/crates/pet/tests/ci_poetry.rs @@ -20,16 +20,22 @@ fn verify_ci_poetry_global() { let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone()); + let locators = create_locators(conda_locator.clone(), environment.clone()); for locator in locators.iter() { locator.configure(&config); } - find_and_report_envs(&reporter, Default::default(), &locators, conda_locator); + find_and_report_envs( + &reporter, + Default::default(), + &locators, + conda_locator, + environment, + ); let result = reporter.get_result(); @@ -80,16 +86,22 @@ fn verify_ci_poetry_project() { let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone()); + let locators = create_locators(conda_locator.clone(), environment.clone()); for locator in locators.iter() { locator.configure(&config); } - find_and_report_envs(&reporter, Default::default(), &locators, conda_locator); + find_and_report_envs( + &reporter, + Default::default(), + &locators, + conda_locator, + environment, + ); let result = reporter.get_result(); diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index 582e1b1d..e108f2c6 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -68,17 +68,23 @@ fn verify_validity_of_discovered_envs() { let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone()); + let locators = create_locators(conda_locator.clone(), environment.clone()); for locator in locators.iter() { locator.configure(&config); } // Find all environments on this machine. - find_and_report_envs(&reporter, Default::default(), &locators, conda_locator); + find_and_report_envs( + &reporter, + Default::default(), + &locators, + conda_locator, + environment, + ); let result = reporter.get_result(); let environments = result.environments; @@ -171,14 +177,15 @@ fn check_if_pipenv_exists() { setup(); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone()), + &create_locators(conda_locator.clone(), environment.clone()), conda_locator, + environment, ); let result = reporter.get_result(); @@ -332,17 +339,16 @@ fn verify_we_can_get_same_env_info_using_from_with_exe( use std::{env, sync::Arc}; let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); - let os_environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&os_environment)); + let os_environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(os_environment.clone())); let mut config = Configuration::default(); let search_paths = vec![project_dir.clone()]; config.project_directories = Some(search_paths.clone()); - let locators = create_locators(conda_locator.clone()); + let locators = create_locators(conda_locator.clone(), os_environment.clone()); for locator in locators.iter() { locator.configure(&config); } - let global_env_search_paths: Vec = - get_search_paths_from_env_variables(&os_environment); + let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_environment); let env = PythonEnv::new(executable.clone(), None, None); let resolved = identify_python_environment_using_locators( @@ -519,18 +525,22 @@ fn verify_we_can_get_same_env_info_using_resolve_with_exe( use std::{env, sync::Arc}; let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); - let os_environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&os_environment)); + let os_environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(os_environment.clone())); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone()); + let locators = create_locators(conda_locator.clone(), os_environment.clone()); for locator in locators.iter() { locator.configure(&config); } - let env = resolve_environment(&executable, &locators, vec![project_dir.clone()]).expect( - format!("Failed to resolve environment using `resolve` for {environment:?}").as_str(), - ); + let env = resolve_environment( + &executable, + &locators, + vec![project_dir.clone()], + os_environment, + ) + .expect(format!("Failed to resolve environment using `resolve` for {environment:?}").as_str()); trace!( "For exe {:?} we got Environment = {:?}, To compare against {:?}", executable, @@ -565,7 +575,7 @@ fn verify_bin_usr_bin_user_local_are_separate_python_envs() { setup(); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); + let environment = Arc::new(EnvironmentApi::new()); let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( @@ -573,6 +583,7 @@ fn verify_bin_usr_bin_user_local_are_separate_python_envs() { Default::default(), &create_locators(conda_locator.clone()), conda_locator, + environment, ); let result = reporter.get_result(); From 78dd1542605661bf8f2d6b2cb78bdbec0c5d3b30 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:04:31 +1000 Subject: [PATCH 02/16] misc --- crates/pet/src/lib.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/pet/src/lib.rs b/crates/pet/src/lib.rs index bec279d2..f1c138af 100644 --- a/crates/pet/src/lib.rs +++ b/crates/pet/src/lib.rs @@ -91,11 +91,8 @@ pub fn find_and_report_envs_stdio(print_list: bool, print_summary: bool, verbose .into_iter() .map(|(k, v)| { ( - format!( - "{}", - k.map(|v| format!("{:?}", v)) - .unwrap_or("Unknown".to_string()) - ), + k.map(|v| format!("{:?}", v)) + .unwrap_or("Unknown".to_string()), v, ) }) From 26e0f93b478aad6fbbe08670791378c9e4a29f9d Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:13:27 +1000 Subject: [PATCH 03/16] Fixes --- crates/pet-conda/tests/lib_test.rs | 10 +++--- crates/pet-pyenv/tests/pyenv_test.rs | 36 ++++++++++--------- crates/pet-windows-store/src/env_variables.rs | 4 +-- crates/pet-windows-store/src/lib.rs | 2 +- crates/pet/src/locators.rs | 2 +- crates/pet/tests/ci_test.rs | 16 +++++---- 6 files changed, 38 insertions(+), 32 deletions(-) diff --git a/crates/pet-conda/tests/lib_test.rs b/crates/pet-conda/tests/lib_test.rs index eb6524d4..17b76089 100644 --- a/crates/pet-conda/tests/lib_test.rs +++ b/crates/pet-conda/tests/lib_test.rs @@ -10,10 +10,10 @@ fn find_conda_env_without_manager() { use pet_conda::Conda; use pet_core::{self, arch::Architecture, python_environment::PythonEnvironmentKind, Locator}; use pet_python_utils::env::PythonEnv; - use std::collections::HashMap; + use std::{collections::HashMap, sync::Arc}; - let environment = create_test_environment(HashMap::new(), None, vec![], None); - let locator = Conda::from(&environment); + let environment = Arc::new(create_test_environment(HashMap::new(), None, vec![], None)); + let locator = Conda::from(environment.clone()); let path = resolve_test_path(&["unix", "conda_env_without_manager", "env_python_3"]); let env = locator @@ -45,8 +45,8 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() { fs::{self}, }; - let environment = create_test_environment(HashMap::new(), None, vec![], None); - let locator = Conda::from(&environment); + let environment = Arc::new(create_test_environment(HashMap::new(), None, vec![], None)); + let locator = Conda::from(environment); let path = resolve_test_path(&[ "unix", "conda_env_without_manager_but_found_in_history", diff --git a/crates/pet-pyenv/tests/pyenv_test.rs b/crates/pet-pyenv/tests/pyenv_test.rs index e03b3937..1fa427cb 100644 --- a/crates/pet-pyenv/tests/pyenv_test.rs +++ b/crates/pet-pyenv/tests/pyenv_test.rs @@ -14,15 +14,15 @@ fn does_not_find_any_pyenv_envs() { use pet_reporter::test::create_reporter; use std::{collections::HashMap, path::PathBuf, sync::Arc}; - let environment = create_test_environment( + let environment = Arc::new(create_test_environment( HashMap::new(), Some(PathBuf::from("SOME_BOGUS_HOME_DIR")), vec![], None, - ); + )); - let conda = Arc::new(Conda::from(&environment)); - let locator = PyEnv::from(&environment, conda); + let conda = Arc::new(Conda::from(environment.clone())); + let locator = PyEnv::from(environment.clone(), conda); let reporter = create_reporter(); locator.find(&reporter); let result = reporter.get_result(); @@ -58,15 +58,15 @@ fn does_not_find_any_pyenv_envs_even_with_pyenv_installed() { "bin", ]); let pyenv_exe = resolve_test_path(&[homebrew_bin.to_str().unwrap(), "pyenv"]); - let environment = create_test_environment( + let environment = Arc::new(create_test_environment( HashMap::new(), Some(home.clone()), vec![PathBuf::from(homebrew_bin)], None, - ); + )); - let conda = Arc::new(Conda::from(&environment)); - let locator = PyEnv::from(&environment, conda); + let conda = Arc::new(Conda::from(environment.clone())); + let locator = PyEnv::from(environment.clone(), conda); let reporter = create_reporter(); locator.find(&reporter); let result = reporter.get_result(); @@ -114,15 +114,15 @@ fn find_pyenv_envs() { ]); let conda_exe = conda_dir.join("bin").join("conda"); - let environment = create_test_environment( + let environment = Arc::new(create_test_environment( HashMap::new(), Some(home.clone()), vec![PathBuf::from(homebrew_bin)], None, - ); + )); - let conda = Arc::new(Conda::from(&environment)); - let locator = PyEnv::from(&environment, conda); + let conda = Arc::new(Conda::from(environment.clone())); + let locator = PyEnv::from(environment.clone(), conda); let reporter = create_reporter(); locator.find(&reporter); let mut result = reporter.get_result(); @@ -381,11 +381,15 @@ fn resolve_pyenv_environment() { let homebrew_bin = resolve_test_path(&["unix", "pyenv", "home", "opt", "homebrew", "bin"]); let pyenv_exe = resolve_test_path(&[homebrew_bin.to_str().unwrap(), "pyenv"]); - let environment = - create_test_environment(HashMap::new(), Some(home.clone()), vec![homebrew_bin], None); + let environment = Arc::new(create_test_environment( + HashMap::new(), + Some(home.clone()), + vec![homebrew_bin], + None, + )); - let conda = Arc::new(Conda::from(&environment)); - let locator = PyEnv::from(&environment, conda.clone()); + let conda = Arc::new(Conda::from(environment.clone())); + let locator = PyEnv::from(environment.clone(), conda.clone()); // let mut result = locator.find().unwrap(); let expected_manager = EnvManager { diff --git a/crates/pet-windows-store/src/env_variables.rs b/crates/pet-windows-store/src/env_variables.rs index ab4af6bb..7ee5ef20 100644 --- a/crates/pet-windows-store/src/env_variables.rs +++ b/crates/pet-windows-store/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -12,7 +12,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: &dyn Environment) -> Self { + pub fn from(env: Arc) -> Self { EnvVariables { home: env.get_user_home(), } diff --git a/crates/pet-windows-store/src/lib.rs b/crates/pet-windows-store/src/lib.rs index aa6d2b22..3e9ae3ed 100644 --- a/crates/pet-windows-store/src/lib.rs +++ b/crates/pet-windows-store/src/lib.rs @@ -30,7 +30,7 @@ pub struct WindowsStore { } impl WindowsStore { - pub fn from(environment: &dyn Environment) -> WindowsStore { + pub fn from(environment: Arc) -> WindowsStore { WindowsStore { searched: AtomicBool::new(false), env_vars: EnvVariables::from(environment), diff --git a/crates/pet/src/locators.rs b/crates/pet/src/locators.rs index e0b3a336..62d63676 100644 --- a/crates/pet/src/locators.rs +++ b/crates/pet/src/locators.rs @@ -39,7 +39,7 @@ pub fn create_locators( #[cfg(windows)] use pet_windows_store::WindowsStore; #[cfg(windows)] - locators.push(Arc::new(WindowsStore::from(&environment))); + locators.push(Arc::new(WindowsStore::from(environment.clone()))); #[cfg(windows)] locators.push(Arc::new(WindowsRegistry::from(conda_locator.clone()))) } diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index e108f2c6..84c50a6c 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -137,13 +137,14 @@ fn check_if_virtualenvwrapper_exists() { setup(); let reporter = test::create_reporter(); let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone()), + &create_locators(conda_locator.clone(), environment.clone()), conda_locator, + environment.clone(), ); let result = reporter.get_result(); @@ -215,14 +216,15 @@ fn check_if_pyenv_virtualenv_exists() { setup(); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); - let conda_locator = Arc::new(Conda::from(&environment)); + let environment = Arc::new(EnvironmentApi::new()); + let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone()), + &create_locators(conda_locator.clone(), environment.clone()), conda_locator, + environment.clone(), ); let result = reporter.get_result(); @@ -576,12 +578,12 @@ fn verify_bin_usr_bin_user_local_are_separate_python_envs() { setup(); let reporter = test::create_reporter(); let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(&environment)); + let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone()), + &create_locators(conda_locator.clone(), environment.clone()), conda_locator, environment, ); From dc4f21c45a0b4226c32423409b4eea68c1853cf9 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:19:09 +1000 Subject: [PATCH 04/16] oops --- crates/pet-conda/tests/lib_test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/pet-conda/tests/lib_test.rs b/crates/pet-conda/tests/lib_test.rs index 17b76089..7f35d506 100644 --- a/crates/pet-conda/tests/lib_test.rs +++ b/crates/pet-conda/tests/lib_test.rs @@ -43,6 +43,7 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() { use std::{ collections::HashMap, fs::{self}, + sync::Arc, }; let environment = Arc::new(create_test_environment(HashMap::new(), None, vec![], None)); From 277e3a1ca84fc2a02fc4d7e262f456ac51113010 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:20:14 +1000 Subject: [PATCH 05/16] oops --- crates/pet/src/locators.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pet/src/locators.rs b/crates/pet/src/locators.rs index 62d63676..8f55b9b0 100644 --- a/crates/pet/src/locators.rs +++ b/crates/pet/src/locators.rs @@ -39,7 +39,7 @@ pub fn create_locators( #[cfg(windows)] use pet_windows_store::WindowsStore; #[cfg(windows)] - locators.push(Arc::new(WindowsStore::from(environment.clone()))); + locators.push(Arc::new(WindowsStore::from(os_environment.clone()))); #[cfg(windows)] locators.push(Arc::new(WindowsRegistry::from(conda_locator.clone()))) } From 426c2f22159e78b2d8290b04692b4b709f028013 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:31:23 +1000 Subject: [PATCH 06/16] more fixes --- crates/pet-conda/tests/ci_test.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/pet-conda/tests/ci_test.rs b/crates/pet-conda/tests/ci_test.rs index 3f44703f..cb6a6d07 100644 --- a/crates/pet-conda/tests/ci_test.rs +++ b/crates/pet-conda/tests/ci_test.rs @@ -23,6 +23,8 @@ fn setup() { #[allow(dead_code)] // We should detect the conda install along with the base env fn detect_conda_root() { + use std::sync::Arc; + use pet_conda::Conda; use pet_core::{ manager::EnvManagerType, os_environment::EnvironmentApi, @@ -31,10 +33,10 @@ fn detect_conda_root() { use pet_reporter::test::create_reporter; setup(); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); let reporter = create_reporter(); - let conda = Conda::from(&env); + let conda = Conda::from(env); conda.find(&reporter); let result = reporter.get_result(); @@ -75,11 +77,11 @@ fn detect_conda_root_from_path() { use std::path::PathBuf; setup(); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); let info = get_conda_info(); let conda_dir = PathBuf::from(info.conda_prefix.clone()); let exe = conda_dir.join("bin").join("python"); - let conda = Conda::from(&env); + let conda = Conda::from(env); let python_env = PythonEnv::new(exe, Some(conda_dir.clone()), None); let env = conda.try_from(&python_env).unwrap(); @@ -116,9 +118,9 @@ fn detect_new_conda_env() { &CondaCreateEnvNameOrPath::Name(env_name.into()), Some("3.10".into()), ); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); - let conda = Conda::from(&env); + let conda = Conda::from(env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); @@ -169,7 +171,7 @@ fn detect_conda_env_from_path() { use std::path::PathBuf; setup(); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); let info = get_conda_info(); let env_name = "env_with_python2"; create_conda_env( @@ -179,7 +181,7 @@ fn detect_conda_env_from_path() { let conda_dir = PathBuf::from(info.conda_prefix.clone()); let prefix = conda_dir.join("envs").join(env_name); let exe = prefix.join("bin").join("python"); - let conda = Conda::from(&env); + let conda = Conda::from(env); let python_env = PythonEnv::new(exe.clone(), Some(prefix.clone()), None); let env = conda.try_from(&python_env).unwrap(); @@ -217,9 +219,9 @@ fn detect_new_conda_env_without_python() { setup(); let env_name = "env_without_python"; create_conda_env(&CondaCreateEnvNameOrPath::Name(env_name.into()), None); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); - let conda = Conda::from(&env); + let conda = Conda::from(env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); @@ -268,9 +270,9 @@ fn detect_new_conda_env_created_with_p_flag_without_python() { let env_name = "env_without_python3"; let prefix = resolve_test_path(&["unix", env_name]); create_conda_env(&CondaCreateEnvNameOrPath::Path(prefix.clone()), None); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); - let conda = Conda::from(&env); + let conda = Conda::from(env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); @@ -320,9 +322,9 @@ fn detect_new_conda_env_created_with_p_flag_with_python() { &CondaCreateEnvNameOrPath::Path(prefix.clone()), Some("3.10".into()), ); - let env = EnvironmentApi::new(); + let env = Arc::new(EnvironmentApi::new()); - let conda = Conda::from(&env); + let conda = Conda::from(env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); From 8134c17d82647058b4f8887850efe4d25dcec437 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:33:15 +1000 Subject: [PATCH 07/16] misc --- crates/pet-core/src/python_environment.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/pet-core/src/python_environment.rs b/crates/pet-core/src/python_environment.rs index 6326b851..095de101 100644 --- a/crates/pet-core/src/python_environment.rs +++ b/crates/pet-core/src/python_environment.rs @@ -114,7 +114,14 @@ impl PythonEnvironment { impl std::fmt::Display for PythonEnvironment { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - writeln!(f, "Environment ({:?})", self.kind).unwrap_or_default(); + writeln!( + f, + "Environment ({})", + self.kind + .map(|v| format!("{v:?}")) + .unwrap_or("Unknown".to_string()) + ) + .unwrap_or_default(); if let Some(name) = &self.display_name { writeln!(f, " Display-Name: {name}").unwrap_or_default(); } From 1c42877e57da0945d317d925f227deb870c41e9f Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:34:08 +1000 Subject: [PATCH 08/16] more fixes --- crates/pet/tests/ci_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index 84c50a6c..634fb132 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -136,7 +136,7 @@ fn check_if_virtualenvwrapper_exists() { setup(); let reporter = test::create_reporter(); - let environment = EnvironmentApi::new(); + let environment = Arc::new(EnvironmentApi::new()); let conda_locator = Arc::new(Conda::from(environment.clone())); find_and_report_envs( From 519e029ddbd7eaee4a15f86643f9881fbef9c7e0 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 08:36:35 +1000 Subject: [PATCH 09/16] wip --- crates/pet-conda/tests/ci_test.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/pet-conda/tests/ci_test.rs b/crates/pet-conda/tests/ci_test.rs index cb6a6d07..1c19e51f 100644 --- a/crates/pet-conda/tests/ci_test.rs +++ b/crates/pet-conda/tests/ci_test.rs @@ -74,7 +74,7 @@ fn detect_conda_root_from_path() { python_environment::PythonEnvironmentKind, Locator, }; use pet_python_utils::env::PythonEnv; - use std::path::PathBuf; + use std::{path::PathBuf, sync::Arc}; setup(); let env = Arc::new(EnvironmentApi::new()); @@ -110,7 +110,7 @@ fn detect_new_conda_env() { os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator, }; use pet_reporter::test::create_reporter; - use std::path::PathBuf; + use std::{path::PathBuf, sync::Arc}; setup(); let env_name = "env_with_python"; @@ -168,7 +168,7 @@ fn detect_conda_env_from_path() { python_environment::PythonEnvironmentKind, Locator, }; use pet_python_utils::env::PythonEnv; - use std::path::PathBuf; + use std::{path::PathBuf, sync::Arc}; setup(); let env = Arc::new(EnvironmentApi::new()); @@ -214,7 +214,7 @@ fn detect_new_conda_env_without_python() { os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator, }; use pet_reporter::test::create_reporter; - use std::path::PathBuf; + use std::{path::PathBuf, sync::Arc}; setup(); let env_name = "env_without_python"; @@ -259,6 +259,8 @@ fn detect_new_conda_env_without_python() { #[allow(dead_code)] // Detect envs created without Python in a custom directory using the -p flag fn detect_new_conda_env_created_with_p_flag_without_python() { + use std::sync::Arc; + use common::resolve_test_path; use pet_conda::Conda; use pet_core::{ @@ -307,6 +309,8 @@ fn detect_new_conda_env_created_with_p_flag_without_python() { #[allow(dead_code)] // Detect envs created Python in a custom directory using the -p flag fn detect_new_conda_env_created_with_p_flag_with_python() { + use std::sync::Arc; + use common::resolve_test_path; use pet_conda::Conda; use pet_core::{ From c2e4ae3befb19dc85e88a604879c26f6c289ad61 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:15:34 +1000 Subject: [PATCH 10/16] Fixes --- crates/pet-conda/src/environment_locations.rs | 14 ++++------ crates/pet-conda/src/environments.rs | 7 ++--- crates/pet-conda/src/lib.rs | 8 +++++- crates/pet-conda/src/manager.rs | 8 +++--- crates/pet-conda/src/utils.rs | 15 +++-------- crates/pet-global-virtualenvs/src/lib.rs | 12 ++++----- .../pet-homebrew/src/environment_locations.rs | 6 ++--- crates/pet-pipenv/src/lib.rs | 6 ++--- crates/pet-pipenv/src/manager.rs | 2 ++ crates/pet-pyenv/src/environment_locations.rs | 8 +++--- crates/pet-pyenv/src/manager.rs | 8 +++--- crates/pet-python-utils/src/executable.rs | 16 +++++------- crates/pet-python-utils/src/headers.rs | 2 +- crates/pet-python-utils/src/pyvenv_cfg.rs | 4 +-- crates/pet-venv/src/lib.rs | 5 ++++ crates/pet-virtualenv/src/lib.rs | 5 +--- .../src/environment_locations.rs | 12 ++++----- crates/pet/src/find.rs | 26 ++++++++++--------- 18 files changed, 76 insertions(+), 88 deletions(-) create mode 100644 crates/pet-pipenv/src/manager.rs diff --git a/crates/pet-conda/src/environment_locations.rs b/crates/pet-conda/src/environment_locations.rs index e276e457..f281c49c 100644 --- a/crates/pet-conda/src/environment_locations.rs +++ b/crates/pet-conda/src/environment_locations.rs @@ -90,10 +90,8 @@ fn get_conda_environment_paths_from_known_paths(env_vars: &EnvVariables) -> Vec< if let Ok(entries) = fs::read_dir(full_path) { for entry in entries.filter_map(Result::ok) { let path = entry.path(); - if let Ok(meta) = fs::metadata(&path) { - if meta.is_dir() { - env_paths.push(path); - } + if path.is_dir() { + env_paths.push(path); } } } @@ -112,10 +110,8 @@ fn get_conda_environment_paths_from_additional_paths( if let Ok(entries) = fs::read_dir(path) { for entry in entries.filter_map(Result::ok) { let path = entry.path(); - if let Ok(meta) = fs::metadata(&path) { - if meta.is_dir() { - env_paths.push(path); - } + if path.is_dir() { + env_paths.push(path); } } } @@ -146,7 +142,7 @@ pub fn get_environments(conda_dir: &Path) -> Vec { } } else if is_conda_env(conda_dir) { envs.push(conda_dir.to_path_buf()); - } else if fs::metadata(conda_dir.join("envs")).is_ok() { + } else if conda_dir.join("envs").exists() { // This could be a directory where conda environments are stored. // I.e. its not necessarily the root conda install directory. // E.g. C:\Users\donjayamanne\.conda diff --git a/crates/pet-conda/src/environments.rs b/crates/pet-conda/src/environments.rs index 7b29b0a4..f2028aba 100644 --- a/crates/pet-conda/src/environments.rs +++ b/crates/pet-conda/src/environments.rs @@ -14,10 +14,7 @@ use pet_core::{ }; use pet_fs::path::{norm_case, resolve_symlink}; use pet_python_utils::executable::{find_executable, find_executables}; -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::path::{Path, PathBuf}; #[derive(Debug, Clone)] pub struct CondaEnvironment { @@ -84,7 +81,7 @@ pub fn get_conda_environment_info( None => get_conda_installation_used_to_create_conda_env(env_path), }; if let Some(conda_dir) = &conda_install_folder { - if fs::metadata(conda_dir).is_err() { + if !conda_dir.exists() { warn!( "Conda install folder {}, does not exist, hence will not be used for the Conda Env: {}", env_path.display(), diff --git a/crates/pet-conda/src/lib.rs b/crates/pet-conda/src/lib.rs index 034a71a8..5786c05c 100644 --- a/crates/pet-conda/src/lib.rs +++ b/crates/pet-conda/src/lib.rs @@ -3,7 +3,7 @@ use conda_info::CondaInfo; use env_variables::EnvVariables; -use environment_locations::{get_conda_environment_paths, get_environments}; +use environment_locations::{get_conda_environment_paths, get_conda_envs_from_environment_txt, get_environments}; use environments::{get_conda_environment_info, CondaEnvironment}; use log::{error, warn}; use manager::CondaManager; @@ -224,6 +224,12 @@ impl Locator for Conda { let additional_paths = self.env_dirs.lock().unwrap().clone(); thread::scope(|s| { // 1. Get a list of all know conda environments file paths + get_conda_envs_from_environment_txt(env_vars).con + get_conda_environment_paths_from_conda_rc(env_vars).con + get_conda_environment_paths_from_known_paths(env_vars).con + get_conda_environment_paths_from_additional_paths(additional_env_dirs).con + get_known_conda_install_locations(env_vars).con + let possible_conda_envs = get_conda_environment_paths(&env_vars, &additional_paths); for path in possible_conda_envs { s.spawn(move || { diff --git a/crates/pet-conda/src/manager.rs b/crates/pet-conda/src/manager.rs index 4073c039..ffeca204 100644 --- a/crates/pet-conda/src/manager.rs +++ b/crates/pet-conda/src/manager.rs @@ -23,7 +23,7 @@ fn get_conda_executable(path: &Path) -> Option { for relative_path in relative_path_to_conda_exe { let exe = path.join(&relative_path); - if exe.metadata().is_ok() { + if exe.exists() { return Some(exe); } } @@ -49,10 +49,8 @@ pub fn find_conda_binary(env_vars: &EnvVariables) -> Option { for path in env::split_paths(&paths) { for bin in get_conda_bin_names() { let conda_path = path.join(bin); - if let Ok(metadata) = std::fs::metadata(&conda_path) { - if metadata.is_file() || metadata.is_symlink() { - return Some(conda_path); - } + if conda_path.is_file() || conda_path.is_symlink() { + return Some(conda_path); } } } diff --git a/crates/pet-conda/src/utils.rs b/crates/pet-conda/src/utils.rs index 46e93833..b6fcdc94 100644 --- a/crates/pet-conda/src/utils.rs +++ b/crates/pet-conda/src/utils.rs @@ -1,25 +1,18 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::path::{Path, PathBuf}; /// conda-meta must exist as this contains a mandatory `history` file. pub fn is_conda_install(path: &Path) -> bool { - (path.join("condabin").metadata().is_ok() || path.join("envs").metadata().is_ok()) - && path.join("conda-meta").metadata().is_ok() + (path.join("condabin").exists() || path.join("envs").exists()) + && path.join("conda-meta").exists() } /// conda-meta must exist as this contains a mandatory `history` file. /// The root conda installation folder is also a conda environment (its the base environment). pub fn is_conda_env(path: &Path) -> bool { - if let Ok(metadata) = fs::metadata(path.join("conda-meta")) { - metadata.is_dir() - } else { - false - } + path.join("conda-meta").is_dir() } /// Only used in tests, noop in production. diff --git a/crates/pet-global-virtualenvs/src/lib.rs b/crates/pet-global-virtualenvs/src/lib.rs index 5241cc53..4e3763e3 100644 --- a/crates/pet-global-virtualenvs/src/lib.rs +++ b/crates/pet-global-virtualenvs/src/lib.rs @@ -14,12 +14,12 @@ fn get_global_virtualenv_dirs( if let Some(work_on_home) = work_on_home_env_var { let work_on_home = norm_case(PathBuf::from(work_on_home)); - if fs::metadata(&work_on_home).is_ok() { + if work_on_home.exists() { venv_dirs.push(work_on_home); } else if let Some(home) = &user_home { if let Ok(work_on_home) = work_on_home.strip_prefix("~") { let work_on_home = home.join(work_on_home); - if fs::metadata(&work_on_home).is_ok() { + if work_on_home.exists() { venv_dirs.push(work_on_home); } } @@ -28,7 +28,7 @@ fn get_global_virtualenv_dirs( // Used by pipenv (https://github.com/pypa/pipenv/blob/main/pipenv/utils/shell.py#L184) if let Some(xdg_data_home) = xdg_data_home.map(|d| PathBuf::from(d).join("virtualenvs")) { - if fs::metadata(&xdg_data_home).is_ok() { + if xdg_data_home.exists() { venv_dirs.push(xdg_data_home); } } @@ -41,7 +41,7 @@ fn get_global_virtualenv_dirs( PathBuf::from(".local").join("share").join("virtualenvs"), // Used by pipenv (https://github.com/pypa/pipenv/blob/main/pipenv/utils/shell.py#L184) ] { let venv_dir = home.join(dir); - if fs::metadata(&venv_dir).is_ok() { + if venv_dir.exists() { venv_dirs.push(venv_dir); } } @@ -49,11 +49,11 @@ fn get_global_virtualenv_dirs( // https://virtualenvwrapper.readthedocs.io/en/latest/index.html // Default recommended location for virtualenvwrapper let envs = PathBuf::from("Envs"); - if fs::metadata(&envs).is_ok() { + if envs.exists() { venv_dirs.push(envs); } let envs = PathBuf::from("envs"); - if fs::metadata(&envs).is_ok() { + if envs.exists() { venv_dirs.push(envs); } } diff --git a/crates/pet-homebrew/src/environment_locations.rs b/crates/pet-homebrew/src/environment_locations.rs index c6af2d59..08e88e91 100644 --- a/crates/pet-homebrew/src/environment_locations.rs +++ b/crates/pet-homebrew/src/environment_locations.rs @@ -4,7 +4,7 @@ use crate::env_variables::EnvVariables; use lazy_static::lazy_static; use regex::Regex; -use std::{fs, path::PathBuf}; +use std::path::PathBuf; lazy_static! { static ref PYTHON_VERSION: Regex = @@ -41,9 +41,7 @@ pub fn get_homebrew_prefix_bin(env_vars: &EnvVariables) -> Vec { // Check the environment variables if let Some(homebrew_prefix) = &env_vars.homebrew_prefix { let homebrew_prefix_bin = PathBuf::from(homebrew_prefix).join("bin"); - if fs::metadata(&homebrew_prefix_bin).is_ok() - && !homebrew_prefixes.contains(&homebrew_prefix_bin) - { + if homebrew_prefix_bin.exists() && !homebrew_prefixes.contains(&homebrew_prefix_bin) { homebrew_prefixes.push(homebrew_prefix_bin); } } diff --git a/crates/pet-pipenv/src/lib.rs b/crates/pet-pipenv/src/lib.rs index 34214226..336b2b2c 100644 --- a/crates/pet-pipenv/src/lib.rs +++ b/crates/pet-pipenv/src/lib.rs @@ -38,7 +38,7 @@ fn get_pipenv_project_from_prefix(prefix: &Path) -> Option { let project_file = prefix.join(".project"); let contents = fs::read_to_string(project_file).ok()?; let project_folder = norm_case(PathBuf::from(contents.trim().to_string())); - if fs::metadata(&project_folder).is_ok() { + if project_folder.exists() { Some(project_folder) } else { None @@ -47,14 +47,14 @@ fn get_pipenv_project_from_prefix(prefix: &Path) -> Option { fn is_pipenv(env: &PythonEnv, env_vars: &EnvVariables) -> bool { if let Some(project_path) = get_pipenv_project(env) { - if fs::metadata(project_path.join(env_vars.pipenv_pipfile.clone())).is_ok() { + if project_path.join(env_vars.pipenv_pipfile.clone()).exists() { return true; } } // If we have a Pipfile, then this is a pipenv environment. // Else likely a virtualenvwrapper or the like. if let Some(project_path) = get_pipenv_project(env) { - fs::metadata(project_path.join(env_vars.pipenv_pipfile.clone())).is_ok() + project_path.join(env_vars.pipenv_pipfile.clone()).exists() } else { false } diff --git a/crates/pet-pipenv/src/manager.rs b/crates/pet-pipenv/src/manager.rs new file mode 100644 index 00000000..fc36ab24 --- /dev/null +++ b/crates/pet-pipenv/src/manager.rs @@ -0,0 +1,2 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. diff --git a/crates/pet-pyenv/src/environment_locations.rs b/crates/pet-pyenv/src/environment_locations.rs index e99cef26..d73c4024 100644 --- a/crates/pet-pyenv/src/environment_locations.rs +++ b/crates/pet-pyenv/src/environment_locations.rs @@ -3,7 +3,7 @@ use crate::env_variables::EnvVariables; use pet_fs::path::norm_case; -use std::{fs, path::PathBuf}; +use std::path::PathBuf; #[cfg(windows)] pub fn get_home_pyenv_dir(env_vars: &EnvVariables) -> Option { @@ -24,10 +24,8 @@ pub fn get_binary_from_known_paths(env_vars: &EnvVariables) -> Option { } else { known_path.join("pyenv") }; - if let Ok(metadata) = fs::metadata(&exe) { - if metadata.is_file() { - return Some(norm_case(exe)); - } + if exe.is_file() { + return Some(norm_case(exe)); } } None diff --git a/crates/pet-pyenv/src/manager.rs b/crates/pet-pyenv/src/manager.rs index 1efcfee9..df3fc04d 100644 --- a/crates/pet-pyenv/src/manager.rs +++ b/crates/pet-pyenv/src/manager.rs @@ -41,11 +41,11 @@ fn get_pyenv_info(environment: &EnvVariables) -> PyEnvInfo { }; if let Some(dir) = get_pyenv_dir(environment) { let versions = dir.join("versions"); - if fs::metadata(&versions).is_ok() { + if versions.exists() { pyenv.versions = Some(versions); } let exe = dir.join("bin").join("pyenv"); - if fs::metadata(&exe).is_ok() { + if exe.exists() { pyenv.exe = Some(exe); } } @@ -57,13 +57,13 @@ fn get_pyenv_info(environment: &EnvVariables) -> PyEnvInfo { if let Some(path) = get_home_pyenv_dir(environment) { if pyenv.exe.is_none() { let exe = path.join("bin").join("pyenv"); - if fs::metadata(&exe).is_ok() { + if exe.exists() { pyenv.exe = Some(exe); } } if pyenv.versions.is_none() { let versions = path.join("versions"); - if fs::metadata(&versions).is_ok() { + if versions.exists() { pyenv.versions = Some(versions); } } diff --git a/crates/pet-python-utils/src/executable.rs b/crates/pet-python-utils/src/executable.rs index cbabd6c2..49c88772 100644 --- a/crates/pet-python-utils/src/executable.rs +++ b/crates/pet-python-utils/src/executable.rs @@ -25,7 +25,7 @@ pub fn find_executable(env_path: &Path) -> Option { env_path.join("python3.exe"), ] .into_iter() - .find(|path| fs::metadata(path).is_ok()) + .find(|path| path.exists()) } #[cfg(unix)] @@ -37,7 +37,7 @@ pub fn find_executable(env_path: &Path) -> Option { env_path.join("python3"), ] .into_iter() - .find(|path| fs::metadata(path).is_ok()) + .find(|path| path.exists()) } pub fn find_executables>(env_path: T) -> Vec { @@ -48,7 +48,7 @@ pub fn find_executables>(env_path: T) -> Vec { let mut python_executables = vec![]; let bin = if cfg!(windows) { "Scripts" } else { "bin" }; let mut env_path = env_path.as_ref().to_path_buf(); - if env_path.join(bin).metadata().is_ok() { + if env_path.join(bin).exists() { env_path = env_path.join(bin); } @@ -72,18 +72,16 @@ pub fn find_executables>(env_path: T) -> Vec { // If you install python@3.10, then only a python3.10 exe is created in that bin directory. // As a compromise, we only enumerate if this is a bin directory and there are no python exes // Else enumerating entire directories is very expensive. - if env_path.join(python_exe).metadata().is_ok() - || env_path.join(python3_exe).metadata().is_ok() + if env_path.join(python_exe).exists() + || env_path.join(python3_exe).exists() || env_path.ends_with(bin) { // Enumerate this directory and get all `python` & `pythonX.X` files. if let Ok(entries) = fs::read_dir(env_path) { for entry in entries.filter_map(Result::ok) { let file = entry.path(); - if let Ok(metadata) = fs::metadata(&file) { - if is_python_executable_name(&entry.path()) && metadata.is_file() { - python_executables.push(file); - } + if file.is_file() && is_python_executable_name(&file) { + python_executables.push(file); } } } diff --git a/crates/pet-python-utils/src/headers.rs b/crates/pet-python-utils/src/headers.rs index c454fc92..ebdd4fc4 100644 --- a/crates/pet-python-utils/src/headers.rs +++ b/crates/pet-python-utils/src/headers.rs @@ -41,7 +41,7 @@ pub fn get_version(path: &Path) -> Option { let mut contents = "".to_string(); if let Ok(result) = fs::read_to_string(patchlevel_h) { contents = result; - } else if fs::metadata(&headers_path).is_err() { + } else if !headers_path.exists() { // TODO: Remove this check, unnecessary, as we try to read the dir below. // Such a path does not exist, get out. continue; diff --git a/crates/pet-python-utils/src/pyvenv_cfg.rs b/crates/pet-python-utils/src/pyvenv_cfg.rs index a36e2d17..01f06015 100644 --- a/crates/pet-python-utils/src/pyvenv_cfg.rs +++ b/crates/pet-python-utils/src/pyvenv_cfg.rs @@ -44,14 +44,14 @@ fn find(path: &Path) -> Option { // Check if the pyvenv.cfg file is in the current directory. // Possible the passed value is the `env`` directory. let cfg = path.join(PYVENV_CONFIG_FILE); - if fs::metadata(&cfg).is_ok() { + if cfg.exists() { return Some(cfg); } let bin = if cfg!(windows) { "Scripts" } else { "bin" }; if path.ends_with(bin) { let cfg = path.parent()?.join(PYVENV_CONFIG_FILE); - if fs::metadata(&cfg).is_ok() { + if cfg.exists() { return Some(cfg); } } diff --git a/crates/pet-venv/src/lib.rs b/crates/pet-venv/src/lib.rs index 46253428..8fc94ec0 100644 --- a/crates/pet-venv/src/lib.rs +++ b/crates/pet-venv/src/lib.rs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +use std::path::Path; + use pet_core::{ python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind}, reporter::Reporter, @@ -20,6 +22,9 @@ fn is_venv_internal(env: &PythonEnv) -> Option { pub fn is_venv(env: &PythonEnv) -> bool { is_venv_internal(env).unwrap_or_default() } +pub fn is_venv_dir(path: &Path) -> bool { + PyVenvCfg::find(path).is_some() +} pub struct Venv {} impl Venv { diff --git a/crates/pet-virtualenv/src/lib.rs b/crates/pet-virtualenv/src/lib.rs index 7e5e11af..d67d3c30 100644 --- a/crates/pet-virtualenv/src/lib.rs +++ b/crates/pet-virtualenv/src/lib.rs @@ -8,7 +8,6 @@ use pet_core::{ }; use pet_python_utils::version; use pet_python_utils::{env::PythonEnv, executable::find_executables}; -use std::fs; pub fn is_virtualenv(env: &PythonEnv) -> bool { if env.prefix.is_none() { @@ -31,9 +30,7 @@ pub fn is_virtualenv(env: &PythonEnv) -> bool { // const directory = path.dirname(interpreterPath); // const files = await fsapi.readdir(directory); // const regex = /^activate(\.([A-z]|\d)+)?$/i; - if fs::metadata(bin.join("activate")).is_ok() - || fs::metadata(bin.join("activate.bat")).is_ok() - { + if bin.join("activate").exists() || bin.join("activate.bat").exists() { return true; } diff --git a/crates/pet-virtualenvwrapper/src/environment_locations.rs b/crates/pet-virtualenvwrapper/src/environment_locations.rs index 4246edf3..72c2e6c2 100644 --- a/crates/pet-virtualenvwrapper/src/environment_locations.rs +++ b/crates/pet-virtualenvwrapper/src/environment_locations.rs @@ -3,7 +3,7 @@ use crate::env_variables::EnvVariables; use pet_fs::path::norm_case; -use std::{fs, path::PathBuf}; +use std::path::PathBuf; #[cfg(windows)] fn get_default_virtualenvwrapper_path(env_vars: &EnvVariables) -> Option { @@ -13,11 +13,11 @@ fn get_default_virtualenvwrapper_path(env_vars: &EnvVariables) -> Option Option Option { - use std::fs; - if let Some(home) = &env_vars.home { let home = home.join(".virtualenvs"); - if fs::metadata(&home).is_ok() { + if home.exists() { return Some(norm_case(&home)); } } @@ -43,7 +41,7 @@ pub fn get_work_on_home_path(environment: &EnvVariables) -> Option { if let Some(work_on_home) = &environment.workon_home { // TODO: Why do we need to canonicalize the path? if let Ok(work_on_home) = std::fs::canonicalize(work_on_home) { - if fs::metadata(&work_on_home).is_ok() { + if work_on_home.exists() { return Some(norm_case(&work_on_home)); } } diff --git a/crates/pet/src/find.rs b/crates/pet/src/find.rs index 62f9059b..997bade3 100644 --- a/crates/pet/src/find.rs +++ b/crates/pet/src/find.rs @@ -13,6 +13,7 @@ use pet_python_utils::env::PythonEnv; use pet_python_utils::executable::{ find_executable, find_executables, should_search_for_environments_in_path, }; +use pet_venv::is_venv_dir; use std::collections::BTreeMap; use std::fs; use std::path::PathBuf; @@ -178,17 +179,18 @@ fn find_python_environments_in_workspace_folders_recursive( ) { thread::scope(|s| { s.spawn(|| { - let bin = if cfg!(windows) { "Scripts" } else { "bin" }; for workspace_folder in workspace_folders { + let paths_to_search_first = vec![ + // Possible this is a virtual env + workspace_folder.clone(), + // Optimize for finding these first. + workspace_folder.join(".venv"), + workspace_folder.join(".conda"), + workspace_folder.join(".virtualenv"), + workspace_folder.join("venv"), + ]; find_python_environments_in_paths_with_locators( - vec![ - // Possible this is a virtual env - workspace_folder.clone(), - // Optimize for finding these first. - workspace_folder.join(".venv"), - // Optimize for finding these first. - workspace_folder.join(".conda"), - ], + paths_to_search_first.clone(), locators, reporter, true, @@ -196,9 +198,8 @@ fn find_python_environments_in_workspace_folders_recursive( Some(workspace_folder.clone()), ); - if workspace_folder.join(bin).exists() { - // If the folder has a bin or scripts, then ignore it, its most likely an env. - // I.e. no point looking for python environments in a Python environment. + // If this is a virtual env folder, no need to scan this. + if is_venv_dir(&workspace_folder) { continue; } @@ -208,6 +209,7 @@ fn find_python_environments_in_workspace_folders_recursive( .filter(|d| d.file_type().is_ok_and(|f| f.is_dir())) .map(|p| p.path()) .filter(should_search_for_environments_in_path) + .filter(|p| !paths_to_search_first.contains(p)) { find_python_environments( vec![folder], From d220d39ef61c8ecacdaf6601236660c538f2962f Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:41:07 +1000 Subject: [PATCH 11/16] Fixes --- crates/pet-conda/src/env_variables.rs | 4 +-- crates/pet-conda/src/lib.rs | 10 ++---- crates/pet-env-var-path/src/lib.rs | 4 +-- crates/pet-homebrew/src/env_variables.rs | 4 +-- crates/pet-homebrew/src/lib.rs | 4 +-- crates/pet-pipenv/src/env_variables.rs | 4 +-- crates/pet-pipenv/src/lib.rs | 3 +- crates/pet-poetry/src/env_variables.rs | 4 +-- crates/pet-poetry/src/lib.rs | 4 +-- crates/pet-pyenv/src/env_variables.rs | 4 +-- crates/pet-pyenv/src/lib.rs | 2 +- .../src/env_variables.rs | 4 +-- crates/pet-virtualenvwrapper/src/lib.rs | 4 +-- crates/pet-windows-store/src/env_variables.rs | 4 +-- crates/pet-windows-store/src/lib.rs | 2 +- crates/pet/src/find.rs | 36 +++++++++---------- crates/pet/src/jsonrpc.rs | 20 ++++++----- crates/pet/src/lib.rs | 8 ++--- crates/pet/src/locators.rs | 17 ++++----- crates/pet/src/resolve.rs | 2 +- crates/pet/tests/ci_test.rs | 3 +- 21 files changed, 69 insertions(+), 78 deletions(-) diff --git a/crates/pet-conda/src/env_variables.rs b/crates/pet-conda/src/env_variables.rs index 5403a5b6..aa1bebc8 100644 --- a/crates/pet-conda/src/env_variables.rs +++ b/crates/pet-conda/src/env_variables.rs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; use pet_core::os_environment::Environment; @@ -26,7 +26,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { EnvVariables { home: env.get_user_home(), root: env.get_root(), diff --git a/crates/pet-conda/src/lib.rs b/crates/pet-conda/src/lib.rs index 5786c05c..8ee17ef9 100644 --- a/crates/pet-conda/src/lib.rs +++ b/crates/pet-conda/src/lib.rs @@ -3,7 +3,7 @@ use conda_info::CondaInfo; use env_variables::EnvVariables; -use environment_locations::{get_conda_environment_paths, get_conda_envs_from_environment_txt, get_environments}; +use environment_locations::{get_conda_environment_paths, get_environments}; use environments::{get_conda_environment_info, CondaEnvironment}; use log::{error, warn}; use manager::CondaManager; @@ -44,7 +44,7 @@ pub struct Conda { } impl Conda { - pub fn from(env: Arc) -> Conda { + pub fn from(env: &dyn Environment) -> Conda { Conda { env_dirs: Arc::new(Mutex::new(vec![])), environments: Arc::new(Mutex::new(HashMap::new())), @@ -224,12 +224,6 @@ impl Locator for Conda { let additional_paths = self.env_dirs.lock().unwrap().clone(); thread::scope(|s| { // 1. Get a list of all know conda environments file paths - get_conda_envs_from_environment_txt(env_vars).con - get_conda_environment_paths_from_conda_rc(env_vars).con - get_conda_environment_paths_from_known_paths(env_vars).con - get_conda_environment_paths_from_additional_paths(additional_env_dirs).con - get_known_conda_install_locations(env_vars).con - let possible_conda_envs = get_conda_environment_paths(&env_vars, &additional_paths); for path in possible_conda_envs { s.spawn(move || { diff --git a/crates/pet-env-var-path/src/lib.rs b/crates/pet-env-var-path/src/lib.rs index 04dc6a9b..95740859 100644 --- a/crates/pet-env-var-path/src/lib.rs +++ b/crates/pet-env-var-path/src/lib.rs @@ -2,9 +2,9 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; -pub fn get_search_paths_from_env_variables(environment: Arc) -> Vec { +pub fn get_search_paths_from_env_variables(environment: &dyn Environment) -> Vec { // Exclude files from this folder, as they would have been discovered elsewhere (widows_store) // Also the exe is merely a pointer to another file. if let Some(home) = environment.get_user_home() { diff --git a/crates/pet-homebrew/src/env_variables.rs b/crates/pet-homebrew/src/env_variables.rs index ae6b90fd..860b7cff 100644 --- a/crates/pet-homebrew/src/env_variables.rs +++ b/crates/pet-homebrew/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -20,7 +20,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { EnvVariables { home: env.get_user_home(), root: env.get_root(), diff --git a/crates/pet-homebrew/src/lib.rs b/crates/pet-homebrew/src/lib.rs index f1d1c999..29b2419e 100644 --- a/crates/pet-homebrew/src/lib.rs +++ b/crates/pet-homebrew/src/lib.rs @@ -13,7 +13,7 @@ use pet_core::{ use pet_fs::path::resolve_symlink; use pet_python_utils::{env::PythonEnv, executable::find_executables}; use pet_virtualenv::is_virtualenv; -use std::{fs, path::PathBuf, sync::Arc, thread}; +use std::{fs, path::PathBuf, thread}; use sym_links::is_homebrew_python; mod env_variables; @@ -26,7 +26,7 @@ pub struct Homebrew { } impl Homebrew { - pub fn from(environment: Arc) -> Homebrew { + pub fn from(environment: &dyn Environment) -> Homebrew { Homebrew { environment: EnvVariables::from(environment), } diff --git a/crates/pet-pipenv/src/env_variables.rs b/crates/pet-pipenv/src/env_variables.rs index ad615aab..cd0e7ab7 100644 --- a/crates/pet-pipenv/src/env_variables.rs +++ b/crates/pet-pipenv/src/env_variables.rs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -use std::sync::Arc; - use pet_core::os_environment::Environment; #[derive(Debug, Clone)] @@ -15,7 +13,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { EnvVariables { pipenv_max_depth: env .get_env_var("PIPENV_MAX_DEPTH".to_string()) diff --git a/crates/pet-pipenv/src/lib.rs b/crates/pet-pipenv/src/lib.rs index 336b2b2c..c6ac20bb 100644 --- a/crates/pet-pipenv/src/lib.rs +++ b/crates/pet-pipenv/src/lib.rs @@ -13,7 +13,6 @@ use pet_python_utils::env::PythonEnv; use pet_python_utils::executable::find_executables; use pet_python_utils::version; use std::path::Path; -use std::sync::Arc; use std::{fs, path::PathBuf}; mod env_variables; @@ -65,7 +64,7 @@ pub struct PipEnv { } impl PipEnv { - pub fn from(environment: Arc) -> PipEnv { + pub fn from(environment: &dyn Environment) -> PipEnv { PipEnv { env_vars: EnvVariables::from(environment), } diff --git a/crates/pet-poetry/src/env_variables.rs b/crates/pet-poetry/src/env_variables.rs index e3516bec..c76c572f 100644 --- a/crates/pet-poetry/src/env_variables.rs +++ b/crates/pet-poetry/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -26,7 +26,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { let mut poetry_home = None; let home = env.get_user_home(); if let (Some(home), Some(poetry_home_value)) = diff --git a/crates/pet-poetry/src/lib.rs b/crates/pet-poetry/src/lib.rs index dd716232..66f20c61 100644 --- a/crates/pet-poetry/src/lib.rs +++ b/crates/pet-poetry/src/lib.rs @@ -37,7 +37,7 @@ pub struct Poetry { } impl Poetry { - pub fn new(environment: Arc) -> Self { + pub fn new(environment: &dyn Environment) -> Self { Poetry { searched: AtomicBool::new(false), search_result: Arc::new(Mutex::new(None)), @@ -46,7 +46,7 @@ impl Poetry { poetry_executable: Arc::new(Mutex::new(None)), } } - pub fn from(environment: Arc) -> impl Locator { + pub fn from(environment: &dyn Environment) -> impl Locator { Poetry::new(environment) } pub fn find_with_executable(&self) -> Option<()> { diff --git a/crates/pet-pyenv/src/env_variables.rs b/crates/pet-pyenv/src/env_variables.rs index 81e2586e..c12ec853 100644 --- a/crates/pet-pyenv/src/env_variables.rs +++ b/crates/pet-pyenv/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -17,7 +17,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { EnvVariables { home: env.get_user_home(), root: env.get_root(), diff --git a/crates/pet-pyenv/src/lib.rs b/crates/pet-pyenv/src/lib.rs index bcbad900..e460c616 100644 --- a/crates/pet-pyenv/src/lib.rs +++ b/crates/pet-pyenv/src/lib.rs @@ -35,7 +35,7 @@ pub struct PyEnv { impl PyEnv { pub fn from( - environment: Arc, + environment: &dyn Environment, conda_locator: Arc, ) -> impl Locator { PyEnv { diff --git a/crates/pet-virtualenvwrapper/src/env_variables.rs b/crates/pet-virtualenvwrapper/src/env_variables.rs index 283e674c..1a8f2e28 100644 --- a/crates/pet-virtualenvwrapper/src/env_variables.rs +++ b/crates/pet-virtualenvwrapper/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -13,7 +13,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { EnvVariables { home: env.get_user_home(), workon_home: env.get_env_var("WORKON_HOME".to_string()), diff --git a/crates/pet-virtualenvwrapper/src/lib.rs b/crates/pet-virtualenvwrapper/src/lib.rs index 2e9cbd87..a52ba8d1 100644 --- a/crates/pet-virtualenvwrapper/src/lib.rs +++ b/crates/pet-virtualenvwrapper/src/lib.rs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::sync::Arc; - use env_variables::EnvVariables; use environments::{get_project, is_virtualenvwrapper}; use pet_core::{ @@ -23,7 +21,7 @@ pub struct VirtualEnvWrapper { } impl VirtualEnvWrapper { - pub fn from(environment: Arc) -> VirtualEnvWrapper { + pub fn from(environment: &dyn Environment) -> VirtualEnvWrapper { VirtualEnvWrapper { env_vars: EnvVariables::from(environment), } diff --git a/crates/pet-windows-store/src/env_variables.rs b/crates/pet-windows-store/src/env_variables.rs index 7ee5ef20..ab4af6bb 100644 --- a/crates/pet-windows-store/src/env_variables.rs +++ b/crates/pet-windows-store/src/env_variables.rs @@ -2,7 +2,7 @@ // Licensed under the MIT License. use pet_core::os_environment::Environment; -use std::{path::PathBuf, sync::Arc}; +use std::path::PathBuf; #[derive(Debug, Clone)] // NOTE: Do not implement Default trait, as we do not want to ever forget to set the values. @@ -12,7 +12,7 @@ pub struct EnvVariables { } impl EnvVariables { - pub fn from(env: Arc) -> Self { + pub fn from(env: &dyn Environment) -> Self { EnvVariables { home: env.get_user_home(), } diff --git a/crates/pet-windows-store/src/lib.rs b/crates/pet-windows-store/src/lib.rs index 3e9ae3ed..aa6d2b22 100644 --- a/crates/pet-windows-store/src/lib.rs +++ b/crates/pet-windows-store/src/lib.rs @@ -30,7 +30,7 @@ pub struct WindowsStore { } impl WindowsStore { - pub fn from(environment: Arc) -> WindowsStore { + pub fn from(environment: &dyn Environment) -> WindowsStore { WindowsStore { searched: AtomicBool::new(false), env_vars: EnvVariables::from(environment), diff --git a/crates/pet/src/find.rs b/crates/pet/src/find.rs index 997bade3..1ec6dcb5 100644 --- a/crates/pet/src/find.rs +++ b/crates/pet/src/find.rs @@ -8,7 +8,6 @@ use pet_core::reporter::Reporter; use pet_core::{Configuration, Locator}; use pet_env_var_path::get_search_paths_from_env_variables; use pet_global_virtualenvs::list_global_virtual_envs_paths; -use pet_poetry::Poetry; use pet_python_utils::env::PythonEnv; use pet_python_utils::executable::{ find_executable, find_executables, should_search_for_environments_in_path, @@ -37,7 +36,7 @@ pub fn find_and_report_envs( configuration: Configuration, locators: &Arc>>, conda_locator: Arc, - os_environment: Arc, + os_environment: &dyn Environment, ) -> Arc> { let summary = Arc::new(Mutex::new(Summary { time: Duration::from_secs(0), @@ -55,7 +54,7 @@ pub fn find_and_report_envs( let conda_executable = configuration.conda_executable; thread::scope(|s| { // 1. Find using known global locators. - let os_env = os_environment.clone(); + // let os_env = os_environment.clone(); s.spawn(|| { // Find in all the finders let start = std::time::Instant::now(); @@ -84,20 +83,20 @@ pub fn find_and_report_envs( conda_locator.find_with_conda_executable(conda_executable); Some(()) }); - // By now all poetry envs have been found - // Spawn poetry exe in a separate thread. - // & see if we can find more environments by spawning poetry. - // But we will not wait for this to complete. - thread::spawn(move || { - Poetry::new(os_env).find_with_executable(); - Some(()) - }); + // // By now all poetry envs have been found + // // Spawn poetry exe in a separate thread. + // // & see if we can find more environments by spawning poetry. + // // But we will not wait for this to complete. + // thread::spawn(move || { + // Poetry::new(os_environment).find_with_executable(); + // Some(()) + // }); }); // Step 2: Search in PATH variable - let os_env = os_environment.clone(); s.spawn(|| { let start = std::time::Instant::now(); - let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_env); + let global_env_search_paths: Vec = + get_search_paths_from_env_variables(os_environment); trace!( "Searching for environments in global folders: {:?}", @@ -114,19 +113,20 @@ pub fn find_and_report_envs( summary.lock().unwrap().find_path_time = start.elapsed(); }); // Step 3: Search in some global locations for virtual envs. - let os_env = os_environment.clone(); + // let os_env = os_environment.clone(); s.spawn(|| { let start = std::time::Instant::now(); let search_paths: Vec = [ list_global_virtual_envs_paths( - os_env.get_env_var("WORKON_HOME".into()), - os_env.get_env_var("XDG_DATA_HOME".into()), - os_env.get_user_home(), + os_environment.get_env_var("WORKON_HOME".into()), + os_environment.get_env_var("XDG_DATA_HOME".into()), + os_environment.get_user_home(), ), environment_directories, ] .concat(); - let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_env); + let global_env_search_paths: Vec = + get_search_paths_from_env_variables(os_environment); trace!( "Searching for environments in global venv folders: {:?}", diff --git a/crates/pet/src/jsonrpc.rs b/crates/pet/src/jsonrpc.rs index 46a73730..1f06e918 100644 --- a/crates/pet/src/jsonrpc.rs +++ b/crates/pet/src/jsonrpc.rs @@ -18,6 +18,7 @@ use pet_telemetry::report_inaccuracies_identified_after_resolving; use serde::{Deserialize, Serialize}; use serde_json::{self, Value}; use std::{ + ops::Deref, path::PathBuf, sync::{Arc, RwLock}, thread, @@ -39,16 +40,16 @@ pub fn start_jsonrpc_server() { // These are globals for the the lifetime of the server. // Hence passed around as Arcs via the context. - let environment = Arc::new(EnvironmentApi::new()); + let environment = EnvironmentApi::new(); let jsonrpc_reporter = Arc::new(jsonrpc::create_reporter()); let reporter = Arc::new(CacheReporter::new(jsonrpc_reporter.clone())); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let conda_locator = Arc::new(Conda::from(&environment)); let context = Context { reporter, - locators: create_locators(conda_locator.clone(), environment.clone()), + locators: create_locators(conda_locator.clone(), &environment), conda_locator, configuration: RwLock::new(Configuration::default()), - os_environment: environment, + os_environment: Arc::new(environment), }; let mut handlers = HandlersKeyedByMethodName::new(Arc::new(context)); @@ -103,7 +104,7 @@ pub fn handle_refresh(context: Arc, id: u32, params: Value) { config, &context.locators, context.conda_locator.clone(), - context.os_environment.clone(), + context.os_environment.deref(), ); let summary = summary.lock().unwrap(); for locator in summary.find_locators_times.iter() { @@ -154,9 +155,12 @@ pub fn handle_resolve(context: Arc, id: u32, params: Value) { thread::spawn(move || { let now = SystemTime::now(); trace!("Resolving env {:?}", executable); - if let Some(result) = - resolve_environment(&executable, &context.locators, search_paths, environment) - { + if let Some(result) = resolve_environment( + &executable, + &context.locators, + search_paths, + environment.deref(), + ) { if let Some(resolved) = result.resolved { // Gather telemetry of this resolved env and see what we got wrong. let _ = report_inaccuracies_identified_after_resolving( diff --git a/crates/pet/src/lib.rs b/crates/pet/src/lib.rs index f1c138af..b45a4275 100644 --- a/crates/pet/src/lib.rs +++ b/crates/pet/src/lib.rs @@ -22,19 +22,19 @@ pub fn find_and_report_envs_stdio(print_list: bool, print_summary: bool, verbose let stdio_reporter = Arc::new(stdio::create_reporter(print_list)); let reporter = CacheReporter::new(stdio_reporter.clone()); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); let mut config = Configuration::default(); if let Ok(cwd) = env::current_dir() { config.project_directories = Some(vec![cwd]); } - let locators = create_locators(conda_locator.clone(), environment.clone()); + let locators = create_locators(conda_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); } - let summary = find_and_report_envs(&reporter, config, &locators, conda_locator, environment); + let summary = find_and_report_envs(&reporter, config, &locators, conda_locator, &environment); if print_summary { let summary = summary.lock().unwrap(); diff --git a/crates/pet/src/locators.rs b/crates/pet/src/locators.rs index 8f55b9b0..9848f15e 100644 --- a/crates/pet/src/locators.rs +++ b/crates/pet/src/locators.rs @@ -25,7 +25,7 @@ use std::sync::Arc; pub fn create_locators( conda_locator: Arc, - os_environment: Arc, + os_environment: &dyn Environment, ) -> Arc>> { // NOTE: The order of the items matter. @@ -39,21 +39,18 @@ pub fn create_locators( #[cfg(windows)] use pet_windows_store::WindowsStore; #[cfg(windows)] - locators.push(Arc::new(WindowsStore::from(os_environment.clone()))); + locators.push(Arc::new(WindowsStore::from(os_environment))); #[cfg(windows)] locators.push(Arc::new(WindowsRegistry::from(conda_locator.clone()))) } // 3. Pyenv Python - locators.push(Arc::new(PyEnv::from( - os_environment.clone(), - conda_locator.clone(), - ))); + locators.push(Arc::new(PyEnv::from(os_environment, conda_locator.clone()))); // 4. Homebrew Python if cfg!(unix) { #[cfg(unix)] use pet_homebrew::Homebrew; #[cfg(unix)] - let homebrew_locator = Homebrew::from(os_environment.clone()); + let homebrew_locator = Homebrew::from(os_environment); #[cfg(unix)] locators.push(Arc::new(homebrew_locator)); } @@ -62,9 +59,9 @@ pub fn create_locators( // 6. Support for Virtual Envs // The order of these matter. // Basically PipEnv is a superset of VirtualEnvWrapper, which is a superset of Venv, which is a superset of VirtualEnv. - locators.push(Arc::new(Poetry::from(os_environment.clone()))); - locators.push(Arc::new(PipEnv::from(os_environment.clone()))); - locators.push(Arc::new(VirtualEnvWrapper::from(os_environment.clone()))); + locators.push(Arc::new(Poetry::from(os_environment))); + locators.push(Arc::new(PipEnv::from(os_environment))); + locators.push(Arc::new(VirtualEnvWrapper::from(os_environment))); locators.push(Arc::new(Venv::new())); // VirtualEnv is the most generic, hence should be the last. locators.push(Arc::new(VirtualEnv::new())); diff --git a/crates/pet/src/resolve.rs b/crates/pet/src/resolve.rs index a1e9864a..d3f0c477 100644 --- a/crates/pet/src/resolve.rs +++ b/crates/pet/src/resolve.rs @@ -25,7 +25,7 @@ pub fn resolve_environment( executable: &PathBuf, locators: &Arc>>, search_paths: Vec, - os_environment: Arc, + os_environment: &dyn Environment, ) -> Option { // First check if this is a known environment let env = PythonEnv::new(executable.to_owned(), None, None); diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index 634fb132..32eae3a2 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -350,7 +350,8 @@ fn verify_we_can_get_same_env_info_using_from_with_exe( for locator in locators.iter() { locator.configure(&config); } - let global_env_search_paths: Vec = get_search_paths_from_env_variables(os_environment); + let global_env_search_paths: Vec = + get_search_paths_from_env_variables(&os_environment); let env = PythonEnv::new(executable.clone(), None, None); let resolved = identify_python_environment_using_locators( From e3b4f67ff3bada2bed900e38ed65b45315cbd7f5 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:49:59 +1000 Subject: [PATCH 12/16] fixes --- crates/pet-conda/tests/ci_test.rs | 42 +++++++++------------ crates/pet-conda/tests/lib_test.rs | 11 +++--- crates/pet-pyenv/tests/pyenv_test.rs | 36 ++++++++---------- crates/pet/tests/ci_homebrew_container.rs | 8 ++-- crates/pet/tests/ci_poetry.rs | 8 ++-- crates/pet/tests/ci_test.rs | 46 +++++++++++------------ 6 files changed, 70 insertions(+), 81 deletions(-) diff --git a/crates/pet-conda/tests/ci_test.rs b/crates/pet-conda/tests/ci_test.rs index 1c19e51f..3f44703f 100644 --- a/crates/pet-conda/tests/ci_test.rs +++ b/crates/pet-conda/tests/ci_test.rs @@ -23,8 +23,6 @@ fn setup() { #[allow(dead_code)] // We should detect the conda install along with the base env fn detect_conda_root() { - use std::sync::Arc; - use pet_conda::Conda; use pet_core::{ manager::EnvManagerType, os_environment::EnvironmentApi, @@ -33,10 +31,10 @@ fn detect_conda_root() { use pet_reporter::test::create_reporter; setup(); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); let reporter = create_reporter(); - let conda = Conda::from(env); + let conda = Conda::from(&env); conda.find(&reporter); let result = reporter.get_result(); @@ -74,14 +72,14 @@ fn detect_conda_root_from_path() { python_environment::PythonEnvironmentKind, Locator, }; use pet_python_utils::env::PythonEnv; - use std::{path::PathBuf, sync::Arc}; + use std::path::PathBuf; setup(); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); let info = get_conda_info(); let conda_dir = PathBuf::from(info.conda_prefix.clone()); let exe = conda_dir.join("bin").join("python"); - let conda = Conda::from(env); + let conda = Conda::from(&env); let python_env = PythonEnv::new(exe, Some(conda_dir.clone()), None); let env = conda.try_from(&python_env).unwrap(); @@ -110,7 +108,7 @@ fn detect_new_conda_env() { os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator, }; use pet_reporter::test::create_reporter; - use std::{path::PathBuf, sync::Arc}; + use std::path::PathBuf; setup(); let env_name = "env_with_python"; @@ -118,9 +116,9 @@ fn detect_new_conda_env() { &CondaCreateEnvNameOrPath::Name(env_name.into()), Some("3.10".into()), ); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); - let conda = Conda::from(env); + let conda = Conda::from(&env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); @@ -168,10 +166,10 @@ fn detect_conda_env_from_path() { python_environment::PythonEnvironmentKind, Locator, }; use pet_python_utils::env::PythonEnv; - use std::{path::PathBuf, sync::Arc}; + use std::path::PathBuf; setup(); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); let info = get_conda_info(); let env_name = "env_with_python2"; create_conda_env( @@ -181,7 +179,7 @@ fn detect_conda_env_from_path() { let conda_dir = PathBuf::from(info.conda_prefix.clone()); let prefix = conda_dir.join("envs").join(env_name); let exe = prefix.join("bin").join("python"); - let conda = Conda::from(env); + let conda = Conda::from(&env); let python_env = PythonEnv::new(exe.clone(), Some(prefix.clone()), None); let env = conda.try_from(&python_env).unwrap(); @@ -214,14 +212,14 @@ fn detect_new_conda_env_without_python() { os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator, }; use pet_reporter::test::create_reporter; - use std::{path::PathBuf, sync::Arc}; + use std::path::PathBuf; setup(); let env_name = "env_without_python"; create_conda_env(&CondaCreateEnvNameOrPath::Name(env_name.into()), None); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); - let conda = Conda::from(env); + let conda = Conda::from(&env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); @@ -259,8 +257,6 @@ fn detect_new_conda_env_without_python() { #[allow(dead_code)] // Detect envs created without Python in a custom directory using the -p flag fn detect_new_conda_env_created_with_p_flag_without_python() { - use std::sync::Arc; - use common::resolve_test_path; use pet_conda::Conda; use pet_core::{ @@ -272,9 +268,9 @@ fn detect_new_conda_env_created_with_p_flag_without_python() { let env_name = "env_without_python3"; let prefix = resolve_test_path(&["unix", env_name]); create_conda_env(&CondaCreateEnvNameOrPath::Path(prefix.clone()), None); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); - let conda = Conda::from(env); + let conda = Conda::from(&env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); @@ -309,8 +305,6 @@ fn detect_new_conda_env_created_with_p_flag_without_python() { #[allow(dead_code)] // Detect envs created Python in a custom directory using the -p flag fn detect_new_conda_env_created_with_p_flag_with_python() { - use std::sync::Arc; - use common::resolve_test_path; use pet_conda::Conda; use pet_core::{ @@ -326,9 +320,9 @@ fn detect_new_conda_env_created_with_p_flag_with_python() { &CondaCreateEnvNameOrPath::Path(prefix.clone()), Some("3.10".into()), ); - let env = Arc::new(EnvironmentApi::new()); + let env = EnvironmentApi::new(); - let conda = Conda::from(env); + let conda = Conda::from(&env); let reporter = create_reporter(); conda.find(&reporter); let result = reporter.get_result(); diff --git a/crates/pet-conda/tests/lib_test.rs b/crates/pet-conda/tests/lib_test.rs index 7f35d506..eb6524d4 100644 --- a/crates/pet-conda/tests/lib_test.rs +++ b/crates/pet-conda/tests/lib_test.rs @@ -10,10 +10,10 @@ fn find_conda_env_without_manager() { use pet_conda::Conda; use pet_core::{self, arch::Architecture, python_environment::PythonEnvironmentKind, Locator}; use pet_python_utils::env::PythonEnv; - use std::{collections::HashMap, sync::Arc}; + use std::collections::HashMap; - let environment = Arc::new(create_test_environment(HashMap::new(), None, vec![], None)); - let locator = Conda::from(environment.clone()); + let environment = create_test_environment(HashMap::new(), None, vec![], None); + let locator = Conda::from(&environment); let path = resolve_test_path(&["unix", "conda_env_without_manager", "env_python_3"]); let env = locator @@ -43,11 +43,10 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() { use std::{ collections::HashMap, fs::{self}, - sync::Arc, }; - let environment = Arc::new(create_test_environment(HashMap::new(), None, vec![], None)); - let locator = Conda::from(environment); + let environment = create_test_environment(HashMap::new(), None, vec![], None); + let locator = Conda::from(&environment); let path = resolve_test_path(&[ "unix", "conda_env_without_manager_but_found_in_history", diff --git a/crates/pet-pyenv/tests/pyenv_test.rs b/crates/pet-pyenv/tests/pyenv_test.rs index 1fa427cb..e03b3937 100644 --- a/crates/pet-pyenv/tests/pyenv_test.rs +++ b/crates/pet-pyenv/tests/pyenv_test.rs @@ -14,15 +14,15 @@ fn does_not_find_any_pyenv_envs() { use pet_reporter::test::create_reporter; use std::{collections::HashMap, path::PathBuf, sync::Arc}; - let environment = Arc::new(create_test_environment( + let environment = create_test_environment( HashMap::new(), Some(PathBuf::from("SOME_BOGUS_HOME_DIR")), vec![], None, - )); + ); - let conda = Arc::new(Conda::from(environment.clone())); - let locator = PyEnv::from(environment.clone(), conda); + let conda = Arc::new(Conda::from(&environment)); + let locator = PyEnv::from(&environment, conda); let reporter = create_reporter(); locator.find(&reporter); let result = reporter.get_result(); @@ -58,15 +58,15 @@ fn does_not_find_any_pyenv_envs_even_with_pyenv_installed() { "bin", ]); let pyenv_exe = resolve_test_path(&[homebrew_bin.to_str().unwrap(), "pyenv"]); - let environment = Arc::new(create_test_environment( + let environment = create_test_environment( HashMap::new(), Some(home.clone()), vec![PathBuf::from(homebrew_bin)], None, - )); + ); - let conda = Arc::new(Conda::from(environment.clone())); - let locator = PyEnv::from(environment.clone(), conda); + let conda = Arc::new(Conda::from(&environment)); + let locator = PyEnv::from(&environment, conda); let reporter = create_reporter(); locator.find(&reporter); let result = reporter.get_result(); @@ -114,15 +114,15 @@ fn find_pyenv_envs() { ]); let conda_exe = conda_dir.join("bin").join("conda"); - let environment = Arc::new(create_test_environment( + let environment = create_test_environment( HashMap::new(), Some(home.clone()), vec![PathBuf::from(homebrew_bin)], None, - )); + ); - let conda = Arc::new(Conda::from(environment.clone())); - let locator = PyEnv::from(environment.clone(), conda); + let conda = Arc::new(Conda::from(&environment)); + let locator = PyEnv::from(&environment, conda); let reporter = create_reporter(); locator.find(&reporter); let mut result = reporter.get_result(); @@ -381,15 +381,11 @@ fn resolve_pyenv_environment() { let homebrew_bin = resolve_test_path(&["unix", "pyenv", "home", "opt", "homebrew", "bin"]); let pyenv_exe = resolve_test_path(&[homebrew_bin.to_str().unwrap(), "pyenv"]); - let environment = Arc::new(create_test_environment( - HashMap::new(), - Some(home.clone()), - vec![homebrew_bin], - None, - )); + let environment = + create_test_environment(HashMap::new(), Some(home.clone()), vec![homebrew_bin], None); - let conda = Arc::new(Conda::from(environment.clone())); - let locator = PyEnv::from(environment.clone(), conda.clone()); + let conda = Arc::new(Conda::from(&environment)); + let locator = PyEnv::from(&environment, conda.clone()); // let mut result = locator.find().unwrap(); let expected_manager = EnvManager { diff --git a/crates/pet/tests/ci_homebrew_container.rs b/crates/pet/tests/ci_homebrew_container.rs index d104981a..73ea605c 100644 --- a/crates/pet/tests/ci_homebrew_container.rs +++ b/crates/pet/tests/ci_homebrew_container.rs @@ -17,15 +17,15 @@ fn verify_python_in_homebrew_contaner() { use std::{path::PathBuf, sync::Arc}; let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone(), environment.clone()), + &create_locators(conda_locator.clone(), &environment), conda_locator, - environment, + &environment, ); let result = reporter.get_result(); diff --git a/crates/pet/tests/ci_poetry.rs b/crates/pet/tests/ci_poetry.rs index 418e880e..6524f21f 100644 --- a/crates/pet/tests/ci_poetry.rs +++ b/crates/pet/tests/ci_poetry.rs @@ -86,11 +86,11 @@ fn verify_ci_poetry_project() { let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone(), environment.clone()); + let locators = create_locators(conda_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); } @@ -100,7 +100,7 @@ fn verify_ci_poetry_project() { Default::default(), &locators, conda_locator, - environment, + &environment, ); let result = reporter.get_result(); diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index 32eae3a2..b3481f5f 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -68,11 +68,11 @@ fn verify_validity_of_discovered_envs() { let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone(), environment.clone()); + let locators = create_locators(conda_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); } @@ -83,7 +83,7 @@ fn verify_validity_of_discovered_envs() { Default::default(), &locators, conda_locator, - environment, + &environment, ); let result = reporter.get_result(); @@ -136,8 +136,8 @@ fn check_if_virtualenvwrapper_exists() { setup(); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( &reporter, @@ -178,15 +178,15 @@ fn check_if_pipenv_exists() { setup(); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone(), environment.clone()), + &create_locators(conda_locator.clone(), &environment), conda_locator, - environment, + &environment, ); let result = reporter.get_result(); @@ -216,8 +216,8 @@ fn check_if_pyenv_virtualenv_exists() { setup(); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( &reporter, @@ -341,12 +341,12 @@ fn verify_we_can_get_same_env_info_using_from_with_exe( use std::{env, sync::Arc}; let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); - let os_environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(os_environment.clone())); + let os_environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&os_environment)); let mut config = Configuration::default(); let search_paths = vec![project_dir.clone()]; config.project_directories = Some(search_paths.clone()); - let locators = create_locators(conda_locator.clone(), os_environment.clone()); + let locators = create_locators(conda_locator.clone(), &os_environment); for locator in locators.iter() { locator.configure(&config); } @@ -528,11 +528,11 @@ fn verify_we_can_get_same_env_info_using_resolve_with_exe( use std::{env, sync::Arc}; let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); - let os_environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(os_environment.clone())); + let os_environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&os_environment)); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone(), os_environment.clone()); + let locators = create_locators(conda_locator.clone(), &os_environment); for locator in locators.iter() { locator.configure(&config); } @@ -541,7 +541,7 @@ fn verify_we_can_get_same_env_info_using_resolve_with_exe( &executable, &locators, vec![project_dir.clone()], - os_environment, + &os_environment, ) .expect(format!("Failed to resolve environment using `resolve` for {environment:?}").as_str()); trace!( @@ -578,15 +578,15 @@ fn verify_bin_usr_bin_user_local_are_separate_python_envs() { setup(); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone(), environment.clone()), + &create_locators(conda_locator.clone(), &environment), conda_locator, - environment, + &environment, ); let result = reporter.get_result(); From 9062c41f2f56f17835ad35ce88a23ffde44ff64d Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:51:31 +1000 Subject: [PATCH 13/16] fixes --- crates/pet/src/find.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/pet/src/find.rs b/crates/pet/src/find.rs index 1ec6dcb5..386f72d1 100644 --- a/crates/pet/src/find.rs +++ b/crates/pet/src/find.rs @@ -36,7 +36,7 @@ pub fn find_and_report_envs( configuration: Configuration, locators: &Arc>>, conda_locator: Arc, - os_environment: &dyn Environment, + environment: &dyn Environment, ) -> Arc> { let summary = Arc::new(Mutex::new(Summary { time: Duration::from_secs(0), @@ -96,7 +96,7 @@ pub fn find_and_report_envs( s.spawn(|| { let start = std::time::Instant::now(); let global_env_search_paths: Vec = - get_search_paths_from_env_variables(os_environment); + get_search_paths_from_env_variables(environment); trace!( "Searching for environments in global folders: {:?}", @@ -118,15 +118,15 @@ pub fn find_and_report_envs( let start = std::time::Instant::now(); let search_paths: Vec = [ list_global_virtual_envs_paths( - os_environment.get_env_var("WORKON_HOME".into()), - os_environment.get_env_var("XDG_DATA_HOME".into()), - os_environment.get_user_home(), + environment.get_env_var("WORKON_HOME".into()), + environment.get_env_var("XDG_DATA_HOME".into()), + environment.get_user_home(), ), environment_directories, ] .concat(); let global_env_search_paths: Vec = - get_search_paths_from_env_variables(os_environment); + get_search_paths_from_env_variables(environment); trace!( "Searching for environments in global venv folders: {:?}", From 15ed6d05609170f845126d765d23ff6e68362733 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:53:08 +1000 Subject: [PATCH 14/16] more fixes --- crates/pet/tests/ci_jupyter_container.rs | 6 +++--- crates/pet/tests/ci_test.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/pet/tests/ci_jupyter_container.rs b/crates/pet/tests/ci_jupyter_container.rs index d6398b03..22bbcdc2 100644 --- a/crates/pet/tests/ci_jupyter_container.rs +++ b/crates/pet/tests/ci_jupyter_container.rs @@ -36,14 +36,14 @@ fn verify_python_in_jupyter_contaner() { let reporter = test::create_reporter(); let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone(), environment.clone()), + &create_locators(conda_locator.clone(), &environment), conda_locator, - environment, + &environment, ); let result = reporter.get_result(); diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index b3481f5f..3cad719e 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -142,9 +142,9 @@ fn check_if_virtualenvwrapper_exists() { find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone(), environment.clone()), + &create_locators(conda_locator.clone(), &environment), conda_locator, - environment.clone(), + &environment, ); let result = reporter.get_result(); @@ -222,9 +222,9 @@ fn check_if_pyenv_virtualenv_exists() { find_and_report_envs( &reporter, Default::default(), - &create_locators(conda_locator.clone(), environment.clone()), + &create_locators(conda_locator.clone(), &environment), conda_locator, - environment.clone(), + &environment, ); let result = reporter.get_result(); From 05672724f40397c9ad64a13020b0a67643141de2 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:57:12 +1000 Subject: [PATCH 15/16] fixes --- crates/pet-pipenv/src/manager.rs | 2 -- crates/pet/tests/ci_jupyter_container.rs | 2 +- crates/pet/tests/ci_poetry.rs | 8 ++++---- 3 files changed, 5 insertions(+), 7 deletions(-) delete mode 100644 crates/pet-pipenv/src/manager.rs diff --git a/crates/pet-pipenv/src/manager.rs b/crates/pet-pipenv/src/manager.rs deleted file mode 100644 index fc36ab24..00000000 --- a/crates/pet-pipenv/src/manager.rs +++ /dev/null @@ -1,2 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. diff --git a/crates/pet/tests/ci_jupyter_container.rs b/crates/pet/tests/ci_jupyter_container.rs index 22bbcdc2..44c863f3 100644 --- a/crates/pet/tests/ci_jupyter_container.rs +++ b/crates/pet/tests/ci_jupyter_container.rs @@ -35,7 +35,7 @@ fn verify_python_in_jupyter_contaner() { setup(); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); + let environment = EnvironmentApi::new(); let conda_locator = Arc::new(Conda::from(&environment)); find_and_report_envs( diff --git a/crates/pet/tests/ci_poetry.rs b/crates/pet/tests/ci_poetry.rs index 6524f21f..5fbf06c4 100644 --- a/crates/pet/tests/ci_poetry.rs +++ b/crates/pet/tests/ci_poetry.rs @@ -20,11 +20,11 @@ fn verify_ci_poetry_global() { let project_dir = PathBuf::from(env::var("GITHUB_WORKSPACE").unwrap_or_default()); let reporter = test::create_reporter(); - let environment = Arc::new(EnvironmentApi::new()); - let conda_locator = Arc::new(Conda::from(environment.clone())); + let environment = EnvironmentApi::new(); + let conda_locator = Arc::new(Conda::from(&environment)); let mut config = Configuration::default(); config.project_directories = Some(vec![project_dir.clone()]); - let locators = create_locators(conda_locator.clone(), environment.clone()); + let locators = create_locators(conda_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); } @@ -34,7 +34,7 @@ fn verify_ci_poetry_global() { Default::default(), &locators, conda_locator, - environment, + &environment, ); let result = reporter.get_result(); From a79cda7d74e99724cdb05fb06405efaef3726df8 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 5 Jul 2024 11:59:00 +1000 Subject: [PATCH 16/16] Misc --- crates/pet/src/find.rs | 2 -- crates/pet/src/locators.rs | 14 +++++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/crates/pet/src/find.rs b/crates/pet/src/find.rs index 386f72d1..3acbf122 100644 --- a/crates/pet/src/find.rs +++ b/crates/pet/src/find.rs @@ -54,7 +54,6 @@ pub fn find_and_report_envs( let conda_executable = configuration.conda_executable; thread::scope(|s| { // 1. Find using known global locators. - // let os_env = os_environment.clone(); s.spawn(|| { // Find in all the finders let start = std::time::Instant::now(); @@ -113,7 +112,6 @@ pub fn find_and_report_envs( summary.lock().unwrap().find_path_time = start.elapsed(); }); // Step 3: Search in some global locations for virtual envs. - // let os_env = os_environment.clone(); s.spawn(|| { let start = std::time::Instant::now(); let search_paths: Vec = [ diff --git a/crates/pet/src/locators.rs b/crates/pet/src/locators.rs index 9848f15e..9aca6d4a 100644 --- a/crates/pet/src/locators.rs +++ b/crates/pet/src/locators.rs @@ -25,7 +25,7 @@ use std::sync::Arc; pub fn create_locators( conda_locator: Arc, - os_environment: &dyn Environment, + environment: &dyn Environment, ) -> Arc>> { // NOTE: The order of the items matter. @@ -39,18 +39,18 @@ pub fn create_locators( #[cfg(windows)] use pet_windows_store::WindowsStore; #[cfg(windows)] - locators.push(Arc::new(WindowsStore::from(os_environment))); + locators.push(Arc::new(WindowsStore::from(environment))); #[cfg(windows)] locators.push(Arc::new(WindowsRegistry::from(conda_locator.clone()))) } // 3. Pyenv Python - locators.push(Arc::new(PyEnv::from(os_environment, conda_locator.clone()))); + locators.push(Arc::new(PyEnv::from(environment, conda_locator.clone()))); // 4. Homebrew Python if cfg!(unix) { #[cfg(unix)] use pet_homebrew::Homebrew; #[cfg(unix)] - let homebrew_locator = Homebrew::from(os_environment); + let homebrew_locator = Homebrew::from(environment); #[cfg(unix)] locators.push(Arc::new(homebrew_locator)); } @@ -59,9 +59,9 @@ pub fn create_locators( // 6. Support for Virtual Envs // The order of these matter. // Basically PipEnv is a superset of VirtualEnvWrapper, which is a superset of Venv, which is a superset of VirtualEnv. - locators.push(Arc::new(Poetry::from(os_environment))); - locators.push(Arc::new(PipEnv::from(os_environment))); - locators.push(Arc::new(VirtualEnvWrapper::from(os_environment))); + locators.push(Arc::new(Poetry::from(environment))); + locators.push(Arc::new(PipEnv::from(environment))); + locators.push(Arc::new(VirtualEnvWrapper::from(environment))); locators.push(Arc::new(Venv::new())); // VirtualEnv is the most generic, hence should be the last. locators.push(Arc::new(VirtualEnv::new()));