Skip to content

Commit 398e32a

Browse files
authored
Ensure we parse env variables once (#94)
1 parent f4f78b0 commit 398e32a

File tree

26 files changed

+175
-147
lines changed

26 files changed

+175
-147
lines changed

crates/pet-conda/src/environment_locations.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ fn get_conda_environment_paths_from_known_paths(env_vars: &EnvVariables) -> Vec<
9090
if let Ok(entries) = fs::read_dir(full_path) {
9191
for entry in entries.filter_map(Result::ok) {
9292
let path = entry.path();
93-
if let Ok(meta) = fs::metadata(&path) {
94-
if meta.is_dir() {
95-
env_paths.push(path);
96-
}
93+
if path.is_dir() {
94+
env_paths.push(path);
9795
}
9896
}
9997
}
@@ -112,10 +110,8 @@ fn get_conda_environment_paths_from_additional_paths(
112110
if let Ok(entries) = fs::read_dir(path) {
113111
for entry in entries.filter_map(Result::ok) {
114112
let path = entry.path();
115-
if let Ok(meta) = fs::metadata(&path) {
116-
if meta.is_dir() {
117-
env_paths.push(path);
118-
}
113+
if path.is_dir() {
114+
env_paths.push(path);
119115
}
120116
}
121117
}
@@ -146,7 +142,7 @@ pub fn get_environments(conda_dir: &Path) -> Vec<PathBuf> {
146142
}
147143
} else if is_conda_env(conda_dir) {
148144
envs.push(conda_dir.to_path_buf());
149-
} else if fs::metadata(conda_dir.join("envs")).is_ok() {
145+
} else if conda_dir.join("envs").exists() {
150146
// This could be a directory where conda environments are stored.
151147
// I.e. its not necessarily the root conda install directory.
152148
// E.g. C:\Users\donjayamanne\.conda

crates/pet-conda/src/environments.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use pet_core::{
1414
};
1515
use pet_fs::path::{norm_case, resolve_symlink};
1616
use pet_python_utils::executable::{find_executable, find_executables};
17-
use std::{
18-
fs,
19-
path::{Path, PathBuf},
20-
};
17+
use std::path::{Path, PathBuf};
2118

2219
#[derive(Debug, Clone)]
2320
pub struct CondaEnvironment {
@@ -84,7 +81,7 @@ pub fn get_conda_environment_info(
8481
None => get_conda_installation_used_to_create_conda_env(env_path),
8582
};
8683
if let Some(conda_dir) = &conda_install_folder {
87-
if fs::metadata(conda_dir).is_err() {
84+
if !conda_dir.exists() {
8885
warn!(
8986
"Conda install folder {}, does not exist, hence will not be used for the Conda Env: {}",
9087
env_path.display(),

crates/pet-conda/src/manager.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn get_conda_executable(path: &Path) -> Option<PathBuf> {
2323

2424
for relative_path in relative_path_to_conda_exe {
2525
let exe = path.join(&relative_path);
26-
if exe.metadata().is_ok() {
26+
if exe.exists() {
2727
return Some(exe);
2828
}
2929
}
@@ -49,10 +49,8 @@ pub fn find_conda_binary(env_vars: &EnvVariables) -> Option<PathBuf> {
4949
for path in env::split_paths(&paths) {
5050
for bin in get_conda_bin_names() {
5151
let conda_path = path.join(bin);
52-
if let Ok(metadata) = std::fs::metadata(&conda_path) {
53-
if metadata.is_file() || metadata.is_symlink() {
54-
return Some(conda_path);
55-
}
52+
if conda_path.is_file() || conda_path.is_symlink() {
53+
return Some(conda_path);
5654
}
5755
}
5856
}

crates/pet-conda/src/utils.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
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
/// conda-meta must exist as this contains a mandatory `history` file.
107
pub fn is_conda_install(path: &Path) -> bool {
11-
(path.join("condabin").metadata().is_ok() || path.join("envs").metadata().is_ok())
12-
&& path.join("conda-meta").metadata().is_ok()
8+
(path.join("condabin").exists() || path.join("envs").exists())
9+
&& path.join("conda-meta").exists()
1310
}
1411

1512
/// conda-meta must exist as this contains a mandatory `history` file.
1613
/// The root conda installation folder is also a conda environment (its the base environment).
1714
pub fn is_conda_env(path: &Path) -> bool {
18-
if let Ok(metadata) = fs::metadata(path.join("conda-meta")) {
19-
metadata.is_dir()
20-
} else {
21-
false
22-
}
15+
path.join("conda-meta").is_dir()
2316
}
2417

2518
/// Only used in tests, noop in production.

crates/pet-core/src/os_environment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
use log::trace;
1111
use pet_fs::path::norm_case;
1212

13-
pub trait Environment {
13+
pub trait Environment: Send + Sync {
1414
fn get_user_home(&self) -> Option<PathBuf>;
1515
/// Only used in tests, None in production.
1616
#[allow(dead_code)]

crates/pet-core/src/python_environment.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ impl PythonEnvironment {
114114

115115
impl std::fmt::Display for PythonEnvironment {
116116
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
117-
writeln!(f, "Environment ({:?})", self.kind).unwrap_or_default();
117+
writeln!(
118+
f,
119+
"Environment ({})",
120+
self.kind
121+
.map(|v| format!("{v:?}"))
122+
.unwrap_or("Unknown".to_string())
123+
)
124+
.unwrap_or_default();
118125
if let Some(name) = &self.display_name {
119126
writeln!(f, " Display-Name: {name}").unwrap_or_default();
120127
}

crates/pet-global-virtualenvs/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ fn get_global_virtualenv_dirs(
1414

1515
if let Some(work_on_home) = work_on_home_env_var {
1616
let work_on_home = norm_case(PathBuf::from(work_on_home));
17-
if fs::metadata(&work_on_home).is_ok() {
17+
if work_on_home.exists() {
1818
venv_dirs.push(work_on_home);
1919
} else if let Some(home) = &user_home {
2020
if let Ok(work_on_home) = work_on_home.strip_prefix("~") {
2121
let work_on_home = home.join(work_on_home);
22-
if fs::metadata(&work_on_home).is_ok() {
22+
if work_on_home.exists() {
2323
venv_dirs.push(work_on_home);
2424
}
2525
}
@@ -28,7 +28,7 @@ fn get_global_virtualenv_dirs(
2828

2929
// Used by pipenv (https://github.com/pypa/pipenv/blob/main/pipenv/utils/shell.py#L184)
3030
if let Some(xdg_data_home) = xdg_data_home.map(|d| PathBuf::from(d).join("virtualenvs")) {
31-
if fs::metadata(&xdg_data_home).is_ok() {
31+
if xdg_data_home.exists() {
3232
venv_dirs.push(xdg_data_home);
3333
}
3434
}
@@ -41,19 +41,19 @@ fn get_global_virtualenv_dirs(
4141
PathBuf::from(".local").join("share").join("virtualenvs"), // Used by pipenv (https://github.com/pypa/pipenv/blob/main/pipenv/utils/shell.py#L184)
4242
] {
4343
let venv_dir = home.join(dir);
44-
if fs::metadata(&venv_dir).is_ok() {
44+
if venv_dir.exists() {
4545
venv_dirs.push(venv_dir);
4646
}
4747
}
4848
if cfg!(target_os = "linux") {
4949
// https://virtualenvwrapper.readthedocs.io/en/latest/index.html
5050
// Default recommended location for virtualenvwrapper
5151
let envs = PathBuf::from("Envs");
52-
if fs::metadata(&envs).is_ok() {
52+
if envs.exists() {
5353
venv_dirs.push(envs);
5454
}
5555
let envs = PathBuf::from("envs");
56-
if fs::metadata(&envs).is_ok() {
56+
if envs.exists() {
5757
venv_dirs.push(envs);
5858
}
5959
}

crates/pet-homebrew/src/environment_locations.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::env_variables::EnvVariables;
55
use lazy_static::lazy_static;
66
use regex::Regex;
7-
use std::{fs, path::PathBuf};
7+
use std::path::PathBuf;
88

99
lazy_static! {
1010
static ref PYTHON_VERSION: Regex =
@@ -41,9 +41,7 @@ pub fn get_homebrew_prefix_bin(env_vars: &EnvVariables) -> Vec<PathBuf> {
4141
// Check the environment variables
4242
if let Some(homebrew_prefix) = &env_vars.homebrew_prefix {
4343
let homebrew_prefix_bin = PathBuf::from(homebrew_prefix).join("bin");
44-
if fs::metadata(&homebrew_prefix_bin).is_ok()
45-
&& !homebrew_prefixes.contains(&homebrew_prefix_bin)
46-
{
44+
if homebrew_prefix_bin.exists() && !homebrew_prefixes.contains(&homebrew_prefix_bin) {
4745
homebrew_prefixes.push(homebrew_prefix_bin);
4846
}
4947
}

crates/pet-pipenv/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn get_pipenv_project_from_prefix(prefix: &Path) -> Option<PathBuf> {
3737
let project_file = prefix.join(".project");
3838
let contents = fs::read_to_string(project_file).ok()?;
3939
let project_folder = norm_case(PathBuf::from(contents.trim().to_string()));
40-
if fs::metadata(&project_folder).is_ok() {
40+
if project_folder.exists() {
4141
Some(project_folder)
4242
} else {
4343
None
@@ -46,14 +46,14 @@ fn get_pipenv_project_from_prefix(prefix: &Path) -> Option<PathBuf> {
4646

4747
fn is_pipenv(env: &PythonEnv, env_vars: &EnvVariables) -> bool {
4848
if let Some(project_path) = get_pipenv_project(env) {
49-
if fs::metadata(project_path.join(env_vars.pipenv_pipfile.clone())).is_ok() {
49+
if project_path.join(env_vars.pipenv_pipfile.clone()).exists() {
5050
return true;
5151
}
5252
}
5353
// If we have a Pipfile, then this is a pipenv environment.
5454
// Else likely a virtualenvwrapper or the like.
5555
if let Some(project_path) = get_pipenv_project(env) {
56-
fs::metadata(project_path.join(env_vars.pipenv_pipfile.clone())).is_ok()
56+
project_path.join(env_vars.pipenv_pipfile.clone()).exists()
5757
} else {
5858
false
5959
}

crates/pet-pyenv/src/environment_locations.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::env_variables::EnvVariables;
55
use pet_fs::path::norm_case;
6-
use std::{fs, path::PathBuf};
6+
use std::path::PathBuf;
77

88
#[cfg(windows)]
99
pub fn get_home_pyenv_dir(env_vars: &EnvVariables) -> Option<PathBuf> {
@@ -24,10 +24,8 @@ pub fn get_binary_from_known_paths(env_vars: &EnvVariables) -> Option<PathBuf> {
2424
} else {
2525
known_path.join("pyenv")
2626
};
27-
if let Ok(metadata) = fs::metadata(&exe) {
28-
if metadata.is_file() {
29-
return Some(norm_case(exe));
30-
}
27+
if exe.is_file() {
28+
return Some(norm_case(exe));
3129
}
3230
}
3331
None

0 commit comments

Comments
 (0)