Home | History | Annotate | Download | only in mini_installer
      1 # Copyright 2013 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Uninstalls Chrome.
      6 
      7 This script reads the uninstall command from registry, calls it, and verifies
      8 the output status code.
      9 """
     10 
     11 import _winreg
     12 import optparse
     13 import subprocess
     14 import sys
     15 
     16 
     17 def main():
     18   parser = optparse.OptionParser(description='Uninstall Chrome.')
     19   parser.add_option('--system-level', action='store_true', dest='system_level',
     20                     default=False, help='Uninstall Chrome at system level.')
     21   parser.add_option('--chrome-long-name', default='Google Chrome',
     22                     help='Google Chrome or Chromium)')
     23   parser.add_option('--interactive', action='store_true', dest='interactive',
     24                     default=False, help='Ask before uninstalling Chrome.')
     25   parser.add_option('--no-error-if-absent', action='store_true',
     26                     dest='no_error_if_absent', default=False,
     27                     help='No error if the registry key for uninstalling Chrome '
     28                          'is absent.')
     29   options, _ = parser.parse_args()
     30 
     31   # TODO(sukolsak): Add support for uninstalling MSI-based Chrome installs when
     32   # we support testing MSIs.
     33   if options.system_level:
     34     root_key = _winreg.HKEY_LOCAL_MACHINE
     35   else:
     36     root_key = _winreg.HKEY_CURRENT_USER
     37   sub_key = ('SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s' %
     38              options.chrome_long_name)
     39   # Query the key. It will throw a WindowsError if the key doesn't exist.
     40   try:
     41     key = _winreg.OpenKey(root_key, sub_key, 0, _winreg.KEY_QUERY_VALUE)
     42   except WindowsError:
     43     if options.no_error_if_absent:
     44       return 0
     45     raise KeyError('Registry key %s\\%s is missing' % (
     46         'HKEY_LOCAL_MACHINE' if options.system_level else 'HKEY_CURRENT_USER',
     47         sub_key))
     48   if options.interactive:
     49     prompt = ('Warning: This will uninstall %s at %s. Do you want to continue? '
     50               '(y/N) ' % (options.chrome_long_name,
     51                           'system-level' if
     52                               options.system_level else 'user-level'))
     53     if raw_input(prompt).strip() != 'y':
     54       print >> sys.stderr, 'User aborted'
     55       return 1
     56   uninstall_string, _ = _winreg.QueryValueEx(key, 'UninstallString')
     57   exit_status = subprocess.call(uninstall_string + ' --force-uninstall',
     58                                 shell=True)
     59   # The exit status for successful uninstallation of Chrome is 19 (see
     60   # chrome/installer/util/util_constants.h).
     61   if exit_status != 19:
     62     raise Exception('Could not uninstall Chrome. The installer exited with '
     63                     'status %d.' % exit_status)
     64   return 0
     65 
     66 
     67 if __name__ == '__main__':
     68   sys.exit(main())
     69