Skip to content

Commit

Permalink
Update refresh request
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Jul 18, 2024
1 parent a7903f2 commit 155165a
Show file tree
Hide file tree
Showing 27 changed files with 349 additions and 264 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/pet-conda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use pet_core::{
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_fs::path::norm_case;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -203,8 +203,8 @@ impl Conda {
}

impl Locator for Conda {
fn get_name(&self) -> &'static str {
"Conda" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::Conda
}
fn configure(&self, config: &pet_core::Configuration) {
if let Some(ref conda_exe) = config.conda_executable {
Expand Down
1 change: 1 addition & 0 deletions crates/pet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
msvc_spectre_libs = { version = "0.1.1", features = ["error"] }

[dependencies]
clap = { version = "4.5.4", features = ["derive", "cargo"] }
pet-fs = { path = "../pet-fs" }
serde = { version = "1.0.152", features = ["derive"] }
lazy_static = "1.4.0"
Expand Down
21 changes: 20 additions & 1 deletion crates/pet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct LocatorResult {
pub struct Configuration {
/// These are paths like workspace folders, where we can look for environments.
pub workspace_directories: Option<Vec<PathBuf>>,
pub executables: Option<Vec<PathBuf>>,
pub conda_executable: Option<PathBuf>,
pub poetry_executable: Option<PathBuf>,
/// Custom locations where environments can be found.
Expand All @@ -37,9 +38,27 @@ pub struct Configuration {
pub cache_directory: Option<PathBuf>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum LocatorKind {
Conda,
Homebrew,
LinuxGlobal,
MacCommandLineTools,
MacPythonOrg,
MacXCode,
PipEnv,
Poetry,
PyEnv,
Venv,
VirtualEnv,
VirtualEnvWrapper,
WindowsRegistry,
WindowsStore,
}

pub trait Locator: Send + Sync {
/// Returns the name of the locator.
fn get_name(&self) -> &'static str;
fn get_kind(&self) -> LocatorKind;
/// Configures the locator with the given configuration.
/// Override this method if you need to have some custom configuration.
/// E.g. storing some of the configuration information in the locator.
Expand Down
6 changes: 3 additions & 3 deletions crates/pet-core/src/python_environment.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use clap::{Parser, ValueEnum};
use log::error;
use pet_fs::path::norm_case;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::{arch::Architecture, manager::EnvManager};

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, Hash)]
#[derive(Parser, ValueEnum, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum PythonEnvironmentKind {
Conda,
Homebrew,
Pyenv, // Relates to Python installations in pyenv that are from Python org.
Pyenv,
GlobalPaths, // Python found in global locations like PATH, /usr/bin etc.
PyenvVirtualEnv, // Pyenv virtualenvs.
Pipenv,
Expand All @@ -21,7 +22,6 @@ pub enum PythonEnvironmentKind {
MacCommandLineTools,
LinuxGlobal,
MacXCode,
Unknown,
Venv,
VirtualEnv,
VirtualEnvWrapper,
Expand Down
6 changes: 3 additions & 3 deletions crates/pet-homebrew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pet_core::{
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::executable::find_executables;
Expand Down Expand Up @@ -110,8 +110,8 @@ fn from(env: &PythonEnv) -> Option<PythonEnvironment> {
}

impl Locator for Homebrew {
fn get_name(&self) -> &'static str {
"Homebrew" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::Homebrew
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::Homebrew]
Expand Down
6 changes: 3 additions & 3 deletions crates/pet-linux-global-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::{env::ResolvedPythonEnv, executable::find_executables};
Expand Down Expand Up @@ -53,8 +53,8 @@ impl Default for LinuxGlobalPython {
}
}
impl Locator for LinuxGlobalPython {
fn get_name(&self) -> &'static str {
"LinuxGlobalPython" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::LinuxGlobal
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::LinuxGlobal]
Expand Down
20 changes: 17 additions & 3 deletions crates/pet-mac-commandlinetools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// Licensed under the MIT License.

use pet_core::{
arch::Architecture,
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::version;
Expand All @@ -26,8 +27,8 @@ impl Default for MacCmdLineTools {
}
}
impl Locator for MacCmdLineTools {
fn get_name(&self) -> &'static str {
"MacCmdLineTools" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::MacCommandLineTools
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::MacCommandLineTools]
Expand Down Expand Up @@ -57,6 +58,7 @@ impl Locator for MacCmdLineTools {
let mut version = env.version.clone();
let mut prefix = env.prefix.clone();
let mut symlinks = vec![env.executable.clone()];
let mut arch = None;

let existing_symlinks = env.symlinks.clone();
if let Some(existing_symlinks) = existing_symlinks {
Expand Down Expand Up @@ -113,6 +115,11 @@ impl Locator for MacCmdLineTools {
// Use the latest accurate information we have.
version = Some(resolved_env.version);
prefix = Some(resolved_env.prefix);
arch = if resolved_env.is64_bit {
Some(Architecture::X64)
} else {
Some(Architecture::X86)
};
}
}
}
Expand Down Expand Up @@ -165,18 +172,25 @@ impl Locator for MacCmdLineTools {
version = version::from_header_files(prefix);
}
}

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);
arch = if resolved_env.is64_bit {
Some(Architecture::X64)
} else {
Some(Architecture::X86)
};
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions crates/pet-mac-python-org/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::executable::find_executables;
Expand All @@ -27,8 +27,8 @@ impl Default for MacPythonOrg {
}
}
impl Locator for MacPythonOrg {
fn get_name(&self) -> &'static str {
"MacPythonOrg" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::MacPythonOrg
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::MacPythonOrg]
Expand Down
19 changes: 16 additions & 3 deletions crates/pet-mac-xcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// Licensed under the MIT License.

use pet_core::{
arch::Architecture,
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_fs::path::resolve_symlink;
use pet_python_utils::version;
Expand All @@ -26,8 +27,8 @@ impl Default for MacXCode {
}
}
impl Locator for MacXCode {
fn get_name(&self) -> &'static str {
"MacXCode" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::MacXCode
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::MacCommandLineTools]
Expand Down Expand Up @@ -56,6 +57,7 @@ impl Locator for MacXCode {
let mut version = env.version.clone();
let mut prefix = env.prefix.clone();
let mut symlinks = vec![env.executable.clone()];
let mut arch = None;

let existing_symlinks = env.symlinks.clone();
if let Some(existing_symlinks) = existing_symlinks {
Expand Down Expand Up @@ -110,6 +112,11 @@ impl Locator for MacXCode {
// Use the latest accurate information we have.
version = Some(resolved_env.version);
prefix = Some(resolved_env.prefix);
arch = if resolved_env.is64_bit {
Some(Architecture::X64)
} else {
Some(Architecture::X86)
};
}
}
}
Expand Down Expand Up @@ -156,13 +163,19 @@ impl Locator for MacXCode {
resolved_environments.push(resolved_env.clone());
version = Some(resolved_env.version);
prefix = Some(resolved_env.prefix);
arch = if resolved_env.is64_bit {
Some(Architecture::X64)
} else {
Some(Architecture::X86)
};
}
}

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

Expand Down
5 changes: 3 additions & 2 deletions crates/pet-pipenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use env_variables::EnvVariables;
use pet_core::env::PythonEnv;
use pet_core::os_environment::Environment;
use pet_core::LocatorKind;
use pet_core::{
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Expand Down Expand Up @@ -71,8 +72,8 @@ impl PipEnv {
}
}
impl Locator for PipEnv {
fn get_name(&self) -> &'static str {
"PipEnv" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::PipEnv
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::Pipenv]
Expand Down
6 changes: 3 additions & 3 deletions crates/pet-poetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use pet_core::{
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Configuration, Locator, LocatorResult,
Configuration, Locator, LocatorKind, LocatorResult,
};
use pet_virtualenv::is_virtualenv;
use std::{
Expand Down Expand Up @@ -127,8 +127,8 @@ impl PoetryLocator for Poetry {
}

impl Locator for Poetry {
fn get_name(&self) -> &'static str {
"Poetry" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::Poetry
}
fn configure(&self, config: &Configuration) {
if let Some(workspace_directories) = &config.workspace_directories {
Expand Down
7 changes: 4 additions & 3 deletions crates/pet-pyenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use pet_core::{
os_environment::Environment,
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
Locator,
Locator, LocatorKind,
};
use pet_python_utils::executable::find_executable;

Expand Down Expand Up @@ -75,13 +75,14 @@ impl PyEnv {
}

impl Locator for PyEnv {
fn get_name(&self) -> &'static str {
"PyEnv" // Do not change this name, as this is used in telemetry.
fn get_kind(&self) -> LocatorKind {
LocatorKind::PyEnv
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![
PythonEnvironmentKind::Pyenv,
PythonEnvironmentKind::PyenvVirtualEnv,
PythonEnvironmentKind::Conda,
]
}

Expand Down
5 changes: 4 additions & 1 deletion crates/pet-python-utils/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl ResolvedPythonEnv {
let entry = cache.lock().unwrap();
entry.track_symlinks(symlinks)
} else {
error!("Invalid Python environment being cached: {:?}", environment);
error!(
"Invalid Python environment being cached: {:?} expected {:?}",
environment, self
);
}
}
/// Given the executable path, resolve the python environment by spawning python.
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-python-utils/src/fs_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct CacheEntry {
}

pub fn generate_cache_file(cache_directory: &Path, executable: &PathBuf) -> PathBuf {
cache_directory.join(format!("{}.2.json", generate_hash(executable)))
cache_directory.join(format!("{}.3.json", generate_hash(executable)))
}

pub fn delete_cache_file(cache_directory: &Path, executable: &PathBuf) {
Expand Down
Loading

0 comments on commit 155165a

Please sign in to comment.