예제 #1
0
def _CheckLicenseAgreement(expected_license_path, actual_license_path,
                           version_number):
  '''
  Checks that the new license is the one already accepted by the user. If it
  isn't, it prompts the user to accept it. Returns whether the expected license
  has been accepted.
  '''

  if utils.FileEquals(expected_license_path, actual_license_path):
    return True

  with open(expected_license_path) as license_file:
    # Uses plain print rather than logging to make sure this is not formatted
    # by the logger.
    print ('Updating the Google Play services SDK to '
           'version %s.' % version_number)

    # The output is buffered when running as part of gclient hooks. We split
    # the text here and flush is explicitly to avoid having part of it dropped
    # out.
    # Note: text contains *escaped* new lines, so we split by '\\n', not '\n'.
    for license_part in license_file.read().split('\\n'):
      print license_part
      sys.stdout.flush()

  # Need to put the prompt on a separate line otherwise the gclient hook buffer
  # only prints it after we received an input.
  print ('Do you accept the license for version %s of the Google Play services '
         'client library? [y/n]: ' % version_number)
  sys.stdout.flush()
  return raw_input('> ') in ('Y', 'y')
예제 #2
0
def Download(args):
  '''
  Downloads the Google Play services library from a Google Cloud Storage bucket
  and installs it to
  //third_party/android_tools/sdk/extras/google/m2repository.

  A license check will be made, and the user might have to accept the license
  if that has not been done before.
  '''

  if not os.path.isdir(args.sdk_root):
    logging.debug('Did not find the Android SDK root directory at "%s".',
                  args.sdk_root)
    if not args.force:
      logging.info('Skipping, not on an android checkout.')
      return 0

  config = utils.ConfigParser(args.config)
  paths = PlayServicesPaths(args.sdk_root, config.version_number,
                            config.clients)

  if os.path.isdir(paths.package) and not os.access(paths.package, os.W_OK):
    logging.error('Failed updating the Google Play Services library. '
                  'The location is not writable. Please remove the '
                  'directory (%s) and try again.', paths.package)
    return -2

  new_lib_zip_sha1 = os.path.join(SHA1_DIRECTORY, ZIP_FILE_NAME + '.sha1')

  logging.debug('Comparing zip hashes: %s and %s', new_lib_zip_sha1,
                paths.lib_zip_sha1)
  if utils.FileEquals(new_lib_zip_sha1, paths.lib_zip_sha1) and not args.force:
    logging.info('Skipping, the Google Play services library is up to date.')
    return 0

  bucket_path = _VerifyBucketPathFormat(args.bucket,
                                        config.version_number,
                                        args.dry_run)

  tmp_root = tempfile.mkdtemp()
  try:
    # setup the destination directory
    if not os.path.isdir(paths.package):
      os.makedirs(paths.package)

    # download license file from bucket/{version_number}/license.sha1
    new_license = os.path.join(tmp_root, LICENSE_FILE_NAME)

    license_sha1 = os.path.join(SHA1_DIRECTORY, LICENSE_FILE_NAME + '.sha1')
    _DownloadFromBucket(bucket_path, license_sha1, new_license,
                        args.verbose, args.dry_run)

    if (not _IsBotEnvironment() and
        not _CheckLicenseAgreement(new_license, paths.license,
                                   config.version_number)):
        logging.warning('Your version of the Google Play services library is '
                        'not up to date. You might run into issues building '
                        'or running the app. Please run `%s download` to '
                        'retry downloading it.', __file__)
        return 0

    new_lib_zip = os.path.join(tmp_root, ZIP_FILE_NAME)
    _DownloadFromBucket(bucket_path, new_lib_zip_sha1, new_lib_zip,
                        args.verbose, args.dry_run)

    try:
      # Remove the deprecated sdk directory.
      deprecated_package_path = os.path.join(args.sdk_root, 'extras', 'google',
                                             'google_play_services')
      if os.path.exists(deprecated_package_path):
        shutil.rmtree(deprecated_package_path)

      # We remove the current version of the Google Play services SDK.
      if os.path.exists(paths.package):
        shutil.rmtree(paths.package)
      os.makedirs(paths.package)

      logging.debug('Extracting the library to %s', paths.package)
      with zipfile.ZipFile(new_lib_zip, "r") as new_lib_zip_file:
        new_lib_zip_file.extractall(paths.package)

      logging.debug('Copying %s to %s', new_license, paths.license)
      shutil.copy(new_license, paths.license)

      logging.debug('Copying %s to %s', new_lib_zip_sha1, paths.lib_zip_sha1)
      shutil.copy(new_lib_zip_sha1, paths.lib_zip_sha1)

      logging.info('Update complete.')

    except Exception as e:  # pylint: disable=broad-except
      logging.error('Failed updating the Google Play Services library. '
                    'An error occurred while installing the new version in '
                    'the SDK directory: %s ', e)
      return -3
  finally:
    shutil.rmtree(tmp_root)

  return 0
