From 28a5eb0040c864fe885bcb60f129a697c114c98b Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 6 Jun 2024 14:09:06 +1000 Subject: [PATCH 1/2] Updates to extraction of version --- crates/pet-utils/src/path.rs | 14 +++++++------- crates/pet-venv/src/lib.rs | 9 ++++++--- crates/pet-virtualenv/src/lib.rs | 11 +++++++++-- crates/pet-virtualenvwrapper/src/lib.rs | 11 +++++++++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crates/pet-utils/src/path.rs b/crates/pet-utils/src/path.rs index 6af051c0..2d965bd4 100644 --- a/crates/pet-utils/src/path.rs +++ b/crates/pet-utils/src/path.rs @@ -1,18 +1,18 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::path::{Path, PathBuf}; // Similar to fs::canonicalize, but ignores UNC paths and returns the path as is (for windows). pub fn normalize>(path: P) -> PathBuf { // On unix do not use canonicalize, results in weird issues with homebrew paths - if cfg!(unix) { - return path.as_ref().to_path_buf(); - } + #[cfg(unix)] + return path.as_ref().to_path_buf(); + + #[cfg(windows)] + use std::fs; + #[cfg(windows)] if let Ok(resolved) = fs::canonicalize(&path) { if cfg!(unix) { return resolved; diff --git a/crates/pet-venv/src/lib.rs b/crates/pet-venv/src/lib.rs index c57a7c2a..d0865d81 100644 --- a/crates/pet-venv/src/lib.rs +++ b/crates/pet-venv/src/lib.rs @@ -5,7 +5,7 @@ use pet_core::{ python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentCategory}, Locator, LocatorResult, }; -use pet_utils::{env::PythonEnv, pyvenv_cfg::PyVenvCfg}; +use pet_utils::{env::PythonEnv, headers::Headers, pyvenv_cfg::PyVenvCfg}; fn is_venv_internal(env: &PythonEnv) -> Option { // env path cannot be empty. @@ -40,12 +40,15 @@ impl Locator for Venv { if let Some(filename) = &env.prefix { name = filename.to_str().map(|f| f.to_string()); } - + let version = match env.version { + Some(ref v) => Some(v.clone()), + None => match &env.prefix { Some(prefix) => Headers::get_version(prefix), None=> None } + }; Some( PythonEnvironmentBuilder::new(PythonEnvironmentCategory::Venv) .name(name) .executable(Some(env.executable.clone())) - .version(env.version.clone()) + .version(version) .prefix(env.prefix.clone()) .build(), ) diff --git a/crates/pet-virtualenv/src/lib.rs b/crates/pet-virtualenv/src/lib.rs index c95e62b8..17e699b9 100644 --- a/crates/pet-virtualenv/src/lib.rs +++ b/crates/pet-virtualenv/src/lib.rs @@ -7,7 +7,7 @@ use pet_core::{ python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentCategory}, Locator, LocatorResult, }; -use pet_utils::env::PythonEnv; +use pet_utils::{env::PythonEnv, headers::Headers}; pub fn is_virtualenv(env: &PythonEnv) -> bool { if env.prefix.is_none() { @@ -70,12 +70,19 @@ impl Locator for VirtualEnv { if let Some(filename) = &env.prefix { name = filename.to_str().map(|f| f.to_string()); } + let version = match env.version { + Some(ref v) => Some(v.clone()), + None => match &env.prefix { + Some(prefix) => Headers::get_version(prefix), + None => None, + }, + }; Some( PythonEnvironmentBuilder::new(PythonEnvironmentCategory::VirtualEnv) .name(name) .executable(Some(env.executable.clone())) - .version(env.version.clone()) + .version(version) .prefix(env.prefix.clone()) .build(), ) diff --git a/crates/pet-virtualenvwrapper/src/lib.rs b/crates/pet-virtualenvwrapper/src/lib.rs index 319333d1..0f44a359 100644 --- a/crates/pet-virtualenvwrapper/src/lib.rs +++ b/crates/pet-virtualenvwrapper/src/lib.rs @@ -9,7 +9,7 @@ use pet_core::{ python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentCategory}, Locator, LocatorResult, }; -use pet_utils::env::PythonEnv; +use pet_utils::{env::PythonEnv, headers::Headers}; mod env_variables; mod environment_locations; @@ -36,12 +36,19 @@ impl Locator for VirtualEnvWrapper { if let Some(filename) = &env.prefix { name = filename.to_str().map(|f| f.to_string()); } + let version = match env.version { + Some(ref v) => Some(v.clone()), + None => match &env.prefix { + Some(prefix) => Headers::get_version(prefix), + None => None, + }, + }; Some( PythonEnvironmentBuilder::new(PythonEnvironmentCategory::VirtualEnvWrapper) .name(name) .executable(Some(env.executable.clone())) - .version(env.version.clone()) + .version(version) .prefix(env.prefix.clone()) .project(get_project(env)) .build(), From 3bab560c18ae0f312fa98246343c897741ac0d62 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 6 Jun 2024 14:10:07 +1000 Subject: [PATCH 2/2] Fix formatting --- crates/pet-venv/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/pet-venv/src/lib.rs b/crates/pet-venv/src/lib.rs index d0865d81..6db9e724 100644 --- a/crates/pet-venv/src/lib.rs +++ b/crates/pet-venv/src/lib.rs @@ -42,7 +42,10 @@ impl Locator for Venv { } let version = match env.version { Some(ref v) => Some(v.clone()), - None => match &env.prefix { Some(prefix) => Headers::get_version(prefix), None=> None } + None => match &env.prefix { + Some(prefix) => Headers::get_version(prefix), + None => None, + }, }; Some( PythonEnvironmentBuilder::new(PythonEnvironmentCategory::Venv)