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
use std::error;
use std::fmt;
use std::str::FromStr;

use rustpython_parser::ast::{Expression, ExpressionType, Number, StatementType, StringGroup};
use rustpython_parser::error::ParseError;
use rustpython_parser::parser;

/// Represents an error during parsing
#[derive(Debug)]
pub enum Error {
    /// Python source code syntax error
    SyntaxError(ParseError),
    /// missing build_time_vars variable
    MissingBuildTimeVars,
    /// missing required key in configuration
    KeyError(&'static str),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::SyntaxError(err) => err.fmt(f),
            Error::MissingBuildTimeVars => write!(f, "missing build_time_vars variable"),
            Error::KeyError(key) => write!(f, "missing required key {}", key),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::SyntaxError(err) => Some(err),
            Error::MissingBuildTimeVars => None,
            Error::KeyError(_) => None,
        }
    }
}

impl From<ParseError> for Error {
    fn from(err: ParseError) -> Self {
        Self::SyntaxError(err)
    }
}

/// Python configuration information
#[derive(Debug, Clone)]
pub struct PythonConfig {
    sys_config_data: SysConfigData,
}

impl PythonConfig {
    /// Parse from `_sysconfigdata.py` content
    pub fn parse(src: &str) -> Result<Self, Error> {
        let sys_config_data = SysConfigData::parse(src)?;
        Ok(Self { sys_config_data })
    }

    /// Returns Python version
    pub fn version(&self) -> &str {
        &self.sys_config_data.build_time_vars.version
    }

    /// Returns Python major version
    pub fn version_major(&self) -> u32 {
        let version = self.version();
        version
            .split('.')
            .next()
            .and_then(|x| x.parse::<u32>().ok())
            .unwrap()
    }

    /// Returns Python minor version
    pub fn version_minor(&self) -> u32 {
        let version = self.version();
        version
            .split('.')
            .nth(1)
            .and_then(|x| x.parse::<u32>().ok())
            .unwrap()
    }

    /// Returns the installation prefix of the Python interpreter
    pub fn prefix(&self) -> &str {
        &self.sys_config_data.build_time_vars.prefix
    }

    /// Returns the executable path prefix for the Python interpreter
    pub fn exec_prefix(&self) -> &str {
        &self.sys_config_data.build_time_vars.exec_prefix
    }

    /// C compilation flags
    pub fn cflags(&self) -> &str {
        &self.sys_config_data.build_time_vars.cflags
    }

    /// Returns linker flags required for linking this Python
    /// distribution. All libraries / frameworks have the appropriate `-l`
    /// or `-framework` prefixes.
    pub fn libs(&self) -> &str {
        &self.sys_config_data.build_time_vars.libs
    }

    /// Returns linker flags required for creating
    /// a shared library for this Python distribution. All libraries / frameworks
    /// have the appropriate `-L`, `-l`, or `-framework` prefixes.
    pub fn ldflags(&self) -> &str {
        &self.sys_config_data.build_time_vars.ldflags
    }

    /// Returns the file extension for this distribution's library
    pub fn ext_suffix(&self) -> &str {
        &self.sys_config_data.build_time_vars.ext_suffix
    }

    /// The ABI flags specified when building this Python distribution
    pub fn abiflags(&self) -> &str {
        &self.sys_config_data.build_time_vars.abiflags
    }

    /// The location of the distribution's `python3-config` script
    pub fn config_dir(&self) -> &str {
        &self.sys_config_data.build_time_vars.config_dir
    }

    /// Returns the C headers include directory
    pub fn include_dir(&self) -> &str {
        &self.sys_config_data.build_time_vars.include_dir
    }

    /// Returns library directory
    pub fn lib_dir(&self) -> &str {
        &self.sys_config_data.build_time_vars.lib_dir
    }

    /// Returns ld version
    pub fn ld_version(&self) -> &str {
        &self.sys_config_data.build_time_vars.ld_version
    }

    /// Returns SOABI
    pub fn soabi(&self) -> &str {
        &self.sys_config_data.build_time_vars.soabi
    }

    /// Returns shared library suffix
    pub fn shlib_suffix(&self) -> &str {
        &self.sys_config_data.build_time_vars.shlib_suffix
    }

    /// Returns whether this distribution is built with `--enable-shared`
    pub fn enable_shared(&self) -> bool {
        self.sys_config_data.build_time_vars.py_enable_shared
    }

    /// Returns whether this distribution is built with `Py_DEBUG`
    pub fn debug(&self) -> bool {
        self.sys_config_data.build_time_vars.py_debug
    }

    /// Returns whether this distribution is built with `Py_REF_DEBUG`
    pub fn ref_debug(&self) -> bool {
        self.sys_config_data.build_time_vars.py_ref_debug
    }

    /// Returns whether this distribution is built with thread
    pub fn with_thread(&self) -> bool {
        self.sys_config_data.build_time_vars.with_thread
    }

    /// Returns pointer size (size of C `void*`) of this distribution
    pub fn pointer_size(&self) -> u32 {
        self.sys_config_data.build_time_vars.size_of_void_p
    }
}

#[derive(Debug, Clone)]
struct SysConfigData {
    pub build_time_vars: BuildTimeVars,
}

#[derive(Debug, Clone, Default)]
struct BuildTimeVars {
    pub abiflags: String,
    pub count_allocs: bool,
    pub cflags: String,
    pub config_dir: String,
    pub ext_suffix: String,
    pub exec_prefix: String,
    pub include_dir: String,
    pub lib_dir: String,
    pub libs: String,
    pub ldflags: String,
    pub ld_version: String,
    pub prefix: String,
    pub py_debug: bool,
    pub py_ref_debug: bool,
    pub py_trace_refs: bool,
    pub py_enable_shared: bool,
    pub soabi: String,
    pub shlib_suffix: String,
    pub size_of_void_p: u32,
    pub with_thread: bool,
    pub version: String,
}

