-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsym_links.rs
More file actions
349 lines (318 loc) · 17 KB
/
sym_links.rs
File metadata and controls
349 lines (318 loc) · 17 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use lazy_static::lazy_static;
use pet_fs::path::resolve_symlink;
use pet_python_utils::executable::find_executables;
use rayon::prelude::*;
use regex::Regex;
use std::{
fs,
path::{Path, PathBuf},
};
lazy_static! {
static ref PYTHON_VERSION: Regex =
Regex::new(r"/python@((\d+\.?)*)/").expect("error parsing Version regex for Homebrew");
}
pub fn is_homebrew_python(exe: &Path) -> bool {
exe.starts_with("/opt/homebrew")
|| exe.starts_with("/usr/local/Cellar")
|| exe.starts_with("/home/linuxbrew/.linuxbrew")
}
pub fn get_known_symlinks(
symlink_resolved_python_exe: &Path,
full_version: &String,
) -> Vec<PathBuf> {
let mut symlinks = get_known_symlinks_impl(symlink_resolved_python_exe, full_version);
// Go through all the exes in all of the above bin directories and verify we have a list of all of them.
// They too could be symlinks, e.g. we could have `/opt/homebrew/bin/python3` & also `/opt/homebrew/bin/python`
// And possible they are all symlnks to the same exe.
let known_symlinks = symlinks.clone();
let other_symlinks: Vec<PathBuf> = symlinks
.par_iter()
.flat_map(|symlink| {
if let Some(bin) = symlink.parent() {
find_executables(bin)
.into_iter()
.filter(|possible_symlink| {
if let Some(resolved) = resolve_symlink(possible_symlink) {
known_symlinks.contains(&resolved)
} else {
false
}
})
.collect::<Vec<_>>()
} else {
vec![]
}
})
.collect();
symlinks.extend(other_symlinks);
symlinks.sort();
symlinks.dedup();
symlinks
}
pub fn get_known_symlinks_impl(
symlink_resolved_python_exe: &Path,
full_version: &String,
) -> Vec<PathBuf> {
if symlink_resolved_python_exe.starts_with("/opt/homebrew") {
// Real exe - /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12
// Known symlinks include
// /opt/homebrew/bin/python3.12
// /opt/homebrew/opt/python3/bin/python3.12
// /opt/homebrew/Cellar/python@3.12/3.12.3/bin/python3.12
// /opt/homebrew/opt/python@3.12/bin/python3.12
// /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12
// /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/Current/bin/python3.12
// /opt/homebrew/Frameworks/Python.framework/Versions/3.12/bin/python3.12
// /opt/homebrew/Frameworks/Python.framework/Versions/Current/bin/python3.12
// /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12
match PYTHON_VERSION.captures(symlink_resolved_python_exe.to_str().unwrap_or_default()) {
Some(captures) => match captures.get(1) {
Some(version) => {
let version = version.as_str().to_string();
let mut symlinks = vec![symlink_resolved_python_exe.to_owned()];
for possible_symlink in [
PathBuf::from(format!("/opt/homebrew/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/opt/python@{version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{version}/{full_version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{version}/{full_version}/Frameworks/Python.framework/Versions/{version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{version}/{full_version}/Frameworks/Python.framework/Versions/Current/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/{version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/Current/bin/python{version}")),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python3")),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python{version}")),
PathBuf::from("/opt/homebrew/opt/python/bin/python3"),
PathBuf::from(format!("/opt/homebrew/opt/python/bin/python{version}")),
PathBuf::from("/opt/homebrew/opt/python@3/bin/python3"),
PathBuf::from(format!("/opt/homebrew/opt/python@3/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/opt/python@{version}/bin/python3")),
PathBuf::from(format!("/opt/homebrew/opt/python@{version}/bin/python{version}")),
PathBuf::from("/usr/local/opt/python@3/bin/python3"),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from(format!("/opt/homebrew/opt/python3/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/opt/homebrew/bin/python3"),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/opt/homebrew/bin/python")
] {
// Validate the symlinks
if symlinks.contains(
&resolve_symlink(&possible_symlink)
.or(fs::canonicalize(&possible_symlink).ok())
.unwrap_or_default(),
) {
symlinks.push(possible_symlink);
}
}
symlinks
}
None => vec![],
},
None => vec![],
}
} else if symlink_resolved_python_exe.starts_with("/usr/local/Cellar") {
// Real exe - /usr/local/Cellar/python@3.8/3.8.20/Frameworks/Python.framework/Versions/3.8/bin/python3.8
// Known symlinks include
// /usr/local/bin/python3.8
// /usr/local/opt/python@3.8/bin/python3.8
// /usr/local/Cellar/python@3.8/3.8.20/bin/python3.8
// /usr/local/Cellar/python@3.8/3.8.20/Frameworks/Python.framework/Versions/3.8/bin/python3.8
match PYTHON_VERSION.captures(symlink_resolved_python_exe.to_str().unwrap_or_default()) {
Some(captures) => match captures.get(1) {
Some(version) => {
let version = version.as_str().to_string();
// Never include `/usr/local/bin/python` into this list.
// See previous explanation
let mut symlinks = vec![symlink_resolved_python_exe.to_owned()];
for possible_symlink in [
// While testing found that on Mac Intel
// 1. python 3.8 has sysprefix in /usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9
// 2. python 3.9 has sysprefix in /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9
// 3. python 3.11 has sysprefix in /usr/local/opt/python@3.11/Frameworks/Python.framework/Versions/3.11
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python3")),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python{version}")),
PathBuf::from("/usr/local/opt/python@3/bin/python3"),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{version}")),
PathBuf::from(format!(
"/usr/local/Cellar/python@{version}/{full_version}/bin/python{version}"
)),
PathBuf::from(format!(
"/usr/local/Cellar/python@{version}/{full_version}/Frameworks/Python.framework/Versions/{version}/bin/python{version}"
)),
// This is a special folder, if users install python using other means, this file
// might get overridden. So we should only add this if this files points to the same place
PathBuf::from(format!("/usr/local/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/usr/local/bin/python3"),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/usr/local/bin/python"),
] {
// Validate the symlinks
if symlinks.contains(
&resolve_symlink(&possible_symlink)
// .or(fs::canonicalize(&possible_symlink).ok())
.unwrap_or_default(),
) {
symlinks.push(possible_symlink);
}
}
symlinks
}
None => vec![],
},
None => vec![],
}
} else if symlink_resolved_python_exe.starts_with("/home/linuxbrew/.linuxbrew") {
// Real exe - /home/linuxbrew/.linuxbrew/Cellar/python@3.12/3.12.3/bin/python3.12
// Known symlinks include
// /usr/local/bin/python3.12
// /home/linuxbrew/.linuxbrew/bin/python3.12
// /home/linuxbrew/.linuxbrew/opt/python@3.12/bin/python3.12
match PYTHON_VERSION.captures(symlink_resolved_python_exe.to_str().unwrap_or_default()) {
Some(captures) => match captures.get(1) {
Some(version) => {
let version = version.as_str().to_string();
// Never include `/usr/local/bin/python` into this list.
// See previous explanation
let mut symlinks = vec![symlink_resolved_python_exe.to_owned()];
for possible_symlink in [
PathBuf::from("/home/linuxbrew/.linuxbrew/bin/python3"),
PathBuf::from(format!("/home/linuxbrew/.linuxbrew/bin/python{version}")),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/Cellar/python@{version}/{full_version}/bin/python{version}"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/Cellar/python@{version}/{full_version}/bin/python3"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@{version}/bin/python{version}"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@{version}/bin/python3"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python3/bin/python{version}"
)),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/python3/bin/python3"),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@3/bin/python{version}"
)),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/python@3/bin/python3"),
// This is a special folder, if users install python using other means, this file
// might get overridden. So we should only add this if this files points to the same place
PathBuf::from(format!("/usr/local/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/usr/local/bin/python3"),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/usr/local/bin/python"),
] {
// Validate the symlinks
if symlinks.contains(
&resolve_symlink(&possible_symlink)
.or(fs::canonicalize(&possible_symlink).ok())
.unwrap_or_default(),
) {
symlinks.push(possible_symlink);
}
}
symlinks
}
None => vec![],
},
None => vec![],
}
} else {
vec![]
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn homebrew_python_paths_are_recognized_across_supported_prefixes() {
assert!(is_homebrew_python(Path::new(
"/opt/homebrew/Cellar/python@3.12/3.12.4/bin/python3.12"
)));
assert!(is_homebrew_python(Path::new(
"/usr/local/Cellar/python@3.11/3.11.9/bin/python3.11"
)));
assert!(is_homebrew_python(Path::new(
"/home/linuxbrew/.linuxbrew/Cellar/python@3.12/3.12.4/bin/python3.12"
)));
assert!(!is_homebrew_python(Path::new("/usr/bin/python3.12")));
}
#[test]
fn is_homebrew_python_recognizes_opt_homebrew_bin_paths() {
assert!(is_homebrew_python(Path::new(
"/opt/homebrew/bin/python3.12"
)));
assert!(is_homebrew_python(Path::new(
"/opt/homebrew/opt/python@3.12/bin/python3.12"
)));
assert!(is_homebrew_python(Path::new(
"/opt/homebrew/Frameworks/Python.framework/Versions/3.12/bin/python3.12"
)));
}
#[test]
fn is_homebrew_python_rejects_non_homebrew_paths() {
assert!(!is_homebrew_python(Path::new("/usr/local/bin/python3.12")));
assert!(!is_homebrew_python(Path::new("/usr/bin/python3")));
assert!(!is_homebrew_python(Path::new(
"/home/user/.pyenv/versions/3.12.0/bin/python3.12"
)));
assert!(!is_homebrew_python(Path::new("")));
}
#[test]
fn known_symlink_templates_include_resolved_executable_for_linuxbrew() {
let resolved_exe =
PathBuf::from("/home/linuxbrew/.linuxbrew/Cellar/python@3.12/3.12.4/bin/python3.12");
let symlinks = get_known_symlinks_impl(&resolved_exe, &"3.12.4".to_string());
assert!(symlinks.contains(&resolved_exe));
}
#[test]
fn known_symlink_templates_return_empty_for_unrecognized_paths() {
assert!(
get_known_symlinks_impl(Path::new("/usr/bin/python3.12"), &"3.12.4".to_string())
.is_empty()
);
}
#[test]
fn known_symlink_templates_include_self_for_opt_homebrew() {
let resolved_exe = PathBuf::from(
"/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12",
);
let symlinks = get_known_symlinks_impl(&resolved_exe, &"3.12.3".to_string());
assert!(symlinks.contains(&resolved_exe));
assert!(symlinks.len() >= 1);
}
#[test]
fn known_symlink_templates_include_self_for_usr_local_cellar() {
let resolved_exe = PathBuf::from(
"/usr/local/Cellar/python@3.8/3.8.20/Frameworks/Python.framework/Versions/3.8/bin/python3.8",
);
let symlinks = get_known_symlinks_impl(&resolved_exe, &"3.8.20".to_string());
assert!(symlinks.contains(&resolved_exe));
assert!(symlinks.len() >= 1);
}
#[test]
fn known_symlink_templates_return_empty_when_version_regex_does_not_match() {
// Path under /opt/homebrew but without a python@version segment
let resolved_exe = PathBuf::from("/opt/homebrew/bin/python3.12");
let symlinks = get_known_symlinks_impl(&resolved_exe, &"3.12.0".to_string());
// No python@version/ in path, so regex won't capture → returns empty
assert!(symlinks.is_empty());
}
#[test]
fn known_symlink_templates_for_linuxbrew_contain_expected_paths() {
let resolved_exe =
PathBuf::from("/home/linuxbrew/.linuxbrew/Cellar/python@3.12/3.12.4/bin/python3.12");
let symlinks = get_known_symlinks_impl(&resolved_exe, &"3.12.4".to_string());
// The resolved exe itself is always included
assert!(symlinks.contains(&resolved_exe));
// On a test system without real symlinks, only the resolved exe will pass validation.
// But verify the function doesn't panic and returns at least the resolved exe.
assert!(!symlinks.is_empty());
}
}