Package platformids ::
Package dist ::
Package nt ::
Module windows_subprocess_reg_exe
1
2 """Reads the MS-Windows registry by calling a subprocess for reg.exe.
3
4 This is a fallback solution when neither *winreg* / *_winreg*, nor in case
5 of Jython the Java APIs such as *WindowsPref*, *JNA*, or *JNI* are not
6 available.
7 This works also on *Cygwin*, when e.g. the path for *kernel32.dll* does not
8 match for the *ctypes*.
9
10 Avoid using it due to serious performance impact compared to
11 the others - but do when nothing else helps and the
12 version information matters.
13 """
14 from __future__ import absolute_import
15
16 import re
17
18 from subprocess import Popen, PIPE
19
20 from platformids import PlatformIDsError
21 from platformids.dist.windows import VER_NT_SERVER, VER_NT_WORKSTATION, \
22 VER_NT_DOMAIN_CONTROLLER, VER_NT_IOT, VER_NT_GENERIC
23
24
25 __author__ = 'Arno-Can Uestuensoez'
26 __license__ = "Artistic-License-2.0 + Forced-Fairplay-Constraints"
27 __copyright__ = "Copyright (C) 2010-2018 Arno-Can Uestuensoez" \
28 " @Ingenieurbuero Arno-Can Uestuensoez"
29 __version__ = '0.1.2'
30 __uuid__ = "7add5ded-c39b-4b6e-8c87-1b3a1c150ee9"
31
32 __docformat__ = "restructuredtext en"
33
34
37
38
40 """Registry read access by *reg.exe* to ::
41
42 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
43
44 """
45
47
48
49
50 self.result = {}
51
52
53 self.product_type = None
54
55
57 """Reads the constant - thus hardcoded - key from ::
58
59 "HKLM\Software\Microsoft\Windows NT\CurrentVersion"
60
61 Args:
62 None
63
64 Returns:
65 The dictionary of the *CurrentVersion*.
66
67 Raises:
68 PlatformIDsReadRegExeError
69
70 pass-through
71
72 """
73 cmd = 'c:\Windows\\system32\\reg.exe query '
74 cmd += '"HKLM\Software\Microsoft\Windows NT\CurrentVersion"'
75
76 p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
77 out = p.communicate()
78
79 if not out[0]:
80 raise PlatformIDsReadRegExeError("read registry failed: " + str(out))
81
82 for k,v in re.findall('^ *([^ ]+) *REG_[^ ]* *(.*)[\r][\n]*$', out[0], flags=re.MULTILINE):
83 try:
84 self.result[k] = int(v)
85 except ValueError:
86 try:
87 self.result[k] = int(v, 16)
88 except ValueError:
89 self.result[k] = v
90
91 return self.result
92
93
95 """Estimates the product type, because this is a fallback
96 when nothing else is available.
97
98 Args:
99 None
100
101 Returns:
102 The estimated product type as a reduction of the
103 original
104
105 "pdwReturnedProductType"
106
107 Raises:
108 pass-through
109
110 """
111 if not self.result:
112
113 self.read_CurrentVersion()
114
115 if self.product_type != None:
116
117 return self.product_type
118
119 try:
120
121
122
123 eid = self.resultp['EditionID']
124 if re.match(r'.*[Ss]erver', eid):
125 self.product_type = VER_NT_SERVER
126 return VER_NT_SERVER
127
128 elif self.resultp['InstallationType'] == 'Client':
129 self.product_type = VER_NT_WORKSTATION
130 return VER_NT_WORKSTATION
131
132 elif re.match(r'.*[Dd]omain', eid):
133 self.product_type = VER_NT_DOMAIN_CONTROLLER
134 return VER_NT_DOMAIN_CONTROLLER
135
136
137 elif re.match(r'.*[Ii][Oo][Tt]', eid):
138 self.product_type = VER_NT_IOT
139 return VER_NT_IOT
140
141 else:
142 self.product_type = VER_NT_GENERIC
143 return VER_NT_GENERIC
144
145 except:
146
147
148
149
150
151
152 pnam = self.resultp['ProductName']
153 if re.match(r'.*XP', pnam):
154
155 self.product_type = VER_NT_WORKSTATION
156 return VER_NT_WORKSTATION
157
158 elif re.match(r'.*SERVER', self.resultp['SourcePath']):
159
160 self.product_type = VER_NT_SERVER
161 return VER_NT_SERVER
162
163 else:
164
165
166
167 self.product_type = VER_NT_GENERIC
168 return VER_NT_GENERIC
169