예제 #3
0
def Download(args):
    '''
  Downloads the Google Play services client library from a Google Cloud Storage
  bucket and installs it to
  //third_party/android_tools/sdk/extras/google/google_play_services.

  A license check will be made, and the user might have to accept the license
  if that has not been done before.
  '''

    paths = _InitPaths(constants.ANDROID_SDK_ROOT)

    new_lib_zip_sha1 = os.path.join(SHA1_DIRECTORY,
                                    LIBRARY_FILE_NAME + '.sha1')
    old_lib_zip_sha1 = os.path.join(paths.package, LIBRARY_FILE_NAME + '.sha1')

    logging.debug('Comparing library hashes: %s and %s', new_lib_zip_sha1,
                  old_lib_zip_sha1)
    if utils.FileEquals(new_lib_zip_sha1, old_lib_zip_sha1) and not args.force:
        logging.debug('The Google Play services library is up to date.')
        return 0

    config = utils.ConfigParser(args.config)
    bucket_path = _VerifyBucketPathFormat(args.bucket, config.version_number,
                                          args.dry_run)

    tmp_root = tempfile.mkdtemp()
    try:
        if not os.environ.get('CHROME_HEADLESS'):
            if not os.path.isdir(paths.package):
                os.makedirs(paths.package)

            # download license file from bucket/{version_number}/license.sha1
            new_license = os.path.join(tmp_root, LICENSE_FILE_NAME)
            old_license = os.path.join(paths.package, LICENSE_FILE_NAME)

            license_sha1 = os.path.join(SHA1_DIRECTORY,
                                        LICENSE_FILE_NAME + '.sha1')
            _DownloadFromBucket(bucket_path, license_sha1, new_license,
                                args.verbose, args.dry_run)
            if not _CheckLicenseAgreement(new_license, old_license):
                logging.warning(
                    'Your version of the Google Play services library is '
                    'not up to date. You might run into issues building '
                    'or running the app. Please run `%s download` to '
                    'retry downloading it.', __file__)
                return 0

        new_lib_zip = os.path.join(tmp_root, LIBRARY_FILE_NAME)
        _DownloadFromBucket(bucket_path, new_lib_zip_sha1, new_lib_zip,
                            args.verbose, args.dry_run)

        # We remove only the library itself. Users having a SDK manager installed
        # library before will keep the documentation and samples from it.
        shutil.rmtree(paths.lib, ignore_errors=True)
        os.makedirs(paths.lib)

        logging.debug('Extracting the library to %s', paths.lib)
        with zipfile.ZipFile(new_lib_zip, "r") as new_lib_zip_file:
            new_lib_zip_file.extractall(paths.lib)

        logging.debug('Copying %s to %s', new_license, old_license)
        shutil.copy(new_license, old_license)

        logging.debug('Copying %s to %s', new_lib_zip_sha1, old_lib_zip_sha1)
        shutil.copy(new_lib_zip_sha1, old_lib_zip_sha1)

    finally:
        shutil.rmtree(tmp_root)

    return 0