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