-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib.rs
More file actions
106 lines (97 loc) · 3.24 KB
/
lib.rs
File metadata and controls
106 lines (97 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#[cfg(windows)]
use environments::get_registry_pythons;
use pet_conda::{utils::is_conda_env, CondaLocator};
use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator, LocatorKind, LocatorResult,
};
use pet_virtualenv::is_virtualenv;
use std::sync::{Arc, Mutex};
mod environments;
pub struct WindowsRegistry {
#[allow(dead_code)]
conda_locator: Arc<dyn CondaLocator>,
#[allow(dead_code)]
search_result: Arc<Mutex<Option<LocatorResult>>>,
}
impl WindowsRegistry {
pub fn from(conda_locator: Arc<dyn CondaLocator>) -> WindowsRegistry {
WindowsRegistry {
conda_locator,
search_result: Arc::new(Mutex::new(None)),
}
}
#[cfg(windows)]
fn find_with_cache(&self, reporter: Option<&dyn Reporter>) -> Option<LocatorResult> {
let mut result = self
.search_result
.lock()
.expect("search_result mutex poisoned");
if let Some(result) = result.clone() {
return Some(result);
}
let registry_result = get_registry_pythons(&self.conda_locator, &reporter);
result.replace(registry_result.clone());
Some(registry_result)
}
#[cfg(windows)]
fn clear(&self) {
let mut search_result = self
.search_result
.lock()
.expect("search_result mutex poisoned");
search_result.take();
}
}
impl Locator for WindowsRegistry {
fn get_kind(&self) -> LocatorKind {
LocatorKind::WindowsRegistry
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![
PythonEnvironmentKind::WindowsRegistry,
PythonEnvironmentKind::Conda,
]
}
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
// Assume we create a virtual env from a python install,
// Then the exe in the virtual env bin will be a symlink to the homebrew python install.
// Hence the first part of the condition will be true, but the second part will be false.
if is_virtualenv(env) {
return None;
}
// We need to check this here, as its possible to install
// a Python environment via an Installer that ends up in Windows Registry
// However that environment is a conda environment.
if let Some(env_path) = &env.prefix {
if is_conda_env(env_path) {
return None;
}
}
#[cfg(windows)]
if let Some(result) = self.find_with_cache(None) {
// Find the same env here
for found_env in result.environments {
if let Some(ref python_executable_path) = found_env.executable {
if python_executable_path == &env.executable {
return Some(found_env);
}
}
}
}
None
}
#[cfg(windows)]
fn find(&self, reporter: &dyn Reporter) {
self.clear();
let _ = self.find_with_cache(Some(reporter));
}
#[cfg(unix)]
fn find(&self, _reporter: &dyn Reporter) {
//
}
}