ducktools-pythonfinder 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ # ducktools-pythonfinder
2
+ # Copyright (C) 2024 David C Ellis
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU Lesser General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public License
15
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+
18
+ """
19
+ Search the Windows registry to find python installs
20
+
21
+ Based on PEP 514 registry entries.
22
+ """
23
+
24
+ import winreg # noqa # pycharm seems to think winreg doesn't exist in python3.12
25
+ from _collections_abc import Iterator
26
+
27
+ from ..shared import PythonInstall
28
+
29
+ exclude_companies = {
30
+ "PyLauncher", # pylauncher is special cased to be ignored
31
+ }
32
+
33
+
34
+ check_pairs = [
35
+ # Keys defined in PEP 514
36
+ (winreg.HKEY_CURRENT_USER, r"SOFTWARE\Python", 0),
37
+ (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Python", winreg.KEY_WOW64_64KEY),
38
+ # For system wide 32 bit python installs
39
+ (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Python", winreg.KEY_WOW64_32KEY),
40
+ ]
41
+
42
+
43
+ def enum_keys(key):
44
+ subkey_count, _, _ = winreg.QueryInfoKey(key)
45
+ for i in range(subkey_count):
46
+ yield winreg.EnumKey(key, i)
47
+
48
+
49
+ def enum_values(key):
50
+ _, value_count, _ = winreg.QueryInfoKey(key)
51
+ for i in range(value_count):
52
+ yield winreg.EnumValue(key, i)
53
+
54
+
55
+ def get_registered_pythons() -> Iterator[PythonInstall]:
56
+ for base, py_folder, flags in check_pairs:
57
+ base_key = None
58
+ try:
59
+ base_key = winreg.OpenKeyEx(base, py_folder, access=winreg.KEY_READ | flags)
60
+ except FileNotFoundError:
61
+ continue
62
+ else:
63
+ # Query the base folder eg: HKEY_LOCAL_MACHINE\SOFTWARE\Python
64
+ # The values here should be "companies" as defined in the PEP
65
+ for company in enum_keys(base_key):
66
+ if company in exclude_companies:
67
+ continue
68
+
69
+ with winreg.OpenKey(base_key, company) as company_key:
70
+ comp_metadata = {}
71
+
72
+ for name, data, _ in enum_values(company_key):
73
+ comp_metadata[f"Company{name}"] = data
74
+
75
+ for py_keyname in enum_keys(company_key):
76
+ metadata = {**comp_metadata}
77
+
78
+ with winreg.OpenKey(company_key, py_keyname) as py_key:
79
+ for name, data, _ in enum_values(py_key):
80
+ metadata[name] = data
81
+
82
+ install_key = None
83
+ try:
84
+ install_key = winreg.OpenKey(py_key, "InstallPath")
85
+ python_path, _ = winreg.QueryValueEx(
86
+ install_key,
87
+ "ExecutablePath",
88
+ )
89
+ except FileNotFoundError:
90
+ python_path = None
91
+ finally:
92
+ if install_key:
93
+ winreg.CloseKey(install_key)
94
+
95
+ python_version = metadata.get("Version")
96
+ architecture = metadata.get("SysArchitecture")
97
+
98
+ metadata["InWindowsRegistry"] = True
99
+
100
+ if python_path and python_version:
101
+ yield PythonInstall.from_str(
102
+ version=python_version,
103
+ executable=python_path,
104
+ architecture=architecture,
105
+ metadata=metadata,
106
+ )
107
+
108
+ finally:
109
+ if base_key:
110
+ winreg.CloseKey(base_key)