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

Cache interpreter info in memory #113

Merged
merged 3 commits into from
Jul 15, 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
16 changes: 2 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions crates/pet-cache/Cargo.toml

This file was deleted.

9 changes: 0 additions & 9 deletions crates/pet-cache/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/pet-conda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use environments::{get_conda_environment_info, CondaEnvironment};
use log::error;
use manager::CondaManager;
use pet_core::{
env::PythonEnv,
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::norm_case;
use pet_python_utils::env::PythonEnv;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
Expand Down
6 changes: 2 additions & 4 deletions crates/pet-conda/tests/ci_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ fn detect_conda_root() {
fn detect_conda_root_from_path() {
use pet_conda::Conda;
use pet_core::{
manager::EnvManagerType, os_environment::EnvironmentApi,
env::PythonEnv, manager::EnvManagerType, os_environment::EnvironmentApi,
python_environment::PythonEnvironmentKind, Locator,
};
use pet_python_utils::env::PythonEnv;
use std::path::PathBuf;

setup();
Expand Down Expand Up @@ -162,10 +161,9 @@ fn detect_new_conda_env() {
fn detect_conda_env_from_path() {
use pet_conda::Conda;
use pet_core::{
manager::EnvManagerType, os_environment::EnvironmentApi,
env::PythonEnv, manager::EnvManagerType, os_environment::EnvironmentApi,
python_environment::PythonEnvironmentKind, Locator,
};
use pet_python_utils::env::PythonEnv;
use std::path::PathBuf;

setup();
Expand Down
12 changes: 8 additions & 4 deletions crates/pet-conda/tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ mod common;
fn find_conda_env_without_manager() {
use common::{create_test_environment, resolve_test_path};
use pet_conda::Conda;
use pet_core::{self, arch::Architecture, python_environment::PythonEnvironmentKind, Locator};
use pet_python_utils::env::PythonEnv;
use pet_core::{
self, arch::Architecture, env::PythonEnv, python_environment::PythonEnvironmentKind,
Locator,
};
use std::collections::HashMap;

let environment = create_test_environment(HashMap::new(), None, vec![], None);
Expand Down Expand Up @@ -38,8 +40,10 @@ fn find_conda_env_without_manager() {
fn find_conda_env_without_manager_but_detect_manager_from_history() {
use common::{create_test_environment, resolve_test_path};
use pet_conda::Conda;
use pet_core::{self, arch::Architecture, python_environment::PythonEnvironmentKind, Locator};
use pet_python_utils::env::PythonEnv;
use pet_core::{
self, arch::Architecture, env::PythonEnv, python_environment::PythonEnvironmentKind,
Locator,
};
use std::{
collections::HashMap,
fs::{self},
Expand Down
1 change: 0 additions & 1 deletion crates/pet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ msvc_spectre_libs = { version = "0.1.1", features = ["error"] }

[dependencies]
pet-fs = { path = "../pet-fs" }
pet-python-utils = { path = "../pet-python-utils" }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to remove this incorrect depenedency, core should not have any other dependencies related to python interpreters and the like.
Hence the large number of files touched.

serde = { version = "1.0.152", features = ["derive"] }
lazy_static = "1.4.0"
regex = "1.10.4"
Expand Down
57 changes: 57 additions & 0 deletions crates/pet-core/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::path::PathBuf;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from pet-python-utils


use pet_fs::path::norm_case;

use crate::pyvenv_cfg::PyVenvCfg;

#[derive(Debug)]
pub struct PythonEnv {
/// Executable of the Python environment.
///
/// Can be `/usr/bin/python` or `/opt/homebrew/bin/python3.12`.
/// Or even the fully (resolved &) qualified path to the python executable such as `/opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12`.
///
/// Note: This can be a symlink as well.
pub executable: PathBuf,
/// Environment prefix
pub prefix: Option<PathBuf>,
/// Version of the Python environment.
pub version: Option<String>,
/// Possible symlink (or known alternative link).
/// For instance:
///
/// If `executable` is `/opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12`,
/// then `symlink`` can be `/opt/homebrew/bin/python3.12` (or vice versa).
pub symlinks: Option<Vec<PathBuf>>,
}

impl PythonEnv {
pub fn new(executable: PathBuf, prefix: Option<PathBuf>, version: Option<String>) -> Self {
let mut prefix = prefix.clone();
if let Some(value) = prefix {
prefix = norm_case(value).into();
}
// if the prefix is not defined, try to get this.
// For instance, if the file is bin/python or Scripts/python
// And we have a pyvenv.cfg file in the parent directory, then we can get the prefix.
if prefix.is_none() {
let mut exe = executable.clone();
exe.pop();
if exe.ends_with("Scripts") || exe.ends_with("bin") {
exe.pop();
if PyVenvCfg::find(&exe).is_some() {
prefix = Some(exe);
}
}
}
Self {
executable: norm_case(executable),
prefix,
version,
symlinks: None,
}
}
}
4 changes: 3 additions & 1 deletion crates/pet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

use std::path::PathBuf;

use env::PythonEnv;
use manager::EnvManager;
use pet_python_utils::env::PythonEnv;
use python_environment::{PythonEnvironment, PythonEnvironmentKind};
use reporter::Reporter;

pub mod arch;
pub mod env;
pub mod manager;
pub mod os_environment;
pub mod python_environment;
pub mod pyvenv_cfg;
pub mod reporter;
pub mod telemetry;

Expand Down
3 changes: 2 additions & 1 deletion crates/pet-homebrew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use env_variables::EnvVariables;
use environment_locations::get_homebrew_prefix_bin;
use environments::get_python_info;
use pet_core::{
env::PythonEnv,
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::{env::PythonEnv, executable::find_executables};
use pet_python_utils::executable::find_executables;
use pet_virtualenv::is_virtualenv;
use std::{fs, path::PathBuf, thread};
use sym_links::is_homebrew_python;
Expand Down
6 changes: 2 additions & 4 deletions crates/pet-linux-global-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ use std::{

use pet_core::{
arch::Architecture,
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::{
env::{PythonEnv, ResolvedPythonEnv},
executable::find_executables,
};
use pet_python_utils::{env::ResolvedPythonEnv, executable::find_executables};
use pet_virtualenv::is_virtualenv;

pub struct LinuxGlobalPython {
Expand Down
6 changes: 2 additions & 4 deletions crates/pet-mac-commandlinetools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// Licensed under the MIT License.

use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::version;
use pet_python_utils::{
env::{PythonEnv, ResolvedPythonEnv},
executable::find_executables,
};
use pet_python_utils::{env::ResolvedPythonEnv, executable::find_executables};
use pet_virtualenv::is_virtualenv;
use std::path::PathBuf;

Expand Down
2 changes: 1 addition & 1 deletion crates/pet-mac-python-org/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT License.

use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::env::PythonEnv;
use pet_python_utils::executable::find_executables;
use pet_python_utils::version;
use pet_virtualenv::is_virtualenv;
Expand Down
6 changes: 2 additions & 4 deletions crates/pet-mac-xcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// Licensed under the MIT License.

use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::version;
use pet_python_utils::{
env::{PythonEnv, ResolvedPythonEnv},
executable::find_executables,
};
use pet_python_utils::{env::ResolvedPythonEnv, executable::find_executables};
use pet_virtualenv::is_virtualenv;
use std::path::PathBuf;

Expand Down
2 changes: 1 addition & 1 deletion crates/pet-pipenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// Licensed under the MIT License.

use env_variables::EnvVariables;
use pet_core::env::PythonEnv;
use pet_core::os_environment::Environment;
use pet_core::{
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::norm_case;
use pet_python_utils::env::PythonEnv;
use pet_python_utils::executable::find_executables;
use pet_python_utils::version;
use std::path::Path;
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-poetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use env_variables::EnvVariables;
use environment_locations::list_environments;
use manager::PoetryManager;
use pet_core::{
env::PythonEnv,
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Configuration, Locator, LocatorResult,
};
use pet_python_utils::env::PythonEnv;
use pet_virtualenv::is_virtualenv;
use std::{
path::PathBuf,
Expand Down
3 changes: 2 additions & 1 deletion crates/pet-pyenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ use environments::{get_generic_python_environment, get_virtual_env_environment};
use manager::PyEnvInfo;
use pet_conda::{utils::is_conda_env, CondaLocator};
use pet_core::{
env::PythonEnv,
manager::{EnvManager, EnvManagerType},
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_python_utils::{env::PythonEnv, executable::find_executable};
use pet_python_utils::executable::find_executable;

pub mod env_variables;
mod environment_locations;
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-pyenv/tests/pyenv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,13 @@ fn resolve_pyenv_environment() {
use pet_conda::Conda;
use pet_core::{
self,
env::PythonEnv,
manager::{EnvManager, EnvManagerType},
python_environment::{PythonEnvironment, PythonEnvironmentKind},
Locator,
};
use pet_pyenv;
use pet_pyenv::PyEnv;
use pet_python_utils::env::PythonEnv;
use std::{collections::HashMap, sync::Arc};

let home = resolve_test_path(&["unix", "pyenv", "user_home"]);
Expand Down
2 changes: 2 additions & 0 deletions crates/pet-python-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ msvc_spectre_libs = { version = "0.1.1", features = ["error"] }
lazy_static = "1.4.0"
regex = "1.10.4"
pet-fs = { path = "../pet-fs" }
pet-core = { path = "../pet-core" }
serde = { version = "1.0.152", features = ["derive"] }
log = "0.4.21"
serde_json = "1.0.93"
sha2 = "0.10.6"
Loading
Loading