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
|
#! /usr/bin/python
# Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import date
import getopt
import os
import sys
import xml.dom.minidom
import ovs.json
from ovs.db import error
import ovs.db.schema
from build.nroff import *
argv0 = sys.argv[0]
def typeAndConstraintsToNroff(column):
type = column.type.toEnglish(escapeNroffLiteral)
constraints = column.type.constraintsToEnglish(escapeNroffLiteral,
textToNroff)
if constraints:
type += ", " + constraints
if column.unique:
type += " (must be unique within table)"
return type
def columnGroupToNroff(table, groupXml, documented_columns):
introNodes = []
columnNodes = []
for node in groupXml.childNodes:
if (node.nodeType == node.ELEMENT_NODE
and node.tagName in ('column', 'group')):
columnNodes += [node]
else:
if (columnNodes
and not (node.nodeType == node.TEXT_NODE
and node.data.isspace())):
raise error.Error("text follows <column> or <group> inside <group>: %s" % node)
introNodes += [node]
summary = []
intro = blockXmlToNroff(introNodes)
body = ''
for node in columnNodes:
if node.tagName == 'column':
name = node.attributes['name'].nodeValue
documented_columns.add(name)
column = table.columns[name]
if node.hasAttribute('key'):
key = node.attributes['key'].nodeValue
if node.hasAttribute('type'):
type_string = node.attributes['type'].nodeValue
type_json = ovs.json.from_string(str(type_string))
if type(type_json) in (str, unicode):
raise error.Error("%s %s:%s has invalid 'type': %s"
% (table.name, name, key, type_json))
type_ = ovs.db.types.BaseType.from_json(type_json)
else:
type_ = column.type.value
nameNroff = "%s : %s" % (name, key)
if column.type.value:
typeNroff = "optional %s" % column.type.value.toEnglish(
escapeNroffLiteral)
if (column.type.value.type == ovs.db.types.StringType and
type_.type == ovs.db.types.BooleanType):
# This is a little more explicit and helpful than
# "containing a boolean"
typeNroff += r", either \fBtrue\fR or \fBfalse\fR"
else:
if type_.type != column.type.value.type:
type_english = type_.toEnglish()
if type_english[0] in 'aeiou':
typeNroff += ", containing an %s" % type_english
else:
typeNroff += ", containing a %s" % type_english
constraints = (
type_.constraintsToEnglish(escapeNroffLiteral,
textToNroff))
if constraints:
typeNroff += ", %s" % constraints
else:
typeNroff = "none"
else:
nameNroff = name
typeNroff = typeAndConstraintsToNroff(column)
if not column.mutable:
typeNroff = "immutable %s" % typeNroff
body += '.IP "\\fB%s\\fR: %s"\n' % (nameNroff, typeNroff)
body += blockXmlToNroff(node.childNodes, '.IP') + "\n"
summary += [('column', nameNroff, typeNroff)]
elif node.tagName == 'group':
title = node.attributes["title"].nodeValue
subSummary, subIntro, subBody = columnGroupToNroff(
table, node, documented_columns)
summary += [('group', title, subSummary)]
body += '.ST "%s:"\n' % textToNroff(title)
body += subIntro + subBody
else:
raise error.Error("unknown element %s in <table>" % node.tagName)
return summary, intro, body
def tableSummaryToNroff(summary, level=0):
s = ""
for type, name, arg in summary:
if type == 'column':
s += ".TQ %.2fin\n\\fB%s\\fR\n%s\n" % (3 - level * .25, name, arg)
else:
s += ".TQ .25in\n\\fI%s:\\fR\n.RS .25in\n" % name
s += tableSummaryToNroff(arg, level + 1)
s += ".RE\n"
return s
def tableToNroff(schema, tableXml):
tableName = tableXml.attributes['name'].nodeValue
table = schema.tables[tableName]
documented_columns = set()
s = """.bp
.SH "%s TABLE"
""" % tableName
summary, intro, body = columnGroupToNroff(table, tableXml,
documented_columns)
s += intro
s += '.SS "Summary:\n'
s += tableSummaryToNroff(summary)
s += '.SS "Details:\n'
s += body
schema_columns = set(table.columns.keys())
undocumented_columns = schema_columns - documented_columns
for column in undocumented_columns:
raise error.Error("table %s has undocumented column %s"
% (tableName, column))
return s
def docsToNroff(schemaFile, xmlFile, erFile, version=None):
schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schemaFile))
doc = xml.dom.minidom.parse(xmlFile).documentElement
schemaDate = os.stat(schemaFile).st_mtime
xmlDate = os.stat(xmlFile).st_mtime
d = date.fromtimestamp(max(schemaDate, xmlDate))
if doc.hasAttribute('name'):
manpage = doc.attributes['name'].nodeValue
else:
manpage = schema.name
if version == None:
version = "UNKNOWN"
# Putting '\" p as the first line tells "man" that the manpage
# needs to be preprocessed by "pic".
s = r''''\" p
.\" -*- nroff -*-
.TH "%s" 5 " DB Schema %s" "Open vSwitch %s" "Open vSwitch Manual"
.fp 5 L CR \\" Make fixed-width font available as \\fL.
.de TQ
. br
. ns
. TP "\\$1"
..
.de ST
. PP
. RS -0.15in
. I "\\$1"
. RE
..
.SH NAME
%s \- %s database schema
.PP
''' % (manpage, schema.version, version, textToNroff(manpage), schema.name)
tables = ""
introNodes = []
tableNodes = []
summary = []
for dbNode in doc.childNodes:
if (dbNode.nodeType == dbNode.ELEMENT_NODE
and dbNode.tagName == "table"):
tableNodes += [dbNode]
name = dbNode.attributes['name'].nodeValue
if dbNode.hasAttribute("title"):
title = dbNode.attributes['title'].nodeValue
else:
title = name + " configuration."
summary += [(name, title)]
else:
introNodes += [dbNode]
documented_tables = set((name for (name, title) in summary))
schema_tables = set(schema.tables.keys())
undocumented_tables = schema_tables - documented_tables
for table in undocumented_tables:
raise error.Error("undocumented table %s" % table)
s += blockXmlToNroff(introNodes) + "\n"
s += r"""
.SH "TABLE SUMMARY"
.PP
The following list summarizes the purpose of each of the tables in the
\fB%s\fR database. Each table is described in more detail on a later
page.
.IP "Table" 1in
Purpose
""" % schema.name
for name, title in summary:
s += r"""
.TQ 1in
\fB%s\fR
%s
""" % (name, textToNroff(title))
if erFile:
s += """
.\\" check if in troff mode (TTY)
.if t \{
.bp
.SH "TABLE RELATIONSHIPS"
.PP
The following diagram shows the relationship among tables in the
database. Each node represents a table. Tables that are part of the
``root set'' are shown with double borders. Each edge leads from the
table that contains it and points to the table that its value
represents. Edges are labeled with their column names, followed by a
constraint on the number of allowed values: \\fB?\\fR for zero or one,
\\fB*\\fR for zero or more, \\fB+\\fR for one or more. Thick lines
represent strong references; thin lines represent weak references.
.RS -1in
"""
erStream = open(erFile, "r")
for line in erStream:
s += line + '\n'
erStream.close()
s += ".RE\\}\n"
for node in tableNodes:
s += tableToNroff(schema, node) + "\n"
return s
def usage():
print """\
%(argv0)s: ovsdb schema documentation generator
Prints documentation for an OVSDB schema as an nroff-formatted manpage.
usage: %(argv0)s [OPTIONS] SCHEMA XML
where SCHEMA is an OVSDB schema in JSON format
and XML is OVSDB documentation in XML format.
The following options are also available:
--er-diagram=DIAGRAM.PIC include E-R diagram from DIAGRAM.PIC
--version=VERSION use VERSION to display on document footer
-h, --help display this help message\
""" % {'argv0': argv0}
sys.exit(0)
if __name__ == "__main__":
try:
try:
options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
['er-diagram=',
'version=', 'help'])
except getopt.GetoptError, geo:
sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
sys.exit(1)
er_diagram = None
version = None
for key, value in options:
if key == '--er-diagram':
er_diagram = value
elif key == '--version':
version = value
elif key in ['-h', '--help']:
usage()
else:
sys.exit(0)
if len(args) != 2:
sys.stderr.write("%s: exactly 2 non-option arguments required "
"(use --help for help)\n" % argv0)
sys.exit(1)
# XXX we should warn about undocumented tables or columns
s = docsToNroff(args[0], args[1], er_diagram, version)
for line in s.split("\n"):
line = line.strip()
if len(line):
print line
except error.Error, e:
sys.stderr.write("%s: %s\n" % (argv0, e.msg))
sys.exit(1)
# Local variables:
# mode: python
# End:
|