Files
idapython-src/tools/docs/hrdoc.py
T
2019-10-15 15:05:24 +02:00

146 lines
3.9 KiB
Python

from __future__ import print_function
import os
import sys
import shutil
from glob import glob
try:
import epydoc.apidoc
import epydoc.cli
except ImportError as e:
import idc
import traceback
idc.msg("Couldn't import module %s\n" % traceback.format_exc())
idc.qexit(-1)
# --------------------------------------------------------------------------
DOC_DIR = 'hr-html'
# --------------------------------------------------------------------------
def log(msg):
#print msg
pass
def add_header(lines):
S1 = 'href="epydoc.css"'
p = lines.find(S1)
if p < 0:
return None
p = lines.find('\n', p)
if p < 0:
return None
return lines[0:p] + '\n <link href="/style.css" rel="stylesheet" type="text/css">' + lines[p:]
# --------------------------------------------------------------------------
def add_footer(lines):
S1 = 'Generated by Epydoc'
S2 = '</table>'
p = lines.find(S1)
if p < 0:
return None
p = lines.find(S2, p)
if p < 0:
return None
p += len(S2)
return lines[0:p] + '\n<!--#include virtual="../_footer.shtml" -->' + lines[p:]
# --------------------------------------------------------------------------
def define_idaapi_resolver():
"""
Whenever a module named \"idaapi_<something>\" is
spotted, turn it into \"idaapi\".
"""
dn = epydoc.apidoc.DottedName.__init__
def resolver(piece):
if piece is not None and isinstance(piece, basestring) and piece.startswith("idaapi_"):
res = "idaapi"
else:
res = piece
log("Resolver '%s' => '%s'" % (piece, res))
return res
def wrapper(self, *pieces, **options):
return dn(self, *map(resolver, pieces), **options);
epydoc.apidoc.DottedName.__init__ = wrapper
# --------------------------------------------------------------------------
def gen_docs():
import swigdocs
define_idaapi_resolver()
swigdocs.gen_docs(outfn = 'pywraps.py')
# append obj/x86_win_vc_32/idaapi.py to it
# os.system(r'copy /b idaapi.py+..\obj\x86_win_vc_32\idaapi.py idaapi.py')
# delete all output files
for fn in glob('hr-html/*'):
os.unlink(fn)
epydoc.cli.optparse.sys.argv = [ 'epydoc',
'--config', '../tools/docs/hrdoc.cfg',
'--simple-term'
]
# Generate the documentation
epydoc.cli.cli()
# --------------------------------------------------------------------------
def patch_docs():
shutil.copy('../../tools/docs/hrdoc.css', 'epydoc.css')
os.system('chmod +w epydoc.css')
for fn in glob('*.html'):
with open(fn, 'r') as f:
lines = f.read()
r = add_header(lines)
if r:
lines = r
r = add_footer(lines)
if not r:
print("-", end=' ')
continue
with open(fn, 'w') as f:
f.write(r)
print("+", end=' ')
print("\nDocumentation patched!")
# --------------------------------------------------------------------------
def main():
# Save old directory and adjust import path
curdir = os.getcwd() + os.sep
sys.path.append(curdir + 'python')
sys.path.append(curdir + 'tools')
sys.path.append(curdir + 'docs')
old_dir = os.getcwd()
try:
print("Generating documentation.....")
import ida_pro
try:
ida_pro._BC695
print("'ida_pro._BC695' exists. Please recompile with BC695 undefined (see makefile). Bailing out.")
return -1
except:
pass # ok
os.chdir('docs')
gen_docs()
os.chdir(DOC_DIR)
patch_docs()
print("Documentation generated!")
finally:
os.chdir(old_dir)
# --------------------------------------------------------------------------
if __name__ == '__main__':
main()
qexit(0)