Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2015 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 '''Checks the status of an android SDK package. | |
| 7 | |
| 8 Verifies the given package has been installed from the Android SDK Manager and | |
| 9 that it is at an appropriate version. | |
|
cjhopman
2015/06/17 21:18:45
Clarify that we just check a minimum version.
dgn
2015/06/18 09:59:05
Done.
| |
| 10 ''' | |
| 11 | |
| 12 import argparse | |
| 13 import json | |
| 14 import os | |
| 15 import re | |
| 16 import sys | |
| 17 | |
| 18 | |
| 19 COLORAMA_ROOT = os.path.join(os.path.dirname(__file__), | |
| 20 os.pardir, 'third_party', 'colorama', 'src') | |
| 21 | |
| 22 sys.path.append(COLORAMA_ROOT) | |
| 23 import colorama | |
| 24 | |
| 25 | |
| 26 UDPATE_SCRIPT_PATH = 'build/install-android-sdks.sh' | |
| 27 | |
| 28 SDK_EXTRAS_JSON_FILE = os.path.join(os.path.dirname(__file__), | |
| 29 'android_sdk_extras.json') | |
| 30 | |
| 31 PACKAGE_VERSION_PATTERN = r'^Pkg\.Revision=(?P<version>\d+).*$' | |
| 32 | |
| 33 PKG_NOT_FOUND_MSG = ('Error while checking Android SDK extras versions. ' | |
| 34 'Could not find the "{package_id}" package in ' | |
| 35 '{checked_location}. Please run {script} to download it.') | |
| 36 UPDATE_NEEDED_MSG = ('Error while checking Android SDK extras versions. ' | |
| 37 'Version {required_version} is required for the package ' | |
|
cjhopman
2015/06/17 21:18:45
Maybe clarify that this is just a minimum version.
dgn
2015/06/18 09:59:05
Done.
| |
| 38 '"{package_id}". Version {actual_version} found. Please ' | |
| 39 'run {script} to update it.') | |
| 40 REQUIRED_VERSION_ERROR_MSG = ('Error while checking Android SDK extras ' | |
| 41 'versions. ' | |
| 42 'Could not retrieve the required version for ' | |
| 43 'package "{package_id}".') | |
| 44 | |
| 45 | |
| 46 def main(): | |
| 47 parser = argparse.ArgumentParser(description=__doc__) | |
| 48 parser.add_argument('--package-id', | |
| 49 help=('Id of the package to check for. The list of ' | |
| 50 'possible ids can be obtained by running ' | |
| 51 'third_party/android_tools/sdk/tools/android list ' | |
| 52 'sdk --extended')) | |
| 53 parser.add_argument('--package-location', | |
| 54 help='Path to the package\'s expected install location.') | |
| 55 parser.add_argument('--stamp', | |
| 56 help=('If specified, a stamp file will be created at the ' | |
| 57 'provided location.')) | |
| 58 | |
| 59 args = parser.parse_args() | |
| 60 | |
| 61 required_version = GetRequiredVersion(args.package_id) | |
|
cjhopman
2015/06/17 21:18:45
I'd call this minimum_version to make it clear tha
dgn
2015/06/18 09:59:05
Done.
| |
| 62 CheckPackageVersion(args.package_id, args.package_location, required_version) | |
| 63 | |
| 64 # Create the stamp file. | |
| 65 if args.stamp: | |
| 66 with open(args.stamp, 'a'): | |
| 67 os.utime(args.stamp, None) | |
| 68 | |
| 69 sys.exit(0) | |
| 70 | |
| 71 def ExitError(msg): | |
| 72 sys.exit(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + msg + | |
| 73 colorama.Fore.RESET) | |
| 74 | |
| 75 | |
| 76 def GetRequiredVersion(package_id): | |
| 77 with open(SDK_EXTRAS_JSON_FILE, 'r') as json_file: | |
| 78 packages = json.load(json_file) | |
| 79 | |
| 80 for package in packages: | |
| 81 if package['package_id'] == package_id: | |
| 82 return int(package['version'].split('.')[0]) | |
| 83 | |
| 84 ExitError(REQUIRED_VERSION_ERROR_MSG.format(package_id=package_id)) | |
| 85 | |
| 86 | |
| 87 def CheckPackageVersion(pkg_id, location, required_version): | |
| 88 version_file_path = os.path.join(location, 'source.properties') | |
| 89 # Extracts the version of the package described by the property file. We only | |
| 90 # care about the major version number here. | |
| 91 version_pattern = re.compile(PACKAGE_VERSION_PATTERN, re.MULTILINE) | |
| 92 | |
| 93 if not os.path.isfile(version_file_path): | |
| 94 ExitError(PKG_NOT_FOUND_MSG.format( | |
| 95 package_id=pkg_id, | |
| 96 checked_location=location, | |
| 97 script=UDPATE_SCRIPT_PATH)) | |
| 98 | |
| 99 with open(version_file_path, 'r') as f: | |
| 100 match = version_pattern.search(f.read()) | |
| 101 | |
| 102 if not match: | |
| 103 ExitError(PKG_NOT_FOUND_MSG.format( | |
| 104 package_id=pkg_id, | |
| 105 checked_location=location, | |
| 106 script=UDPATE_SCRIPT_PATH)) | |
| 107 | |
| 108 pkg_version = int(match.group('version')) | |
| 109 if pkg_version < required_version: | |
| 110 ExitError(UPDATE_NEEDED_MSG.format( | |
| 111 package_id=pkg_id, | |
| 112 required_version=required_version, | |
| 113 actual_version=pkg_version, | |
| 114 script=UDPATE_SCRIPT_PATH)) | |
| 115 | |
| 116 # Everything looks ok, print nothing. | |
| 117 | |
| 118 | |
| 119 if __name__ == '__main__': | |
| 120 main() | |
| OLD | NEW |