-
Notifications
You must be signed in to change notification settings - Fork 855
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use windows registry to discover python
Our current strategy of parsing the output of `py --list-paths` to get the installed python versions on windows is brittle (#6524, missing `py`, etc.) and it's slow (10ms last time i measured). Instead, we should behave spec-compliant and read the python versions from the registry following PEP 514. We're using the official rust-for-windows crates for accessing the registry. Fixes #1521 Fixes #6524
- Loading branch information
Showing
6 changed files
with
126 additions
and
99 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,114 @@ | ||
use regex::Regex; | ||
use std::io; | ||
use crate::PythonVersion; | ||
use std::path::PathBuf; | ||
use std::process::{Command, ExitStatus}; | ||
use std::sync::LazyLock; | ||
use thiserror::Error; | ||
use tracing::info_span; | ||
use std::str::FromStr; | ||
use tracing::debug; | ||
use windows_registry::{Key, Value, CURRENT_USER, LOCAL_MACHINE}; | ||
|
||
/// A Python interpreter found in the Windows registry through PEP 514. | ||
/// | ||
/// There are a lot more (optional) fields defined in PEP 514, but we only care about path and | ||
/// version here, for everything else we probe with a Python script. | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct PyListPath { | ||
pub(crate) major: u8, | ||
pub(crate) minor: u8, | ||
pub(crate) executable_path: PathBuf, | ||
pub(crate) struct RegistryPython { | ||
pub(crate) path: PathBuf, | ||
pub(crate) version: Option<PythonVersion>, | ||
} | ||
|
||
/// An error was encountered when using the `py` launcher on Windows. | ||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("{message} with {exit_code}\n--- stdout:\n{stdout}\n--- stderr:\n{stderr}\n---")] | ||
StatusCode { | ||
message: String, | ||
exit_code: ExitStatus, | ||
stdout: String, | ||
stderr: String, | ||
}, | ||
#[error("Failed to run `py --list-paths` to find Python installations")] | ||
Io(#[source] io::Error), | ||
#[error("The `py` launcher could not be found")] | ||
NotFound, | ||
/// Àdding `windows_registry::Value::into_string()`. | ||
fn value_to_string(value: Value) -> Option<String> { | ||
match value { | ||
Value::String(string) => Some(string), | ||
Value::Bytes(bytes) => String::from_utf8(bytes.to_vec()).ok(), | ||
Value::U32(_) | Value::U64(_) | Value::MultiString(_) | Value::Unknown(_) => None, | ||
} | ||
} | ||
|
||
/// ```text | ||
/// -V:3.12 C:\Users\Ferris\AppData\Local\Programs\Python\Python312\python.exe | ||
/// -V:3.8 C:\Users\Ferris\AppData\Local\Programs\Python\Python38\python.exe | ||
/// ``` | ||
static PY_LIST_PATHS: LazyLock<Regex> = LazyLock::new(|| { | ||
// Without the `R` flag, paths have trailing \r | ||
Regex::new(r"(?mR)^ -(?:V:)?(\d).(\d+)-?(?:arm)?\d*\s*\*?\s*(.*)$").unwrap() | ||
}); | ||
|
||
/// Use the `py` launcher to find installed Python versions. | ||
/// | ||
/// Calls `py --list-paths`. | ||
pub(crate) fn py_list_paths() -> Result<Vec<PyListPath>, Error> { | ||
// konstin: The command takes 8ms on my machine. | ||
let output = info_span!("py_list_paths") | ||
.in_scope(|| Command::new("py").arg("--list-paths").output()) | ||
.map_err(|err| { | ||
if err.kind() == std::io::ErrorKind::NotFound { | ||
Error::NotFound | ||
} else { | ||
Error::Io(err) | ||
/// Find all Pythons registered in the Windows registry following PEP 514. | ||
pub(crate) fn registry_pythons() -> Result<Vec<RegistryPython>, windows_result::Error> { | ||
let mut registry_pythons = Vec::new(); | ||
for root_key in [CURRENT_USER, LOCAL_MACHINE] { | ||
let Ok(key_python) = root_key.open(r"Software\Python") else { | ||
continue; | ||
}; | ||
for company in key_python.keys()? { | ||
// Reserved name according to the PEP. | ||
if company == "PyLauncher" { | ||
continue; | ||
} | ||
})?; | ||
let Ok(company_key) = key_python.open(&company) else { | ||
// Ignore invalid entries | ||
continue; | ||
}; | ||
for tag in company_key.keys()? { | ||
let tag_key = company_key.open(&tag)?; | ||
|
||
// `py` sometimes prints "Installed Pythons found by py Launcher for Windows" to stderr which we ignore. | ||
if !output.status.success() { | ||
return Err(Error::StatusCode { | ||
message: format!( | ||
"Running `py --list-paths` failed with status {}", | ||
output.status | ||
), | ||
exit_code: output.status, | ||
stdout: String::from_utf8_lossy(&output.stdout).trim().to_string(), | ||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(), | ||
}); | ||
if let Some(registry_python) = read_registry_entry(&company, &tag, &tag_key) { | ||
registry_pythons.push(registry_python); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Find the first python of the version we want in the list | ||
let stdout = String::from_utf8(output.stdout).map_err(|err| Error::StatusCode { | ||
message: format!("The stdout of `py --list-paths` isn't UTF-8 encoded: {err}"), | ||
exit_code: output.status, | ||
stdout: String::from_utf8_lossy(err.as_bytes()).trim().to_string(), | ||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(), | ||
})?; | ||
// The registry has no natural ordering, so we're processing the latest version first. | ||
registry_pythons.sort_by(|a, b| { | ||
// Highest version first (reverse), but missing versions at the bottom (regular order). | ||
if let (Some(version_a), Some(version_b)) = (&a.version, &b.version) { | ||
version_a | ||
.cmp(&version_b) | ||
.reverse() | ||
.then(a.path.cmp(&b.path)) | ||
} else { | ||
a.version | ||
.as_ref() | ||
.map(|version| &***version) | ||
.cmp(&b.version.as_ref().map(|version| &***version)) | ||
.then(a.path.cmp(&b.path)) | ||
} | ||
}); | ||
|
||
Ok(PY_LIST_PATHS | ||
.captures_iter(&stdout) | ||
.filter_map(|captures| { | ||
let (_, [major, minor, path]) = captures.extract(); | ||
if let (Some(major), Some(minor)) = (major.parse::<u8>().ok(), minor.parse::<u8>().ok()) | ||
{ | ||
Some(PyListPath { | ||
major, | ||
minor, | ||
executable_path: PathBuf::from(path), | ||
}) | ||
} else { | ||
Ok(registry_pythons) | ||
} | ||
|
||
fn read_registry_entry(company: &str, tag: &str, tag_key: &Key) -> Option<RegistryPython> { | ||
// `ExecutablePath` is mandatory for executable Pythons. | ||
let Some(executable_path) = tag_key | ||
.open("InstallPath") | ||
.and_then(|install_path| install_path.get_value("ExecutablePath")) | ||
.ok() | ||
.and_then(value_to_string) | ||
else { | ||
debug!( | ||
r"Registry Python is not executable: `Software\Python\{}\{}", | ||
company, tag | ||
); | ||
return None; | ||
}; | ||
debug!( | ||
"Found registry Python {} {}: `{}`", | ||
company, tag, executable_path | ||
); | ||
|
||
// `SysVersion` is optional. | ||
let version = tag_key | ||
.get_value("SysVersion") | ||
.ok() | ||
.and_then(|value| match value { | ||
Value::String(s) => Some(s), | ||
_ => None, | ||
}) | ||
.and_then(|s| match PythonVersion::from_str(&s) { | ||
Ok(version) => Some(version), | ||
Err(err) => { | ||
debug!( | ||
"Invalid registry Python version at {}: {}", | ||
executable_path, err | ||
); | ||
None | ||
} | ||
}) | ||
.collect()) | ||
}); | ||
|
||
Some(RegistryPython { | ||
path: PathBuf::from(executable_path), | ||
version, | ||
}) | ||
} |