-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_rst.py
executable file
·96 lines (80 loc) · 2.89 KB
/
generate_rst.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
#!/usr/bin/env python
import os
import glob
from os.path import join, dirname, abspath, basename
import gevent
# do not generate .rst for the following modules as they imported into gevent package
# and covered there
SKIP = ['hub', 'timeout', 'greenlet']
template = '''.. AUTOGENERATED -- will be overwritten (remove this comment to save changes)
%(title)s
%(title_underline)s
.. automodule:: gevent.%(module)s
:members:
:undoc-members:
'''
directory = dirname(abspath(gevent.__file__))
print 'Imported gevent from %s' % (directory, )
modules = glob.glob(join(directory, '*.py')) + glob.glob(join(directory, '*.pyc'))
modules = set(basename(filename).split('.')[0] for filename in modules)
modules = set(name for name in modules if not name.startswith('_'))
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
def generate_rst_for_module(module, do=True):
rst_filename = 'gevent.%s.rst' % module
exists = os.path.exists(rst_filename)
if exists:
autogenerated = 'autogenerated' in open(rst_filename).read(200).lower()
if not autogenerated:
return
m = __import__('gevent.%s' % module)
m = getattr(m, module)
title = getattr(m, '__doc__', None)
if title:
title = title.strip().split('\n')[0]
title = title.strip(' .')
prefix = ':mod:`gevent.%s`' % module
if title:
title = prefix + ' -- %s' % (title, )
else:
title = prefix
title_underline = '=' * len(title)
params = globals().copy()
params.update(locals())
result = template % params
if exists:
if open(rst_filename).read(len(result) + 1) == result:
return # already exists one which is the same
if do:
print 'Generated %s from %s' % (rst_filename, m.__file__)
open(rst_filename, 'w').write(result)
else:
print 'Would generate %s from %s' % (rst_filename, m.__file__)
def generate_rst(do=True):
assert os.path.exists('contents.rst'), 'Wrong directory, contents.rst not found'
for module in modules:
if module not in SKIP:
generate_rst_for_module(module, do=do)
def iter_autogenerated():
for module in modules:
rst_filename = 'gevent.%s.rst' % module
exists = os.path.exists(rst_filename)
if exists:
autogenerated = 'autogenerated' in open(rst_filename).read(200).lower()
if autogenerated:
yield rst_filename
if __name__ == '__main__':
import sys
if sys.argv[1:] == ['show']:
for filename in iter_autogenerated():
print filename
elif sys.argv[1:] == ['delete']:
for filename in iter_autogenerated():
print 'Removing', filename
os.unlink(filename)
elif sys.argv[1:] == ['generate']:
generate_rst()
elif sys.argv[1:] == []:
generate_rst(do=False)
else:
sys.exit('Invalid command line: %s' % (sys.argv[1:], ))