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,280 @@ |
| +#!/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.""" |
|
Nirnimesh
2012/08/09 21:32:48
Is this supposed to be for win only? If so, mentio
nkang
2012/08/16 23:46:24
Added a line that mentions that currently the only
|
| + |
| +import _winreg |
|
Nirnimesh
2012/08/09 21:32:48
will fail on non-win
nkang
2012/08/16 23:46:24
Previously I had a check in here that only importe
Nirnimesh
2012/08/22 07:06:34
You know that it's supposed to be used on win beca
|
| +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: |
|
Nirnimesh
2012/08/09 21:32:48
inherit from object
nkang
2012/08/16 23:46:24
Done.
|
| + """Defines the Chrome installation types.""" |
| + SYSTEM = 0 |
| + USER = 1 |
| + |
| + |
| +class ChromeRegistryValues: |
|
Nirnimesh
2012/08/09 21:32:48
inherit from object
nkang
2012/08/16 23:46:24
Done.
|
| + """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. |
| + |
| + Returns: |
| + A ChromeInstallation object. |
|
Nirnimesh
2012/08/09 21:32:48
an instance of ChromeInstallation.
nkang
2012/08/16 23:46:24
Done.
|
| + """ |
| + def DoPreliminaryChecks(regedit): |
|
Nirnimesh
2012/08/09 21:32:48
prefix _
nkang
2012/08/16 23:46:24
Done.
|
| + """Validates the test parameters and Chrome version. |
|
Nirnimesh
2012/08/09 21:32:48
Be more specific. Call out exactly what it does.
nkang
2012/08/16 23:46:24
Updated the docstring, so its more descriptive.
|
| + |
| + Args: |
| + regedit: ChromeRegistryKeys object. |
| + """ |
| + assert(os.path.isfile(installer_path)) |
|
Nirnimesh
2012/08/09 21:32:48
why?
nkang
2012/08/16 23:46:24
The installer was initially downloaded by a class
|
| + 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/09 21:32:48
remove "!= None"
nkang
2012/08/16 23:46:24
I explicitly added '!= None' because if a Chrome v
Nirnimesh
2012/08/22 07:06:34
That sounds bizarre. Why not add another installat
nkang
2012/08/24 22:45:26
Added a third variable called NOT_INSTALLED and se
|
| + # Make sure new build can be installed over existing Chrome build. |
| + if (current_type == InstallationType.SYSTEM and install_type == |
| + InstallationType.USER): |
| + raise RuntimeError('System level Chrome exists, aborting user level ' |
| + 'installation.') |
| + build_num = regedit.GetKeyValue(current_type, |
| + ChromeRegistryValues.PRODUCT_VERSION) |
| + # Confirm that the new Chrome build is higher than the installed build. |
| + if build_num >= build: |
| + raise RuntimeError('Please specify a version higher than the one ' |
|
Nirnimesh
2012/08/09 21:32:48
Remove "Please"
nkang
2012/08/16 23:46:24
Sorry, I was just trying to be courteous. But, res
|
| + 'already installed.') |
| + |
| + regedit = ChromeRegistryKeys() |
| + DoPreliminaryChecks(regedit) |
| + options += ' --install --do-not-launch-chrome' |
| + logging.log(logging.INFO, 'Launching Chrome installer...') |
| + cmd = '%s %s' % (installer_path, options) |
|
Nirnimesh
2012/08/09 21:32:48
either use + to join strings (line 76), or use thi
nkang
2012/08/16 23:46:24
Got rid of the +=. I now use string formatting to
|
| + ret = subprocess.Popen(cmd, shell=True).wait() |
|
Nirnimesh
2012/08/09 21:32:48
make cmd a list. remove shell=True
nkang
2012/08/16 23:46:24
Made cmd a list and got rid of shell=True.
|
| + if ret == 0: |
|
Nirnimesh
2012/08/09 21:32:48
Invert the logic. Raise first if ret != 0
nkang
2012/08/16 23:46:24
Inverted the logic. An exception is now raised if
|
| + logging.log(logging.INFO, 'Installation complete.') |
| + return ChromeInstallation.GetCurrent() |
| + 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', '') |
|
Nirnimesh
2012/08/09 21:32:48
either use r'..' or \\ thoughout.
nkang
2012/08/16 23:46:24
Changed to r'..'.
|
| + _chrome_version = r'Clients\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
| + _chrome_args = r'ClientState\{8A69D345-D564-463C-AFF1-A69D9E530F96}' |
| + |
| + def _GetKeyName(self, install_type, value): |
| + """Generates the registry key name for the specified value. |
|
Nirnimesh
2012/08/09 21:32:48
Generate -> Get
nkang
2012/08/16 23:46:24
Done.
|
| + |
| + 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. |
| + """ |
| + b_exists = False |
|
Nirnimesh
2012/08/09 21:32:48
remove "b_"
nkang
2012/08/16 23:46:24
Got rid of the boolean var, so this no longer appl
|
| + key = self._GetRegistryType(install_type) |
| + key_name = self._GetKeyName(install_type, subkey) |
| + try: |
| + hkey = _winreg.OpenKey(key, key_name) |
| + if hkey.handle: |
| + b_exists = True |
| + hkey.Close() |
|
Nirnimesh
2012/08/09 21:32:48
If you just return True from here, you don't even
nkang
2012/08/16 23:46:24
Done. Got rid of the boolean var.
|
| + 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. |
| + """ |
| + 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', |
|
Nirnimesh
2012/08/09 21:32:48
What about Chromium?
nkang
2012/08/16 23:46:24
Per Anantha, we will not be using Chromium.
Nirnimesh
2012/08/22 07:06:34
Do you declare this somewhere in the docs?
nkang
2012/08/24 22:45:26
I checked with Ken on this, and he advised to decl
|
| + '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() |
| + 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() |
| + 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 |