Package platformids :: Package dist :: Package nt :: Module windows_subprocess_reg_exe

Source Code for Module platformids.dist.nt.windows_subprocess_reg_exe

  1  # -*- coding: utf-8 -*- 
  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   
35 -class PlatformIDsReadRegExeError(PlatformIDsError):
36 pass
37 38
39 -class RegistryByExe(object):
40 """Registry read access by *reg.exe* to :: 41 42 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" 43 44 """ 45
46 - def __init__(self):
47 48 #: used as cache for following calls 49 #: the cache represents this platform, so may not be altered 50 self.result = {} 51 52 # cached product type 53 self.product_type = None
54 55
56 - def read_CurrentVersion(self):
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): # @UndefinedVariable 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
94 - def get_win32_OSProductInfo(self):
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 # set cache 113 self.read_CurrentVersion() 114 115 if self.product_type != None: 116 # use cached product type 117 return self.product_type 118 119 try: 120 # 121 # for NT >= 6.0 122 # 123 eid = self.resultp['EditionID'] 124 if re.match(r'.*[Ss]erver', eid): 125 self.product_type = VER_NT_SERVER # 0x0000003 #: see wProductType 126 return VER_NT_SERVER 127 128 elif self.resultp['InstallationType'] == 'Client': # has to be present when *EditionID* is 129 self.product_type = VER_NT_WORKSTATION # 0x0000001 #: see wProductType 130 return VER_NT_WORKSTATION 131 132 elif re.match(r'.*[Dd]omain', eid): 133 self.product_type = VER_NT_DOMAIN_CONTROLLER # 0x0000002 #: see wProductType 134 return VER_NT_DOMAIN_CONTROLLER 135 136 137 elif re.match(r'.*[Ii][Oo][Tt]', eid): 138 self.product_type = VER_NT_IOT # 0x0000004 #: non standard enum 139 return VER_NT_IOT 140 141 else: 142 self.product_type = VER_NT_GENERIC # 0x0000000 #: non standard enum 143 return VER_NT_GENERIC 144 145 except: 146 # 147 # for NT < 6.0 148 # 149 # do not want spend much effort for legacy has past EOL, so... 150 # 151 152 pnam = self.resultp['ProductName'] 153 if re.match(r'.*XP', pnam): 154 # WindowsXP 155 self.product_type = VER_NT_WORKSTATION # 0x0000001 #: see wProductType 156 return VER_NT_WORKSTATION 157 158 elif re.match(r'.*SERVER', self.resultp['SourcePath']): # not reliable... 159 # WindowsXP 160 self.product_type = VER_NT_SERVER # 0x0000003 #: see wProductType 161 return VER_NT_SERVER 162 163 else: 164 # 165 # may match a lot... 166 # 167 self.product_type = VER_NT_GENERIC # 0x0000000 #: non standard enum 168 return VER_NT_GENERIC
169