-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathpyvenv_cfg.rs
More file actions
170 lines (155 loc) · 5.09 KB
/
pyvenv_cfg.rs
File metadata and controls
170 lines (155 loc) · 5.09 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use lazy_static::lazy_static;
use regex::Regex;
use std::{
fs,
path::{Path, PathBuf},
};
lazy_static! {
static ref VERSION: Regex = Regex::new(r"^version\s*=\s*(\d+\.\d+\.\d+)$")
.expect("error parsing Version regex for pyenv_cfg");
static ref VERSION_INFO: Regex = Regex::new(r"^version_info\s*=\s*(\d+\.\d+\.\d+.*)$")
.expect("error parsing Version_info regex for pyenv_cfg");
}
const PYVENV_CONFIG_FILE: &str = "pyvenv.cfg";
#[derive(Debug)]
pub struct PyVenvCfg {
pub version: String,
pub version_major: u64,
pub version_minor: u64,
pub prompt: Option<String>,
}
impl PyVenvCfg {
fn new(
version: String,
version_major: u64,
version_minor: u64,
prompt: Option<String>,
) -> Self {
Self {
version,
version_major,
version_minor,
prompt,
}
}
pub fn find(path: &Path) -> Option<Self> {
if let Some(ref file) = find(path) {
parse(file)
} else {
None
}
}
}
fn find(path: &Path) -> Option<PathBuf> {
// env
// |__ pyvenv.cfg <--- check if this file exists
// |__ bin or Scripts
// |__ python <--- interpreterPath
// Check if the pyvenv.cfg file is in the current directory.
// Possible the passed value is the `env`` directory.
let cfg = path.join(PYVENV_CONFIG_FILE);
if cfg.exists() {
return Some(cfg);
}
let bin = if cfg!(windows) { "Scripts" } else { "bin" };
if path.ends_with(bin) {
let cfg = path.parent()?.join(PYVENV_CONFIG_FILE);
if cfg.exists() {
return Some(cfg);
}
}
// let cfg = path.parent()?.join(PYVENV_CONFIG_FILE);
// println!("{:?}", cfg);
// if fs::metadata(&cfg).is_ok() {
// return Some(cfg);
// }
// // Check if the pyvenv.cfg file is in the parent directory.
// // Possible the passed value is the `bin` directory.
// let cfg = path.parent()?.parent()?.join(PYVENV_CONFIG_FILE);
// if fs::metadata(&cfg).is_ok() {
// return Some(cfg);
// }
None
}
fn parse(file: &Path) -> Option<PyVenvCfg> {
let contents = fs::read_to_string(file).ok()?;
let mut version: Option<String> = None;
let mut version_major: Option<u64> = None;
let mut version_minor: Option<u64> = None;
let mut prompt: Option<String> = None;
for line in contents.lines() {
if version.is_none() {
if let Some((ver, major, minor)) = parse_version(line, &VERSION) {
version = Some(ver);
version_major = Some(major);
version_minor = Some(minor);
continue;
}
if let Some((ver, major, minor)) = parse_version(line, &VERSION_INFO) {
version = Some(ver);
version_major = Some(major);
version_minor = Some(minor);
continue;
}
}
if prompt.is_none() {
if let Some(p) = parse_prompt(line) {
prompt = Some(p);
}
}
if version.is_some() && prompt.is_some() {
break;
}
}
match (version, version_major, version_minor) {
(Some(ver), Some(major), Some(minor)) => Some(PyVenvCfg::new(ver, major, minor, prompt)),
_ => None,
}
}
fn parse_version(line: &str, regex: &Regex) -> Option<(String, u64, u64)> {
if let Some(captures) = regex.captures(line) {
if let Some(value) = captures.get(1) {
let version = value.as_str();
let parts: Vec<&str> = version.split('.').collect();
if parts.len() >= 2 {
let version_major = parts[0]
.parse()
.expect("python major version to be an integer");
let version_minor = parts[1]
.parse()
.expect("python minor version to be an integer");
return Some((version.to_string(), version_major, version_minor));
}
}
}
None
}
fn parse_prompt(line: &str) -> Option<String> {
let trimmed = line.trim();
if trimmed.starts_with("prompt") {
if let Some(eq_idx) = trimmed.find('=') {
// let value = trimmed[eq_idx + 1..].trim();
let mut name = trimmed[eq_idx + 1..].trim().to_string();
// Strip any leading or trailing single or double quotes
if name.starts_with('"') {
name = name.trim_start_matches('"').to_string();
}
if name.ends_with('"') {
name = name.trim_end_matches('"').to_string();
}
// Strip any leading or trailing single or double quotes
if name.starts_with('\'') {
name = name.trim_start_matches('\'').to_string();
}
if name.ends_with('\'') {
name = name.trim_end_matches('\'').to_string();
}
if !name.is_empty() {
return Some(name);
}
}
}
None
}