-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_binder.py
executable file
·82 lines (64 loc) · 2.01 KB
/
extract_binder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# coding: utf-8
#
# extract all TRANSACTION_* fields from specific DEX/ODEX/OAT files
from __future__ import print_function
import argparse
import subprocess
import cStringIO
FIELD_BEGIN = 'Name: '
FIELD_END = ' type:'
CLASS_BEGIN = ' '
CLASS_END = '\t\tFile: '
def extract_to(file, result):
output = subprocess.check_output(['dextra', '-f', file])
lines = output.split('\n')
clazz = None
for line in lines:
# try match field first
beg = line.find(FIELD_BEGIN)
end = line.rfind(FIELD_END)
field = None
if beg >= 0 and end >= 0:
field = line[beg+len(FIELD_BEGIN) : end]
if field.startswith('TRANSACTION_'):
result[clazz].append(field)
continue
# try match class
line = line.strip()
if not line.startswith('Class '):
continue
end = line.rfind(CLASS_END)
if end < 0:
end = len(line)
beg = line.rfind(CLASS_BEGIN, 0, end)
clazz = line[beg+len(CLASS_BEGIN) : end]
if clazz not in result:
result[clazz] = []
def main():
parser = argparse.ArgumentParser(
description='extract all TRANSACTION_* fields from specific DEX/ODEX/OAT files')
parser.add_argument('-i', nargs='+', dest='input', required=True,
help='input files')
parser.add_argument('-o', action='store', dest='output', required=True,
help='output file')
args = parser.parse_args()
result = {}
for file in args.input:
extract_to(file, result)
items = result.items()
items.sort()
buf = cStringIO.StringIO()
try:
for clazz, fields in items:
if not fields:
continue
buf.write('%s\n' % clazz)
for field in fields:
buf.write('\t%s\n' % field)
with open(args.output, 'w') as output:
output.write(buf.getvalue())
finally:
buf.close()
if __name__ == '__main__':
main()