Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support venv, virtualEnv in global folders #23395

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions native_locator/src/global_virtualenvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

use crate::pipenv;
use crate::venv;
use crate::virtualenv;
use crate::virtualenvwrapper;
use crate::{
known,
Expand Down Expand Up @@ -82,6 +84,12 @@ pub fn find_and_report(
if virtualenvwrapper::find_and_report(&env, dispatcher, environment).is_some() {
continue;
}
if venv::find_and_report(&env, dispatcher).is_some() {
continue;
}
if virtualenv::find_and_report(&env, dispatcher).is_some() {
continue;
}
}

None
Expand Down
1 change: 1 addition & 0 deletions native_locator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod global_virtualenvs;
pub mod virtualenvwrapper;
pub mod pipenv;
pub mod virtualenv;
pub mod venv;
3 changes: 2 additions & 1 deletion native_locator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod utils;
mod virtualenv;
mod virtualenvwrapper;
mod windows_python;
mod venv;

fn main() {
let mut dispatcher = create_dispatcher();
Expand Down Expand Up @@ -55,6 +56,6 @@ fn main() {
dispatcher.log_error(&format!("Error getting elapsed time: {:?}", e));
}
}

dispatcher.exit();
}
2 changes: 2 additions & 0 deletions native_locator/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub enum PythonEnvironmentCategory {
WindowsStore,
Pipenv,
VirtualEnvWrapper,
Venv,
VirtualEnv,
}

#[derive(Serialize, Deserialize)]
Expand Down
5 changes: 2 additions & 3 deletions native_locator/src/pyenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::known;
use crate::messaging;
use crate::messaging::EnvManager;
use crate::messaging::EnvManagerType;
use crate::utils::find_and_parse_pyvenv_cfg;
use crate::utils::find_python_binary_path;
use crate::utils::parse_pyenv_cfg;

