Skip to content

Commit 2288b94

Browse files
authored
Updates to extraction of version (#20)
1 parent c8511c3 commit 2288b94

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

crates/pet-utils/src/path.rs

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

4-
use std::{
5-
fs,
6-
path::{Path, PathBuf},
7-
};
4+
use std::path::{Path, PathBuf};
85

96
// Similar to fs::canonicalize, but ignores UNC paths and returns the path as is (for windows).
107
pub fn normalize<P: AsRef<Path>>(path: P) -> PathBuf {
118
// On unix do not use canonicalize, results in weird issues with homebrew paths
12-
if cfg!(unix) {
13-
return path.as_ref().to_path_buf();
14-
}
9+
#[cfg(unix)]
10+
return path.as_ref().to_path_buf();
11+
12+
#[cfg(windows)]
13+
use std::fs;
1514

15+
#[cfg(windows)]
1616
if let Ok(resolved) = fs::canonicalize(&path) {
1717
if cfg!(unix) {
1818
return resolved;

crates/pet-venv/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use pet_core::{
55
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentCategory},
66
Locator, LocatorResult,
77
};
8-
use pet_utils::{env::PythonEnv, pyvenv_cfg::PyVenvCfg};
8+
use pet_utils::{env::PythonEnv, headers::Headers, pyvenv_cfg::PyVenvCfg};
99

1010
fn is_venv_internal(env: &PythonEnv) -> Option<bool> {
1111
// env path cannot be empty.
@@ -40,12 +40,18 @@ impl Locator for Venv {
4040
if let Some(filename) = &env.prefix {
4141
name = filename.to_str().map(|f| f.to_string());
4242
}
43-
43+
let version = match env.version {
44+
Some(ref v) => Some(v.clone()),
45+
None => match &env.prefix {
46+
Some(prefix) => Headers::get_version(prefix),
47+
None => None,
48+
},
49+
};
4450
Some(
4551
PythonEnvironmentBuilder::new(PythonEnvironmentCategory::Venv)
4652
.name(name)
4753
.executable(Some(env.executable.clone()))
48-
.version(env.version.clone())
54+
.version(version)
4955
.prefix(env.prefix.clone())
5056
.build(),
5157
)

crates/pet-virtualenv/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use pet_core::{
77
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentCategory},
88
Locator, LocatorResult,
99
};
10-
use pet_utils::env::PythonEnv;
10+
use pet_utils::{env::PythonEnv, headers::Headers};
1111

1212
pub fn is_virtualenv(env: &PythonEnv) -> bool {
1313
if env.prefix.is_none() {
@@ -70,12 +70,19 @@ impl Locator for VirtualEnv {
7070
if let Some(filename) = &env.prefix {
7171
name = filename.to_str().map(|f| f.to_string());
7272
}
73+
let version = match env.version {
74+
Some(ref v) => Some(v.clone()),
75+
None => match &env.prefix {
76+
Some(prefix) => Headers::get_version(prefix),
77+
None => None,
78+
},
79+
};
7380

7481
Some(
7582
PythonEnvironmentBuilder::new(PythonEnvironmentCategory::VirtualEnv)
7683
.name(name)
7784
.executable(Some(env.executable.clone()))
78-
.version(env.version.clone())
85+
.version(version)
7986
.prefix(env.prefix.clone())
8087
.build(),
8188
)

crates/pet-virtualenvwrapper/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use pet_core::{
99
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentCategory},
1010
Locator, LocatorResult,
1111
};
12-
use pet_utils::env::PythonEnv;
12+
use pet_utils::{env::PythonEnv, headers::Headers};
1313

1414
mod env_variables;
1515
mod environment_locations;
@@ -36,12 +36,19 @@ impl Locator for VirtualEnvWrapper {
3636
if let Some(filename) = &env.prefix {
3737
name = filename.to_str().map(|f| f.to_string());
3838
}
39+
let version = match env.version {
40+
Some(ref v) => Some(v.clone()),
41+
None => match &env.prefix {
42+
Some(prefix) => Headers::get_version(prefix),
43+
None => None,
44+
},
45+
};
3946

4047
Some(
4148
PythonEnvironmentBuilder::new(PythonEnvironmentCategory::VirtualEnvWrapper)
4249
.name(name)
4350
.executable(Some(env.executable.clone()))
44-
.version(env.version.clone())
51+
.version(version)
4552
.prefix(env.prefix.clone())
4653
.project(get_project(env))
4754
.build(),

0 commit comments

Comments
 (0)