Skip to content

Cache python interpreter info in file #115

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

Merged
merged 6 commits into from
Jul 16, 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
1 change: 1 addition & 0 deletions crates/pet-linux-global-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn find_and_report_global_pythons_in(
}
if let Some(resolved) = ResolvedPythonEnv::from(exe) {
if let Some(env) = get_python_in_bin(&resolved.to_python_env(), resolved.is64_bit) {
resolved.add_to_cache(env.clone());
let mut reported_executables = reported_executables.lock().unwrap();
// env.symlinks = Some([symlinks, env.symlinks.clone().unwrap_or_default()].concat());
if let Some(symlinks) = &env.symlinks {
Expand Down
28 changes: 20 additions & 8 deletions crates/pet-mac-commandlinetools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ impl Locator for MacCmdLineTools {
}
}

let mut resolved_environments = vec![];

// We know /usr/bin/python3 can end up pointing to this same Python exe as well
// Hence look for those symlinks as well.
// Unfortunately /usr/bin/python3 is not a real symlink
Expand All @@ -105,6 +107,8 @@ impl Locator for MacCmdLineTools {
if !symlinks.contains(&possible_exes) {
if let Some(resolved_env) = ResolvedPythonEnv::from(&possible_exes) {
if symlinks.contains(&resolved_env.executable) {
resolved_environments.push(resolved_env.clone());

symlinks.push(possible_exes);
// Use the latest accurate information we have.
version = Some(resolved_env.version);
Expand Down Expand Up @@ -163,19 +167,27 @@ impl Locator for MacCmdLineTools {
}
if version.is_none() || prefix.is_none() {
if let Some(resolved_env) = ResolvedPythonEnv::from(&env.executable) {
resolved_environments.push(resolved_env.clone());
version = Some(resolved_env.version);
prefix = Some(resolved_env.prefix);
}
}

Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacCommandLineTools))
.executable(Some(env.executable.clone()))
.version(version)
.prefix(prefix)
.symlinks(Some(symlinks))
.build(),
)
let env = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacCommandLineTools))
.executable(Some(env.executable.clone()))
.version(version)
.prefix(prefix)
.symlinks(Some(symlinks.clone()))
.build();

// If we had spawned Python, then ensure we cache the details.
// We do this here, to ensure we keep track of the symlinks as well,
// I.e. if any of the symlinks change, then the cache is invalidated.
for resolved_env in resolved_environments {
resolved_env.add_to_cache(env.clone());
}

Some(env)
}

fn find(&self, _reporter: &dyn Reporter) {
Expand Down
27 changes: 19 additions & 8 deletions crates/pet-mac-xcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ impl Locator for MacXCode {
}
}

let mut resolved_environments = vec![];

// We know /usr/bin/python3 can end up pointing to this same Python exe as well
// Hence look for those symlinks as well.
// Unfortunately /usr/bin/python3 is not a real symlink
Expand All @@ -103,6 +105,7 @@ impl Locator for MacXCode {
if !symlinks.contains(&possible_exes) {
if let Some(resolved_env) = ResolvedPythonEnv::from(&possible_exes) {
if symlinks.contains(&resolved_env.executable) {
resolved_environments.push(resolved_env.clone());
symlinks.push(possible_exes);
// Use the latest accurate information we have.
version = Some(resolved_env.version);
Expand Down Expand Up @@ -150,19 +153,27 @@ impl Locator for MacXCode {
}
if version.is_none() || prefix.is_none() {
if let Some(resolved_env) = ResolvedPythonEnv::from(&env.executable) {
resolved_environments.push(resolved_env.clone());
version = Some(resolved_env.version);
prefix = Some(resolved_env.prefix);
}
}

Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacXCode))
.executable(Some(env.executable.clone()))
.version(version)
.prefix(prefix)
.symlinks(Some(symlinks))
.build(),
)
let env = PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::MacXCode))
.executable(Some(env.executable.clone()))
.version(version)
.prefix(prefix)
.symlinks(Some(symlinks))
.build();

// If we had spawned Python, then ensure we cache the details.
// We do this here, to ensure we keep track of the symlinks as well,
// I.e. if any of the symlinks change, then the cache is invalidated.
for resolved_env in resolved_environments {
resolved_env.add_to_cache(env.clone());
}

Some(env)
}

fn find(&self, _reporter: &dyn Reporter) {
Expand Down
2 changes: 2 additions & 0 deletions crates/pet-poetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use env_variables::EnvVariables;
use environment_locations::list_environments;
use log::trace;
use manager::PoetryManager;
use pet_core::{
env::PythonEnv,
Expand Down Expand Up @@ -69,6 +70,7 @@ impl Poetry {
self.poetry_executable.lock().unwrap().clone(),
&self.env_vars,
);
trace!("Poetry Manager {:?}", manager);
let mut result = LocatorResult {
managers: vec![],
environments: vec![],
Expand Down
17 changes: 6 additions & 11 deletions crates/pet-pyenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
use std::{
fs,
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
},
sync::{Arc, Mutex},
thread,
};

use env_variables::EnvVariables;
use environments::{get_generic_python_environment, get_virtual_env_environment};
use log::trace;
use manager::PyEnvInfo;
use pet_conda::{utils::is_conda_env, CondaLocator};
use pet_core::{
Expand All @@ -33,7 +31,6 @@ mod manager;
pub struct PyEnv {
pub env_vars: EnvVariables,
pub conda_locator: Arc<dyn CondaLocator>,
found_pyenv: AtomicBool,
manager: Arc<Mutex<Option<EnvManager>>>,
versions_dir: Arc<Mutex<Option<PathBuf>>>,
}
Expand All @@ -44,24 +41,22 @@ impl PyEnv {
conda_locator: Arc<dyn CondaLocator>,
) -> impl Locator {
PyEnv {
found_pyenv: AtomicBool::new(false),
env_vars: EnvVariables::from(environment),
conda_locator,
manager: Arc::new(Mutex::new(None)),
versions_dir: Arc::new(Mutex::new(None)),
}
}
fn clear(&self) {
self.found_pyenv
.store(false, std::sync::atomic::Ordering::Relaxed);
self.manager.lock().unwrap().take();
self.versions_dir.lock().unwrap().take();
}
fn get_manager_versions_dir(&self) -> (Option<EnvManager>, Option<PathBuf>) {
let mut managers = self.manager.lock().unwrap();
let mut versions = self.versions_dir.lock().unwrap();
if !self.found_pyenv.load(Ordering::Relaxed) && (managers.is_none() || versions.is_none()) {
if managers.is_none() || versions.is_none() {
let pyenv_info = PyEnvInfo::from(&self.env_vars);
trace!("PyEnv Info {:?}", pyenv_info);
if let Some(ref exe) = pyenv_info.exe {
let version = pyenv_info.version.clone();
let manager = EnvManager::new(exe.clone(), EnvManagerType::Pyenv, version);
Expand All @@ -74,9 +69,7 @@ impl PyEnv {
} else {
versions.take();
}
self.found_pyenv.store(true, Ordering::Relaxed);
}

(managers.clone(), versions.clone())
}
}
Expand Down Expand Up @@ -164,6 +157,8 @@ impl Locator for PyEnv {
}
}
});
} else {
trace!("PyEnv versions directory not found");
}
}
}
Loading
Loading