#[cfg(windows)]
fn get_home_pyenv_dir(environment: &impl known::Environment) -> Option<PathBuf> {
Expand Down Expand Up @@ -124,7 +124,7 @@ fn report_if_virtual_env_environment(
manager: Option<EnvManager>,
dispatcher: &mut impl messaging::MessageDispatcher,
) -> Option<()> {
let pyenv_cfg = parse_pyenv_cfg(path)?;
let pyenv_cfg = find_and_parse_pyvenv_cfg(executable)?;
let folder_name = path.file_name().unwrap().to_string_lossy().to_string();
dispatcher.report_environment(messaging::PythonEnvironment::new(
Some(folder_name),
Expand Down Expand Up @@ -182,7 +182,6 @@ pub fn find_and_report(
{
continue;
}

report_if_virtual_env_environment(&executable, &path, manager.clone(), dispatcher);
}
}
Expand Down
48 changes: 36 additions & 12 deletions native_locator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,41 @@ pub struct PyEnvCfg {
pub version: String,
}

pub fn parse_pyenv_cfg(path: &PathBuf) -> Option<PyEnvCfg> {
let cfg = path.join("pyvenv.cfg");
const PYVENV_CONFIG_FILE: &str = "pyvenv.cfg";

pub fn find_pyvenv_config_path(python_executable: &PathBuf) -> Option<PathBuf> {
// Check if the pyvenv.cfg file is in the parent directory relative to the interpreter.
// env
// |__ pyvenv.cfg <--- check if this file exists
// |__ bin or Scripts
// |__ python <--- interpreterPath
let cfg = python_executable.parent()?.join(PYVENV_CONFIG_FILE);
if fs::metadata(&cfg).is_ok() {
return Some(cfg);
}

// Check if the pyvenv.cfg file is in the directory as the interpreter.
// env
// |__ pyvenv.cfg <--- check if this file exists
// |__ python <--- interpreterPath
let cfg = python_executable
.parent()?
.parent()?
.join(PYVENV_CONFIG_FILE);
if fs::metadata(&cfg).is_ok() {
return Some(cfg);
}

None
}

pub fn find_and_parse_pyvenv_cfg(python_executable: &PathBuf) -> Option<PyEnvCfg> {
let cfg = find_pyvenv_config_path(&PathBuf::from(python_executable))?;
if !fs::metadata(&cfg).is_ok() {
return None;
}

let contents = fs::read_to_string(cfg).ok()?;
let contents = fs::read_to_string(&cfg).ok()?;
let version_regex = Regex::new(r"^version\s*=\s*(\d+\.\d+\.\d+)$").unwrap();
let version_info_regex = Regex::new(r"^version_info\s*=\s*(\d+\.\d+\.\d+.*)$").unwrap();
for line in contents.lines() {
Expand All @@ -51,18 +79,14 @@ pub fn parse_pyenv_cfg(path: &PathBuf) -> Option<PyEnvCfg> {
None
}

pub fn get_version(path: &str) -> Option<String> {
if let Some(parent_folder) = PathBuf::from(path).parent() {
if let Some(pyenv_cfg) = parse_pyenv_cfg(&parent_folder.to_path_buf()) {
pub fn get_version(python_executable: &str) -> Option<String> {
if let Some(parent_folder) = PathBuf::from(python_executable).parent() {
if let Some(pyenv_cfg) = find_and_parse_pyvenv_cfg(&parent_folder.to_path_buf()) {
return Some(pyenv_cfg.version);
}
if let Some(parent_folder) = parent_folder.parent() {
if let Some(pyenv_cfg) = parse_pyenv_cfg(&parent_folder.to_path_buf()) {
return Some(pyenv_cfg.version);
}
}
}
let output = Command::new(path)

let output = Command::new(python_executable)
.arg("-c")
.arg("import sys; print(sys.version)")
.output()
Expand Down
35 changes: 35 additions & 0 deletions native_locator/src/venv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::{
messaging::{MessageDispatcher, PythonEnvironment, PythonEnvironmentCategory},
utils::{self, PythonEnv},
};

pub fn is_venv(env: &PythonEnv) -> bool {
return utils::find_pyvenv_config_path(&env.executable).is_some();
}

pub fn find_and_report(env: &PythonEnv, dispatcher: &mut impl MessageDispatcher) -> Option<()> {
if is_venv(env) {
let env = PythonEnvironment {
name: match env.path.file_name().to_owned() {
Some(name) => Some(name.to_string_lossy().to_owned().to_string()),
None => None,
},
python_executable_path: Some(env.executable.clone()),
category: PythonEnvironmentCategory::Venv,
version: env.version.clone(),
env_path: Some(env.path.clone()),
sys_prefix_path: Some(env.path.clone()),
env_manager: None,
python_run_command: Some(vec![env.executable.to_str().unwrap().to_string()]),
project_path: None,
};

dispatcher.report_environment(env);

return Some(());
}
None
}
28 changes: 26 additions & 2 deletions native_locator/src/virtualenv.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::path::PathBuf;

use crate::messaging::{MessageDispatcher, PythonEnvironment, PythonEnvironmentCategory};
use crate::utils::PythonEnv;
use std::path::PathBuf;

pub fn is_virtualenv(env: &PythonEnv) -> bool {
if let Some(file_path) = PathBuf::from(env.executable.clone()).parent() {
Expand Down Expand Up @@ -41,3 +41,27 @@ pub fn is_virtualenv(env: &PythonEnv) -> bool {

false
}

pub fn find_and_report(env: &PythonEnv, dispatcher: &mut impl MessageDispatcher) -> Option<()> {
if is_virtualenv(env) {
let env = PythonEnvironment {
name: match env.path.file_name().to_owned() {
Some(name) => Some(name.to_string_lossy().to_owned().to_string()),
None => None,
},
python_executable_path: Some(env.executable.clone()),
category: PythonEnvironmentCategory::VirtualEnv,
version: env.version.clone(),
env_path: Some(env.path.clone()),
sys_prefix_path: Some(env.path.clone()),
env_manager: None,
python_run_command: Some(vec![env.executable.to_str().unwrap().to_string()]),
project_path: None,
};

dispatcher.report_environment(env);

return Some(());
}
None
}
34 changes: 16 additions & 18 deletions native_locator/src/virtualenvwrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,6 @@ fn get_work_on_home_path(environment: &impl Environment) -> Option<PathBuf> {
get_default_virtualenvwrapper_path(environment)
}

fn create_virtualenvwrapper_env(env: &PythonEnv) -> PythonEnvironment {
PythonEnvironment {
name: match env.path.file_name().to_owned() {
Some(name) => Some(name.to_string_lossy().to_owned().to_string()),
None => None,
},
python_executable_path: Some(env.executable.clone()),
category: PythonEnvironmentCategory::VirtualEnvWrapper,
version: env.version.clone(),
env_path: Some(env.path.clone()),
sys_prefix_path: Some(env.path.clone()),
env_manager: None,
python_run_command: Some(vec![env.executable.to_str().unwrap().to_string()]),
project_path: None,
}
}

pub fn is_virtualenvwrapper(env: &PythonEnv, environment: &impl Environment) -> bool {
// For environment to be a virtualenvwrapper based it has to follow these two rules:
// 1. It should be in a sub-directory under the WORKON_HOME
Expand All @@ -84,7 +67,22 @@ pub fn find_and_report(
environment: &impl Environment,
) -> Option<()> {
if is_virtualenvwrapper(env, environment) {
dispatcher.report_environment(create_virtualenvwrapper_env(env));
let env = PythonEnvironment {
name: match env.path.file_name().to_owned() {
Some(name) => Some(name.to_string_lossy().to_owned().to_string()),
None => None,
},
python_executable_path: Some(env.executable.clone()),
category: PythonEnvironmentCategory::VirtualEnvWrapper,
version: env.version.clone(),
env_path: Some(env.path.clone()),
sys_prefix_path: Some(env.path.clone()),
env_manager: None,
python_run_command: Some(vec![env.executable.to_str().unwrap().to_string()]),
project_path: None,
};

dispatcher.report_environment(env);
return Some(());
}
None
Expand Down
Loading