forked from BrightcoveOS/Diamond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_doc.py
executable file
·218 lines (162 loc) · 6.89 KB
/
build_doc.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
#!/usr/bin/env python
################################################################################
import os
import sys
import optparse
import configobj
import traceback
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))
def getIncludePaths(path):
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isfile(cPath) and len(f) > 3 and f[-3:] == '.py':
sys.path.append(os.path.dirname(cPath))
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isdir(cPath):
getIncludePaths(cPath)
collectors = {}
def getCollectors(path):
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isfile(cPath) and len(f) > 3 and f[-3:] == '.py':
modname = f[:-3]
try:
# Import the module
module = __import__(modname, globals(), locals(), ['*'])
# Find the name
for attr in dir(module):
if not attr.endswith('Collector'):
continue
cls = getattr(module, attr)
if cls.__name__ not in collectors:
collectors[cls.__name__] = module
except Exception:
print "Failed to import module: %s. %s" % (
modname, traceback.format_exc())
collectors[modname] = False
continue
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isdir(cPath):
getCollectors(cPath)
handlers = {}
def getHandlers(path):
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isfile(cPath) and len(f) > 3 and f[-3:] == '.py':
modname = f[:-3]
try:
# Import the module
module = __import__(modname, globals(), locals(), ['*'])
# Find the name
for attr in dir(module):
if (not attr.endswith('Handler')
or attr.startswith('Handler')):
continue
cls = getattr(module, attr)
if cls.__name__ not in handlers:
handlers[cls.__name__] = module
except Exception:
print "Failed to import module: %s. %s" % (
modname, traceback.format_exc())
handlers[modname] = False
continue
for f in os.listdir(path):
cPath = os.path.abspath(os.path.join(path, f))
if os.path.isdir(cPath):
getHandlers(cPath)
################################################################################
if __name__ == "__main__":
# Initialize Options
parser = optparse.OptionParser()
parser.add_option("-c", "--configfile",
dest="configfile",
default="/etc/diamond/diamond.conf",
help="Path to the config file")
parser.add_option("-C", "--collector",
dest="collector",
default=None,
help="Configure a single collector")
parser.add_option("-p", "--print",
action="store_true",
dest="dump",
default=False,
help="Just print the defaults")
# Parse Command Line Args
(options, args) = parser.parse_args()
# Initialize Config
if os.path.exists(options.configfile):
config = configobj.ConfigObj(os.path.abspath(options.configfile))
config['configfile'] = options.configfile
else:
print >> sys.stderr, "ERROR: Config file: %s does not exist." % (
options.configfile)
print >> sys.stderr, ("Please run python config.py -c "
+ "/path/to/diamond.conf")
parser.print_help(sys.stderr)
sys.exit(1)
collector_path = config['server']['collectors_path']
docs_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'docs'))
handler_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'src', 'diamond', 'handler'))
getIncludePaths(collector_path)
# Ugly hack for snmp collector overrides
getCollectors(os.path.join(collector_path, 'snmp'))
getCollectors(collector_path)
collectorIndexFile = open(os.path.join(docs_path, "Collectors.md"), 'w')
collectorIndexFile.write("## Collectors\n")
collectorIndexFile.write("\n")
for collector in sorted(collectors.iterkeys()):
# Skip configuring the basic collector object
if collector == "Collector":
continue
if collector.startswith('Test'):
continue
print "Processing %s..." % (collector)
if not hasattr(collectors[collector], collector):
continue
cls = getattr(collectors[collector], collector)
obj = cls(config=config, handlers={})
options = obj.get_default_config_help()
docFile = open(os.path.join(docs_path,
"collectors-" + collector + ".md"), 'w')
collectorIndexFile.write(" - [%s](collectors-%s)\n" % (collector,
collector))
docFile.write("%s\n" % (collector))
docFile.write("=====\n")
docFile.write("%s" % (collectors[collector].__doc__))
docFile.write("#### Options\n")
docFile.write("\n")
docFile.write(" * [Generic Options](Configuration)\n")
for option in options:
docFile.write(" * %s: %s\n" % (option, options[option]))
docFile.write("\n")
docFile.write("#### Example Output\n")
docFile.write("\n")
docFile.write("```\n")
docFile.write("__EXAMPLESHERE__\n")
docFile.write("```\n")
docFile.write("\n")
docFile.close()
collectorIndexFile.close()
getIncludePaths(handler_path)
getHandlers(handler_path)
handlerIndexFile = open(os.path.join(docs_path, "Handlers.md"), 'w')
handlerIndexFile.write("## Handlers\n")
handlerIndexFile.write("\n")
for handler in sorted(handlers.iterkeys()):
# Skip configuring the basic handler object
if handler == "Handler":
continue
print "Processing %s..." % (handler)
if not hasattr(handlers[handler], handler):
continue
docFile = open(os.path.join(docs_path,
"handler-" + handler + ".md"), 'w')
handlerIndexFile.write(" - [%s](handler-%s)\n" % (handler, handler))
docFile.write("%s\n" % (handler))
docFile.write("====\n")
docFile.write("%s" % (handlers[handler].__doc__))
docFile.close()
handlerIndexFile.close()