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,288 @@ |
| +#!/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. |
| + |
| +At present the only platform it supports is Windows. |
|
Nirnimesh
2012/08/22 07:06:35
rename the file to append _win to the name. You'll
nkang
2012/08/24 22:45:26
Changed name to chrome_installer_win.py. Updated o
|
| +""" |
| + |
| +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(object): |
| + """Defines the Chrome installation types.""" |
| + SYSTEM = 0 |
|
Nirnimesh
2012/08/22 07:06:35
If you're going to do if calls on this, make it no
nkang
2012/08/24 22:45:26
Done. Added a third variable called NOT_INSTALLED.
|
| + USER = 1 |
| + |
| + |
| +class ChromeRegistryValues(object): |
| + """Defines the Chrome registry key values.""" |
| + PRODUCT_VERSION = 'pv' |
| + UNINSTALL_STRING = 'UninstallString' |
| + UNINSTALL_ARGUMENTS = 'UninstallArguments' |
| + |
| + |
| +def Install(installer_path, install_type, build, options=''): |
| + """Installs the specified Chrome build. |
| + |
| + Args: |
| + installer_path: Path to the Chrome installer. |
| + install_type: Type of installation (i.e., system or user). |
| + build: Chrome build number. |
| + options: Any additional installation options. |
|
Nirnimesh
2012/08/22 07:06:35
remove 'Any'
Nirnimesh
2012/08/22 07:06:35
make it a list instead of having to split by ' ' a
nkang
2012/08/24 22:45:26
Changed options to a list. For this I also had to
nkang
2012/08/24 22:45:26
Removed 'Any'.
|
| + |
| + Returns: |
| + An instance of ChromeInstallation. |
| + """ |
| + def _DoPreliminaryChecks(regedit): |
| + """Validates the test parameters and Chrome version. |
| + |
| + Checks if a Chrome version is already installed on the system. If so it |
| + confirms that the version specified by the user is higher than the one |
| + currently installed. It also checks to make sure the installation type |
| + is valid, as a user level version cannot be installed over an existing |
| + system level version of Chrome. |
| + |
| + Args: |
| + regedit: ChromeRegistryKeys object. |
| + """ |
| + current_type = None |
| + # Check if Chrome is already installed on the system. |
| + if regedit.DoesKeyExist(InstallationType.USER, |
| + ChromeRegistryValues.PRODUCT_VERSION): |
| + current_type = InstallationType.USER |
| + elif regedit.DoesKeyExist(InstallationType.SYSTEM, |
| + ChromeRegistryValues.PRODUCT_VERSION): |
| + current_type = InstallationType.SYSTEM |
| + if current_type != None: |
|
Nirnimesh
2012/08/22 07:06:35
remove "!= None"
nkang
2012/08/24 22:45:26
Changed the InstallationType class and created a t
|
| + # Make sure new build can be installed over existing Chrome build. |
| + if (current_type == InstallationType.SYSTEM and install_type == |
|
Nirnimesh
2012/08/22 07:06:35
move install_type == to the next line
nkang
2012/08/24 22:45:26
Moved 'install_type' to the next line.
|
| + InstallationType.USER): |
| + raise RuntimeError('System level Chrome exists, aborting user level ' |
| + 'installation.') |
| + build_num = regedit.GetKeyValue(current_type, |
| + ChromeRegistryValues.PRODUCT_VERSION) |
| + # Confirm the new Chrome build is higher than the installed build. |
| + if build_num >= build: |
| + raise RuntimeError('Specify a version higher than the one already ' |
| + 'installed.') |
| + |
| + regedit = ChromeRegistryKeys() |
| + _DoPreliminaryChecks(regedit) |
| + options = '%s %s' % (options, '--install --do-not-launch-chrome') |
|
Nirnimesh
2012/08/22 07:06:35
remove the last %s and move the strings there dire
nkang
2012/08/24 22:45:26
Got rid of the last %s and moved the strings there
|
| + logging.log(logging.INFO, 'Launching Chrome installer...') |
| + args = [installer_path] |
| + args.extend(options.split(' ')) |
| + ret = subprocess.Popen(args).wait() |
|
Nirnimesh
2012/08/22 07:06:35
use a better varname.
It looks like you don't even
nkang
2012/08/24 22:45:26
Got rid of this arg. Moved the 'if' statement to t
|
| + if ret != 0: |
| + raise RuntimeError('Chrome installation for build %s failed.' % build) |
| + logging.log(logging.INFO, 'Installation complete.') |
| + return ChromeInstallation.GetCurrent() |
| + |
| + |
| +class ChromeRegistryKeys(object): |
| + """An interface for accessing and manipulating Chrome registry keys.""" |
| + |
| + _HKEY_LOCAL = r'SOFTWARE\Wow6432Node\Google\Update' |
| + _HKEY_USER = _HKEY_LOCAL.replace(r'\Wow6432Node', '') |
| + _chrome_version = r'Clients\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
| + _chrome_args = r'ClientState\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
|
Nirnimesh
2012/08/22 07:06:35
Why is this lower case, whereas line 98,99 are in
nkang
2012/08/24 22:45:26
It's just because the names HKEY_USER and HKEY_LOC
|
| + |
| + def _GetKeyName(self, install_type, value): |
| + """Gets 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_args) |
| + elif value == ChromeRegistryValues.UNINSTALL_STRING: |
| + return '%s\%s' % (key_name, self._chrome_args) |
| + raise RuntimeError('Invalid registry value.') |
| + |
| + 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 |
| + raise RuntimeError('Invalid installation type.') |
| + |
| + 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. |
| + """ |
| + key = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, subkey) |
| + try: |
| + hkey = _winreg.OpenKey(key, key_name) |
|
Nirnimesh
2012/08/22 07:06:35
only this line needs to be in the try block right?
nkang
2012/08/24 22:45:26
Done.
|
| + if not hkey.handle: |
| + return False |
| + hkey.Close() |
| + return True |
| + 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. |
| + """ |
| + reg_value = '' |
| + key = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, subkey) |
| + hkey = _winreg.OpenKey(key, key_name) |
| + if hkey.handle: |
| + reg_value = str(_winreg.QueryValueEx(hkey, subkey)[0]) |
| + hkey.Close() |
| + return reg_value |
| + |
| + def DeleteRegistryEntries(self, install_type): |
| + """Deletes chrome registry settings. |
| + |
| + Args: |
| + install_type: Type of installation, must be InstallationType type. |
| + """ |
| + reg_type = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, |
| + ChromeRegistryValues.UNINSTALL_ARGUMENTS) |
| + root = key_name[:key_name.rfind('\\')] |
| + child = key_name[key_name.rfind('\\') + 1:] |
| + key = _winreg.OpenKey(reg_type, root, 0, _winreg.KEY_ALL_ACCESS) |
| + _winreg.DeleteKey(key, child) |
| + key.Close() |
| + |
| + |
| +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 GetExePath(self): |
| + """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 (chrome_path if os.path.exists(chrome_path) else '') |
| + |
| + @staticmethod |
| + def GetCurrent(): |
| + """Determines Chrome installation type. |
| + |
| + Returns: |
| + ChromeInstallation object if Chrome is present, otherwise None. |
| + """ |
| + registry = ChromeRegistryKeys() |
| + if registry.DoesKeyExist(InstallationType.SYSTEM, |
| + ChromeRegistryValues.PRODUCT_VERSION): |
| + return ChromeInstallation(InstallationType.SYSTEM) |
| + elif registry.DoesKeyExist(InstallationType.USER, |
| + ChromeRegistryValues.PRODUCT_VERSION): |
| + return ChromeInstallation(InstallationType.USER) |
| + return None |
| + |
| + def Uninstall(self): |
| + """Uninstalls Chrome.""" |
| + chrome_path = self.GetExePath() |
| + reg_opts = self._GetUninstallArguments() |
| + uninstall_str = self._GetUninstallString() |
|
Nirnimesh
2012/08/22 07:06:35
does this not contain .exe?
nkang
2012/08/24 22:45:26
The UninstallString contains the path to the insta
|
| + 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...') |
| + cmd = '"%s" %s' % (uninstall_str, options) |
| + subprocess.Popen(cmd, shell=True).wait() |
|
Nirnimesh
2012/08/22 07:06:35
subprocess.call()
nkang
2012/08/24 22:45:26
Done.
|
| + if not os.path.exists(chrome_path): |
| + logging.log(logging.INFO, 'Chrome was uninstalled successfully...') |
|
Nirnimesh
2012/08/22 07:06:35
Instead of using logging.log(logging.INFO, ...), u
nkang
2012/08/24 22:45:26
Updated everywhere logging.log was used in chrome_
|
| + 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 |