Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / build / check_sdk_extras_version.py
blob9b2f10d2abc038940bd48b7aa8c2f740c652e1bb
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.
6 '''Checks the status of an Android SDK package.
8 Verifies the given package has been installed from the Android SDK Manager and
9 that its version is at least the minimum version required by the project
10 configuration.
11 '''
13 import argparse
14 import json
15 import os
16 import re
17 import sys
20 COLORAMA_ROOT = os.path.join(os.path.dirname(__file__),
21 os.pardir, 'third_party', 'colorama', 'src')
23 sys.path.append(COLORAMA_ROOT)
24 import colorama
27 UDPATE_SCRIPT_PATH = 'build/install-android-sdks.sh'
29 SDK_EXTRAS_JSON_FILE = os.path.join(os.path.dirname(__file__),
30 'android_sdk_extras.json')
32 PACKAGE_VERSION_PATTERN = r'^Pkg\.Revision=(?P<version>\d+).*$'
34 PKG_NOT_FOUND_MSG = ('Error while checking Android SDK extras versions. '
35 'Could not find the "{package_id}" package in '
36 '{checked_location}. Please run {script} to download it.')
37 UPDATE_NEEDED_MSG = ('Error while checking Android SDK extras versions. '
38 'Version {minimum_version} or greater is required for the '
39 'package "{package_id}". Version {actual_version} found. '
40 'Please run {script} to update it.')
41 REQUIRED_VERSION_ERROR_MSG = ('Error while checking Android SDK extras '
42 'versions. '
43 'Could not retrieve the required version for '
44 'package "{package_id}".')
47 def main():
48 parser = argparse.ArgumentParser(description=__doc__)
49 parser.add_argument('--package-id',
50 help=('id of the package to check for. The list of '
51 'available packages and their ids can be obtained '
52 'by running '
53 'third_party/android_tools/sdk/tools/android list '
54 'sdk --extended'))
55 parser.add_argument('--package-location',
56 help='path to the package\'s expected install location.',
57 metavar='DIR')
58 parser.add_argument('--stamp',
59 help=('if specified, a stamp file will be created at the '
60 'provided location.'),
61 metavar='FILE')
63 args = parser.parse_args()
65 if not ShouldSkipVersionCheck():
66 minimum_version = GetRequiredMinimumVersion(args.package_id)
67 CheckPackageVersion(args.package_id, args.package_location, minimum_version)
69 # Create the stamp file.
70 if args.stamp:
71 with open(args.stamp, 'a'):
72 os.utime(args.stamp, None)
74 sys.exit(0)
76 def ExitError(msg):
77 sys.exit(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + msg +
78 colorama.Style.RESET_ALL)
81 def GetRequiredMinimumVersion(package_id):
82 with open(SDK_EXTRAS_JSON_FILE, 'r') as json_file:
83 packages = json.load(json_file)
85 for package in packages:
86 if package['package_id'] == package_id:
87 return int(package['version'].split('.')[0])
89 ExitError(REQUIRED_VERSION_ERROR_MSG.format(package_id=package_id))
92 def CheckPackageVersion(pkg_id, location, minimum_version):
93 version_file_path = os.path.join(location, 'source.properties')
94 # Extracts the version of the package described by the property file. We only
95 # care about the major version number here.
96 version_pattern = re.compile(PACKAGE_VERSION_PATTERN, re.MULTILINE)
98 if not os.path.isfile(version_file_path):
99 ExitError(PKG_NOT_FOUND_MSG.format(
100 package_id=pkg_id,
101 checked_location=location,
102 script=UDPATE_SCRIPT_PATH))
104 with open(version_file_path, 'r') as f:
105 match = version_pattern.search(f.read())
107 if not match:
108 ExitError(PKG_NOT_FOUND_MSG.format(
109 package_id=pkg_id,
110 checked_location=location,
111 script=UDPATE_SCRIPT_PATH))
113 pkg_version = int(match.group('version'))
114 if pkg_version < minimum_version:
115 ExitError(UPDATE_NEEDED_MSG.format(
116 package_id=pkg_id,
117 minimum_version=minimum_version,
118 actual_version=pkg_version,
119 script=UDPATE_SCRIPT_PATH))
121 # Everything looks ok, print nothing.
123 def ShouldSkipVersionCheck():
125 Bots should not run the version check, since they download the sdk extras
126 in a different way.
128 return bool(os.environ.get('CHROME_HEADLESS'))
130 if __name__ == '__main__':
131 main()