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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::{
collections::HashMap,
env,
fmt::Write,
fs::File,
io::{BufRead, BufReader, Read},
iter::FromIterator,
path::{Path, PathBuf},
str::FromStr,
string::ToString,
};
use comfy_table::{Table, TableComponent};
use crate::{ExactVersion, RequestedVersion};
pub static DEFAULT_VENV_DIR: &str = ".venv";
#[derive(Clone, Debug, Hash, PartialEq)]
pub enum Action {
Help(String, PathBuf),
List(String),
Execute {
launcher_path: PathBuf,
executable: PathBuf,
args: Vec<String>,
},
}
impl Action {
pub fn from_main(argv: &[String]) -> crate::Result<Self> {
let launcher_path = PathBuf::from(&argv[0]);
match argv.get(1) {
Some(flag) if flag == "-h" || flag == "--help" || flag == "--list" => {
if argv.len() > 2 {
Err(crate::Error::IllegalArgument(
launcher_path,
flag.to_string(),
))
} else if flag == "--list" {
Ok(Action::List(list_executables(&crate::all_executables())?))
} else {
crate::find_executable(RequestedVersion::Any)
.ok_or(crate::Error::NoExecutableFound(RequestedVersion::Any))
.map(|executable_path| {
Action::Help(
help_message(&launcher_path, &executable_path),
executable_path,
)
})
}
}
Some(version) if version_from_flag(version).is_some() => {
Ok(Action::Execute {
launcher_path,
executable: find_executable(version_from_flag(version).unwrap(), &argv[2..])?,
args: argv[2..].to_vec(),
})
}
Some(_) | None => Ok(Action::Execute {
launcher_path,
executable: find_executable(RequestedVersion::Any, &argv[1..])?,
args: argv[1..].to_vec(),
}),
}
}
}
fn help_message(launcher_path: &Path, executable_path: &Path) -> String {
let mut message = String::new();
writeln!(
message,
include_str!("HELP.txt"),
env!("CARGO_PKG_VERSION"),
launcher_path.to_string_lossy(),
executable_path.to_string_lossy()
)
.unwrap();
message
}
fn version_from_flag(arg: &str) -> Option<RequestedVersion> {
if !arg.starts_with('-') {
None
} else {
RequestedVersion::from_str(&arg[1..]).ok()
}
}
fn list_executables(executables: &HashMap<ExactVersion, PathBuf>) -> crate::Result<String> {
if executables.is_empty() {
return Err(crate::Error::NoExecutableFound(RequestedVersion::Any));
}
let mut executable_pairs = Vec::from_iter(executables);
executable_pairs.sort_unstable();
executable_pairs.reverse();
let mut table = Table::new();
table.load_preset(comfy_table::presets::NOTHING);
table.set_style(TableComponent::VerticalLines, '│');
for (version, path) in executable_pairs {
table.add_row(vec![version.to_string(), path.display().to_string()]);
}
Ok(table.to_string() + "\n")
}
fn relative_venv_path(add_default: bool) -> PathBuf {
let mut path = PathBuf::new();
if add_default {
path.push(DEFAULT_VENV_DIR);
}
path.push("bin");
path.push("python");
path
}
fn venv_executable_path(venv_root: &str) -> PathBuf {
PathBuf::from(venv_root).join(relative_venv_path(false))
}
fn activated_venv() -> Option<PathBuf> {
log::info!("Checking for VIRTUAL_ENV environment variable");
env::var_os("VIRTUAL_ENV").map(|venv_root| {
log::debug!("VIRTUAL_ENV set to {:?}", venv_root);
venv_executable_path(&venv_root.to_string_lossy())
})
}
fn venv_path_search() -> Option<PathBuf> {
if env::current_dir().is_err() {
log::warn!("current working directory is invalid");
None
} else {
let cwd = env::current_dir().unwrap();
log::info!(
"Searching for a venv in {} and parent directories",
cwd.display()
);
cwd.ancestors().find_map(|path| {
let venv_path = path.join(relative_venv_path(true));
log::info!("Checking {}", venv_path.display());
venv_path.is_file().then(|| venv_path)
})
}
}
fn venv_executable() -> Option<PathBuf> {
activated_venv().or_else(venv_path_search)
}
fn parse_python_shebang(reader: &mut impl Read) -> Option<RequestedVersion> {
let mut shebang_buffer = [0; 2];
log::info!("Looking for a Python-related shebang");
if reader.read(&mut shebang_buffer).is_err() || shebang_buffer != [0x23, 0x21] {
log::debug!("No '#!' at the start of the first line of the file");
return None;
}
let mut buffered_reader = BufReader::new(reader);
let mut first_line = String::new();
if buffered_reader.read_line(&mut first_line).is_err() {
log::debug!("Can't read first line of the file");
return None;
};
let line = first_line.trim();
let accepted_paths = [
"python",
"/usr/bin/python",
"/usr/local/bin/python",
"/usr/bin/env python",
];
for acceptable_path in &accepted_paths {
if !line.starts_with(acceptable_path) {
continue;
}
log::debug!("Found shebang: {}", acceptable_path);
let version = line[acceptable_path.len()..].to_string();
log::debug!("Found version: {}", version);
return RequestedVersion::from_str(&version).ok();
}
None
}
fn find_executable(version: RequestedVersion, args: &[String]) -> crate::Result<PathBuf> {
let mut requested_version = version;
let mut chosen_path: Option<PathBuf> = None;
if requested_version == RequestedVersion::Any {
if let Some(venv_path) = venv_executable() {
chosen_path = Some(venv_path);
} else if !args.is_empty() {
let possible_file = &args[0];
log::info!("Checking {:?} for a shebang", possible_file);
if let Ok(mut open_file) = File::open(possible_file) {
if let Some(shebang_version) = parse_python_shebang(&mut open_file) {
requested_version = shebang_version;
}
}
}
}
if chosen_path.is_none() {
if let Some(env_var) = requested_version.env_var() {
log::info!("Checking the {} environment variable", env_var);
if let Ok(env_var_value) = env::var(&env_var) {
if !env_var_value.is_empty() {
log::debug!("{} = '{}'", env_var, env_var_value);
let env_requested_version = RequestedVersion::from_str(&env_var_value)?;
requested_version = env_requested_version;
}
} else {
log::info!("{} not set", env_var);
};
}
if let Some(executable_path) = crate::find_executable(requested_version) {
chosen_path = Some(executable_path);
}
}
chosen_path.ok_or(crate::Error::NoExecutableFound(requested_version))
}
#[cfg(test)]
mod tests {
use test_case::test_case;
use super::*;
#[test_case(&["py".to_string(), "--help".to_string(), "--list".to_string()] => Err(crate::Error::IllegalArgument(PathBuf::from("py"), "--help".to_string())))]
#[test_case(&["py".to_string(), "--list".to_string(), "--help".to_string()] => Err(crate::Error::IllegalArgument(PathBuf::from("py"), "--list".to_string())))]
fn from_main_illegal_argument_tests(argv: &[String]) -> crate::Result<Action> {
Action::from_main(argv)
}
#[test_case("-S" => None ; "unrecognized short flag is None")]
#[test_case("--something" => None ; "unrecognized long flag is None")]
#[test_case("-3" => Some(RequestedVersion::MajorOnly(3)) ; "major version")]
#[test_case("-3.6" => Some(RequestedVersion::Exact(3, 6)) ; "Exact/major.minor")]
#[test_case("-42.13" => Some(RequestedVersion::Exact(42, 13)) ; "double-digit major & minor versions")]
#[test_case("-3.6.4" => None ; "version flag with micro version is None")]
fn version_from_flag_tests(flag: &str) -> Option<RequestedVersion> {
version_from_flag(flag)
}
#[test]
fn test_help_message() {
let launcher_path = "/some/path/to/launcher";
let python_path = "/a/path/to/python";
let help = help_message(&PathBuf::from(launcher_path), &PathBuf::from(python_path));
assert!(help.contains(env!("CARGO_PKG_VERSION")));
assert!(help.contains(launcher_path));
assert!(help.contains(python_path));
}
#[test]
fn test_list_executables() {
let mut executables: HashMap<ExactVersion, PathBuf> = HashMap::new();
assert_eq!(
list_executables(&executables),
Err(crate::Error::NoExecutableFound(RequestedVersion::Any))
);
let python27_path = "/path/to/2/7/python";
executables.insert(
ExactVersion { major: 2, minor: 7 },
PathBuf::from(python27_path),
);
let python36_path = "/path/to/3/6/python";
executables.insert(
ExactVersion { major: 3, minor: 6 },
PathBuf::from(python36_path),
);
let python37_path = "/path/to/3/7/python";
executables.insert(
ExactVersion { major: 3, minor: 7 },
PathBuf::from(python37_path),
);
let executables_list = list_executables(&executables).unwrap();
assert!(executables_list.contains("2.7"));
assert!(executables_list.contains(python27_path));
assert!(executables_list.contains("3.6"));
assert!(executables_list.contains(python36_path));
assert!(executables_list.contains("3.7"));
assert!(executables_list.contains(python37_path));
assert!(executables_list.find("3.7").unwrap() < executables_list.find("3.6").unwrap());
assert!(executables_list.find("3.6").unwrap() < executables_list.find("2.7").unwrap());
assert!(
executables_list.find("3.6").unwrap() < executables_list.find(python36_path).unwrap()
);
assert!(
executables_list.find("3.7").unwrap() < executables_list.find(python36_path).unwrap()
);
}
#[test]
fn test_venv_executable_path() {
let venv_root = "/path/to/venv";
assert_eq!(
venv_executable_path(&venv_root),
PathBuf::from("/path/to/venv/bin/python")
);
}
#[test_case("/usr/bin/python" => None ; "missing shebang comment")]
#[test_case("# /usr/bin/python" => None ; "missing exclamation point")]
#[test_case("! /usr/bin/python" => None ; "missing octothorpe")]
#[test_case("#! /bin/sh" => None ; "non-Python shebang")]
#[test_case("#! /usr/bin/env python" => Some(RequestedVersion::Any) ; "typical 'env python'")]
#[test_case("#! /usr/bin/python" => Some(RequestedVersion::Any) ; "typical 'python'")]
#[test_case("#! /usr/local/bin/python" => Some(RequestedVersion::Any) ; "/usr/local")]
#[test_case("#! python" => Some(RequestedVersion::Any) ; "bare 'python'")]
#[test_case("#! /usr/bin/env python3.7" => Some(RequestedVersion::Exact(3, 7)) ; "typical 'env python' with minor version")]
#[test_case("#! /usr/bin/python3.7" => Some(RequestedVersion::Exact(3, 7)) ; "typical 'python' with minor version")]
#[test_case("#! python3.7" => Some(RequestedVersion::Exact(3, 7)) ; "bare 'python' with minor version")]
#[test_case("#!/usr/bin/python" => Some(RequestedVersion::Any) ; "no space between shebang and path")]
fn parse_python_shebang_tests(shebang: &str) -> Option<RequestedVersion> {
parse_python_shebang(&mut shebang.as_bytes())
}
#[test_case(&[0x23, 0x21, 0xc0, 0xaf] => None ; "invalid UTF-8")]
fn parse_python_sheban_include_invalid_bytes_tests(
mut shebang: &[u8],
) -> Option<RequestedVersion> {
parse_python_shebang(&mut shebang)
}
}