Chromium Code Reviews| Index: build/check_sdk_extras_version.py |
| diff --git a/build/check_sdk_extras_version.py b/build/check_sdk_extras_version.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..f6d61f5549d45766f41373c983947c1a965e22b6 |
| --- /dev/null |
| +++ b/build/check_sdk_extras_version.py |
| @@ -0,0 +1,120 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +'''Checks the status of an android SDK package. |
| + |
| +Verifies the given package has been installed from the Android SDK Manager and |
| +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.
|
| +''' |
| + |
| +import argparse |
| +import json |
| +import os |
| +import re |
| +import sys |
| + |
| + |
| +COLORAMA_ROOT = os.path.join(os.path.dirname(__file__), |
| + os.pardir, 'third_party', 'colorama', 'src') |
| + |
| +sys.path.append(COLORAMA_ROOT) |
| +import colorama |
| + |
| + |
| +UDPATE_SCRIPT_PATH = 'build/install-android-sdks.sh' |
| + |
| +SDK_EXTRAS_JSON_FILE = os.path.join(os.path.dirname(__file__), |
| + 'android_sdk_extras.json') |
| + |
| +PACKAGE_VERSION_PATTERN = r'^Pkg\.Revision=(?P<version>\d+).*$' |
| + |
| +PKG_NOT_FOUND_MSG = ('Error while checking Android SDK extras versions. ' |
| + 'Could not find the "{package_id}" package in ' |
| + '{checked_location}. Please run {script} to download it.') |
| +UPDATE_NEEDED_MSG = ('Error while checking Android SDK extras versions. ' |
| + '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.
|
| + '"{package_id}". Version {actual_version} found. Please ' |
| + 'run {script} to update it.') |
| +REQUIRED_VERSION_ERROR_MSG = ('Error while checking Android SDK extras ' |
| + 'versions. ' |
| + 'Could not retrieve the required version for ' |
| + 'package "{package_id}".') |
| + |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser(description=__doc__) |
| + parser.add_argument('--package-id', |
| + help=('Id of the package to check for. The list of ' |
| + 'possible ids can be obtained by running ' |
| + 'third_party/android_tools/sdk/tools/android list ' |
| + 'sdk --extended')) |
| + parser.add_argument('--package-location', |
| + help='Path to the package\'s expected install location.') |
| + parser.add_argument('--stamp', |
| + help=('If specified, a stamp file will be created at the ' |
| + 'provided location.')) |
| + |
| + args = parser.parse_args() |
| + |
| + 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.
|
| + CheckPackageVersion(args.package_id, args.package_location, required_version) |
| + |
| + # Create the stamp file. |
| + if args.stamp: |
| + with open(args.stamp, 'a'): |
| + os.utime(args.stamp, None) |
| + |
| + sys.exit(0) |
| + |
| +def ExitError(msg): |
| + sys.exit(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + msg + |
| + colorama.Fore.RESET) |
| + |
| + |
| +def GetRequiredVersion(package_id): |
| + with open(SDK_EXTRAS_JSON_FILE, 'r') as json_file: |
| + packages = json.load(json_file) |
| + |
| + for package in packages: |
| + if package['package_id'] == package_id: |
| + return int(package['version'].split('.')[0]) |
| + |
| + ExitError(REQUIRED_VERSION_ERROR_MSG.format(package_id=package_id)) |
| + |
| + |
| +def CheckPackageVersion(pkg_id, location, required_version): |
| + version_file_path = os.path.join(location, 'source.properties') |
| + # Extracts the version of the package described by the property file. We only |
| + # care about the major version number here. |
| + version_pattern = re.compile(PACKAGE_VERSION_PATTERN, re.MULTILINE) |
| + |
| + if not os.path.isfile(version_file_path): |
| + ExitError(PKG_NOT_FOUND_MSG.format( |
| + package_id=pkg_id, |
| + checked_location=location, |
| + script=UDPATE_SCRIPT_PATH)) |
| + |
| + with open(version_file_path, 'r') as f: |
| + match = version_pattern.search(f.read()) |
| + |
| + if not match: |
| + ExitError(PKG_NOT_FOUND_MSG.format( |
| + package_id=pkg_id, |
| + checked_location=location, |
| + script=UDPATE_SCRIPT_PATH)) |
| + |
| + pkg_version = int(match.group('version')) |
| + if pkg_version < required_version: |
| + ExitError(UPDATE_NEEDED_MSG.format( |
| + package_id=pkg_id, |
| + required_version=required_version, |
| + actual_version=pkg_version, |
| + script=UDPATE_SCRIPT_PATH)) |
| + |
| + # Everything looks ok, print nothing. |
| + |
| + |
| +if __name__ == '__main__': |
| + main() |