2019-08-11 21:51:13 +00:00
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
2019-04-25 15:06:18 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
|
|
|
use crate::configs::python::PythonConfig;
|
2019-05-01 20:34:24 +00:00
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module with the current Python version
|
2019-04-25 15:06:18 +00:00
|
|
|
///
|
|
|
|
/// Will display the Python version if any of the following criteria are met:
|
|
|
|
/// - Current directory contains a `.python-version` file
|
|
|
|
/// - Current directory contains a `requirements.txt` file
|
|
|
|
/// - Current directory contains a `pyproject.toml` file
|
2019-07-19 20:18:52 +00:00
|
|
|
/// - Current directory contains a file with the `.py` extension
|
2019-08-21 22:54:22 +00:00
|
|
|
/// - Current directory contains a `Pipfile` file
|
2019-09-15 16:21:40 +00:00
|
|
|
/// - Current directory contains a `tox.ini` file
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-05-12 17:37:23 +00:00
|
|
|
let is_py_project = context
|
2019-09-14 14:23:53 +00:00
|
|
|
.try_begin_scan()?
|
2019-08-21 22:54:22 +00:00
|
|
|
.set_files(&[
|
|
|
|
"requirements.txt",
|
|
|
|
".python-version",
|
|
|
|
"pyproject.toml",
|
|
|
|
"Pipfile",
|
2019-09-15 16:21:40 +00:00
|
|
|
"tox.ini",
|
2019-08-21 22:54:22 +00:00
|
|
|
])
|
2019-05-12 17:37:23 +00:00
|
|
|
.set_extensions(&["py"])
|
2019-09-14 14:23:53 +00:00
|
|
|
.is_match();
|
2019-05-12 17:37:23 +00:00
|
|
|
|
2019-11-02 11:10:21 +00:00
|
|
|
let is_venv = env::var("VIRTUAL_ENV").ok().is_some();
|
|
|
|
|
|
|
|
if !is_py_project && !is_venv {
|
2019-04-25 15:06:18 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("python");
|
2019-10-19 01:51:38 +00:00
|
|
|
let config: PythonConfig = PythonConfig::try_load(module.config);
|
2019-04-25 15:06:18 +00:00
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
module.set_style(config.style);
|
|
|
|
module.create_segment("symbol", &config.symbol);
|
2019-04-25 15:06:18 +00:00
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
if config.pyenv_version_name {
|
|
|
|
let python_version = get_pyenv_version()?;
|
|
|
|
module.create_segment("pyenv_prefix", &config.pyenv_prefix);
|
|
|
|
module.create_segment("version", &SegmentConfig::new(&python_version.trim()));
|
2019-08-13 01:12:55 +00:00
|
|
|
} else {
|
2019-10-19 01:51:38 +00:00
|
|
|
let python_version = get_python_version()?;
|
2019-08-13 01:12:55 +00:00
|
|
|
let formatted_version = format_python_version(&python_version);
|
2019-10-19 01:51:38 +00:00
|
|
|
module.create_segment("version", &SegmentConfig::new(&formatted_version));
|
2019-08-13 01:12:55 +00:00
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
if let Some(virtual_env) = get_python_virtual_env() {
|
|
|
|
module.create_segment(
|
|
|
|
"virtualenv",
|
|
|
|
&SegmentConfig::new(&format!(" ({})", virtual_env)),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
2019-08-13 01:12:55 +00:00
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
Some(module)
|
2019-04-25 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 01:12:55 +00:00
|
|
|
fn get_pyenv_version() -> Option<String> {
|
|
|
|
Command::new("pyenv")
|
|
|
|
.arg("version-name")
|
|
|
|
.output()
|
|
|
|
.ok()
|
|
|
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
|
|
|
}
|
|
|
|
|
2019-04-25 15:06:18 +00:00
|
|
|
fn get_python_version() -> Option<String> {
|
|
|
|
match Command::new("python").arg("--version").output() {
|
2019-05-27 06:28:14 +00:00
|
|
|
Ok(output) => {
|
2019-10-20 08:42:27 +00:00
|
|
|
if !output.status.success() {
|
|
|
|
log::warn!(
|
|
|
|
"Non-Zero exit code '{}' when executing `python --version`",
|
|
|
|
output.status
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
2019-05-27 06:28:14 +00:00
|
|
|
// We have to check both stdout and stderr since for Python versions
|
|
|
|
// < 3.4, Python reports to stderr and for Python version >= 3.5,
|
|
|
|
// Python reports to stdout
|
|
|
|
if output.stdout.is_empty() {
|
|
|
|
let stderr_string = String::from_utf8(output.stderr).unwrap();
|
|
|
|
Some(stderr_string)
|
|
|
|
} else {
|
|
|
|
let stdout_string = String::from_utf8(output.stdout).unwrap();
|
|
|
|
Some(stdout_string)
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 15:06:18 +00:00
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
fn format_python_version(python_stdout: &str) -> String {
|
2019-04-25 15:06:18 +00:00
|
|
|
format!("v{}", python_stdout.trim_start_matches("Python ").trim())
|
|
|
|
}
|
|
|
|
|
2019-08-11 21:51:13 +00:00
|
|
|
fn get_python_virtual_env() -> Option<String> {
|
|
|
|
env::var("VIRTUAL_ENV").ok().and_then(|venv| {
|
|
|
|
Path::new(&venv)
|
|
|
|
.file_name()
|
|
|
|
.map(|filename| String::from(filename.to_str().unwrap_or("")))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-25 15:06:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_python_version() {
|
2019-07-31 23:48:51 +00:00
|
|
|
let input = "Python 3.7.2";
|
2019-04-25 15:06:18 +00:00
|
|
|
assert_eq!(format_python_version(input), "v3.7.2");
|
|
|
|
}
|
|
|
|
}
|