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. |
| 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 PKG_NOT_FOUND_MSG = ('Error while checking Android SDK extras versions. ' |
| 32 'Could not find the "{package_id}" package in ' |
| 33 '{checked_location}. Please run {script} to download it.') |
| 34 UPDATE_NEEDED_MSG = ('Error while checking Android SDK extras versions. ' |
| 35 'Version {required_version} is required for the package ' |
| 36 '"{package_id}". Version {actual_version} found. Please ' |
| 37 'run {script} to update it.') |
| 38 REQUIRED_VERSION_ERROR_MSG = ('Error while checking Android SDK extras ' |
| 39 'versions. ' |
| 40 'Could not retrieve the required version for ' |
| 41 'package "{package_id}".') |
| 42 |
| 43 |
| 44 def ExitError(msg): |
| 45 sys.exit(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + msg + |
| 46 colorama.Fore.RESET) |
| 47 |
| 48 |
| 49 def GetRequiredVersion(package_id): |
| 50 with open(SDK_EXTRAS_JSON_FILE, 'r') as json_file: |
| 51 packages = json.load(json_file) |
| 52 |
| 53 for package in packages: |
| 54 if package['package_id'] == package_id: |
| 55 return int(package['version'].split('.')[0]) |
| 56 |
| 57 ExitError(REQUIRED_VERSION_ERROR_MSG.format(package_id=package_id)) |
| 58 |
| 59 |
| 60 def CheckPackageVersion(pkg_id, location, required_version): |
| 61 version_file_path = os.path.join(location, 'source.properties') |
| 62 # Extracts the version of the package described by the property file. We only |
| 63 # care about the major version number here. |
| 64 version_pattern = re.compile(r'^Pkg\.Revision=(?P<version>\d+).*$', |
| 65 re.MULTILINE) |
| 66 |
| 67 if not os.path.isdir(location) or not os.path.isfile(version_file_path): |
| 68 ExitError(PKG_NOT_FOUND_MSG.format( |
| 69 package_id=pkg_id, |
| 70 checked_location=location, |
| 71 script=UDPATE_SCRIPT_PATH)) |
| 72 |
| 73 with open(version_file_path, 'r') as f: |
| 74 match = version_pattern.search(f.read()) |
| 75 |
| 76 if not match: |
| 77 ExitError(PKG_NOT_FOUND_MSG.format( |
| 78 package_id=pkg_id, |
| 79 checked_location=location, |
| 80 script=UDPATE_SCRIPT_PATH)) |
| 81 |
| 82 pkg_version = int(match.group('version')) |
| 83 if pkg_version < required_version: |
| 84 ExitError(UPDATE_NEEDED_MSG.format( |
| 85 package_id=pkg_id, |
| 86 required_version=required_version, |
| 87 actual_version=pkg_version, |
| 88 script=UDPATE_SCRIPT_PATH)) |
| 89 |
| 90 # Everything looks ok, print nothing. |
| 91 |
| 92 |
| 93 if __name__ == '__main__': |
| 94 parser = argparse.ArgumentParser(description=__doc__) |
| 95 parser.add_argument('--package-id', |
| 96 help=('Id of the package to check for. The list of ' |
| 97 'possible ids can be obtained by running ' |
| 98 'third_party/android_tools/sdk/tools/android list ' |
| 99 'sdk --extended')) |
| 100 parser.add_argument('--package-location', |
| 101 help='Path to the package\'s expected install location.') |
| 102 parser.add_argument('--stamp', |
| 103 help=('If specified, a stamp file will be created at the ' |
| 104 'provided location.')) |
| 105 |
| 106 args = parser.parse_args() |
| 107 |
| 108 required_version = GetRequiredVersion(args.package_id) |
| 109 CheckPackageVersion(args.package_id, args.package_location, required_version) |
| 110 |
| 111 # Create the stamp file. |
| 112 if args.stamp: |
| 113 with open(args.stamp, 'a'): |
| 114 os.utime(args.stamp, None) |
| 115 |
| 116 sys.exit(0) |
OLD | NEW |