Skip to content

Look for Python (PythonCore) in windows registry #23414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions native_locator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ pub mod pipenv;
pub mod virtualenv;
pub mod venv;
pub mod locator;
pub mod windows_registry;
pub mod windows_store;
11 changes: 8 additions & 3 deletions native_locator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ mod utils;
mod venv;
mod virtualenv;
mod virtualenvwrapper;
mod windows_python;
mod windows_store;
mod windows_registry;

fn main() {
let environment = EnvironmentApi {};
Expand All @@ -44,7 +45,9 @@ fn main() {
#[cfg(unix)]
let homebrew_locator = homebrew::Homebrew::with(&environment);
#[cfg(windows)]
let windows_locator = windows_python::WindowsPython::with(&environment);
let windows_store = windows_store::WindowsStore::with(&environment);
#[cfg(windows)]
let windows_registry = windows_registry::WindowsRegistry::new();
let conda_locator = conda::Conda::with(&environment);

// Step 1: These environments take precedence over all others.
Expand All @@ -54,7 +57,9 @@ fn main() {
find_environments(&homebrew_locator, &mut dispatcher);
find_environments(&conda_locator, &mut dispatcher);
#[cfg(windows)]
find_environments(&windows_locator, &mut dispatcher);
find_environments(&windows_registry, &mut dispatcher);
#[cfg(windows)]
find_environments(&windows_store, &mut dispatcher);

// Step 2: Search in some global locations.
for env in list_global_virtual_envs(&environment).iter() {
Expand Down
1 change: 1 addition & 0 deletions native_locator/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum PythonEnvironmentCategory {
Pyenv,
PyenvVirtualEnv,
WindowsStore,
WindowsRegistry,
Pipenv,
VirtualEnvWrapper,
Venv,
Expand Down
150 changes: 91 additions & 59 deletions native_locator/src/windows_registry.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,91 @@
// // Copyright (c) Microsoft Corporation. All rights reserved.
// // Licensed under the MIT License.
// use crate::messaging;
// use std::path::PathBuf;
// use winreg::RegKey;

// fn get_registry_pythons_from_key(
// dispatcher: &mut impl messaging::MessageDispatcher,
// hk: &RegKey,
// company: &str,
// ) -> Option<Vec<PathBuf>> {
// let python_key = hk.open_subkey("Software\\Python").ok()?;
// let company_key = python_key.open_subkey(company).ok()?;

// let mut pythons = vec![];
// for key in company_key.enum_keys().filter_map(Result::ok) {
// let version_key = company_key.open_subkey(key).ok()?;
// let install_path_key = version_key.open_subkey("InstallPath").ok()?;
// let executable: String = install_path_key.get_value("ExecutablePath").ok()?;
// let version = version_key.get_value("Version").ok()?;

// dispatcher.report_environment(messaging::PythonEnvironment::new(
// None,
// Some(PathBuf::from(executable.clone())),
// messaging::PythonEnvironmentCategory::WindowsRegistry,
// Some(version),
// None,
// None,
// None,
// None,
// ));

// pythons.push(PathBuf::from(executable));
// }

// Some(pythons)
// }

// #[cfg(windows)]
// pub fn report_and_get_registry_pythons(
// dispatcher: &mut impl messaging::MessageDispatcher,
// company: &str,
// ) -> Option<Vec<PathBuf>> {
// let hklm = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
// let hkcu = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER);

// let mut pythons = vec![];
// if let Some(hklm_pythons) = get_registry_pythons_from_key(dispatcher, &hklm, company) {
// pythons.extend(hklm_pythons);
// }
// if let Some(hkcu_pythons) = get_registry_pythons_from_key(dispatcher, &hkcu, company) {
// pythons.extend(hkcu_pythons);
// }

// Some(pythons)
// }

// // PythonCore
// // ContinuumAnalytics
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#[cfg(windows)]
use crate::locator::{Locator, LocatorResult};
#[cfg(windows)]
use crate::messaging::{PythonEnvironment, PythonEnvironmentCategory};
#[cfg(windows)]
use crate::utils::PythonEnv;
#[cfg(windows)]
use winreg::RegKey;
#[cfg(windows)]
use std::path::PathBuf;

#[cfg(windows)]
fn get_registry_pythons_from_key(hk: &RegKey, company: &str) -> Option<Vec<PythonEnvironment>> {
let python_key = hk.open_subkey("Software\\Python").ok()?;
let company_key = python_key.open_subkey(company).ok()?;

let mut pythons = vec![];
for key in company_key.enum_keys().filter_map(Result::ok) {
let version_key = company_key.open_subkey(key).ok()?;
let install_path_key = version_key.open_subkey("InstallPath").ok()?;
let executable: String = install_path_key.get_value("ExecutablePath").ok()?;
let version = version_key.get_value("Version").ok()?;

let env = PythonEnvironment::new(
None,
Some(PathBuf::from(executable.clone())),
PythonEnvironmentCategory::WindowsRegistry,
Some(version),
None,
None,
None,
Some(vec![executable.clone()]),
);

pythons.push(env);
}

Some(pythons)
}

#[cfg(windows)]
pub fn get_registry_pythons(company: &str) -> Option<Vec<PythonEnvironment>> {
let hklm = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
let hkcu = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER);

let mut pythons = vec![];
if let Some(hklm_pythons) = get_registry_pythons_from_key(&hklm, company) {
pythons.extend(hklm_pythons);
}
if let Some(hkcu_pythons) = get_registry_pythons_from_key(&hkcu, company) {
pythons.extend(hkcu_pythons);
}

Some(pythons)
}

#[cfg(windows)]
pub struct WindowsRegistry {}

#[cfg(windows)]
impl WindowsRegistry {
#[allow(dead_code)]
pub fn new() -> WindowsRegistry {
WindowsRegistry {}
}
}

#[cfg(windows)]
impl Locator for WindowsRegistry {
fn resolve(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
None
}

fn find(&self) -> Option<LocatorResult> {
let environments = get_registry_pythons("PythonCore")?;
if environments.is_empty() {
None
} else {
Some(LocatorResult {
managers: vec![],
environments,
})
}
}
}

// PythonCore
// ContinuumAnalytics
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use crate::utils::PythonEnv;
use std::path::Path;
use std::path::PathBuf;

fn is_windows_python_executable(path: &PathBuf) -> bool {
pub fn is_windows_python_executable(path: &PathBuf) -> bool {
let name = path.file_name().unwrap().to_string_lossy().to_lowercase();
// TODO: Is it safe to assume the number 3?
name.starts_with("python3.") && name.ends_with(".exe")
}

fn list_windows_store_python_executables(
environment: &dyn known::Environment,
) -> Option<Vec<PathBuf>> {
Expand All @@ -38,22 +40,18 @@ fn list_windows_store_python_executables(
Some(python_envs)
}

fn list_registry_pythons() -> Option<Vec<PathBuf>> {
None
}

pub struct WindowsPython<'a> {
pub struct WindowsStore<'a> {
pub environment: &'a dyn Environment,
}

impl WindowsPython<'_> {
impl WindowsStore<'_> {
#[allow(dead_code)]
pub fn with<'a>(environment: &'a impl Environment) -> WindowsPython {
WindowsPython { environment }
pub fn with<'a>(environment: &'a impl Environment) -> WindowsStore {
WindowsStore { environment }
}
}

impl Locator for WindowsPython<'_> {
impl Locator for WindowsStore<'_> {
fn resolve(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if is_windows_python_executable(&env.executable) {
return Some(PythonEnvironment {
Expand All @@ -80,13 +78,6 @@ impl Locator for WindowsPython<'_> {
}
});
}
if let Some(envs) = list_registry_pythons() {
envs.iter().for_each(|env| {
if let Some(env) = self.resolve(&&PythonEnv::from(env.clone())) {
environments.push(env);
}
});
}

if environments.is_empty() {
None
Expand Down
Loading