OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 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 """Helper class for instrumenation test jar.""" |
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 from pylib import cmd_helper | 13 from pylib import cmd_helper |
14 from pylib import constants | 14 from pylib import constants |
15 | 15 |
16 | 16 |
17 # If you change the cached output of proguard, increment this number | 17 # If you change the cached output of proguard, increment this number |
18 PICKLE_FORMAT_VERSION = 1 | 18 PICKLE_FORMAT_VERSION = 1 |
19 | 19 |
20 def GetPackageNameForApk(apk_path): | |
21 """Returns the package name of the apk file.""" | |
22 aapt_output = cmd_helper.GetCmdOutput( | |
23 ['aapt', 'dump', 'badging', apk_path]).split('\n') | |
24 package_name_re = re.compile(r'package: .*name=\'(\S*)\'') | |
25 for line in aapt_output: | |
26 m = package_name_re.match(line) | |
27 if m: | |
28 return m.group(1) | |
29 raise Exception('Failed to determine package name of %s' % apk_path) | |
30 | 20 |
31 | 21 class TestJar(object): |
32 class ApkInfo(object): | 22 def __init__(self, jar_path): |
33 """Helper class for inspecting APKs.""" | |
34 | |
35 def __init__(self, apk_path, jar_path): | |
36 sdk_root = os.getenv('ANDROID_SDK_ROOT', constants.ANDROID_SDK_ROOT) | 23 sdk_root = os.getenv('ANDROID_SDK_ROOT', constants.ANDROID_SDK_ROOT) |
37 self._PROGUARD_PATH = os.path.join(sdk_root, | 24 self._PROGUARD_PATH = os.path.join(sdk_root, |
38 'tools/proguard/bin/proguard.sh') | 25 'tools/proguard/bin/proguard.sh') |
39 if not os.path.exists(self._PROGUARD_PATH): | 26 if not os.path.exists(self._PROGUARD_PATH): |
40 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], | 27 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], |
41 'external/proguard/bin/proguard.sh') | 28 'external/proguard/bin/proguard.sh') |
42 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | 29 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
43 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') | 30 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') |
44 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') | 31 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') |
45 self._PROGUARD_ANNOTATION_CONST_RE = ( | 32 self._PROGUARD_ANNOTATION_CONST_RE = ( |
46 re.compile(r'\s*?- Constant element value.*$')) | 33 re.compile(r'\s*?- Constant element value.*$')) |
47 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') | 34 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') |
48 | 35 |
49 if not os.path.exists(apk_path): | |
50 raise Exception('%s not found, please build it' % apk_path) | |
51 self._apk_path = apk_path | |
52 if not os.path.exists(jar_path): | 36 if not os.path.exists(jar_path): |
53 raise Exception('%s not found, please build it' % jar_path) | 37 raise Exception('%s not found, please build it' % jar_path) |
54 self._jar_path = jar_path | 38 self._jar_path = jar_path |
55 self._annotation_map = collections.defaultdict(list) | 39 self._annotation_map = collections.defaultdict(list) |
56 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' | 40 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' |
57 self._test_methods = [] | 41 self._test_methods = [] |
58 self._Initialize() | |
59 | |
60 def _Initialize(self): | |
61 if not self._GetCachedProguardData(): | 42 if not self._GetCachedProguardData(): |
62 self._GetProguardData() | 43 self._GetProguardData() |
63 | 44 |
64 def _GetCachedProguardData(self): | 45 def _GetCachedProguardData(self): |
65 if (os.path.exists(self._pickled_proguard_name) and | 46 if (os.path.exists(self._pickled_proguard_name) and |
66 (os.path.getmtime(self._pickled_proguard_name) > | 47 (os.path.getmtime(self._pickled_proguard_name) > |
67 os.path.getmtime(self._jar_path))): | 48 os.path.getmtime(self._jar_path))): |
68 logging.info('Loading cached proguard output from %s', | 49 logging.info('Loading cached proguard output from %s', |
69 self._pickled_proguard_name) | 50 self._pickled_proguard_name) |
70 try: | 51 try: |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 with open(self._pickled_proguard_name, 'w') as f: | 119 with open(self._pickled_proguard_name, 'w') as f: |
139 f.write(pickle.dumps(d)) | 120 f.write(pickle.dumps(d)) |
140 | 121 |
141 def _GetAnnotationMap(self): | 122 def _GetAnnotationMap(self): |
142 return self._annotation_map | 123 return self._annotation_map |
143 | 124 |
144 def _IsTestMethod(self, test): | 125 def _IsTestMethod(self, test): |
145 class_name, method = test.split('#') | 126 class_name, method = test.split('#') |
146 return class_name.endswith('Test') and method.startswith('test') | 127 return class_name.endswith('Test') and method.startswith('test') |
147 | 128 |
148 def GetApkPath(self): | |
149 return self._apk_path | |
150 | |
151 def GetPackageName(self): | |
152 """Returns the package name of this APK.""" | |
153 return GetPackageNameForApk(self._apk_path) | |
154 | |
155 def GetTestAnnotations(self, test): | 129 def GetTestAnnotations(self, test): |
156 """Returns a list of all annotations for the given |test|. May be empty.""" | 130 """Returns a list of all annotations for the given |test|. May be empty.""" |
157 if not self._IsTestMethod(test): | 131 if not self._IsTestMethod(test): |
158 return [] | 132 return [] |
159 return self._GetAnnotationMap()[test] | 133 return self._GetAnnotationMap()[test] |
160 | 134 |
161 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): | 135 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): |
162 """Checks if annotations match any of the filters.""" | 136 """Checks if annotations match any of the filters.""" |
163 if not annotation_filter_list: | 137 if not annotation_filter_list: |
164 return True | 138 return True |
(...skipping 15 matching lines...) Expand all Loading... |
180 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( | 154 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
181 annotation_filter_list, annotations)] | 155 annotation_filter_list, annotations)] |
182 | 156 |
183 def GetTestMethods(self): | 157 def GetTestMethods(self): |
184 """Returns a list of all test methods in this apk as Class#testMethod.""" | 158 """Returns a list of all test methods in this apk as Class#testMethod.""" |
185 return self._test_methods | 159 return self._test_methods |
186 | 160 |
187 @staticmethod | 161 @staticmethod |
188 def IsPythonDrivenTest(test): | 162 def IsPythonDrivenTest(test): |
189 return 'pythonDrivenTests' in test | 163 return 'pythonDrivenTests' in test |
OLD | NEW |