impl SysConfigData {
    pub fn parse(src: &str) -> Result<Self, Error> {
        let program = parser::parse_program(src)?;
        let mut vars = BuildTimeVars::default();
        for stmt in program.statements {
            if let StatementType::Assign { targets, value } = stmt.node {
                let var_name = targets.get(0).ok_or(Error::MissingBuildTimeVars)?;
                match &var_name.node {
                    ExpressionType::Identifier { name } if name == "build_time_vars" => {}
                    _ => continue,
                }
                if let ExpressionType::Dict { elements } = value.node {
                    for (key, value) in elements {
                        if let Some(key) = key.and_then(|key| get_string(&key)) {
                            match key.as_str() {
                                "ABIFLAGS" => {
                                    vars.abiflags = get_string(&value).unwrap_or_default()
                                }
                                "COUNT_ALLOCS" => vars.count_allocs = get_bool(&value),
                                "CFLAGS" => vars.cflags = get_string(&value).unwrap_or_default(),
                                "LIBPL" => vars.config_dir = get_string(&value).unwrap_or_default(),
                                "EXT_SUFFIX" => {
                                    vars.ext_suffix = get_string(&value).unwrap_or_default()
                                }
                                "exec_prefix" => {
                                    vars.exec_prefix = get_string(&value).unwrap_or_default()
                                }
                                "INCLUDEDIR" => {
                                    vars.include_dir = get_string(&value).unwrap_or_default()
                                }
                                "LIBDIR" => vars.lib_dir = get_string(&value).unwrap_or_default(),
                                "LIBS" => vars.libs = get_string(&value).unwrap_or_default(),
                                "LDFLAGS" => vars.ldflags = get_string(&value).unwrap_or_default(),
                                "LDVERSION" => {
                                    vars.ld_version = get_string(&value).unwrap_or_default()
                                }
                                "prefix" => vars.prefix = get_string(&value).unwrap_or_default(),
                                "Py_DEBUG" => vars.py_debug = get_bool(&value),
                                "Py_ENABLE_SHARED" => vars.py_enable_shared = get_bool(&value),
                                "Py_REF_DEBUG" => vars.py_ref_debug = get_bool(&value),
                                "Py_TRACE_REFS" => vars.py_trace_refs = get_bool(&value),
                                "SOABI" => vars.soabi = get_string(&value).unwrap_or_default(),
                                "SHLIB_SUFFIX" => {
                                    vars.shlib_suffix = get_string(&value).unwrap_or_default()
                                }
                                "SIZEOF_VOID_P" => {
                                    vars.size_of_void_p = get_number(&value)
                                        .ok_or(Error::KeyError("SIZEOF_VOID_P"))?
                                        as u32
                                }
                                "VERSION" => {
                                    vars.version =
                                        get_string(&value).ok_or(Error::KeyError("VERSION"))?
                                }
                                _ => continue,
                            }
                        } else {
                            continue;
                        }
                    }
                }
            }
        }
        if vars.version.is_empty() {
            // no build_time_vars found
            return Err(Error::MissingBuildTimeVars);
        }
        Ok(SysConfigData {
            build_time_vars: vars,
        })
    }
}

fn get_string(expr: &Expression) -> Option<String> {
    match &expr.node {
        ExpressionType::String { value: sg } => match sg {
            StringGroup::Constant { value } => Some(value.to_string()),
            StringGroup::Joined { values } => {
                let mut s = String::new();
                for value in values {
                    if let StringGroup::Constant { value: cs } = value {
                        s.push_str(&cs)
                    }
                }
                Some(s)
            }
            _ => None,
        },
        _ => None,
    }
}

fn get_number(expr: &Expression) -> Option<i32> {
    use num_traits::cast::ToPrimitive;

    match &expr.node {
        ExpressionType::Number { value } => {
            if let Number::Integer { value } = value {
                value.to_i32()
            } else {
                None
            }
        }
        _ => None,
    }
}

fn get_bool(expr: &Expression) -> bool {
    get_number(expr).map(|x| x == 1).unwrap_or(false)
}

impl FromStr for PythonConfig {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

#[cfg(test)]
mod tests {
    use super::{Error, PythonConfig};
    use std::fs;

    #[test]
    fn read_python_sysconfig_data() {
        let src =
            fs::read_to_string("tests/fixtures/cpython38_sysconfigdata__darwin_darwin.py").unwrap();
        let config = PythonConfig::parse(&src).unwrap();
        assert_eq!(config.abiflags(), "");
        assert_eq!(config.soabi(), "cpython-38-darwin");
        assert_eq!(config.version(), "3.8");
        assert_eq!(config.version_major(), 3);
        assert_eq!(config.version_minor(), 8);

        // Test FromStr impl
        let config: PythonConfig = src.parse().unwrap();
        assert_eq!(config.abiflags(), "");
    }

    #[test]
    fn read_invalid_python_sysconfig_data() {
        let config = PythonConfig::parse("i++").unwrap_err();
        assert!(matches!(config, Error::SyntaxError(_)));

        let config = PythonConfig::parse("").unwrap_err();
        assert!(matches!(config, Error::MissingBuildTimeVars));

        let config = PythonConfig::parse("i = 0").unwrap_err();
        assert!(matches!(config, Error::MissingBuildTimeVars));

        let config =
            PythonConfig::parse("build_time_vars = {'VERSION': '3.8', 'SIZEOF_VOID_P': ''}")
                .unwrap_err();
        assert!(matches!(config, Error::KeyError("SIZEOF_VOID_P")));
    }
}