| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Script to download sdk/extras packages on the bots from google storage. | |
| 7 | |
| 8 The script expects arguments that specify zips file in the google storage | |
| 9 bucket named: <dir in SDK extras>_<package name>_<version>.zip. The file will | |
| 10 be extracted in the android_tools/sdk/extras directory on the test bots. This | |
| 11 script will not do anything for developers. | |
| 12 | |
| 13 TODO(navabi): Move this script (crbug.com/459819). | |
| 14 """ | |
| 15 | |
| 16 import find_depot_tools | |
| 17 import json | |
| 18 import os | |
| 19 import shutil | |
| 20 import subprocess | |
| 21 import sys | |
| 22 import zipfile | |
| 23 | |
| 24 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) | |
| 25 CHROME_SRC = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir)) | |
| 26 sys.path.insert(0, os.path.join(SCRIPT_DIR, 'android')) | |
| 27 | |
| 28 from pylib import constants | |
| 29 | |
| 30 DEPOT_PATH = find_depot_tools.add_depot_tools_to_path() | |
| 31 GSUTIL_PATH = os.path.join(DEPOT_PATH, 'gsutil.py') | |
| 32 SDK_EXTRAS_BUCKET = 'gs://chrome-sdk-extras' | |
| 33 SDK_EXTRAS_PATH = os.path.join(constants.ANDROID_SDK_ROOT, 'extras') | |
| 34 SDK_EXTRAS_JSON_FILE = os.path.join(os.path.dirname(__file__), | |
| 35 'android_sdk_extras.json') | |
| 36 | |
| 37 | |
| 38 def clean_and_extract(dir_name, package_name, zip_file): | |
| 39 local_dir = '%s/%s/%s' % (SDK_EXTRAS_PATH, dir_name, package_name) | |
| 40 if os.path.exists(local_dir): | |
| 41 shutil.rmtree(local_dir) | |
| 42 local_zip = '%s/%s' % (SDK_EXTRAS_PATH, zip_file) | |
| 43 with zipfile.ZipFile(local_zip) as z: | |
| 44 z.extractall(path=SDK_EXTRAS_PATH) | |
| 45 | |
| 46 | |
| 47 def download_package_if_needed(remote_file, local_file): | |
| 48 """Download a file from GCS. | |
| 49 | |
| 50 Returns: | |
| 51 success (bool): True if the download succeeded, False otherwise. | |
| 52 """ | |
| 53 if not os.path.exists(local_file): | |
| 54 try: | |
| 55 subprocess.check_call(['python', GSUTIL_PATH, '--force-version', '4.7', | |
| 56 'cp', remote_file, local_file]) | |
| 57 except subprocess.CalledProcessError: | |
| 58 print ('WARNING: Failed to download SDK packages. If this bot compiles ' | |
| 59 'for Android, it may have errors.') | |
| 60 return False | |
| 61 return True | |
| 62 | |
| 63 def main(): | |
| 64 if not os.environ.get('CHROME_HEADLESS'): | |
| 65 # This is not a buildbot checkout. | |
| 66 return 0 | |
| 67 # Update the android_sdk_extras.json file to update downloaded packages. | |
| 68 with open(SDK_EXTRAS_JSON_FILE) as json_file: | |
| 69 packages = json.load(json_file) | |
| 70 for package in packages: | |
| 71 local_zip = '%s/%s' % (SDK_EXTRAS_PATH, package['zip']) | |
| 72 package_zip = '%s/%s' % (SDK_EXTRAS_BUCKET, package['zip']) | |
| 73 for attempt in xrange(2): | |
| 74 print '(%d) Downloading package %s' % (attempt + 1, package['zip']) | |
| 75 if not download_package_if_needed(package_zip, local_zip): | |
| 76 # Ignore errors when download failed to keep the corresponding build | |
| 77 # step green. The error we're ignoring here is essentially | |
| 78 # 'permission denied', because we're using the presence or absence of | |
| 79 # credentials on a build machine as the way to mark android builders. | |
| 80 # See crbug.com/460463 for more context. | |
| 81 return 0 | |
| 82 try: | |
| 83 # Always clean dir and extract zip to ensure correct contents. | |
| 84 clean_and_extract(package['dir_name'], | |
| 85 package['package'], | |
| 86 package['zip']) | |
| 87 break | |
| 88 except zipfile.BadZipfile: | |
| 89 print 'Failed unpacking zip file. Deleting and retrying...' | |
| 90 os.remove(local_zip) | |
| 91 | |
| 92 else: | |
| 93 print ('WARNING: Failed to unpack SDK packages. If this bot compiles ' | |
| 94 'for Android, it may have errors.') | |
| 95 return 1 | |
| 96 | |
| 97 | |
| 98 if __name__ == '__main__': | |
| 99 sys.exit(main()) | |
| OLD | NEW |