blob: 9b2f10d2abc038940bd48b7aa8c2f740c652e1bb [file] [log] [blame]
dgn1ad35c82015-06-26 19:30:05 +09001#!/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
8Verifies the given package has been installed from the Android SDK Manager and
9that its version is at least the minimum version required by the project
10configuration.
11'''
12
13import argparse
14import json
15import os
16import re
17import sys
18
19
20COLORAMA_ROOT = os.path.join(os.path.dirname(__file__),
21 os.pardir, 'third_party', 'colorama', 'src')
22
23sys.path.append(COLORAMA_ROOT)
24import colorama
25
26
27UDPATE_SCRIPT_PATH = 'build/install-android-sdks.sh'
28
29SDK_EXTRAS_JSON_FILE = os.path.join(os.path.dirname(__file__),
30 'android_sdk_extras.json')
31
32PACKAGE_VERSION_PATTERN = r'^Pkg\.Revision=(?P<version>\d+).*$'
33
34PKG_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.')
37UPDATE_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.')
41REQUIRED_VERSION_ERROR_MSG = ('Error while checking Android SDK extras '
42 'versions. '
43 'Could not retrieve the required version for '
44 'package "{package_id}".')
45
46
47def 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')
62
63 args = parser.parse_args()
64
dgnae8a1d72015-07-10 17:49:13 +090065 if not ShouldSkipVersionCheck():
66 minimum_version = GetRequiredMinimumVersion(args.package_id)
67 CheckPackageVersion(args.package_id, args.package_location, minimum_version)
dgn1ad35c82015-06-26 19:30:05 +090068
69 # Create the stamp file.
70 if args.stamp:
71 with open(args.stamp, 'a'):
72 os.utime(args.stamp, None)
73
74 sys.exit(0)
75
76def ExitError(msg):
77 sys.exit(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + msg +
dgnae8a1d72015-07-10 17:49:13 +090078 colorama.Style.RESET_ALL)
dgn1ad35c82015-06-26 19:30:05 +090079
80
81def GetRequiredMinimumVersion(package_id):
82 with open(SDK_EXTRAS_JSON_FILE, 'r') as json_file:
83 packages = json.load(json_file)
84
85 for package in packages:
86 if package['package_id'] == package_id:
87 return int(package['version'].split('.')[0])
88
89 ExitError(REQUIRED_VERSION_ERROR_MSG.format(package_id=package_id))
90
91
92def 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)
97
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))
103
104 with open(version_file_path, 'r') as f:
105 match = version_pattern.search(f.read())
106
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))
112
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))
120
121 # Everything looks ok, print nothing.
122
dgnae8a1d72015-07-10 17:49:13 +0900123def ShouldSkipVersionCheck():
124 '''
125 Bots should not run the version check, since they download the sdk extras
126 in a different way.
127 '''
128 return bool(os.environ.get('CHROME_HEADLESS'))
dgn1ad35c82015-06-26 19:30:05 +0900129
130if __name__ == '__main__':
131 main()