Skip to content

Add support for finding without resolving #107

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 4 commits into from
Jul 11, 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
2 changes: 1 addition & 1 deletion crates/pet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod python_environment;
pub mod reporter;
pub mod telemetry;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocatorResult {
pub managers: Vec<EnvManager>,
pub environments: Vec<PythonEnvironment>,
Expand Down
6 changes: 3 additions & 3 deletions crates/pet-pyenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ impl Locator for PyEnv {
}
if let Some(ref versions) = &pyenv_info.versions {
if let Some(envs) = list_pyenv_environments(&manager, versions, &self.conda_locator) {
for env in envs.environments {
reporter.report_environment(&env);
}
for mgr in envs.managers {
reporter.report_manager(&mgr);
}
for env in envs.environments {
reporter.report_environment(&env);
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions crates/pet-reporter/src/collect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use pet_core::{manager::EnvManager, python_environment::PythonEnvironment, reporter::Reporter};
use std::sync::{Arc, Mutex};

/// Used to just collect the environments and managers and will not report anytihng anywhere.
pub struct CollectReporter {
pub managers: Arc<Mutex<Vec<EnvManager>>>,
pub environments: Arc<Mutex<Vec<PythonEnvironment>>>,
}

impl Default for CollectReporter {
fn default() -> Self {
Self::new()
}
}

impl CollectReporter {
pub fn new() -> CollectReporter {
CollectReporter {
managers: Arc::new(Mutex::new(vec![])),
environments: Arc::new(Mutex::new(vec![])),
}
}
}
impl Reporter for CollectReporter {
fn report_telemetry(&self, _event: &pet_core::telemetry::TelemetryEvent) {
//
}
fn report_manager(&self, manager: &EnvManager) {
self.managers.lock().unwrap().push(manager.clone());
}

fn report_environment(&self, env: &PythonEnvironment) {
self.environments.lock().unwrap().push(env.clone());
}
}

pub fn create_reporter() -> CollectReporter {
CollectReporter::new()
}
1 change: 1 addition & 0 deletions crates/pet-reporter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

pub mod cache;
pub mod collect;
pub mod environment;
pub mod jsonrpc;
pub mod stdio;
Expand Down
65 changes: 41 additions & 24 deletions crates/pet-virtualenv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::path::Path;

use pet_core::{
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Expand All @@ -20,34 +22,49 @@ pub fn is_virtualenv(env: &PythonEnv) -> bool {
}
}
if let Some(bin) = env.executable.parent() {
// Check if there are any activate.* files in the same directory as the interpreter.
//
// env
// |__ activate, activate.* <--- check if any of these files exist
// |__ python <--- interpreterPath
return is_virtualenv_dir(bin);
}

false
}

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

// Support for activate.ps, etc.
if let Ok(files) = std::fs::read_dir(bin) {
for file in files.filter_map(Result::ok).map(|e| e.path()) {
if file
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.starts_with("activate")
{
return true;
}
// if let Some(parent_path) = PathBuf::from(env.)
// const directory = path.dirname(interpreterPath);
// const files = await fsapi.readdir(directory);
// const regex = /^activate(\.([A-z]|\d)+)?$/i;
if path.join("activate").exists() || path.join("activate.bat").exists() {
return true;
}

// Support for activate.ps, etc.
if let Ok(files) = std::fs::read_dir(path) {
for file in files.filter_map(Result::ok).map(|e| e.path()) {
if file
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.starts_with("activate")
{
return true;
}
return false;
}
}

Expand Down
Loading
Loading