Chromium Code Reviews| Index: install_test/chrome_installer.py |
| =================================================================== |
| --- install_test/chrome_installer.py (revision 0) |
| +++ install_test/chrome_installer.py (revision 0) |
| @@ -0,0 +1,306 @@ |
| +#!/usr/bin/python |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Provides an interface for installing Chrome.""" |
| + |
| +import _winreg |
| +import ctypes |
| +from ctypes import wintypes, windll |
| +import httplib |
| +import logging |
| +import os |
| +import shutil |
| +import socket |
| +import subprocess |
| +import tempfile |
| +import urllib |
| + |
| + |
| +class InstallationType: |
| + """Defines the Chrome installation types.""" |
| + SYSTEM = 0 |
| + USER = 1 |
| + |
| + |
| +class ChromeRegistryValues: |
| + """Defines the Chrome registry key values.""" |
| + PRODUCT_VERSION = 0 |
|
kkania
2012/08/03 18:41:08
you can just change these values to the string equ
nkang
2012/08/06 23:57:13
Changed the three values from numerical to string.
|
| + UNINSTALL_ARGUMENTS = 1 |
| + UNINSTALL_STRING = 2 |
| + |
| + |
| +def Install(installer_path, chrome_type, build, options='', clean=True): |
| + """Installs the specified Chrome build. |
| + |
| + Args: |
| + installer_path: Path to the Chrome installer. |
| + chrome_type: Type of installation (i.e., system or user). |
| + build: Chrome build number. |
| + options: Any additional installation options. |
| + clean: Determines whether to delete registry settings before installation. |
| + |
| + Returns: |
| + A ChromeInstallation object if successful, otherwise None. |
|
kkania
2012/08/03 18:41:08
this comment needs to be updated
nkang
2012/08/06 23:57:13
Changed the comment. It now says, 'Returns: A Chro
|
| + """ |
| + def DoPreliminaryChecks(regedit): |
| + """Validates the test parameters and Chrome version. |
| + |
| + Args: |
| + regedit: ChromeRegistryKeys object. |
| + """ |
| + assert(os.path.isfile(installer_path)) |
| + install_type = ('system-level' in options and InstallationType.SYSTEM |
|
kkania
2012/08/03 18:41:08
this statement no longer works. that is why I typi
nkang
2012/08/06 23:57:13
Good find! Changed this statement to the following
|
| + or InstallationType.USER) |
| + cur_build = regedit.GetKeyValue(chrome_type, |
| + ChromeRegistryValues.PRODUCT_VERSION) |
| + # Chrome already installed, make sure new build can be installed over it. |
| + if cur_build: |
| + current_type = ChromeInstallation.GetType() |
| + if(current_type == InstallationType.SYSTEM and install_type == |
| + InstallationType.USER): |
| + raise RuntimeError('System level Chrome exists, aborting user level ' |
| + 'installation.') |
| + # Installing a build that's older than the currently installed build. |
| + elif current_type == install_type: |
| + if cur_build >= build: |
| + raise RuntimeError('Please specify a newer version of Chrome.') |
| + |
| + regedit = ChromeRegistryKeys() |
| + DoPreliminaryChecks(regedit) |
| + options += ' --install --do-not-launch-chrome' |
| + logging.log(logging.INFO, 'Launching Chrome installer...') |
| + cmd = '%s %s' % (installer_path, options) |
| + ret = subprocess.Popen(cmd, shell=True).wait() |
| + if ret == 0: |
| + logging.log(logging.INFO, 'Installation complete.') |
| + return ChromeInstallation(ChromeInstallation.GetType()) |
| + raise RuntimeError('Chrome installation for build %s failed.' % build) |
| + |
| + |
| +class ChromeRegistryKeys(object): |
| + """An interface for accessing and manipulating Chrome registry keys.""" |
| + |
| + _HKEY_LOCAL = r'SOFTWARE\Wow6432Node\Google\Update' |
| + _HKEY_USER = _HKEY_LOCAL.replace('\\Wow6432Node', '') |
| + _chrome_version = r'Clients\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
| + _chrome_uargs = r'ClientState\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
| + _chrome_ustring = r'ClientState\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
|
kkania
2012/08/03 18:41:08
what's diff between this and above var? merge them
nkang
2012/08/06 23:57:13
There's no difference; they are the same. I just c
|
| + |
| + def _GetKeyName(self, install_type, value): |
| + """Generates the registry key name for the specified value. |
| + |
| + Args: |
| + install_type: Type of installation, must be InstallationType type. |
| + value: ChromeRegistryValues type for which the key name is required. |
| + |
| + Returns: |
| + A string representing the full key name of the specified key value. |
| + """ |
| + key_name = None |
| + if install_type == InstallationType.USER: |
| + key_name = self._HKEY_USER |
| + elif install_type == InstallationType.SYSTEM: |
| + key_name = self._HKEY_LOCAL |
| + if value == ChromeRegistryValues.PRODUCT_VERSION: |
| + return '%s\%s' % (key_name, self._chrome_version) |
| + elif value == ChromeRegistryValues.UNINSTALL_ARGUMENTS: |
| + return '%s\%s' % (key_name, self._chrome_uargs) |
| + elif value == ChromeRegistryValues.UNINSTALL_STRING: |
| + return '%s\%s' % (key_name, self._chrome_ustring) |
| + return '' |
|
kkania
2012/08/03 18:41:08
throw instead
nkang
2012/08/06 23:57:13
Instead of returning an empty string, the method n
|
| + |
| + def _GetRegistryType(self, install_type): |
| + """Determines the registry key to use based on installation type. |
| + |
| + Args: |
| + install_type: Type of installation, must be InstallationType type. |
| + |
| + Returns: |
| + A long representing HKLM or HKCU, depending on installation type. |
| + """ |
| + if install_type == InstallationType.SYSTEM: |
| + return _winreg.HKEY_LOCAL_MACHINE |
| + elif install_type == InstallationType.USER: |
| + return _winreg.HKEY_CURRENT_USER |
| + return None |
|
kkania
2012/08/03 18:41:08
throw instead
nkang
2012/08/06 23:57:13
Instead of returning None, the method raises a Run
|
| + |
| + def DoesKeyExist(self, install_type, subkey): |
| + """Determines if a particular key exists in the registry. |
| + |
| + Args: |
| + install_type: Type of installation, must be InstallationType type. |
| + subkey: Subkey to look up. It must be a ChromeRegistryValues type. |
| + |
| + Returns: |
| + True if the key exists, otherwise False. |
| + """ |
| + b_exists = False |
| + key = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, subkey) |
| + if not key_name or not key: |
| + return b_exists |
|
kkania
2012/08/03 18:41:08
throw instead
nkang
2012/08/06 23:57:13
Got rid of this statement altogether. We can't thr
|
| + try: |
| + hkey = _winreg.OpenKey(key, key_name) |
| + if hkey.handle: |
| + b_exists = True |
| + hkey.Close() |
| + return b_exists |
| + except _winreg.error: |
| + return False |
| + |
| + def GetKeyValue(self, install_type, subkey): |
| + """Gets value of the specified subkey from the registry. |
| + |
| + Args: |
| + install_type: Type of installation, must be InstallationType type. |
| + subkey: ChromeRegistryValue type representing the value to be returned. |
| + |
| + Returns: |
| + A string representing the subkey value if successful, otherwise None. |
|
kkania
2012/08/03 18:41:08
don't return none in this func in any circumstance
nkang
2012/08/06 23:57:13
Got rid of the None return value if a _winreg.erro
|
| + """ |
| + reg_value = '' |
| + key = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, subkey) |
| + value = ({ChromeRegistryValues.PRODUCT_VERSION : 'pv', |
| + ChromeRegistryValues.UNINSTALL_STRING : 'UninstallString', |
| + ChromeRegistryValues.UNINSTALL_ARGUMENTS : 'UninstallArguments' |
| + }).get(subkey) |
| + if not key or not key_name: |
|
kkania
2012/08/03 18:41:08
don't check here, let the other funcs throw
nkang
2012/08/06 23:57:13
Got rid of the 'if' statement, altogether. If key
|
| + return None |
| + try: |
| + hkey = _winreg.OpenKey(key, key_name) |
|
kkania
2012/08/03 18:41:08
don't catch exceptions here, let them be raised
nkang
2012/08/06 23:57:13
Got rid of the try/except block. We will no longer
|
| + if hkey.handle: |
| + reg_value = str(_winreg.QueryValueEx(hkey, value)[0]) |
| + hkey.Close() |
| + return reg_value |
| + except _winreg.error: |
| + return None |
| + |
| + def DeleteRegistryEntries(self, install_type): |
| + """Deletes chrome registry settings. |
| + |
| + Args: |
| + install_type: Type of installation, must be InstallationType type. |
| + """ |
| + key = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, |
| + ChromeRegistryValues.UNINSTALL_ARGUMENTS) |
| + root = key_name[: key_name.rfind('\\')] |
|
kkania
2012/08/03 18:41:08
no space between :
nkang
2012/08/06 23:57:13
Fixed.
|
| + child = key_name[key_name.rfind('\\') + 1 :] |
|
kkania
2012/08/03 18:41:08
same
nkang
2012/08/06 23:57:13
Fixed.
|
| + try: |
| + key = _winreg.OpenKey(key, root, 0, _winreg.KEY_ALL_ACCESS) |
| + _winreg.DeleteKey(key, child) |
| + key.Close() |
| + except _winreg.error, err: |
|
kkania
2012/08/03 18:41:08
why bother catching if you're just going to raise?
nkang
2012/08/06 23:57:13
Previously 'DeleteRegistryEntries' did not raise a
|
| + raise(err) |
| + |
| + |
| +class ChromeInstallation(object): |
| + """Provides pertinent information about the installed Chrome version. |
| + |
| + The type of Chrome version must be passed as an argument to the constructor, |
| + (i.e. - user or system level). |
| + """ |
| + |
| + _CSIDL_COMMON_APPDATA = 0x1C |
| + _CSIDL_PROGRAM_FILESX86 = 0x2A |
| + |
| + def __init__(self, install_type): |
| + assert(install_type == InstallationType.SYSTEM or |
| + install_type == InstallationType.USER) |
| + self._type = install_type |
| + self._regedit = ChromeRegistryKeys() |
| + |
| + def _GetWinLocalFolder(self, ftype=_CSIDL_COMMON_APPDATA): |
| + """Returns full path of the 'Local' folder on Windows. |
| + |
| + Args: |
| + ftype: Location to look up, which could vary based on installation type. |
| + |
| + Returns: |
| + A String representing the folder path if successful, otherwise an empty |
| + string. |
| + """ |
| + SHGetFolderPathW = windll.shell32.SHGetFolderPathW |
| + SHGetFolderPathW.argtypes = [wintypes.HWND, |
| + ctypes.c_int, |
| + wintypes.HANDLE, |
| + wintypes.DWORD, |
| + wintypes.LPCWSTR] |
| + path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH) |
| + result = SHGetFolderPathW(0, ftype, 0, 0, path_buf) |
| + return str(path_buf.value) |
| + |
| + def _GetUninstallString(self): |
| + """Returns the Chrome uninstall string from the registry.""" |
| + return self._regedit.GetKeyValue(self._type, |
| + ChromeRegistryValues.UNINSTALL_STRING) |
| + |
| + def _GetUninstallArguments(self): |
| + """Returns the Chrome uninstall arguments from the registry.""" |
| + return self._regedit.GetKeyValue(self._type, |
| + ChromeRegistryValues.UNINSTALL_ARGUMENTS) |
| + |
| + def _LaunchInstaller(self, installer_path, options): |
|
kkania
2012/08/03 18:41:08
remove this separate func and put subproces.popen
nkang
2012/08/06 23:57:13
Got rid of the '_LaunchInstaller' method. The whol
|
| + """Launches the Chrome installer. |
| + |
| + Args: |
| + installer_path: Path where the installer is located. |
| + options: Any additional options to be used for installation. |
| + """ |
| + cmd = '%s %s' % (installer_path, options) |
| + try: |
| + subprocess.Popen(cmd, shell=True).wait() |
| + except OSError, err: |
| + raise err |
| + |
| + def GetPath(self): |
|
kkania
2012/08/03 18:41:08
GetExePath
nkang
2012/08/06 23:57:13
Changed method name to GetExePath.
|
| + """Returns Chrome binary location based on installation type.""" |
| + if self._type == InstallationType.USER: |
| + folder_id = self._CSIDL_COMMON_APPDATA |
| + elif self._type == InstallationType.SYSTEM: |
| + folder_id = self._CSIDL_PROGRAM_FILESX86 |
| + chrome_path = os.path.join(self._GetWinLocalFolder(folder_id), 'Google', |
| + 'Chrome', 'Application', 'chrome.exe') |
| + return (os.path.exists(chrome_path) and chrome_path or '') |
| + |
| + @staticmethod |
| + def GetType(): |
|
kkania
2012/08/03 18:41:08
change this to GetCurrent, which returns None or a
nkang
2012/08/06 23:57:13
Changed method name to GetCurrent. Also changed th
|
| + """Determines Chrome installation type. |
| + |
| + Returns: |
| + InstallationType type if Chrome is present, otherwise None. |
| + """ |
| + registry = ChromeRegistryKeys() |
| + if registry.DoesKeyExist(InstallationType.SYSTEM, |
| + ChromeRegistryValues.PRODUCT_VERSION): |
| + return InstallationType.SYSTEM |
| + elif registry.DoesKeyExist(InstallationType.USER, |
| + ChromeRegistryValues.PRODUCT_VERSION): |
| + return InstallationType.USER |
| + return None |
| + |
| + def Uninstall(self): |
| + """Uninstalls Chrome.""" |
| + install_type = ChromeInstallation.GetType() |
|
kkania
2012/08/03 18:41:08
don't do this
nkang
2012/08/06 23:57:13
Fine, I won't :)
|
| + if not install_type: |
| + raise RuntimeError('No Chrome version found on this system.') |
| + chrome_path = self.GetPath() |
| + reg_opts = self._GetUninstallArguments() |
| + uninstall_str = self._GetUninstallString() |
| + options = '%s --force-uninstall' % (reg_opts) |
| + if self._type == InstallationType.SYSTEM: |
| + options += ' --system-level' |
| + if not os.path.exists(chrome_path): |
| + raise RuntimeError('Could not find chrome, aborting uninstall.') |
| + logging.log(logging.INFO, 'Launching Chrome installer...') |
| + self._LaunchInstaller(uninstall_str, options) |
| + if not os.path.exists(chrome_path): |
| + logging.log(logging.INFO, 'Chrome was uninstalled successfully...') |
| + logging.log(logging.INFO, 'Deleting registry entries...') |
| + self._regedit.DeleteRegistryEntries(self._type) |
| + logging.log(logging.INFO, 'Uninstall complete.') |
| + else: |
| + raise RuntimeError('Uninstall failed.') |
| Property changes on: install_test\chrome_installer.py |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |