def is_autostart_enabled(self):
if os.name == 'nt':
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, self._windows_run_reg_key, 0, winreg.KEY_ALL_ACCESS) as key:
try:
reg_value, reg_type = winreg.QueryValueEx(key, const.APP_NAME)
if reg_type == winreg.REG_SZ and reg_value == self._get_executable_path():
return True
else:
try:
winreg.DeleteValue(key, self.WIN_REG_AUTORUN_KEY)
except OSError:
pass # key does not exist
except:
return False
else:
return os.path.exists(self._linux_autostart_file)
python类KEY_ALL_ACCESS的实例源码
def EnableCrashDumpCollection():
"""Tell Windows Error Reporting to record crash dumps so that we can diagnose
linker crashes and other toolchain failures. Documented at:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181.aspx
"""
if sys.platform == 'win32' and os.environ.get('CHROME_HEADLESS') == '1':
key_name = r'SOFTWARE\Microsoft\Windows\Windows Error Reporting'
try:
key = winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, key_name, 0,
winreg.KEY_WOW64_64KEY | winreg.KEY_ALL_ACCESS)
# Merely creating LocalDumps is sufficient to enable the defaults.
winreg.CreateKey(key, "LocalDumps")
# Disable the WER UI, as documented here:
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb513638.aspx
winreg.SetValueEx(key, "DontShowUI", 0, winreg.REG_DWORD, 1)
# Trap OSError instead of WindowsError so pylint will succeed on Linux.
# Catching errors is important because some build machines are not elevated
# and writing to HKLM requires elevation.
except OSError:
pass
def _delete_key_if_empty(self, service):
key_name = r'Software\%s\Keyring' % service
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_name, 0,
winreg.KEY_ALL_ACCESS)
try:
winreg.EnumValue(key, 0)
return
except WindowsError:
pass
winreg.CloseKey(key)
# it's empty; delete everything
while key_name != 'Software':
parent, sep, base = key_name.rpartition('\\')
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, parent, 0,
winreg.KEY_ALL_ACCESS)
winreg.DeleteKey(key, base)
winreg.CloseKey(key)
key_name = parent
def _save_autostart_win(self, on):
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, self._windows_run_reg_key, 0, winreg.KEY_ALL_ACCESS) as key:
if on:
winreg.SetValueEx(key, const.APP_NAME, 0, winreg.REG_SZ, self._get_executable_path())
else:
try:
winreg.DeleteValue(key, const.APP_NAME)
except OSError:
pass # key does not exist
def setenv(self, name, value):
# Note: for 'system' scope, you must run this as Administrator
key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_ALL_ACCESS)
winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value)
winreg.CloseKey(key)
# For some strange reason, calling SendMessage from the current process
# doesn't propagate environment changes at all.
# TODO: handle CalledProcessError (for assert)
check_call('''\
"%s" -c "import win32api, win32con; assert win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')"''' % sys.executable)
def set_up_windows_7_oem_background_reg():
mid_key_str = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI\\Background'
target_key_name = 'OEMBackground'
top_key = winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, mid_key_str,
access=winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_64KEY)
winreg.SetValueEx(top_key, target_key_name, 0, winreg.REG_DWORD, 1)
def setEnvironment(scope, name, value):
assert scope in ('user', 'system')
#INFO_MSG('set environment: name=%s, value=%s' % (name, value))
if platform.system() == 'Windows':
root, subkey = getWindowsEnvironmentKey(scope)
# Note: for 'system' scope, you must run this as Administrator
key = winreg.OpenKey(root, subkey, 0, winreg.KEY_ALL_ACCESS)
winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value)
winreg.CloseKey(key)
else:
if name.lower() == 'uid':
uid, username = value
if uid != str(os.geteuid()):
ret, cret = syscommand('bash -c \'usermod -d /home/%s/ -u %s %s\'' % (pwd.getpwnam(username).pw_dir, uid, username), True)
INFO_MSG(ret)
INFO_MSG(cret)
return
userhome = "~"
if len(os_user_name) > 0:
userhome = pwd.getpwnam(os_user_name).pw_dir
f = open('%s/.bashrc' % userhome, 'a')
f.write("export %s=%s\n\n" % (name, value))
f.close()
if os.geteuid() > 0:
syscommand('bash -c \'source %s/.bashrc\'' % userhome, False)
def remmoveEnvironment(scope, name):
assert scope in ('user', 'system')
if platform.system() == 'Windows':
root, subkey = getWindowsEnvironmentKey(scope)
key = winreg.OpenKey(root, subkey, 0, winreg.KEY_ALL_ACCESS)
try:
winreg.DeleteValue(key, name)
except WindowsError:
pass
else:
removeLinuxEnvironment(scope, name)
def delete_password(self, service, username):
"""Delete the password for the username of the service.
"""
try:
key_name = r'Software\%s\Keyring' % service
hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_name, 0,
winreg.KEY_ALL_ACCESS)
winreg.DeleteValue(hkey, username)
winreg.CloseKey(hkey)
except WindowsError:
e = sys.exc_info()[1]
raise PasswordDeleteError(e)
self._delete_key_if_empty(service)
def checkstartup():
# check if it is linux
if os_type == "Linux" or os_type == "FreeBSD" or os_type == 'OpenBSD':
# check if the startup exists
if os.path.exists(home_address + "/.config/autostart/persepolis.desktop"):
return True
else:
return False
# check if it is mac
elif os_type == "Darwin":
# OS X
if os.path.exists(home_address + "/Library/LaunchAgents/com.persepolisdm.plist"):
return True
else:
return False
# check if it is Windows
elif os_type == "Windows":
# try to open startup key and check persepolis value
try:
aKey = winreg.OpenKey(
winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_ALL_ACCESS)
startupvalue = winreg.QueryValueEx(aKey, 'persepolis')
startup = True
except WindowsError:
startup = False
# Close the connection
winreg.CloseKey(aKey)
# if the startup enabled or disabled
if startup:
return True
if not startup:
return False
# add startup file
def removestartup():
# check if it is linux
if os_type == 'Linux' or os_type == 'FreeBSD' or os_type == 'OpenBSD':
# remove it
os.remove(home_address + "/.config/autostart/persepolis.desktop")
# check if it is mac OS
elif os_type == "Darwin":
# OS X
if checkstartup():
os.system('launchctl unload ' + home_address +
"/Library/LaunchAgents/com.persepolisdm.plist")
os.remove(home_address +
"/Library/LaunchAgents/com.persepolisdm.plist")
# check if it is Windows
elif os_type == 'Windows':
if checkstartup():
# Connect to the startup path in Registry
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_ALL_ACCESS)
# remove persepolis from startup
winreg.DeleteValue(key, 'persepolis')
# Close connection
winreg.CloseKey(key)
def get_runonce():
return winreg.OpenKey(_registry,
r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0,
winreg.KEY_ALL_ACCESS)