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
use failure::{Error, Fail, ResultExt};
use serde_json;
use std::collections::HashMap;
use std::fmt;
use std::io;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
use target_info::Target;
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct PythonInterpreter {
pub major: usize,
pub minor: usize,
pub has_u: bool,
pub executable: PathBuf,
}
impl PythonInterpreter {
pub fn get_tag(&self) -> String {
let platform = match Target::os() {
"linux" => "manylinux1_x86_64",
"macos" => {
"macosx_10_6_intel.\
macosx_10_9_intel.\
macosx_10_9_x86_64.\
macosx_10_10_intel.\
macosx_10_10_x86_64"
}
"windows" => if Target::pointer_width() == "64" {
"win_amd64"
} else {
"win32"
},
_ => panic!("This platform is not supported"),
};
format!(
"cp{major}{minor}-cp{major}{minor}{abiflags}-{platform}",
major = self.major,
minor = self.minor,
abiflags = if self.has_u { "mu" } else { "m" },
platform = platform
)
}
pub fn find_all(python_versions: &[String]) -> Result<Vec<PythonInterpreter>, Error> {
let mut available_versions = Vec::new();
let import = "import json, sysconfig;";
let format = "value = {k:str(v) for k, v in sysconfig.get_config_vars().items()};";
let dump = r#"print(json.dumps(value))"#;
for executable in python_versions {
let output = Command::new(&executable)
.args(&["-c", &format!(r#"{}{}{}"#, import, format, dump)])
.stderr(Stdio::inherit())
.output();
let err_msg = format!("Trying to get the sysconfig from {} failed", executable);
let output = match output {
Ok(output) => {
if output.status.success() {
output
} else {
bail!(err_msg);
}
}
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
continue;
} else {
bail!(err.context(err_msg));
}
}
};
let values: HashMap<String, String> =
serde_json::from_slice(&output.stdout).context(err_msg)?;
let version = values
.get("VERSION")
.ok_or_else(|| format_err!("sysconfig does not define a version"))?;
let split: Vec<_> = version.split('.').collect();
let major = split[0].parse::<usize>()?;
let minor = split[1].parse::<usize>()?;
if major == 3 && minor < 5 {
bail!("Only python 2.7 and python 3.5 are supported");
}
let has_u;
if let Some(abiflags) = values.get("ABIFLAGS") {
match abiflags.as_ref() {
"m" => has_u = false,
"mu" => has_u = true,
_ => bail!(
r#"Only the "m" and "mu" abiflags are suppoted, not "{}""#,
abiflags
),
};
} else if let Some(unicode_size) = values.get("Py_UNICODE_SIZE") {
match unicode_size.as_ref() {
"2" => has_u = false,
"4" => has_u = true,
_ => bail!(
"Invalid unicode size: {} (Expected either 2 or 4)",
unicode_size
),
};
} else {
bail!(
"Expected the sysconfig module of {} to define a value for abiflags",
executable
);
}
available_versions.push(PythonInterpreter {
major,
minor,
has_u,
executable: PathBuf::from(executable),
});
}
Ok(available_versions)
}
}
impl fmt::Display for PythonInterpreter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Python {}.{}{} at {}",
self.major,
self.minor,
if self.has_u { "mu" } else { "m" },
self.executable.display()
)
}
}