Skip to content

Commit 8299d1d

Browse files
authored
Add support for finding without resolving (#107)
1 parent 9b9f66f commit 8299d1d

File tree

14 files changed

+692
-258
lines changed

14 files changed

+692
-258
lines changed

crates/pet-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod python_environment;
1515
pub mod reporter;
1616
pub mod telemetry;
1717

18-
#[derive(Debug, Clone)]
18+
#[derive(Debug, Clone, PartialEq, Eq)]
1919
pub struct LocatorResult {
2020
pub managers: Vec<EnvManager>,
2121
pub environments: Vec<PythonEnvironment>,

crates/pet-pyenv/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ impl Locator for PyEnv {
128128
}
129129
if let Some(ref versions) = &pyenv_info.versions {
130130
if let Some(envs) = list_pyenv_environments(&manager, versions, &self.conda_locator) {
131-
for env in envs.environments {
132-
reporter.report_environment(&env);
133-
}
134131
for mgr in envs.managers {
135132
reporter.report_manager(&mgr);
136133
}
134+
for env in envs.environments {
135+
reporter.report_environment(&env);
136+
}
137137
}
138138
}
139139
}

crates/pet-reporter/src/collect.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
use pet_core::{manager::EnvManager, python_environment::PythonEnvironment, reporter::Reporter};
5+
use std::sync::{Arc, Mutex};
6+
7+
/// Used to just collect the environments and managers and will not report anytihng anywhere.
8+
pub struct CollectReporter {
9+
pub managers: Arc<Mutex<Vec<EnvManager>>>,
10+
pub environments: Arc<Mutex<Vec<PythonEnvironment>>>,
11+
}
12+
13+
impl Default for CollectReporter {
14+
fn default() -> Self {
15+
Self::new()
16+
}
17+
}
18+
19+
impl CollectReporter {
20+
pub fn new() -> CollectReporter {
21+
CollectReporter {
22+
managers: Arc::new(Mutex::new(vec![])),
23+
environments: Arc::new(Mutex::new(vec![])),
24+
}
25+
}
26+
}
27+
impl Reporter for CollectReporter {
28+
fn report_telemetry(&self, _event: &pet_core::telemetry::TelemetryEvent) {
29+
//
30+
}
31+
fn report_manager(&self, manager: &EnvManager) {
32+
self.managers.lock().unwrap().push(manager.clone());
33+
}
34+
35+
fn report_environment(&self, env: &PythonEnvironment) {
36+
self.environments.lock().unwrap().push(env.clone());
37+
}
38+
}
39+
40+
pub fn create_reporter() -> CollectReporter {
41+
CollectReporter::new()
42+
}

crates/pet-reporter/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
pub mod cache;
5+
pub mod collect;
56
pub mod environment;
67
pub mod jsonrpc;
78
pub mod stdio;

crates/pet-virtualenv/src/lib.rs

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

4+
use std::path::Path;
5+
46
use pet_core::{
57
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
68
reporter::Reporter,
@@ -20,34 +22,49 @@ pub fn is_virtualenv(env: &PythonEnv) -> bool {
2022
}
2123
}
2224
if let Some(bin) = env.executable.parent() {
23-
// Check if there are any activate.* files in the same directory as the interpreter.
24-
//
25-
// env
26-
// |__ activate, activate.* <--- check if any of these files exist
27-
// |__ python <--- interpreterPath
25+
return is_virtualenv_dir(bin);
26+
}
27+
28+
false
29+
}
2830

29-
// if let Some(parent_path) = PathBuf::from(env.)
30-
// const directory = path.dirname(interpreterPath);
31-
// const files = await fsapi.readdir(directory);
32-
// const regex = /^activate(\.([A-z]|\d)+)?$/i;
33-
if bin.join("activate").exists() || bin.join("activate.bat").exists() {
34-
return true;
31+
pub fn is_virtualenv_dir(path: &Path) -> bool {
32+
// Check if the executable is in a bin or Scripts directory.
33+
// Possible for some reason we do not have the prefix.
34+
let mut path = path.to_path_buf();
35+
if !path.ends_with("bin") && !path.ends_with("Scripts") {
36+
if cfg!(windows) {
37+
path = path.join("Scripts");
38+
} else {
39+
path = path.join("bin");
3540
}
41+
}
42+
// Check if there are any activate.* files in the same directory as the interpreter.
43+
//
44+
// env
45+
// |__ activate, activate.* <--- check if any of these files exist
46+
// |__ python <--- interpreterPath
3647

37-
// Support for activate.ps, etc.
38-
if let Ok(files) = std::fs::read_dir(bin) {
39-
for file in files.filter_map(Result::ok).map(|e| e.path()) {
40-
if file
41-
.file_name()
42-
.unwrap_or_default()
43-
.to_str()
44-
.unwrap_or_default()
45-
.starts_with("activate")
46-
{
47-
return true;
48-
}
48+
// if let Some(parent_path) = PathBuf::from(env.)
49+
// const directory = path.dirname(interpreterPath);
50+
// const files = await fsapi.readdir(directory);
51+
// const regex = /^activate(\.([A-z]|\d)+)?$/i;
52+
if path.join("activate").exists() || path.join("activate.bat").exists() {
53+
return true;
54+
}
55+
56+
// Support for activate.ps, etc.
57+
if let Ok(files) = std::fs::read_dir(path) {
58+
for file in files.filter_map(Result::ok).map(|e| e.path()) {
59+
if file
60+
.file_name()
61+
.unwrap_or_default()
62+
.to_str()
63+
.unwrap_or_default()
64+
.starts_with("activate")
65+
{
66+
return true;
4967
}
50-
return false;
5168
}
5269
}
5370

0 commit comments

Comments
 (0)