Skip to content

Commit 3ba3d26

Browse files
authored
Add Samples and remove a JSON serialization layer (#90)
1 parent 91d1227 commit 3ba3d26

File tree

16 files changed

+450
-186
lines changed

16 files changed

+450
-186
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ target/
1515
# Testing directories
1616
.venv/
1717
tmp/
18-
temp/
18+
temp/
19+
docs/node_modules/
20+
docs/package.json
21+
docs/package-lock.json

crates/pet-core/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub struct LocatorResult {
2424
#[derive(Debug, Default, Clone)]
2525
pub struct Configuration {
2626
/// These are paths like workspace folders, where we can look for environments.
27-
pub search_paths: Option<Vec<PathBuf>>,
27+
pub project_directories: Option<Vec<PathBuf>>,
2828
pub conda_executable: Option<PathBuf>,
2929
pub poetry_executable: Option<PathBuf>,
3030
/// Custom locations where environments can be found.
3131
/// These are different from search_paths, as these are specific directories where environments are expected.
32-
/// search_paths on the other hand can be any directory such as a workspace folder, where envs might never exist.
33-
pub environment_paths: Option<Vec<PathBuf>>,
32+
/// environment_directories on the other hand can be any directory such as a workspace folder, where envs might never exist.
33+
pub environment_directories: Option<Vec<PathBuf>>,
3434
}
3535

3636
pub trait Locator: Send + Sync {

crates/pet-core/src/manager.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
use serde::{Deserialize, Serialize};
55
use std::path::PathBuf;
66

7-
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq)]
8-
#[serde(rename_all = "camelCase")]
9-
#[derive(Debug, Hash)]
7+
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug, Hash)]
108
pub enum EnvManagerType {
119
Conda,
1210
Poetry,

crates/pet-core/src/python_environment.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use std::path::PathBuf;
88

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

11-
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
12-
#[serde(rename_all = "camelCase")]
13-
#[derive(Debug, Hash)]
11+
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, Hash)]
1412
pub enum PythonEnvironmentCategory {
1513
Conda,
1614
Homebrew,
@@ -19,7 +17,6 @@ pub enum PythonEnvironmentCategory {
1917
PyenvVirtualEnv, // Pyenv virtualenvs.
2018
Pipenv,
2119
Poetry,
22-
System,
2320
MacPythonOrg,
2421
MacCommandLineTools,
2522
LinuxGlobal,

crates/pet-poetry/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub mod manager;
2929
mod pyproject_toml;
3030

3131
pub struct Poetry {
32-
pub project_dirs: Arc<Mutex<Vec<PathBuf>>>,
32+
pub project_directories: Arc<Mutex<Vec<PathBuf>>>,
3333
pub env_vars: EnvVariables,
3434
pub poetry_executable: Arc<Mutex<Option<PathBuf>>>,
3535
searched: AtomicBool,
@@ -41,7 +41,7 @@ impl Poetry {
4141
Poetry {
4242
searched: AtomicBool::new(false),
4343
search_result: Arc::new(Mutex::new(None)),
44-
project_dirs: Arc::new(Mutex::new(vec![])),
44+
project_directories: Arc::new(Mutex::new(vec![])),
4545
env_vars: EnvVariables::from(environment),
4646
poetry_executable: Arc::new(Mutex::new(None)),
4747
}
@@ -57,7 +57,7 @@ impl Poetry {
5757

5858
let environments_using_spawn = environment_locations_spawn::list_environments(
5959
&manager.executable,
60-
self.project_dirs.lock().unwrap().clone(),
60+
self.project_directories.lock().unwrap().clone(),
6161
&manager,
6262
)
6363
.iter()
@@ -108,7 +108,7 @@ impl Poetry {
108108
if let Some(manager) = &manager {
109109
result.managers.push(manager.to_manager());
110110
}
111-
if let Ok(values) = self.project_dirs.lock() {
111+
if let Ok(values) = self.project_directories.lock() {
112112
let project_dirs = values.clone();
113113
drop(values);
114114
let envs = list_environments(&self.env_vars, &project_dirs.clone(), manager)
@@ -139,13 +139,13 @@ impl Locator for Poetry {
139139
"Poetry"
140140
}
141141
fn configure(&self, config: &Configuration) {
142-
if let Some(search_paths) = &config.search_paths {
143-
self.project_dirs.lock().unwrap().clear();
144-
if !search_paths.is_empty() {
145-
self.project_dirs
142+
if let Some(project_directories) = &config.project_directories {
143+
self.project_directories.lock().unwrap().clear();
144+
if !project_directories.is_empty() {
145+
self.project_directories
146146
.lock()
147147
.unwrap()
148-
.extend(search_paths.clone());
148+
.extend(project_directories.clone());
149149
}
150150
}
151151
if let Some(exe) = &config.poetry_executable {

crates/pet-reporter/src/environment.rs

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
use crate::manager::Manager;
54
use log::error;
6-
use pet_core::{
7-
arch::Architecture,
8-
python_environment::{PythonEnvironment, PythonEnvironmentCategory},
9-
};
10-
use serde::{Deserialize, Serialize};
5+
use pet_core::python_environment::{PythonEnvironment, PythonEnvironmentCategory};
116
use std::path::PathBuf;
127

13-
// We want to maintain full control over serialization instead of relying on the enums or the like.
14-
// Else its too easy to break the API by changing the enum variants.
15-
fn python_category_to_string(category: &PythonEnvironmentCategory) -> &'static str {
16-
match category {
17-
PythonEnvironmentCategory::System => "system",
18-
PythonEnvironmentCategory::MacCommandLineTools => "mac-command-line-tools",
19-
PythonEnvironmentCategory::MacXCode => "mac-xcode",
20-
PythonEnvironmentCategory::MacPythonOrg => "mac-python-org",
21-
PythonEnvironmentCategory::GlobalPaths => "global-paths",
22-
PythonEnvironmentCategory::Homebrew => "homebrew",
23-
PythonEnvironmentCategory::Conda => "conda",
24-
PythonEnvironmentCategory::LinuxGlobal => "linux-global",
25-
PythonEnvironmentCategory::Pyenv => "pyenv",
26-
PythonEnvironmentCategory::PyenvVirtualEnv => "pyenv-virtualenv",
27-
PythonEnvironmentCategory::WindowsStore => "windows-store",
28-
PythonEnvironmentCategory::WindowsRegistry => "windows-registry",
29-
PythonEnvironmentCategory::Pipenv => "pipenv",
30-
PythonEnvironmentCategory::VirtualEnvWrapper => "virtualenvwrapper",
31-
PythonEnvironmentCategory::Venv => "venv",
32-
PythonEnvironmentCategory::VirtualEnv => "virtualenv",
33-
PythonEnvironmentCategory::Unknown => "unknown",
34-
PythonEnvironmentCategory::Poetry => "poetry",
35-
}
36-
}
37-
38-
// We want to maintain full control over serialization instead of relying on the enums or the like.
39-
// Else its too easy to break the API by changing the enum variants.
40-
fn architecture_to_string(arch: &Architecture) -> &'static str {
41-
match arch {
42-
Architecture::X64 => "x64",
43-
Architecture::X86 => "x86",
44-
}
45-
}
46-
47-
#[derive(Serialize, Deserialize, Clone)]
48-
#[serde(rename_all = "camelCase")]
49-
#[derive(Debug)]
50-
pub struct Environment {
51-
pub display_name: Option<String>,
52-
pub name: Option<String>,
53-
pub executable: Option<PathBuf>,
54-
pub category: String,
55-
pub version: Option<String>,
56-
pub prefix: Option<PathBuf>,
57-
pub manager: Option<Manager>,
58-
pub project: Option<PathBuf>,
59-
pub arch: Option<String>,
60-
pub symlinks: Option<Vec<PathBuf>>,
61-
}
62-
63-
impl Environment {
64-
pub fn from(env: &PythonEnvironment) -> Environment {
65-
Environment {
66-
display_name: env.display_name.clone(),
67-
name: env.name.clone(),
68-
executable: env.executable.clone(),
69-
category: python_category_to_string(&env.category).to_string(),
70-
version: env.version.clone(),
71-
prefix: env.prefix.clone(),
72-
manager: match &env.manager {
73-
Some(manager) => Manager::from(manager).into(),
74-
None => None,
75-
},
76-
project: env.project.clone(),
77-
arch: env
78-
.arch
79-
.as_ref()
80-
.map(architecture_to_string)
81-
.map(|s| s.to_string()),
82-
symlinks: env.symlinks.clone(),
83-
}
84-
}
85-
}
86-
878
pub fn get_environment_key(env: &PythonEnvironment) -> Option<PathBuf> {
889
if let Some(exe) = &env.executable {
8910
Some(exe.clone())

crates/pet-reporter/src/jsonrpc.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::{environment::Environment, manager::Manager};
54
use env_logger::Builder;
65
use log::{trace, LevelFilter};
76
use pet_core::{manager::EnvManager, python_environment::PythonEnvironment, reporter::Reporter};
@@ -13,12 +12,12 @@ pub struct JsonRpcReporter {}
1312
impl Reporter for JsonRpcReporter {
1413
fn report_manager(&self, manager: &EnvManager) {
1514
trace!("Reporting Manager {:?}", manager);
16-
send_message("manager", Manager::from(manager).into())
15+
send_message("manager", manager.into())
1716
}
1817

1918
fn report_environment(&self, env: &PythonEnvironment) {
2019
trace!("Reporting Environment {:?}", env);
21-
send_message("environment", Environment::from(env).into())
20+
send_message("environment", env.into())
2221
}
2322
}
2423

crates/pet-reporter/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
pub mod cache;
55
pub mod environment;
66
pub mod jsonrpc;
7-
pub mod manager;
87
pub mod stdio;
98
pub mod test;

crates/pet-reporter/src/manager.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

crates/pet/src/find.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use log::{info, trace, warn};
4+
use log::{trace, warn};
55
use pet_conda::CondaLocator;
66
use pet_core::os_environment::{Environment, EnvironmentApi};
77
use pet_core::reporter::Reporter;
@@ -45,12 +45,11 @@ pub fn find_and_report_envs(
4545
find_global_virtual_envs_time: Duration::from_secs(0),
4646
find_search_paths_time: Duration::from_secs(0),
4747
}));
48-
info!("Started Refreshing Environments");
4948
let start = std::time::Instant::now();
5049

5150
// From settings
52-
let environment_paths = configuration.environment_paths.unwrap_or_default();
53-
let search_paths = configuration.search_paths.unwrap_or_default();
51+
let environment_directories = configuration.environment_directories.unwrap_or_default();
52+
let project_directories = configuration.project_directories.unwrap_or_default();
5453
let conda_executable = configuration.conda_executable;
5554
thread::scope(|s| {
5655
// 1. Find using known global locators.
@@ -123,7 +122,7 @@ pub fn find_and_report_envs(
123122
environment.get_env_var("XDG_DATA_HOME".into()),
124123
environment.get_user_home(),
125124
),
126-
environment_paths,
125+
environment_directories,
127126
]
128127
.concat();
129128
let global_env_search_paths: Vec<PathBuf> =
@@ -152,16 +151,16 @@ pub fn find_and_report_envs(
152151
// & users can have a lot of workspace folders and can have a large number fo files/directories
153152
// that could the discovery.
154153
s.spawn(|| {
155-
if search_paths.is_empty() {
154+
if project_directories.is_empty() {
156155
return;
157156
}
158157
trace!(
159158
"Searching for environments in custom folders: {:?}",
160-
search_paths
159+
project_directories
161160
);
162161
let start = std::time::Instant::now();
163162
find_python_environments_in_workspace_folders_recursive(
164-
search_paths,
163+
project_directories,
165164
reporter,
166165
locators,
167166
);

0 commit comments

Comments
 (0)