OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 """Module containing utilities for jar packages.""" | |
6 | |
7 import collections | |
8 import logging | |
9 import os | |
10 import pickle | |
11 import re | |
12 | |
13 from pylib import cmd_helper | |
14 from pylib import constants | |
15 | |
16 | |
17 # If you change the cached output of proguard, increment this number | |
18 PICKLE_FORMAT_VERSION = 1 | |
19 | |
20 | |
21 class TestPackageJar(object): | |
22 def __init__(self, jar_path): | |
23 sdk_root = os.getenv('ANDROID_SDK_ROOT', constants.ANDROID_SDK_ROOT) | |
24 self._PROGUARD_PATH = os.path.join(sdk_root, | |
25 'tools/proguard/bin/proguard.sh') | |
26 if not os.path.exists(self._PROGUARD_PATH): | |
27 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], | |
28 'external/proguard/bin/proguard.sh') | |
29 self._PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | |
30 self._PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') | |
31 self._PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') | |
32 self._PROGUARD_ANNOTATION_CONST_RE = ( | |
33 re.compile(r'\s*?- Constant element value.*$')) | |
34 self._PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') | |
35 | |
36 if not os.path.exists(jar_path): | |
37 raise Exception('%s not found, please build it' % jar_path) | |
38 self._jar_path = jar_path | |
39 self._annotation_map = collections.defaultdict(list) | |
40 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' | |
41 self._test_methods = [] | |
42 if not self._GetCachedProguardData(): | |
43 self._GetProguardData() | |
44 | |
45 def _GetCachedProguardData(self): | |
46 if (os.path.exists(self._pickled_proguard_name) and | |
47 (os.path.getmtime(self._pickled_proguard_name) > | |
48 os.path.getmtime(self._jar_path))): | |
49 logging.info('Loading cached proguard output from %s', | |
50 self._pickled_proguard_name) | |
51 try: | |
52 with open(self._pickled_proguard_name, 'r') as r: | |
53 d = pickle.loads(r.read()) | |
54 if d['VERSION'] == PICKLE_FORMAT_VERSION: | |
55 self._annotation_map = d['ANNOTATION_MAP'] | |
56 self._test_methods = d['TEST_METHODS'] | |
57 return True | |
58 except: | |
59 logging.warning('PICKLE_FORMAT_VERSION has changed, ignoring cache') | |
60 return False | |
61 | |
62 def _GetProguardData(self): | |
63 proguard_output = cmd_helper.GetCmdOutput([self._PROGUARD_PATH, | |
64 '-injars', self._jar_path, | |
65 '-dontshrink', | |
66 '-dontoptimize', | |
67 '-dontobfuscate', | |
68 '-dontpreverify', | |
69 '-dump', | |
70 ]).split('\n') | |
71 clazz = None | |
72 method = None | |
73 annotation = None | |
74 has_value = False | |
75 qualified_method = None | |
76 for line in proguard_output: | |
77 m = self._PROGUARD_CLASS_RE.match(line) | |
78 if m: | |
79 clazz = m.group(1).replace('/', '.') # Change package delim. | |
80 annotation = None | |
81 continue | |
82 | |
83 m = self._PROGUARD_METHOD_RE.match(line) | |
84 if m: | |
85 method = m.group(1) | |
86 annotation = None | |
87 qualified_method = clazz + '#' + method | |
88 if method.startswith('test') and clazz.endswith('Test'): | |
89 self._test_methods += [qualified_method] | |
90 continue | |
91 | |
92 if not qualified_method: | |
93 # Ignore non-method annotations. | |
94 continue | |
95 | |
96 m = self._PROGUARD_ANNOTATION_RE.match(line) | |
97 if m: | |
98 annotation = m.group(1).split('/')[-1] # Ignore the annotation package. | |
99 self._annotation_map[qualified_method].append(annotation) | |
100 has_value = False | |
101 continue | |
102 if annotation: | |
103 if not has_value: | |
104 m = self._PROGUARD_ANNOTATION_CONST_RE.match(line) | |
105 if m: | |
106 has_value = True | |
107 else: | |
108 m = self._PROGUARD_ANNOTATION_VALUE_RE.match(line) | |
109 if m: | |
110 value = m.group(1) | |
111 self._annotation_map[qualified_method].append( | |
112 annotation + ':' + value) | |
113 has_value = False | |
114 | |
115 logging.info('Storing proguard output to %s', self._pickled_proguard_name) | |
116 d = {'VERSION': PICKLE_FORMAT_VERSION, | |
117 'ANNOTATION_MAP': self._annotation_map, | |
118 'TEST_METHODS': self._test_methods} | |
119 with open(self._pickled_proguard_name, 'w') as f: | |
120 f.write(pickle.dumps(d)) | |
121 | |
122 def _GetAnnotationMap(self): | |
123 return self._annotation_map | |
124 | |
125 def _IsTestMethod(self, test): | |
126 class_name, method = test.split('#') | |
127 return class_name.endswith('Test') and method.startswith('test') | |
128 | |
129 def GetTestAnnotations(self, test): | |
130 """Returns a list of all annotations for the given |test|. May be empty.""" | |
131 if not self._IsTestMethod(test): | |
132 return [] | |
133 return self._GetAnnotationMap()[test] | |
134 | |
135 def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): | |
136 """Checks if annotations match any of the filters.""" | |
137 if not annotation_filter_list: | |
138 return True | |
139 for annotation_filter in annotation_filter_list: | |
140 filters = annotation_filter.split('=') | |
141 if len(filters) == 2: | |
142 key = filters[0] | |
143 value_list = filters[1].split(',') | |
144 for value in value_list: | |
145 if key + ':' + value in annotations: | |
146 return True | |
147 elif annotation_filter in annotations: | |
148 return True | |
149 return False | |
150 | |
151 def GetAnnotatedTests(self, annotation_filter_list): | |
152 """Returns a list of all tests that match the given annotation filters.""" | |
153 return [test for test, annotations in self._GetAnnotationMap().iteritems() | |
154 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( | |
155 annotation_filter_list, annotations)] | |
156 | |
157 def GetTestMethods(self): | |
158 """Returns a list of all test methods in this apk as Class#testMethod.""" | |
159 return self._test_methods | |
160 | |
161 @staticmethod | |
162 def IsPythonDrivenTest(test): | |
163 return 'pythonDrivenTests' in test | |
OLD | NEW |