blob: 7488f8b940d787853b561a2633a4f1dbae02aafc [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
65 minimum_version = GetRequiredMinimumVersion(args.package_id)
66 CheckPackageVersion(args.package_id, args.package_location, minimum_version)
67
68 # Create the stamp file.
69 if args.stamp:
70 with open(args.stamp, 'a'):
71 os.utime(args.stamp, None)
72
73 sys.exit(0)
74
75def ExitError(msg):
76 sys.exit(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + msg +
77 colorama.Fore.RESET)
78
79
80def GetRequiredMinimumVersion(package_id):
81 with open(SDK_EXTRAS_JSON_FILE, 'r') as json_file:
82 packages = json.load(json_file)
83
84 for package in packages:
85 if package['package_id'] == package_id:
86 return int(package['version'].split('.')[0])
87
88 ExitError(REQUIRED_VERSION_ERROR_MSG.format(package_id=package_id))
89
90
91def CheckPackageVersion(pkg_id, location, minimum_version):
92 version_file_path = os.path.join(location, 'source.properties')
93 # Extracts the version of the package described by the property file. We only
94 # care about the major version number here.
95 version_pattern = re.compile(PACKAGE_VERSION_PATTERN, re.MULTILINE)
96
97 if not os.path.isfile(version_file_path):
98 ExitError(PKG_NOT_FOUND_MSG.format(
99 package_id=pkg_id,
100 checked_location=location,
101 script=UDPATE_SCRIPT_PATH))
102
103 with open(version_file_path, 'r') as f:
104 match = version_pattern.search(f.read())
105
106 if not match:
107 ExitError(PKG_NOT_FOUND_MSG.format(
108 package_id=pkg_id,
109 checked_location=location,
110 script=UDPATE_SCRIPT_PATH))
111
112 pkg_version = int(match.group('version'))
113 if pkg_version < minimum_version:
114 ExitError(UPDATE_NEEDED_MSG.format(
115 package_id=pkg_id,
116 minimum_version=minimum_version,
117 actual_version=pkg_version,
118 script=UDPATE_SCRIPT_PATH))
119
120 # Everything looks ok, print nothing.
121
122
123if __name__ == '__main__':
124 main()