Incorrect accquiring bugdir command line argument
[be.git] / doc / generate-libbe-txt.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2010-2012 Chris Ball <cjb@laptop.org>
4 #                         W. Trevor King <wking@tremily.us>
5 #
6 # This file is part of Bugs Everywhere.
7 #
8 # Bugs Everywhere is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by the Free
10 # Software Foundation, either version 2 of the License, or (at your option) any
11 # later version.
12 #
13 # Bugs Everywhere is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16 # more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
20
21 """Auto-generate reStructuredText of the libbe module tree for Sphinx.
22 """
23
24 import sys
25 import os, os.path
26
27 sys.path.insert(0, os.path.abspath('..'))
28 from test import python_tree
29
30 def title(modname):
31     t = ':py:mod:`%s`' % modname
32     delim = '*'*len(t)
33     return '\n'.join([delim, t, delim, '', ''])
34
35 def automodule(modname):
36     return '\n'.join([
37             '.. automodule:: %s' % modname,
38             '   :members:',
39             '   :undoc-members:',
40             '', ''])
41
42 def toctree(children):
43     if len(children) == 0:
44         return ''
45     return '\n'.join([
46             '.. toctree::',
47             '   :maxdepth: 2',
48             '',
49             ] + [
50             '   %s.txt' % c for c in sorted(children)
51             ] + ['', ''])
52
53 def make_module_txt(modname, children, subdir='libbe'):
54     filename = os.path.join(subdir, '%s.txt' % modname)
55     if not os.path.exists(subdir):
56         os.mkdir(subdir)
57     if os.path.exists(filename):
58         return None # don't overwrite potentially hand-written files.
59     f = file(filename, 'w')
60     f.write(title(modname))
61     f.write(automodule(modname))
62     f.write(toctree(children))
63     f.close()
64
65 if __name__ == '__main__':
66     pt = python_tree(root_path='../libbe', root_modname='libbe')
67     for node in pt.traverse():
68         make_module_txt(node.modname, [c.modname for c in node])