Skip to content

Commit 7b20a20

Browse files
authored
Cache python interpreter info in file (#115)
1 parent 060ed51 commit 7b20a20

File tree

15 files changed

+385
-134
lines changed

15 files changed

+385
-134
lines changed

crates/pet-linux-global-python/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn find_and_report_global_pythons_in(
115115
}
116116
if let Some(resolved) = ResolvedPythonEnv::from(exe) {
117117
if let Some(env) = get_python_in_bin(&resolved.to_python_env(), resolved.is64_bit) {
118+
resolved.add_to_cache(env.clone());
118119
let mut reported_executables = reported_executables.lock().unwrap();
119120
// env.symlinks = Some([symlinks, env.symlinks.clone().unwrap_or_default()].concat());
120121
if let Some(symlinks) = &env.symlinks {

crates/pet-mac-commandlinetools/src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ impl Locator for MacCmdLineTools {
9797
}
9898
}
9999

100+
let mut resolved_environments = vec![];
101+
100102
// We know /usr/bin/python3 can end up pointing to this same Python exe as well
101103
// Hence look for those symlinks as well.
102104
// Unfortunately /usr/bin/python3 is not a real symlink
@@ -105,6 +107,8 @@ impl Locator for MacCmdLineTools {
105107
if !symlinks.contains(&possible_exes) {
106108
if let Some(resolved_env) = ResolvedPythonEnv::from(&possible_exes) {
107109
if symlinks.contains(&resolved_env.executable) {
110+
resolved_environments.push(resolved_env.clone());
111+
108112
symlinks.push(possible_exes);
109113
// Use the latest accurate information we have.
110114
version = Some(resolved_env.version);
@@ -163,19 +167,27 @@ impl Locator for MacCmdLineTools {
163167
}
164168
if version.is_none() || prefix.is_none() {
165169
if let Some(resolved_env) = ResolvedPythonEnv::from(&env.executable) {
170+
resolved_environments.push(resolved_env.clone());
166171
version = Some(resolved_env.version);
167172
prefix = Some(resolved_env.prefix);
168173
}
169174
}
170175

171-
Some(
172-
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacCommandLineTools))
173-
.executable(Some(env.executable.clone()))
174-
.version(version)
175-
.prefix(prefix)
176-
.symlinks(Some(symlinks))
177-
.build(),
178-
)
176+
let env = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacCommandLineTools))
177+
.executable(Some(env.executable.clone()))
178+
.version(version)
179+
.prefix(prefix)
180+
.symlinks(Some(symlinks.clone()))
181+
.build();
182+
183+
// If we had spawned Python, then ensure we cache the details.
184+
// We do this here, to ensure we keep track of the symlinks as well,
185+
// I.e. if any of the symlinks change, then the cache is invalidated.
186+
for resolved_env in resolved_environments {
187+
resolved_env.add_to_cache(env.clone());
188+
}
189+
190+
Some(env)
179191
}
180192

181193
fn find(&self, _reporter: &dyn Reporter) {

crates/pet-mac-xcode/src/lib.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ impl Locator for MacXCode {
9595
}
9696
}
9797

98+
let mut resolved_environments = vec![];
99+
98100
// We know /usr/bin/python3 can end up pointing to this same Python exe as well
99101
// Hence look for those symlinks as well.
100102
// Unfortunately /usr/bin/python3 is not a real symlink
@@ -103,6 +105,7 @@ impl Locator for MacXCode {
103105
if !symlinks.contains(&possible_exes) {
104106
if let Some(resolved_env) = ResolvedPythonEnv::from(&possible_exes) {
105107
if symlinks.contains(&resolved_env.executable) {
108+
resolved_environments.push(resolved_env.clone());
106109
symlinks.push(possible_exes);
107110
// Use the latest accurate information we have.
108111
version = Some(resolved_env.version);
@@ -150,19 +153,27 @@ impl Locator for MacXCode {
150153
}
151154
if version.is_none() || prefix.is_none() {
152155
if let Some(resolved_env) = ResolvedPythonEnv::from(&env.executable) {
156+
resolved_environments.push(resolved_env.clone());
153157
version = Some(resolved_env.version);
154158
prefix = Some(resolved_env.prefix);
155159
}
156160
}
157161

158-
Some(
159-
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacXCode))
160-
.executable(Some(env.executable.clone()))
161-
.version(version)
162-
.prefix(prefix)
163-
.symlinks(Some(symlinks))
164-
.build(),
165-
)
162+
let env = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacXCode))
163+
.executable(Some(env.executable.clone()))
164+
.version(version)
165+
.prefix(prefix)
166+
.symlinks(Some(symlinks))
167+
.build();
168+
169+
// If we had spawned Python, then ensure we cache the details.
170+
// We do this here, to ensure we keep track of the symlinks as well,
171+
// I.e. if any of the symlinks change, then the cache is invalidated.
172+
for resolved_env in resolved_environments {
173+
resolved_env.add_to_cache(env.clone());
174+
}
175+
176+
Some(env)
166177
}
167178

168179
fn find(&self, _reporter: &dyn Reporter) {

crates/pet-poetry/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use env_variables::EnvVariables;
55
use environment_locations::list_environments;
6+
use log::trace;
67
use manager::PoetryManager;
78
use pet_core::{
89
env::PythonEnv,
@@ -69,6 +70,7 @@ impl Poetry {
6970
self.poetry_executable.lock().unwrap().clone(),
7071
&self.env_vars,
7172
);
73+
trace!("Poetry Manager {:?}", manager);
7274
let mut result = LocatorResult {
7375
managers: vec![],
7476
environments: vec![],

crates/pet-pyenv/src/lib.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
use std::{
55
fs,
66
path::PathBuf,
7-
sync::{
8-
atomic::{AtomicBool, Ordering},
9-
Arc, Mutex,
10-
},
7+
sync::{Arc, Mutex},
118
thread,
129
};
1310

1411
use env_variables::EnvVariables;
1512
use environments::{get_generic_python_environment, get_virtual_env_environment};
13+
use log::trace;
1614
use manager::PyEnvInfo;
1715
use pet_conda::{utils::is_conda_env, CondaLocator};
1816
use pet_core::{
@@ -33,7 +31,6 @@ mod manager;
3331
pub struct PyEnv {
3432
pub env_vars: EnvVariables,
3533
pub conda_locator: Arc<dyn CondaLocator>,
36-
found_pyenv: AtomicBool,
3734
manager: Arc<Mutex<Option<EnvManager>>>,
3835
versions_dir: Arc<Mutex<Option<PathBuf>>>,
3936
}
@@ -44,24 +41,22 @@ impl PyEnv {
4441
conda_locator: Arc<dyn CondaLocator>,
4542
) -> impl Locator {
4643
PyEnv {
47-
found_pyenv: AtomicBool::new(false),
4844
env_vars: EnvVariables::from(environment),
4945
conda_locator,
5046
manager: Arc::new(Mutex::new(None)),
5147
versions_dir: Arc::new(Mutex::new(None)),
5248
}
5349
}
5450
fn clear(&self) {
55-
self.found_pyenv
56-
.store(false, std::sync::atomic::Ordering::Relaxed);
5751
self.manager.lock().unwrap().take();
5852
self.versions_dir.lock().unwrap().take();
5953
}
6054
fn get_manager_versions_dir(&self) -> (Option<EnvManager>, Option<PathBuf>) {
6155
let mut managers = self.manager.lock().unwrap();
6256
let mut versions = self.versions_dir.lock().unwrap();
63-
if !self.found_pyenv.load(Ordering::Relaxed) && (managers.is_none() || versions.is_none()) {
57+
if managers.is_none() || versions.is_none() {
6458
let pyenv_info = PyEnvInfo::from(&self.env_vars);
59+
trace!("PyEnv Info {:?}", pyenv_info);
6560
if let Some(ref exe) = pyenv_info.exe {
6661
let version = pyenv_info.version.clone();
6762
let manager = EnvManager::new(exe.clone(), EnvManagerType::Pyenv, version);
@@ -74,9 +69,7 @@ impl PyEnv {
7469
} else {
7570
versions.take();
7671
}
77-
self.found_pyenv.store(true, Ordering::Relaxed);
7872
}
79-
8073
(managers.clone(), versions.clone())
8174
}
8275
}
@@ -164,6 +157,8 @@ impl Locator for PyEnv {
164157
}
165158
}
166159
});
160+
} else {
161+
trace!("PyEnv versions directory not found");
167162
}
168163
}
169164
}

0 commit comments

Comments
 (0)