| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 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 import os | |
| 6 import re | |
| 7 import tempfile | |
| 8 | |
| 9 from pylib import constants | |
| 10 from pylib import cmd_helper | |
| 11 | |
| 12 | |
| 13 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | |
| 14 _PROGUARD_SUPERCLASS_RE = re.compile(r'\s*? Superclass:\s*([\S]+)$') | |
| 15 _PROGUARD_SECTION_RE = re.compile( | |
| 16 r'^(?:Interfaces|Constant Pool|Fields|Methods|Class file attributes) ' | |
| 17 r'\(count = \d+\):$') | |
| 18 _PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') | |
| 19 _PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') | |
| 20 _PROGUARD_ANNOTATION_CONST_RE = ( | |
| 21 re.compile(r'\s*?- Constant element value.*$')) | |
| 22 _PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') | |
| 23 | |
| 24 _PROGUARD_PATH_SDK = os.path.join( | |
| 25 constants.ANDROID_SDK_ROOT, 'tools', 'proguard', 'lib', 'proguard.jar') | |
| 26 _PROGUARD_PATH_BUILT = ( | |
| 27 os.path.join(os.environ['ANDROID_BUILD_TOP'], 'external', 'proguard', | |
| 28 'lib', 'proguard.jar') | |
| 29 if 'ANDROID_BUILD_TOP' in os.environ else None) | |
| 30 _PROGUARD_PATH = ( | |
| 31 _PROGUARD_PATH_SDK if os.path.exists(_PROGUARD_PATH_SDK) | |
| 32 else _PROGUARD_PATH_BUILT) | |
| 33 | |
| 34 | |
| 35 def Dump(jar_path): | |
| 36 """Dumps class and method information from a JAR into a dict via proguard. | |
| 37 | |
| 38 Args: | |
| 39 jar_path: An absolute path to the JAR file to dump. | |
| 40 Returns: | |
| 41 A dict in the following format: | |
| 42 { | |
| 43 'classes': [ | |
| 44 { | |
| 45 'class': '', | |
| 46 'superclass': '', | |
| 47 'annotations': {}, | |
| 48 'methods': [ | |
| 49 { | |
| 50 'method': '', | |
| 51 'annotations': {}, | |
| 52 }, | |
| 53 ... | |
| 54 ], | |
| 55 }, | |
| 56 ... | |
| 57 ], | |
| 58 } | |
| 59 """ | |
| 60 | |
| 61 with tempfile.NamedTemporaryFile() as proguard_output: | |
| 62 cmd_helper.RunCmd(['java', '-jar', | |
| 63 _PROGUARD_PATH, | |
| 64 '-injars', jar_path, | |
| 65 '-dontshrink', | |
| 66 '-dontoptimize', | |
| 67 '-dontobfuscate', | |
| 68 '-dontpreverify', | |
| 69 '-dump', proguard_output.name]) | |
| 70 | |
| 71 | |
| 72 results = { | |
| 73 'classes': [], | |
| 74 } | |
| 75 | |
| 76 annotation = None | |
| 77 annotation_has_value = False | |
| 78 class_result = None | |
| 79 method_result = None | |
| 80 | |
| 81 for line in proguard_output: | |
| 82 line = line.strip('\r\n') | |
| 83 | |
| 84 m = _PROGUARD_CLASS_RE.match(line) | |
| 85 if m: | |
| 86 class_result = { | |
| 87 'class': m.group(1).replace('/', '.'), | |
| 88 'superclass': '', | |
| 89 'annotations': {}, | |
| 90 'methods': [], | |
| 91 } | |
| 92 results['classes'].append(class_result) | |
| 93 annotation = None | |
| 94 annotation_has_value = False | |
| 95 method_result = None | |
| 96 continue | |
| 97 | |
| 98 if not class_result: | |
| 99 continue | |
| 100 | |
| 101 m = _PROGUARD_SUPERCLASS_RE.match(line) | |
| 102 if m: | |
| 103 class_result['superclass'] = m.group(1).replace('/', '.') | |
| 104 continue | |
| 105 | |
| 106 m = _PROGUARD_SECTION_RE.match(line) | |
| 107 if m: | |
| 108 annotation = None | |
| 109 annotation_has_value = False | |
| 110 method_result = None | |
| 111 continue | |
| 112 | |
| 113 m = _PROGUARD_METHOD_RE.match(line) | |
| 114 if m: | |
| 115 method_result = { | |
| 116 'method': m.group(1), | |
| 117 'annotations': {}, | |
| 118 } | |
| 119 class_result['methods'].append(method_result) | |
| 120 annotation = None | |
| 121 annotation_has_value = False | |
| 122 continue | |
| 123 | |
| 124 m = _PROGUARD_ANNOTATION_RE.match(line) | |
| 125 if m: | |
| 126 # Ignore the annotation package. | |
| 127 annotation = m.group(1).split('/')[-1] | |
| 128 if method_result: | |
| 129 method_result['annotations'][annotation] = None | |
| 130 else: | |
| 131 class_result['annotations'][annotation] = None | |
| 132 continue | |
| 133 | |
| 134 if annotation: | |
| 135 if not annotation_has_value: | |
| 136 m = _PROGUARD_ANNOTATION_CONST_RE.match(line) | |
| 137 annotation_has_value = bool(m) | |
| 138 else: | |
| 139 m = _PROGUARD_ANNOTATION_VALUE_RE.match(line) | |
| 140 if m: | |
| 141 if method_result: | |
| 142 method_result['annotations'][annotation] = m.group(1) | |
| 143 else: | |
| 144 class_result['annotations'][annotation] = m.group(1) | |
| 145 annotation_has_value = None | |
| 146 | |
| 147 return results | |
| 148 | |
| OLD | NEW |