Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | """Gathers information about APKs.""" |
michael@0 | 6 | |
michael@0 | 7 | import collections |
michael@0 | 8 | import os |
michael@0 | 9 | import re |
michael@0 | 10 | |
michael@0 | 11 | import cmd_helper |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | class ApkInfo(object): |
michael@0 | 15 | """Helper class for inspecting APKs.""" |
michael@0 | 16 | _PROGUARD_PATH = os.path.join(os.environ['ANDROID_SDK_ROOT'], |
michael@0 | 17 | 'tools/proguard/bin/proguard.sh') |
michael@0 | 18 | if not os.path.exists(_PROGUARD_PATH): |
michael@0 | 19 | _PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], |
michael@0 | 20 | 'external/proguard/bin/proguard.sh') |
michael@0 | 21 | _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
michael@0 | 22 | _PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') |
michael@0 | 23 | _PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') |
michael@0 | 24 | _PROGUARD_ANNOTATION_CONST_RE = re.compile(r'\s*?- Constant element value.*$') |
michael@0 | 25 | _PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') |
michael@0 | 26 | _AAPT_PACKAGE_NAME_RE = re.compile(r'package: .*name=\'(\S*)\'') |
michael@0 | 27 | |
michael@0 | 28 | def __init__(self, apk_path, jar_path): |
michael@0 | 29 | if not os.path.exists(apk_path): |
michael@0 | 30 | raise Exception('%s not found, please build it' % apk_path) |
michael@0 | 31 | self._apk_path = apk_path |
michael@0 | 32 | if not os.path.exists(jar_path): |
michael@0 | 33 | raise Exception('%s not found, please build it' % jar_path) |
michael@0 | 34 | self._jar_path = jar_path |
michael@0 | 35 | self._annotation_map = collections.defaultdict(list) |
michael@0 | 36 | self._test_methods = [] |
michael@0 | 37 | self._Initialize() |
michael@0 | 38 | |
michael@0 | 39 | def _Initialize(self): |
michael@0 | 40 | proguard_output = cmd_helper.GetCmdOutput([self._PROGUARD_PATH, |
michael@0 | 41 | '-injars', self._jar_path, |
michael@0 | 42 | '-dontshrink', |
michael@0 | 43 | '-dontoptimize', |
michael@0 | 44 | '-dontobfuscate', |
michael@0 | 45 | '-dontpreverify', |
michael@0 | 46 | '-dump', |
michael@0 | 47 | ]).split('\n') |
michael@0 | 48 | clazz = None |
michael@0 | 49 | method = None |
michael@0 | 50 | annotation = None |
michael@0 | 51 | has_value = False |
michael@0 | 52 | qualified_method = None |
michael@0 | 53 | for line in proguard_output: |
michael@0 | 54 | m = self._PROGUARD_CLASS_RE.match(line) |
michael@0 | 55 | if m: |
michael@0 | 56 | clazz = m.group(1).replace('/', '.') # Change package delim. |
michael@0 | 57 | annotation = None |
michael@0 | 58 | continue |
michael@0 | 59 | m = self._PROGUARD_METHOD_RE.match(line) |
michael@0 | 60 | if m: |
michael@0 | 61 | method = m.group(1) |
michael@0 | 62 | annotation = None |
michael@0 | 63 | qualified_method = clazz + '#' + method |
michael@0 | 64 | if method.startswith('test') and clazz.endswith('Test'): |
michael@0 | 65 | self._test_methods += [qualified_method] |
michael@0 | 66 | continue |
michael@0 | 67 | m = self._PROGUARD_ANNOTATION_RE.match(line) |
michael@0 | 68 | if m: |
michael@0 | 69 | assert qualified_method |
michael@0 | 70 | annotation = m.group(1).split('/')[-1] # Ignore the annotation package. |
michael@0 | 71 | self._annotation_map[qualified_method].append(annotation) |
michael@0 | 72 | has_value = False |
michael@0 | 73 | continue |
michael@0 | 74 | if annotation: |
michael@0 | 75 | assert qualified_method |
michael@0 | 76 | if not has_value: |
michael@0 | 77 | m = self._PROGUARD_ANNOTATION_CONST_RE.match(line) |
michael@0 | 78 | if m: |
michael@0 | 79 | has_value = True |
michael@0 | 80 | else: |
michael@0 | 81 | m = self._PROGUARD_ANNOTATION_VALUE_RE.match(line) |
michael@0 | 82 | if m: |
michael@0 | 83 | value = m.group(1) |
michael@0 | 84 | self._annotation_map[qualified_method].append( |
michael@0 | 85 | annotation + ':' + value) |
michael@0 | 86 | has_value = False |
michael@0 | 87 | |
michael@0 | 88 | def _GetAnnotationMap(self): |
michael@0 | 89 | return self._annotation_map |
michael@0 | 90 | |
michael@0 | 91 | def _IsTestMethod(self, test): |
michael@0 | 92 | class_name, method = test.split('#') |
michael@0 | 93 | return class_name.endswith('Test') and method.startswith('test') |
michael@0 | 94 | |
michael@0 | 95 | def GetApkPath(self): |
michael@0 | 96 | return self._apk_path |
michael@0 | 97 | |
michael@0 | 98 | def GetPackageName(self): |
michael@0 | 99 | """Returns the package name of this APK.""" |
michael@0 | 100 | aapt_output = cmd_helper.GetCmdOutput( |
michael@0 | 101 | ['aapt', 'dump', 'badging', self._apk_path]).split('\n') |
michael@0 | 102 | for line in aapt_output: |
michael@0 | 103 | m = self._AAPT_PACKAGE_NAME_RE.match(line) |
michael@0 | 104 | if m: |
michael@0 | 105 | return m.group(1) |
michael@0 | 106 | raise Exception('Failed to determine package name of %s' % self._apk_path) |
michael@0 | 107 | |
michael@0 | 108 | def GetTestAnnotations(self, test): |
michael@0 | 109 | """Returns a list of all annotations for the given |test|. May be empty.""" |
michael@0 | 110 | if not self._IsTestMethod(test): |
michael@0 | 111 | return [] |
michael@0 | 112 | return self._GetAnnotationMap()[test] |
michael@0 | 113 | |
michael@0 | 114 | def _AnnotationsMatchFilters(self, annotation_filter_list, annotations): |
michael@0 | 115 | """Checks if annotations match any of the filters.""" |
michael@0 | 116 | if not annotation_filter_list: |
michael@0 | 117 | return True |
michael@0 | 118 | for annotation_filter in annotation_filter_list: |
michael@0 | 119 | filters = annotation_filter.split('=') |
michael@0 | 120 | if len(filters) == 2: |
michael@0 | 121 | key = filters[0] |
michael@0 | 122 | value_list = filters[1].split(',') |
michael@0 | 123 | for value in value_list: |
michael@0 | 124 | if key + ':' + value in annotations: |
michael@0 | 125 | return True |
michael@0 | 126 | elif annotation_filter in annotations: |
michael@0 | 127 | return True |
michael@0 | 128 | return False |
michael@0 | 129 | |
michael@0 | 130 | def GetAnnotatedTests(self, annotation_filter_list): |
michael@0 | 131 | """Returns a list of all tests that match the given annotation filters.""" |
michael@0 | 132 | return [test for test, annotations in self._GetAnnotationMap().iteritems() |
michael@0 | 133 | if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
michael@0 | 134 | annotation_filter_list, annotations)] |
michael@0 | 135 | |
michael@0 | 136 | def GetTestMethods(self): |
michael@0 | 137 | """Returns a list of all test methods in this apk as Class#testMethod.""" |
michael@0 | 138 | return self._test_methods |
michael@0 | 139 | |
michael@0 | 140 | @staticmethod |
michael@0 | 141 | def IsPythonDrivenTest(test): |
michael@0 | 142 | return 'pythonDrivenTests' in test |