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 APKs.""" |
6 | 6 |
7 import collections | 7 import collections |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
11 import cmd_helper | 11 import cmd_helper |
12 | 12 |
13 | 13 |
| 14 def GetPackageNameForApk(apk_path): |
| 15 """Returns the package name of the apk file.""" |
| 16 aapt_output = cmd_helper.GetCmdOutput( |
| 17 ['aapt', 'dump', 'badging', apk_path]).split('\n') |
| 18 package_name_re = re.compile(r'package: .*name=\'(\S*)\'') |
| 19 for line in aapt_output: |
| 20 m = package_name_re.match(line) |
| 21 if m: |
| 22 return m.group(1) |
| 23 raise Exception('Failed to determine package name of %s' % apk_path) |
| 24 |
| 25 |
14 class ApkInfo(object): | 26 class ApkInfo(object): |
15 """Helper class for inspecting APKs.""" | 27 """Helper class for inspecting APKs.""" |
16 | 28 |
17 def __init__(self, apk_path, jar_path): | 29 def __init__(self, apk_path, jar_path): |
18 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_SDK_ROOT'], | 30 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_SDK_ROOT'], |
19 'tools/proguard/bin/proguard.sh') | 31 'tools/proguard/bin/proguard.sh') |
20 if not os.path.exists(self._PROGUARD_PATH): | 32 if not os.path.exists(self._PROGUARD_PATH): |
21 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], | 33 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], |
22 'external/proguard/bin/proguard.sh') | 34 'external/proguard/bin/proguard.sh') |
23 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | 35 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
24 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') | 36 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') |
25 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') | 37 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') |
26 self._PROGUARD_ANNOTATION_CONST_RE = ( | 38 self._PROGUARD_ANNOTATION_CONST_RE = ( |
27 re.compile(r'\s*?- Constant element value.*$')) | 39 re.compile(r'\s*?- Constant element value.*$')) |
28 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') | 40 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') |
29 self._AAPT_PACKAGE_NAME_RE = re.compile(r'package: .*name=\'(\S*)\'') | |
30 | 41 |
31 if not os.path.exists(apk_path): | 42 if not os.path.exists(apk_path): |
32 raise Exception('%s not found, please build it' % apk_path) | 43 raise Exception('%s not found, please build it' % apk_path) |
33 self._apk_path = apk_path | 44 self._apk_path = apk_path |
34 if not os.path.exists(jar_path): | 45 if not os.path.exists(jar_path): |
35 raise Exception('%s not found, please build it' % jar_path) | 46 raise Exception('%s not found, please build it' % jar_path) |
36 self._jar_path = jar_path | 47 self._jar_path = jar_path |
37 self._annotation_map = collections.defaultdict(list) | 48 self._annotation_map = collections.defaultdict(list) |
38 self._test_methods = [] | 49 self._test_methods = [] |
39 self._Initialize() | 50 self._Initialize() |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 103 |
93 def _IsTestMethod(self, test): | 104 def _IsTestMethod(self, test): |
94 class_name, method = test.split('#') | 105 class_name, method = test.split('#') |
95 return class_name.endswith('Test') and method.startswith('test') | 106 return class_name.endswith('Test') and method.startswith('test') |
96 | 107 |
97 def GetApkPath(self): | 108 def GetApkPath(self): |
98 return self._apk_path | 109 return self._apk_path |
99 | 110 |
100 def GetPackageName(self): | 111 def GetPackageName(self): |
101 """Returns the package name of this APK.""" | 112 """Returns the package name of this APK.""" |
102 aapt_output = cmd_helper.GetCmdOutput( | 113 return GetPackageNameForApk(self._apk_path) |
103 ['aapt', 'dump', 'badging', self._apk_path]).split('\n') | |
104 for line in aapt_output: | |
105 m = self._AAPT_PACKAGE_NAME_RE.match(line) | |
106 if m: | |
107 return m.group(1) | |
108 raise Exception('Failed to determine package name of %s' % self._apk_path) | |
109 | 114 |
110 def GetTestAnnotations(self, test): | 115 def GetTestAnnotations(self, test): |
111 """Returns a list of all annotations for the given |test|. May be empty.""" | 116 """Returns a list of all annotations for the given |test|. May be empty.""" |
112 if not self._IsTestMethod(test): | 117 if not self._IsTestMethod(test): |
113 return [] | 118 return [] |
114 return self._GetAnnotationMap()[test] | 119 return self._GetAnnotationMap()[test] |
115 | 120 |
116 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): | 121 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): |
117 """Checks if annotations match any of the filters.""" | 122 """Checks if annotations match any of the filters.""" |
118 if not annotation_filter_list: | 123 if not annotation_filter_list: |
(...skipping 16 matching lines...) Expand all Loading... |
135 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( | 140 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
136 annotation_filter_list, annotations)] | 141 annotation_filter_list, annotations)] |
137 | 142 |
138 def GetTestMethods(self): | 143 def GetTestMethods(self): |
139 """Returns a list of all test methods in this apk as Class#testMethod.""" | 144 """Returns a list of all test methods in this apk as Class#testMethod.""" |
140 return self._test_methods | 145 return self._test_methods |
141 | 146 |
142 @staticmethod | 147 @staticmethod |
143 def IsPythonDrivenTest(test): | 148 def IsPythonDrivenTest(test): |
144 return 'pythonDrivenTests' in test | 149 return 'pythonDrivenTests' in test |
OLD | NEW |