|
1 |
| -// // Copyright (c) Microsoft Corporation. All rights reserved. |
2 |
| -// // Licensed under the MIT License. |
3 |
| -// use crate::messaging; |
4 |
| -// use std::path::PathBuf; |
5 |
| -// use winreg::RegKey; |
6 |
| - |
7 |
| -// fn get_registry_pythons_from_key( |
8 |
| -// dispatcher: &mut impl messaging::MessageDispatcher, |
9 |
| -// hk: &RegKey, |
10 |
| -// company: &str, |
11 |
| -// ) -> Option<Vec<PathBuf>> { |
12 |
| -// let python_key = hk.open_subkey("Software\\Python").ok()?; |
13 |
| -// let company_key = python_key.open_subkey(company).ok()?; |
14 |
| - |
15 |
| -// let mut pythons = vec![]; |
16 |
| -// for key in company_key.enum_keys().filter_map(Result::ok) { |
17 |
| -// let version_key = company_key.open_subkey(key).ok()?; |
18 |
| -// let install_path_key = version_key.open_subkey("InstallPath").ok()?; |
19 |
| -// let executable: String = install_path_key.get_value("ExecutablePath").ok()?; |
20 |
| -// let version = version_key.get_value("Version").ok()?; |
21 |
| - |
22 |
| -// dispatcher.report_environment(messaging::PythonEnvironment::new( |
23 |
| -// None, |
24 |
| -// Some(PathBuf::from(executable.clone())), |
25 |
| -// messaging::PythonEnvironmentCategory::WindowsRegistry, |
26 |
| -// Some(version), |
27 |
| -// None, |
28 |
| -// None, |
29 |
| -// None, |
30 |
| -// None, |
31 |
| -// )); |
32 |
| - |
33 |
| -// pythons.push(PathBuf::from(executable)); |
34 |
| -// } |
35 |
| - |
36 |
| -// Some(pythons) |
37 |
| -// } |
38 |
| - |
39 |
| -// #[cfg(windows)] |
40 |
| -// pub fn report_and_get_registry_pythons( |
41 |
| -// dispatcher: &mut impl messaging::MessageDispatcher, |
42 |
| -// company: &str, |
43 |
| -// ) -> Option<Vec<PathBuf>> { |
44 |
| -// let hklm = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); |
45 |
| -// let hkcu = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER); |
46 |
| - |
47 |
| -// let mut pythons = vec![]; |
48 |
| -// if let Some(hklm_pythons) = get_registry_pythons_from_key(dispatcher, &hklm, company) { |
49 |
| -// pythons.extend(hklm_pythons); |
50 |
| -// } |
51 |
| -// if let Some(hkcu_pythons) = get_registry_pythons_from_key(dispatcher, &hkcu, company) { |
52 |
| -// pythons.extend(hkcu_pythons); |
53 |
| -// } |
54 |
| - |
55 |
| -// Some(pythons) |
56 |
| -// } |
57 |
| - |
58 |
| -// // PythonCore |
59 |
| -// // ContinuumAnalytics |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#[cfg(windows)] |
| 5 | +use crate::locator::{Locator, LocatorResult}; |
| 6 | +#[cfg(windows)] |
| 7 | +use crate::messaging::{PythonEnvironment, PythonEnvironmentCategory}; |
| 8 | +#[cfg(windows)] |
| 9 | +use crate::utils::PythonEnv; |
| 10 | +#[cfg(windows)] |
| 11 | +use winreg::RegKey; |
| 12 | +#[cfg(windows)] |
| 13 | +use std::path::PathBuf; |
| 14 | + |
| 15 | +#[cfg(windows)] |
| 16 | +fn get_registry_pythons_from_key(hk: &RegKey, company: &str) -> Option<Vec<PythonEnvironment>> { |
| 17 | + let python_key = hk.open_subkey("Software\\Python").ok()?; |
| 18 | + let company_key = python_key.open_subkey(company).ok()?; |
| 19 | + |
| 20 | + let mut pythons = vec![]; |
| 21 | + for key in company_key.enum_keys().filter_map(Result::ok) { |
| 22 | + let version_key = company_key.open_subkey(key).ok()?; |
| 23 | + let install_path_key = version_key.open_subkey("InstallPath").ok()?; |
| 24 | + let executable: String = install_path_key.get_value("ExecutablePath").ok()?; |
| 25 | + let version = version_key.get_value("Version").ok()?; |
| 26 | + |
| 27 | + let env = PythonEnvironment::new( |
| 28 | + None, |
| 29 | + Some(PathBuf::from(executable.clone())), |
| 30 | + PythonEnvironmentCategory::WindowsRegistry, |
| 31 | + Some(version), |
| 32 | + None, |
| 33 | + None, |
| 34 | + None, |
| 35 | + Some(vec![executable.clone()]), |
| 36 | + ); |
| 37 | + |
| 38 | + pythons.push(env); |
| 39 | + } |
| 40 | + |
| 41 | + Some(pythons) |
| 42 | +} |
| 43 | + |
| 44 | +#[cfg(windows)] |
| 45 | +pub fn get_registry_pythons(company: &str) -> Option<Vec<PythonEnvironment>> { |
| 46 | + let hklm = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); |
| 47 | + let hkcu = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER); |
| 48 | + |
| 49 | + let mut pythons = vec![]; |
| 50 | + if let Some(hklm_pythons) = get_registry_pythons_from_key(&hklm, company) { |
| 51 | + pythons.extend(hklm_pythons); |
| 52 | + } |
| 53 | + if let Some(hkcu_pythons) = get_registry_pythons_from_key(&hkcu, company) { |
| 54 | + pythons.extend(hkcu_pythons); |
| 55 | + } |
| 56 | + |
| 57 | + Some(pythons) |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(windows)] |
| 61 | +pub struct WindowsRegistry {} |
| 62 | + |
| 63 | +#[cfg(windows)] |
| 64 | +impl WindowsRegistry { |
| 65 | + #[allow(dead_code)] |
| 66 | + pub fn new() -> WindowsRegistry { |
| 67 | + WindowsRegistry {} |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(windows)] |
| 72 | +impl Locator for WindowsRegistry { |
| 73 | + fn resolve(&self, env: &PythonEnv) -> Option<PythonEnvironment> { |
| 74 | + None |
| 75 | + } |
| 76 | + |
| 77 | + fn find(&self) -> Option<LocatorResult> { |
| 78 | + let environments = get_registry_pythons("PythonCore")?; |
| 79 | + if environments.is_empty() { |
| 80 | + None |
| 81 | + } else { |
| 82 | + Some(LocatorResult { |
| 83 | + managers: vec![], |
| 84 | + environments, |
| 85 | + }) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// PythonCore |
| 91 | +// ContinuumAnalytics |
0 commit comments