Skip to content

Add Samples and remove a JSON serialization layer #90

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 8 commits into from
Jul 4, 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ target/
# Testing directories
.venv/
tmp/
temp/
temp/
docs/node_modules/
docs/package.json
docs/package-lock.json
6 changes: 3 additions & 3 deletions crates/pet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub struct LocatorResult {
#[derive(Debug, Default, Clone)]
pub struct Configuration {
/// These are paths like workspace folders, where we can look for environments.
pub search_paths: Option<Vec<PathBuf>>,
pub project_directories: Option<Vec<PathBuf>>,
pub conda_executable: Option<PathBuf>,
pub poetry_executable: Option<PathBuf>,
/// Custom locations where environments can be found.
/// These are different from search_paths, as these are specific directories where environments are expected.
/// search_paths on the other hand can be any directory such as a workspace folder, where envs might never exist.
pub environment_paths: Option<Vec<PathBuf>>,
/// environment_directories on the other hand can be any directory such as a workspace folder, where envs might never exist.
pub environment_directories: Option<Vec<PathBuf>>,
}

pub trait Locator: Send + Sync {
Expand Down
4 changes: 1 addition & 3 deletions crates/pet-core/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Hash)]
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum EnvManagerType {
Conda,
Poetry,
Expand Down
5 changes: 1 addition & 4 deletions crates/pet-core/src/python_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use std::path::PathBuf;

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

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Hash)]
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum PythonEnvironmentCategory {
Conda,
Homebrew,
Expand All @@ -19,7 +17,6 @@ pub enum PythonEnvironmentCategory {
PyenvVirtualEnv, // Pyenv virtualenvs.
Pipenv,
Poetry,
System,
MacPythonOrg,
MacCommandLineTools,
LinuxGlobal,
Expand Down
18 changes: 9 additions & 9 deletions crates/pet-poetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod manager;
mod pyproject_toml;

pub struct Poetry {
pub project_dirs: Arc<Mutex<Vec<PathBuf>>>,
pub project_directories: Arc<Mutex<Vec<PathBuf>>>,
pub env_vars: EnvVariables,
pub poetry_executable: Arc<Mutex<Option<PathBuf>>>,
searched: AtomicBool,
Expand All @@ -41,7 +41,7 @@ impl Poetry {
Poetry {
searched: AtomicBool::new(false),
search_result: Arc::new(Mutex::new(None)),
project_dirs: Arc::new(Mutex::new(vec![])),
project_directories: Arc::new(Mutex::new(vec![])),
env_vars: EnvVariables::from(environment),
poetry_executable: Arc::new(Mutex::new(None)),
}
Expand All @@ -57,7 +57,7 @@ impl Poetry {

let environments_using_spawn = environment_locations_spawn::list_environments(
&manager.executable,
self.project_dirs.lock().unwrap().clone(),
self.project_directories.lock().unwrap().clone(),
&manager,
)
.iter()
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Poetry {
if let Some(manager) = &manager {
result.managers.push(manager.to_manager());
}
if let Ok(values) = self.project_dirs.lock() {
if let Ok(values) = self.project_directories.lock() {
let project_dirs = values.clone();
drop(values);
let envs = list_environments(&self.env_vars, &project_dirs.clone(), manager)
Expand Down Expand Up @@ -139,13 +139,13 @@ impl Locator for Poetry {
"Poetry"
}
fn configure(&self, config: &Configuration) {
if let Some(search_paths) = &config.search_paths {
self.project_dirs.lock().unwrap().clear();
if !search_paths.is_empty() {
self.project_dirs
if let Some(project_directories) = &config.project_directories {
self.project_directories.lock().unwrap().clear();
if !project_directories.is_empty() {
self.project_directories
.lock()
.unwrap()
.extend(search_paths.clone());
.extend(project_directories.clone());
}
}
if let Some(exe) = &config.poetry_executable {
Expand Down
81 changes: 1 addition & 80 deletions crates/pet-reporter/src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,89 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use crate::manager::Manager;
use log::error;
use pet_core::{
arch::Architecture,
python_environment::{PythonEnvironment, PythonEnvironmentCategory},
};
use serde::{Deserialize, Serialize};
use pet_core::python_environment::{PythonEnvironment, PythonEnvironmentCategory};
use std::path::PathBuf;

// We want to maintain full control over serialization instead of relying on the enums or the like.
// Else its too easy to break the API by changing the enum variants.
fn python_category_to_string(category: &PythonEnvironmentCategory) -> &'static str {
match category {
PythonEnvironmentCategory::System => "system",
PythonEnvironmentCategory::MacCommandLineTools => "mac-command-line-tools",
PythonEnvironmentCategory::MacXCode => "mac-xcode",
PythonEnvironmentCategory::MacPythonOrg => "mac-python-org",
PythonEnvironmentCategory::GlobalPaths => "global-paths",
PythonEnvironmentCategory::Homebrew => "homebrew",
PythonEnvironmentCategory::Conda => "conda",
PythonEnvironmentCategory::LinuxGlobal => "linux-global",
PythonEnvironmentCategory::Pyenv => "pyenv",
PythonEnvironmentCategory::PyenvVirtualEnv => "pyenv-virtualenv",
PythonEnvironmentCategory::WindowsStore => "windows-store",
PythonEnvironmentCategory::WindowsRegistry => "windows-registry",
PythonEnvironmentCategory::Pipenv => "pipenv",
PythonEnvironmentCategory::VirtualEnvWrapper => "virtualenvwrapper",
PythonEnvironmentCategory::Venv => "venv",
PythonEnvironmentCategory::VirtualEnv => "virtualenv",
PythonEnvironmentCategory::Unknown => "unknown",
PythonEnvironmentCategory::Poetry => "poetry",
}
}

// We want to maintain full control over serialization instead of relying on the enums or the like.
// Else its too easy to break the API by changing the enum variants.
fn architecture_to_string(arch: &Architecture) -> &'static str {
match arch {
Architecture::X64 => "x64",
Architecture::X86 => "x86",
}
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[derive(Debug)]
pub struct Environment {
pub display_name: Option<String>,
pub name: Option<String>,
pub executable: Option<PathBuf>,
pub category: String,
pub version: Option<String>,
pub prefix: Option<PathBuf>,
pub manager: Option<Manager>,
pub project: Option<PathBuf>,
pub arch: Option<String>,
pub symlinks: Option<Vec<PathBuf>>,
}

impl Environment {
pub fn from(env: &PythonEnvironment) -> Environment {
Environment {
display_name: env.display_name.clone(),
name: env.name.clone(),
executable: env.executable.clone(),
category: python_category_to_string(&env.category).to_string(),
version: env.version.clone(),
prefix: env.prefix.clone(),
manager: match &env.manager {
Some(manager) => Manager::from(manager).into(),
None => None,
},
project: env.project.clone(),
arch: env
.arch
.as_ref()
.map(architecture_to_string)
.map(|s| s.to_string()),
symlinks: env.symlinks.clone(),
}
}
}

pub fn get_environment_key(env: &PythonEnvironment) -> Option<PathBuf> {
if let Some(exe) = &env.executable {
Some(exe.clone())
Expand Down
5 changes: 2 additions & 3 deletions crates/pet-reporter/src/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::{environment::Environment, manager::Manager};
use env_logger::Builder;
use log::{trace, LevelFilter};
use pet_core::{manager::EnvManager, python_environment::PythonEnvironment, reporter::Reporter};
Expand All @@ -13,12 +12,12 @@ pub struct JsonRpcReporter {}
impl Reporter for JsonRpcReporter {
fn report_manager(&self, manager: &EnvManager) {
trace!("Reporting Manager {:?}", manager);
send_message("manager", Manager::from(manager).into())
send_message("manager", manager.into())
}

fn report_environment(&self, env: &PythonEnvironment) {
trace!("Reporting Environment {:?}", env);
send_message("environment", Environment::from(env).into())
send_message("environment", env.into())
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/pet-reporter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
pub mod cache;
pub mod environment;
pub mod jsonrpc;
pub mod manager;
pub mod stdio;
pub mod test;
33 changes: 0 additions & 33 deletions crates/pet-reporter/src/manager.rs

This file was deleted.

15 changes: 7 additions & 8 deletions crates/pet/src/find.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use log::{info, trace, warn};
use log::{trace, warn};
use pet_conda::CondaLocator;
use pet_core::os_environment::{Environment, EnvironmentApi};
use pet_core::reporter::Reporter;
Expand Down Expand Up @@ -45,12 +45,11 @@ pub fn find_and_report_envs(
find_global_virtual_envs_time: Duration::from_secs(0),
find_search_paths_time: Duration::from_secs(0),
}));
info!("Started Refreshing Environments");
let start = std::time::Instant::now();

// From settings
let environment_paths = configuration.environment_paths.unwrap_or_default();
let search_paths = configuration.search_paths.unwrap_or_default();
let environment_directories = configuration.environment_directories.unwrap_or_default();
let project_directories = configuration.project_directories.unwrap_or_default();
let conda_executable = configuration.conda_executable;
thread::scope(|s| {
// 1. Find using known global locators.
Expand Down Expand Up @@ -123,7 +122,7 @@ pub fn find_and_report_envs(
environment.get_env_var("XDG_DATA_HOME".into()),
environment.get_user_home(),
),
environment_paths,
environment_directories,
]
.concat();
let global_env_search_paths: Vec<PathBuf> =
Expand Down Expand Up @@ -152,16 +151,16 @@ pub fn find_and_report_envs(
// & users can have a lot of workspace folders and can have a large number fo files/directories
// that could the discovery.
s.spawn(|| {
if search_paths.is_empty() {
if project_directories.is_empty() {
return;
}
trace!(
"Searching for environments in custom folders: {:?}",
search_paths
project_directories
);
let start = std::time::Instant::now();
find_python_environments_in_workspace_folders_recursive(
search_paths,
project_directories,
reporter,
locators,
);
Expand Down
Loading
Loading