class MachineRegisteredEnvironment(RegisteredEnvironment): path = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' hklm = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) try: key = winreg.OpenKey(hklm, path, 0, winreg.KEY_READ | winreg.KEY_WRITE) except WindowsError: key = winreg.OpenKey(hklm, path, 0, winreg.KEY_READ)
def check_fips_mode_os(): """ Function to check for the OS fips mode :param key: string to encrypt with :type key: str. :returns: returns True if FIPS mode is active, False otherwise """ fips = False if os.name == 'nt': reg = winreg.ConnectRegistry(None, HKEY_LOCAL_MACHINE) try: reg = winreg.OpenKey(reg, 'System\\CurrentControlSet\\Control\\'\ 'Lsa\\FipsAlgorithmPolicy') winreg.QueryInfoKey(reg) value, _ = winreg.QueryValueEx(reg, 'Enabled') if value: fips = True except: fips = False else: try: fipsfile = open("/proc/sys/crypto/fips_enabled") result = fipsfile.readline() if int(result) > 0: fipsfile = True fipsfile.close() except: fips = False return fips
def __init__(self, name): self._name = name # multiple contexts only possible in 2.7 and 3.1, we still support 2.6 with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: with winreg.OpenKey(handle, "%s\%s" % (TZKEYNAME, name)) as tzkey: keydict = valuestodict(tzkey) self._stdname = keydict["Std"] self._dstname = keydict["Dlt"] self._display = keydict["Display"] # See http://ww_winreg.jsiinc.com/SUBA/tip0300/rh0398.htm tup = struct.unpack("=3l16h", keydict["TZI"]) self._stdoffset = -tup[0] - tup[1] # Bias + StandardBias * -1 self._dstoffset = self._stdoffset - tup[2] # + DaylightBias * -1 # for the meaning see the win32 TIME_ZONE_INFORMATION structure docs # http://msdn.microsoft.com/en-us/library/windows/desktop/ms725481(v=vs.85).aspx ( self._stdmonth, self._stddayofweek, # Sunday = 0 self._stdweeknumber, # Last = 5 self._stdhour, self._stdminute) = tup[4:9] ( self._dstmonth, self._dstdayofweek, # Sunday = 0 self._dstweeknumber, # Last = 5 self._dsthour, self._dstminute) = tup[12:17]
def list(): """Return a list of all time zones known to the system.""" with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: with winreg.OpenKey(handle, TZKEYNAME) as tzkey: result = [winreg.EnumKey(tzkey, i) for i in range(winreg.QueryInfoKey(tzkey)[0])] return result
def __init__(self, name): self._name = name handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) tzkey = winreg.OpenKey(handle, "%s\%s" % (TZKEYNAME, name)) keydict = valuestodict(tzkey) tzkey.Close() handle.Close() self._stdname = keydict["Std"].encode("iso-8859-1") self._dstname = keydict["Dlt"].encode("iso-8859-1") self._display = keydict["Display"] # See http://ww_winreg.jsiinc.com/SUBA/tip0300/rh0398.htm tup = struct.unpack("=3l16h", keydict["TZI"]) self._stdoffset = -tup[0] - tup[1] # Bias + StandardBias * -1 self._dstoffset = self._stdoffset - tup[2] # + DaylightBias * -1 ( self._stdmonth, self._stddayofweek, # Sunday = 0 self._stdweeknumber, # Last = 5 self._stdhour, self._stdminute) = tup[4:9] ( self._dstmonth, self._dstdayofweek, # Sunday = 0 self._dstweeknumber, # Last = 5 self._dsthour, self._dstminute) = tup[12:17]
def _settzkeyname(): handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) try: winreg.OpenKey(handle, TZKEYNAMENT).Close() TZKEYNAME = TZKEYNAMENT except WindowsError: TZKEYNAME = TZKEYNAME9X handle.Close() return TZKEYNAME
def list(): """Return a list of all time zones known to the system.""" handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) tzkey = winreg.OpenKey(handle, TZKEYNAME) result = [winreg.EnumKey(tzkey, i) for i in range(winreg.QueryInfoKey(tzkey)[0])] tzkey.Close() handle.Close() return result
def set_upgrade_envvar(self): if not os.environ.get("OPENSVC_AGENT_UPGRADE"): return path = r"SYSTEM\CurrentControlSet\Services\%s\Environment" % WINSVCNAME reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) key = winreg.CreateKeyEx(reg, path, 0, winreg.KEY_WRITE) try: winreg.SetValue(key, "OPENSVC_AGENT_UPGRADE", winreg.REG_SZ, "1") except EnvironmentError: raise ex.excError("failed to set OPENSVC_AGENT_UPGRADE=1 in %s" % path) winreg.CloseKey(key) winreg.CloseKey(reg)
def unset_upgrade_envvar(self): path = r"SYSTEM\CurrentControlSet\Services\%s\Environment" % WINSVCNAME reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) try: key = winreg.OpenKey(reg, path, 0, winreg.KEY_WRITE) winreg.DeleteValue(key, "OPENSVC_AGENT_UPGRADE") winreg.CloseKey(key) except Exception as exc: if hasattr(exc, "errno") and getattr(exc, "errno") == 2: # key does not exist return raise finally: winreg.CloseKey(reg)
def __init__(self): with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: with winreg.OpenKey(handle, TZLOCALKEYNAME) as tzlocalkey: keydict = valuestodict(tzlocalkey) self._std_abbr = keydict["StandardName"] self._dst_abbr = keydict["DaylightName"] try: tzkeyname = text_type("{kn}\\{sn}").format(kn=TZKEYNAME, sn=self._std_abbr) with winreg.OpenKey(handle, tzkeyname) as tzkey: _keydict = valuestodict(tzkey) self._display = _keydict["Display"] except OSError: self._display = None stdoffset = -keydict["Bias"] - keydict["StandardBias"] dstoffset = stdoffset - keydict["DaylightBias"] self._std_offset = datetime.timedelta(minutes=stdoffset) self._dst_offset = datetime.timedelta(minutes=dstoffset) # For reasons unclear, in this particular key, the day of week has been # moved to the END of the SYSTEMTIME structure. tup = struct.unpack("=8h", keydict["StandardStart"]) ( self._stdmonth, self._stdweeknumber, # Last = 5 self._stdhour, self._stdminute, ) = tup[1:5] self._stddayofweek = tup[7] tup = struct.unpack("=8h", keydict["DaylightStart"]) ( self._dstmonth, self._dstweeknumber, # Last = 5 self._dsthour, self._dstminute, ) = tup[1:5] self._dstdayofweek = tup[7] self._dst_base_offset_ = self._dst_offset - self._std_offset self.hasdst = self._get_hasdst()
def __init__( self, vmx, user='', password='', vmrun='', debug=False ): self.VM_FILE = vmx # TODO strict censor? self.VM_ADMIN = user self.VM_PASS = password self.DEBUG = debug if vmrun != '': self.VMRUN_PATH = vmrun else: if os.sys.platform == 'darwin': self.provider = 'fusion' self.VMRUN_PATH = '/Applications/VMware\ Fusion.app/Contents/Library/vmrun' elif os.sys.platform == "win32": # get vmrun.exe's full path via registry from six.moves import winreg as _winreg reg = _winreg.ConnectRegistry( None, _winreg.HKEY_LOCAL_MACHINE ) try: # Known registry key location in Windows 10 rh = _winreg.OpenKey( reg, r'SOFTWARE\WOW6432Node\VMware, Inc.\VMware Workstation' ) try: vw_dir = _winreg.QueryValueEx( rh, 'InstallPath' )[0] finally: _winreg.CloseKey( rh ) except WindowsError: # Previously used registry key location rh = _winreg.OpenKey( reg, r'SOFTWARE\VMware, Inc.\VMware Workstation' ) try: vw_dir = _winreg.QueryValueEx( rh, 'InstallPath' )[0] finally: _winreg.CloseKey( rh ) finally: reg.Close() self.provider = 'ws' if vw_dir != '': self.VMRUN_PATH = vw_dir + 'vmrun.exe' else: self.provider = 'ws' if os.environ.has_key("PATH"): for path in os.environ["PATH"].split(os.pathsep): tmp_file = path + os.sep + "vmrun" if os.path.exists(tmp_file): self.VMRUN_PATH = tmp_file break vmrun_path = getattr(self, 'VMRUN_PATH', None) if vmrun_path is None: raise RuntimeError('Could not locate vmrun executable!')
def __init__(self): handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) tzlocalkey = winreg.OpenKey(handle, TZLOCALKEYNAME) keydict = valuestodict(tzlocalkey) tzlocalkey.Close() self._stdname = keydict["StandardName"].encode("iso-8859-1") self._dstname = keydict["DaylightName"].encode("iso-8859-1") try: tzkey = winreg.OpenKey(handle, "%s\%s" % (TZKEYNAME, self._stdname)) _keydict = valuestodict(tzkey) self._display = _keydict["Display"] tzkey.Close() except OSError: self._display = None handle.Close() self._stdoffset = -keydict["Bias"] - keydict["StandardBias"] self._dstoffset = self._stdoffset - keydict["DaylightBias"] # See http://ww_winreg.jsiinc.com/SUBA/tip0300/rh0398.htm tup = struct.unpack("=8h", keydict["StandardStart"]) ( self._stdmonth, self._stddayofweek, # Sunday = 0 self._stdweeknumber, # Last = 5 self._stdhour, self._stdminute) = tup[1:6] tup = struct.unpack("=8h", keydict["DaylightStart"]) ( self._dstmonth, self._dstdayofweek, # Sunday = 0 self._dstweeknumber, # Last = 5 self._dsthour, self._dstminute) = tup[1:6]
def __init__(self, name): self._name = name with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: tzkeyname = text_type("{kn}\\{name}").format(kn=TZKEYNAME, name=name) with winreg.OpenKey(handle, tzkeyname) as tzkey: keydict = valuestodict(tzkey) self._std_abbr = keydict["Std"] self._dst_abbr = keydict["Dlt"] self._display = keydict["Display"] # See http://ww_winreg.jsiinc.com/SUBA/tip0300/rh0398.htm tup = struct.unpack("=3l16h", keydict["TZI"]) stdoffset = -tup[0] - tup[1] # Bias + StandardBias * -1 dstoffset = stdoffset - tup[2] # + DaylightBias * -1 self._std_offset = datetime.timedelta(minutes=stdoffset) self._dst_offset = datetime.timedelta(minutes=dstoffset) # for the meaning see the win32 TIME_ZONE_INFORMATION structure docs # http://msdn.microsoft.com/en-us/library/windows/desktop/ms725481(v=vs.85).aspx ( self._stdmonth, self._stddayofweek, # Sunday = 0 self._stdweeknumber, # Last = 5 self._stdhour, self._stdminute, ) = tup[4:9] ( self._dstmonth, self._dstdayofweek, # Sunday = 0 self._dstweeknumber, # Last = 5 self._dsthour, self._dstminute, ) = tup[12:17] self._dst_base_offset_ = self._dst_offset - self._std_offset self.hasdst = self._get_hasdst()
def __init__(self): with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: with winreg.OpenKey(handle, TZLOCALKEYNAME) as tzlocalkey: keydict = valuestodict(tzlocalkey) self._stdname = keydict["StandardName"] self._dstname = keydict["DaylightName"] try: with winreg.OpenKey( handle, "%s\%s" % (TZKEYNAME, self._stdname)) as tzkey: _keydict = valuestodict(tzkey) self._display = _keydict["Display"] except OSError: self._display = None self._stdoffset = -keydict["Bias"]-keydict["StandardBias"] self._dstoffset = self._stdoffset-keydict["DaylightBias"] # For reasons unclear, in this particular key, the day of week has been # moved to the END of the SYSTEMTIME structure. tup = struct.unpack("=8h", keydict["StandardStart"]) (self._stdmonth, self._stdweeknumber, # Last = 5 self._stdhour, self._stdminute) = tup[1:5] self._stddayofweek = tup[7] tup = struct.unpack("=8h", keydict["DaylightStart"]) (self._dstmonth, self._dstweeknumber, # Last = 5 self._dsthour, self._dstminute) = tup[1:5] self._dstdayofweek = tup[7]
class UserRegisteredEnvironment(RegisteredEnvironment): hkcu = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) key = winreg.OpenKey(hkcu, 'Environment', 0, winreg.KEY_READ | winreg.KEY_WRITE)
def set_win_env(vars): """ Set Windows environment variable persistently by editing the registry :param vars: ['VAR1=VAL1', 'VAR2=VAL2', 'PATH=SOMEPATH'] """ if is_conda_env(): return set_conda_env(vars) if (not 'win32' in sys.platform): return for newvar in vars: from string import find try: if sys.version_info.major == 2: import six.moves.winreg as winreg else: import winreg except ImportError as e: print("!!ERROR: Can not access to Windows registry.") return def queryValue(qkey, qname): qvalue, type_id = winreg.QueryValueEx(qkey, qname) return qvalue name, value = newvar.split('=') regpath = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) try: key = winreg.OpenKey(reg, regpath, 0, winreg.KEY_ALL_ACCESS) except WindowsError as we: print(("Cannot set "+repr(name)+" for all users. Set for current user.")) winreg.CloseKey(reg) regpath = r'Environment' reg = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) key = winreg.OpenKey(reg, regpath, 0, winreg.KEY_ALL_ACCESS) # Specific treatment for PATH variable if name.upper() == 'PATH': value = os.path.normpath(value) try: actualpath = queryValue(key, name) except: print('No PATH variable found') actualpath = '' listpath = actualpath.split(';') if not (value in listpath): value = actualpath + ';' + value print(("ADD %s to PATH" % (value,))) else: value = actualpath # TEST SIZE if (len(value) >= 8191): print("!!ERROR!! : PATH variable cannot contain more than 8191 characters") print("!!ERROR!! : Please : remove unused value in your environement") value = actualpath if (name and value): expand = winreg.REG_SZ # Expand variable if necessary if ("%" in value): expand = winreg.REG_EXPAND_SZ winreg.SetValueEx(key, name, 0, expand, value) # os.environ[name] = value #not necessary winreg.CloseKey(key) winreg.CloseKey(reg) # Refresh Environment try: HWND_BROADCAST = 0xFFFF WM_SETTINGCHANGE = 0x001A SMTO_ABORTIFHUNG = 0x0002 sParam = "Environment" import win32gui res1, res2 = win32gui.SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, sParam, SMTO_ABORTIFHUNG, 100) if not res1: print(("result %s, %s from SendMessageTimeout" % (bool(res1), res2))) except Exception as e: print(e)