-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlib.rs
More file actions
256 lines (236 loc) · 8.39 KB
/
lib.rs
File metadata and controls
256 lines (236 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use env_variables::EnvVariables;
use pet_core::env::PythonEnv;
use pet_core::os_environment::Environment;
use pet_core::LocatorKind;
use pet_core::{
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
reporter::Reporter,
Locator,
};
use pet_fs::path::norm_case;
use pet_python_utils::executable::find_executables;
use pet_python_utils::version;
use std::path::Path;
use std::{fs, path::PathBuf};
mod env_variables;
fn get_pipenv_project(env: &PythonEnv) -> Option<PathBuf> {
if let Some(prefix) = &env.prefix {
if let Some(project) = get_pipenv_project_from_prefix(prefix) {
return Some(project);
}
// If there's no .project file, but the venv lives inside the project folder
// (e.g., <project>/.venv or <project>/venv), then the project is the parent
// directory of the venv. Detect that by checking for a Pipfile next to the venv.
if let Some(parent) = prefix.parent() {
let project_folder = parent;
if project_folder.join("Pipfile").exists() {
return Some(project_folder.to_path_buf());
}
}
}
// We can also have a venv in the workspace that has pipenv installed in it.
// In such cases, the project is the workspace folder containing the venv.
// Derive the project folder from the executable path when prefix isn't available.
// Typical layout: <project>/.venv/{bin|Scripts}/python
// So walk up to {bin|Scripts} -> venv dir -> project dir and check for Pipfile.
if let Some(bin) = env.executable.parent() {
let venv_dir = if bin.file_name().unwrap_or_default() == Path::new("bin")
|| bin.file_name().unwrap_or_default() == Path::new("Scripts")
{
bin.parent()
} else {
Some(bin)
};
if let Some(venv_dir) = venv_dir {
if let Some(project_dir) = venv_dir.parent() {
if project_dir.join("Pipfile").exists() {
return Some(project_dir.to_path_buf());
}
}
}
}
// If the parent is bin or script, then get the parent.
let bin = env.executable.parent()?;
if bin.file_name().unwrap_or_default() == Path::new("bin")
|| bin.file_name().unwrap_or_default() == Path::new("Scripts")
{
get_pipenv_project_from_prefix(env.executable.parent()?.parent()?)
} else {
get_pipenv_project_from_prefix(env.executable.parent()?)
}
}
fn get_pipenv_project_from_prefix(prefix: &Path) -> Option<PathBuf> {
let project_file = prefix.join(".project");
if !project_file.exists() {
return None;
}
let contents = fs::read_to_string(project_file).ok()?;
let project_folder = norm_case(PathBuf::from(contents.trim().to_string()));
if project_folder.exists() {
Some(project_folder)
} else {
None
}
}
fn is_pipenv_from_project(env: &PythonEnv) -> bool {
// If the env prefix is inside a project folder, check that folder for a Pipfile.
if let Some(prefix) = &env.prefix {
if let Some(project_dir) = prefix.parent() {
if project_dir.join("Pipfile").exists() {
return true;
}
}
}
// Derive from the executable path as a fallback.
if let Some(bin) = env.executable.parent() {
let venv_dir = if bin.file_name().unwrap_or_default() == Path::new("bin")
|| bin.file_name().unwrap_or_default() == Path::new("Scripts")
{
bin.parent()
} else {
Some(bin)
};
if let Some(venv_dir) = venv_dir {
if let Some(project_dir) = venv_dir.parent() {
if project_dir.join("Pipfile").exists() {
return true;
}
}
}
}
false
}
fn is_pipenv(env: &PythonEnv, env_vars: &EnvVariables) -> bool {
if let Some(project_path) = get_pipenv_project(env) {
if project_path.join(env_vars.pipenv_pipfile.clone()).exists() {
return true;
}
}
if is_pipenv_from_project(env) {
return true;
}
// If we have a Pipfile, then this is a pipenv environment.
// Else likely a virtualenvwrapper or the like.
if let Some(project_path) = get_pipenv_project(env) {
project_path.join(env_vars.pipenv_pipfile.clone()).exists()
} else {
false
}
}
pub struct PipEnv {
env_vars: EnvVariables,
}
impl PipEnv {
pub fn from(environment: &dyn Environment) -> PipEnv {
PipEnv {
env_vars: EnvVariables::from(environment),
}
}
}
impl Locator for PipEnv {
fn get_kind(&self) -> LocatorKind {
LocatorKind::PipEnv
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::Pipenv]
}
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if !is_pipenv(env, &self.env_vars) {
return None;
}
let project_path = get_pipenv_project(env)?;
let mut prefix = env.prefix.clone();
if prefix.is_none() {
if let Some(bin) = env.executable.parent() {
if bin.file_name().unwrap_or_default() == Path::new("bin")
|| bin.file_name().unwrap_or_default() == Path::new("Scripts")
{
if let Some(dir) = bin.parent() {
prefix = Some(dir.to_owned());
}
}
}
}
let bin = env.executable.parent()?;
let symlinks = find_executables(bin);
let mut version = env.version.clone();
if version.is_none() && prefix.is_some() {
if let Some(prefix) = &prefix {
version = version::from_creator_for_virtual_env(prefix);
}
}
Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Pipenv))
.executable(Some(env.executable.clone()))
.version(version)
.prefix(prefix)
.project(Some(project_path))
.symlinks(Some(symlinks))
.build(),
)
}
fn find(&self, _reporter: &dyn Reporter) {
//
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{SystemTime, UNIX_EPOCH};
fn unique_temp_dir() -> PathBuf {
let mut dir = std::env::temp_dir();
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
dir.push(format!("pet_pipenv_test_{}", nanos));
dir
}
#[test]
fn infer_project_for_venv_in_project() {
let project_dir = unique_temp_dir();
let venv_dir = project_dir.join(".venv");
let bin_dir = if cfg!(windows) {
venv_dir.join("Scripts")
} else {
venv_dir.join("bin")
};
let python_exe = if cfg!(windows) {
bin_dir.join("python.exe")
} else {
bin_dir.join("python")
};
// Create directories and files
std::fs::create_dir_all(&bin_dir).unwrap();
std::fs::write(project_dir.join("Pipfile"), b"[[source]]\n").unwrap();
// Touch python exe file
std::fs::write(&python_exe, b"").unwrap();
// Touch pyvenv.cfg in venv root so PythonEnv::new logic would normally detect prefix
std::fs::write(venv_dir.join("pyvenv.cfg"), b"version = 3.12.0\n").unwrap();
// Construct PythonEnv directly
let env = PythonEnv {
executable: norm_case(python_exe.clone()),
prefix: Some(norm_case(venv_dir.clone())),
version: None,
symlinks: None,
};
// Validate helper infers project
let inferred = get_pipenv_project(&env).expect("expected project path");
assert_eq!(inferred, norm_case(project_dir.clone()));
// Validate locator populates project
let locator = PipEnv {
env_vars: EnvVariables {
pipenv_max_depth: 3,
pipenv_pipfile: "Pipfile".to_string(),
},
};
let result = locator
.try_from(&env)
.expect("expected locator to return environment");
assert_eq!(result.project, Some(norm_case(project_dir.clone())));
// Cleanup
std::fs::remove_dir_all(&project_dir).ok();
}
}