OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Gathers information about APKs.""" | 5 """Gathers information about JARs such as test methods and annotations.""" |
6 | 6 |
7 import collections | 7 import collections |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import pickle | 10 import pickle |
11 import re | 11 import re |
12 | 12 |
13 import cmd_helper | 13 from pylib import cmd_helper |
craigdh
2013/01/04 02:00:32
ditto
| |
14 | 14 |
15 # If you change the cached output of proguard, increment this number | 15 # If you change the cached output of proguard, increment this number |
16 PICKLE_FORMAT_VERSION = 1 | 16 PICKLE_FORMAT_VERSION = 1 |
17 | 17 |
18 def GetPackageNameForApk(apk_path): | |
19 """Returns the package name of the apk file.""" | |
20 aapt_output = cmd_helper.GetCmdOutput( | |
21 ['aapt', 'dump', 'badging', apk_path]).split('\n') | |
22 package_name_re = re.compile(r'package: .*name=\'(\S*)\'') | |
23 for line in aapt_output: | |
24 m = package_name_re.match(line) | |
25 if m: | |
26 return m.group(1) | |
27 raise Exception('Failed to determine package name of %s' % apk_path) | |
28 | 18 |
19 class JarInfo(object): | |
20 """Helper class for inspecting Jars.""" | |
29 | 21 |
30 class ApkInfo(object): | 22 def __init__(self, jar_path): |
31 """Helper class for inspecting APKs.""" | |
32 | |
33 def __init__(self, apk_path, jar_path): | |
34 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_SDK_ROOT'], | 23 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_SDK_ROOT'], |
35 'tools/proguard/bin/proguard.sh') | 24 'tools/proguard/bin/proguard.sh') |
36 if not os.path.exists(self._PROGUARD_PATH): | 25 if not os.path.exists(self._PROGUARD_PATH): |
37 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], | 26 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], |
38 'external/proguard/bin/proguard.sh') | 27 'external/proguard/bin/proguard.sh') |
39 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | 28 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
40 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') | 29 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') |
41 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') | 30 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') |
42 self._PROGUARD_ANNOTATION_CONST_RE = ( | 31 self._PROGUARD_ANNOTATION_CONST_RE = ( |
43 re.compile(r'\s*?- Constant element value.*$')) | 32 re.compile(r'\s*?- Constant element value.*$')) |
44 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') | 33 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') |
45 | 34 |
46 if not os.path.exists(apk_path): | |
47 raise Exception('%s not found, please build it' % apk_path) | |
48 self._apk_path = apk_path | |
49 if not os.path.exists(jar_path): | 35 if not os.path.exists(jar_path): |
50 raise Exception('%s not found, please build it' % jar_path) | 36 raise Exception('%s not found, please build it' % jar_path) |
51 self._jar_path = jar_path | 37 self._jar_path = jar_path |
52 self._annotation_map = collections.defaultdict(list) | 38 self._annotation_map = collections.defaultdict(list) |
53 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' | 39 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' |
54 self._test_methods = [] | 40 self._test_methods = [] |
55 self._Initialize() | |
56 | |
57 def _Initialize(self): | |
58 if not self._GetCachedProguardData(): | 41 if not self._GetCachedProguardData(): |
59 self._GetProguardData() | 42 self._GetProguardData() |
60 | 43 |
61 def _GetCachedProguardData(self): | 44 def _GetCachedProguardData(self): |
62 if (os.path.exists(self._pickled_proguard_name) and | 45 if (os.path.exists(self._pickled_proguard_name) and |
63 (os.path.getmtime(self._pickled_proguard_name) > | 46 (os.path.getmtime(self._pickled_proguard_name) > |
64 os.path.getmtime(self._jar_path))): | 47 os.path.getmtime(self._jar_path))): |
65 logging.info('Loading cached proguard output from %s', | 48 logging.info('Loading cached proguard output from %s', |
66 self._pickled_proguard_name) | 49 self._pickled_proguard_name) |
67 try: | 50 try: |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 with open(self._pickled_proguard_name, 'w') as f: | 118 with open(self._pickled_proguard_name, 'w') as f: |
136 f.write(pickle.dumps(d)) | 119 f.write(pickle.dumps(d)) |
137 | 120 |
138 def _GetAnnotationMap(self): | 121 def _GetAnnotationMap(self): |
139 return self._annotation_map | 122 return self._annotation_map |
140 | 123 |
141 def _IsTestMethod(self, test): | 124 def _IsTestMethod(self, test): |
142 class_name, method = test.split('#') | 125 class_name, method = test.split('#') |
143 return class_name.endswith('Test') and method.startswith('test') | 126 return class_name.endswith('Test') and method.startswith('test') |
144 | 127 |
145 def GetApkPath(self): | |
146 return self._apk_path | |
147 | |
148 def GetPackageName(self): | |
149 """Returns the package name of this APK.""" | |
150 return GetPackageNameForApk(self._apk_path) | |
151 | |
152 def GetTestAnnotations(self, test): | 128 def GetTestAnnotations(self, test): |
153 """Returns a list of all annotations for the given |test|. May be empty.""" | 129 """Returns a list of all annotations for the given |test|. May be empty.""" |
154 if not self._IsTestMethod(test): | 130 if not self._IsTestMethod(test): |
155 return [] | 131 return [] |
156 return self._GetAnnotationMap()[test] | 132 return self._GetAnnotationMap()[test] |
157 | 133 |
158 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): | 134 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): |
159 """Checks if annotations match any of the filters.""" | 135 """Checks if annotations match any of the filters.""" |
160 if not annotation_filter_list: | 136 if not annotation_filter_list: |
161 return True | 137 return True |
162 for annotation_filter in annotation_filter_list: | 138 for annotation_filter in annotation_filter_list: |
163 filters = annotation_filter.split('=') | 139 filters = annotation_filter.split('=') |
164 if len(filters) == 2: | 140 if len(filters) == 2: |
165 key = filters[0] | 141 key = filters[0] |
166 value_list = filters[1].split(',') | 142 value_list = filters[1].split(',') |
167 for value in value_list: | 143 for value in value_list: |
168 if key + ':' + value in annotations: | 144 if key + ':' + value in annotations: |
169 return True | 145 return True |
170 elif annotation_filter in annotations: | 146 elif annotation_filter in annotations: |
171 return True | 147 return True |
172 return False | 148 return False |
173 | 149 |
174 def GetAnnotatedTests(self, annotation_filter_list): | 150 def GetAnnotatedTests(self, annotation_filter_list): |
175 """Returns a list of all tests that match the given annotation filters.""" | 151 """Returns a list of all tests that match the given annotation filters.""" |
176 return [test for test, annotations in self._GetAnnotationMap().iteritems() | 152 return [test for test, annotations in self._GetAnnotationMap().iteritems() |
177 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( | 153 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
178 annotation_filter_list, annotations)] | 154 annotation_filter_list, annotations)] |
179 | 155 |
180 def GetTestMethods(self): | 156 def GetTestMethods(self): |
181 """Returns a list of all test methods in this apk as Class#testMethod.""" | 157 """Returns a list of all test methods in this jar as Class#testMethod.""" |
182 return self._test_methods | 158 return self._test_methods |
183 | 159 |
184 @staticmethod | 160 @staticmethod |
185 def IsPythonDrivenTest(test): | 161 def IsPythonDrivenTest(test): |
186 return 'pythonDrivenTests' in test | 162 return 'pythonDrivenTests' in test |
OLD | NEW |