Skip to content

Commit

Permalink
Get Python version from filename or run --version
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Apr 9, 2023
1 parent 01f513d commit 161bf42
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/huak/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,21 +1644,27 @@ pub fn active_conda_env_path() -> Option<PathBuf> {
fn parse_python_interpreter_version<T: AsRef<Path>>(
path: T,
) -> HuakResult<Option<Version>> {
let version = match path
let version = path
.as_ref()
.file_name()
.and_then(|raw_file_name| raw_file_name.to_str())
{
Some(file_name) => {
.map(|file_name| {
version_from_python_interpreter_file_name(file_name).ok()
}
None => {
let mut cmd = Command::new(path.as_ref());
cmd.args(["--version"]);
let output = cmd.output()?;
Version::from_str(&sys::parse_command_output(output)?).ok()
}
};
})
.unwrap_or(parse_python_version_from_command(path)?);

Ok(version)
}

/// Get the version of a Python `Interpreter` using a `{path} --version` command.
fn parse_python_version_from_command<T: AsRef<Path>>(
path: T,
) -> HuakResult<Option<Version>> {
let mut cmd = Command::new(path.as_ref());
cmd.args(["--version"]);
let output = cmd.output()?;
let version = Version::from_str(&sys::parse_command_output(output)?).ok();

Ok(version)
}

Expand Down

0 comments on commit 161bf42

Please sign in to comment.