-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_pydoc.py
340 lines (301 loc) · 11.9 KB
/
inject_pydoc.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#
# This (non-idiomatic) python script is in charge of
# 1) Parsing all .i files in the 'swig/' directory, and
# collecting all function, classes & methods comments
# that can be found between <pydoc>/</pydoc> tags.
# 2) Reading, line by line, the idaapi_<platform>.py.raw
# file, and for each function, class & method found
# there, associate a possily previously-harvested
# pydoc documentation.
# 3) Generating the idaapi_<platform>.py file.
#
import re
import os
import os.path
DOCSTR_MARKER = "\"\"\""
# --------------------------------------------------------------------------
def split_oneliner_comments(lines):
out_lines = []
for line in lines:
line = line.rstrip()
if line.startswith("#"):
out_lines.append(line)
continue
if len(line) == 0:
out_lines.append("")
continue
pfx = None
while line.find(DOCSTR_MARKER) > -1:
idx = line.find(DOCSTR_MARKER)
meat = line[0:idx]
try:
if len(meat.strip()) == 0:
pfx = meat
out_lines.append(pfx + DOCSTR_MARKER)
else:
out_lines.append((pfx if pfx is not None else "") + meat)
out_lines.append((pfx if pfx is not None else "") + DOCSTR_MARKER)
except:
raise BaseException("Error at line: " + line)
line = line[idx + len(DOCSTR_MARKER):]
if len(line.strip()) > 0:
out_lines.append((pfx if pfx is not None else "") + line)
return out_lines
# --------------------------------------------------------------------------
def dedent(lines):
if len(lines) < 1:
return lines
line0 = lines[0]
indent = len(line0) - len(line0.lstrip())
if indent < 0:
raise BaseException("Couldn't find \" in '" + line0 + "'")
expect = " " * indent
def proc(l):
#print "DE-INDENTING '%s'" % l
if len(l) == 0:
return l # Keep empty lines
prefix = l[0:indent]
if prefix != expect:
raise BaseException("Line: '" + l + "' has wrong indentation. Expected " + str(indent) + " spaces.")
return l[indent:]
return map(proc, lines)
# --------------------------------------------------------------------------
def get_fun_name(line):
return re.search("def ([^\(]*)\(", line).group(1)
# --------------------------------------------------------------------------
def get_class_name(line):
return re.search("class ([^\(:]*)[\(:]?", line).group(1)
# --------------------------------------------------------------------------
def get_indent_string(line):
indent = len(line) - len(line.lstrip())
return " " * indent
# --------------------------------------------------------------------------
class collect_idaapi_pydoc_t(object):
"""
Search in all files in the 'plugins/idapython/swig/' directory
for possible additional <pydoc> we could use later.
"""
S_UNKNOWN = 0
S_IN_PYDOC = 1
S_IN_DOCSTR = 2
# S_STOP = 5
PYDOC_START = "#<pydoc>"
PYDOC_END = "#</pydoc>"
DOCSTR_MARKER = DOCSTR_MARKER #"\"\"\""
state = S_UNKNOWN
lines = None
def __init__(self):
self.idaapi_pydoc = {"funcs" : {}, "classes" : {}}
def next(self):
line = self.lines[0]
self.lines = self.lines[1:]
return line
def set_fun(self, name, collected):
self.idaapi_pydoc["funcs"][name] = dedent(collected)
def collect_fun(self, fun_name):
collected = []
while len(self.lines) > 0:
line = self.next()
if self.state is self.S_IN_PYDOC:
if line.startswith(self.PYDOC_END):
self.state = self.S_UNKNOWN
return self.set_fun(fun_name, collected)
elif line.find(self.DOCSTR_MARKER) > -1:
self.state = self.S_IN_DOCSTR
elif not line.startswith(" "):
return self.set_fun(fun_name, collected)
elif self.state is self.S_IN_DOCSTR:
if line.find(self.DOCSTR_MARKER) > -1:
self.state = self.S_IN_PYDOC
return self.set_fun(fun_name, collected)
else:
collected.append(line)
else:
raise BaseException("Unexpected state: " + str(self.state))
def set_method(self, cls, method_name, collected):
cls["methods"][method_name] = dedent(collected)
def collect_method(self, cls, method_name):
collected = []
while len(self.lines) > 0:
line = self.next()
if self.state is self.S_IN_PYDOC:
if line.startswith(self.PYDOC_END):
self.state = self.S_UNKNOWN
return self.set_method(cls, method_name, collected)
elif line.find(self.DOCSTR_MARKER) > -1:
self.state = self.S_IN_DOCSTR
elif self.state is self.S_IN_DOCSTR:
if line.find(self.DOCSTR_MARKER) > -1:
self.state = self.S_IN_PYDOC
return self.set_method(cls, method_name, collected)
else:
collected.append(line)
def set_class(self, name, cls_data, collected):
cls_data["doc"] = dedent(collected) if len(collected) > 0 else None
self.idaapi_pydoc["classes"][name] = cls_data
def collect_cls(self, cls_name):
collected = []
cls = {"methods":{},"doc":None}
while len(self.lines) > 0:
line = self.next()
if self.state is self.S_IN_PYDOC:
if line.startswith(" def "):
self.collect_method(cls, get_fun_name(line))
if self.state == self.S_UNKNOWN: # method marked end of <pydoc>
return self.set_class(cls_name, cls, collected)
elif line.find(self.DOCSTR_MARKER) > -1:
self.state = self.S_IN_DOCSTR
elif line.startswith(self.PYDOC_END):
self.state = self.S_UNKNOWN
return self.set_class(cls_name, cls, collected)
elif len(line) > 1 and not line.startswith(" "):
return self.set_class(cls_name, cls, collected)
elif self.state is self.S_IN_DOCSTR:
if line.find(self.DOCSTR_MARKER) > -1:
self.state = self.S_IN_PYDOC
else:
collected.append(line)
def collect_file_pydoc(self, filename):
self.state = self.S_UNKNOWN
with open(filename, "rt") as f:
self.lines = split_oneliner_comments(f.readlines())
context = None
doc = []
while len(self.lines) > 0:
line = self.next()
if self.state is self.S_UNKNOWN:
if line.startswith(self.PYDOC_START):
self.state = self.S_IN_PYDOC
elif self.state is self.S_IN_PYDOC:
if line.startswith("def "):
self.collect_fun(get_fun_name(line))
elif line.startswith("class "):
self.collect_cls(get_class_name(line))
elif line.startswith(self.PYDOC_END):
self.state = self.S_UNKNOWN
def collect(self, dirpath):
for root, dirs, files in os.walk(dirpath):
for f in files:
self.collect_file_pydoc(os.path.join(root, f))
return self.idaapi_pydoc
# --------------------------------------------------------------------------
class idaapi_fixer_t(object):
lines = None
def __init__(self, collected_info):
self.collected_info = collected_info
def next(self):
line = self.lines[0]
self.lines = self.lines[1:]
return line
def copy(self, out):
line = self.next()
out.append(line)
return line
def push_front(self, line):
self.lines.insert(0, line)
def get_fun_info(self, fun_name):
if fun_name in self.collected_info["funcs"]:
return self.collected_info["funcs"][fun_name]
else:
return None
def get_class_info(self, class_name):
if class_name in self.collected_info["classes"]:
return self.collected_info["classes"][class_name]
else:
return None
def get_method_info(self, class_info, method_name):
if method_name in class_info["methods"]:
return class_info["methods"][method_name]
else:
return None
def fix_fun(self, out, class_info=None):
line = self.copy(out)
fun_name = get_fun_name(line)
line = self.copy(out)
if line.find(DOCSTR_MARKER) > -1:
# Determine indentation level
indent = get_indent_string(line)
while True:
line = self.next()
if line.find(DOCSTR_MARKER) > -1:
if class_info is None:
found = self.get_fun_info(fun_name)
else:
found = self.get_method_info(class_info, fun_name)
if found is not None:
out.append("\n")
for fl in found:
out.append(indent + fl)
out.append(line)
break
else:
out.append(line)
def fix_method(self, class_info, out):
return self.fix_fun(out, class_info)
def fix_cls(self, out):
line = self.copy(out)
cls_name = get_class_name(line)
class_info = self.get_class_info(cls_name)
if class_info is None:
return
line = self.copy(out)
indent = get_indent_string(line)
# If class has doc, maybe inject additional <pydoc>
if line.find(DOCSTR_MARKER) > -1:
while True:
line = self.next()
if line.find(DOCSTR_MARKER) > -1:
doc = class_info["doc"]
if doc is not None:
out.append("\n")
for dl in doc:
out.append(indent + dl)
out.append(line)
break
else:
out.append(line)
# Iterate on class methods, and possibly patch
# their docstring
method_start = indent + "def "
while True:
line = self.next()
# print "Fixing methods.. Line is '%s'" % line
if line.startswith(indent) or line.strip() == "":
if line.startswith(method_start):
self.push_front(line)
self.fix_method(class_info, out)
else:
out.append(line)
else:
self.push_front(line)
break
def fix_file(self, idaapi_filename, out_filename):
with open(idaapi_filename, "rt") as f:
self.lines = split_oneliner_comments(f.readlines())
out = []
while len(self.lines) > 0:
line = self.next()
# print "LINE: %s" % line
if line.startswith("def "):
self.push_front(line)
self.fix_fun(out)
elif line.startswith("class "):
self.push_front(line)
self.fix_cls(out)
else:
out.append(line)
with open(out_filename, "wt") as o:
for ol in out:
o.write(ol)
o.write("\n")
# --------------------------------------------------------------------------
if __name__ == '__main__':
import sys
collecter = collect_idaapi_pydoc_t()
collected = collecter.collect(sys.argv[1])
# import pprint
# pprint.pprint(collected, indent=2)
fixer = idaapi_fixer_t(collected)
target_file = sys.argv[2]
result_file = sys.argv[3]
fixer.fix_file(target_file, result_file)