]>
Commit | Line | Data |
---|---|---|
f8d739a9 BP |
1 | #! @PYTHON@ |
2 | ||
3 | from datetime import date | |
a5eef57e BP |
4 | import ovs.db.error |
5 | import ovs.db.schema | |
f8d739a9 BP |
6 | import getopt |
7 | import os | |
8 | import re | |
9 | import sys | |
10 | ||
f8d739a9 BP |
11 | argv0 = sys.argv[0] |
12 | ||
13 | def printEdge(tableName, baseType, label): | |
a5eef57e | 14 | if baseType.ref_table: |
f8d739a9 BP |
15 | options = {} |
16 | options['label'] = '"%s"' % label | |
a5eef57e | 17 | if baseType.ref_type == 'weak': |
f8d739a9 BP |
18 | options['constraint'] = 'false' |
19 | print "\t%s -> %s [%s];" % ( | |
20 | tableName, | |
a5eef57e | 21 | baseType.ref_table, |
f8d739a9 BP |
22 | ', '.join(['%s=%s' % (k,v) for k,v in options.items()])) |
23 | ||
24 | def schemaToDot(schemaFile): | |
a5eef57e | 25 | schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schemaFile)) |
f8d739a9 BP |
26 | |
27 | print "digraph %s {" % schema.name | |
28 | for tableName, table in schema.tables.iteritems(): | |
29 | print '\tsize="6.5,4";' | |
30 | print '\tmargin="0";' | |
31 | print "\tnode [shape=box];" | |
fca64c12 | 32 | print "\tedge [dir=none, arrowhead=none, arrowtail=none];" |
f8d739a9 BP |
33 | print "\t%s;" % tableName |
34 | for columnName, column in table.columns.iteritems(): | |
35 | if column.type.value: | |
36 | printEdge(tableName, column.type.key, "%s key" % columnName) | |
37 | printEdge(tableName, column.type.value, "%s value" % columnName) | |
38 | else: | |
39 | printEdge(tableName, column.type.key, columnName) | |
40 | print "}"; | |
41 | ||
42 | def usage(): | |
43 | print """\ | |
44 | %(argv0)s: compiles ovsdb schemas to graphviz format | |
45 | Prints a .dot file that "dot" can render to an entity-relationship diagram | |
46 | usage: %(argv0)s [OPTIONS] SCHEMA | |
47 | where SCHEMA is an OVSDB schema in JSON format | |
48 | ||
49 | The following options are also available: | |
50 | -h, --help display this help message | |
51 | -V, --version display version information\ | |
52 | """ % {'argv0': argv0} | |
53 | sys.exit(0) | |
54 | ||
55 | if __name__ == "__main__": | |
56 | try: | |
57 | try: | |
58 | options, args = getopt.gnu_getopt(sys.argv[1:], 'hV', | |
59 | ['help', 'version']) | |
60 | except getopt.GetoptError, geo: | |
61 | sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) | |
62 | sys.exit(1) | |
63 | ||
64 | for key, value in options: | |
65 | if key in ['-h', '--help']: | |
66 | usage() | |
67 | elif key in ['-V', '--version']: | |
68 | print "ovsdb-dot (Open vSwitch) @VERSION@" | |
69 | else: | |
70 | sys.exit(0) | |
71 | ||
72 | if len(args) != 1: | |
73 | sys.stderr.write("%s: exactly 1 non-option argument required " | |
74 | "(use --help for help)\n" % argv0) | |
75 | sys.exit(1) | |
76 | ||
77 | schemaToDot(args[0]) | |
78 | ||
a5eef57e | 79 | except ovs.db.error.Error, e: |
f8d739a9 BP |
80 | sys.stderr.write("%s: %s\n" % (argv0, e.msg)) |
81 | sys.exit(1) | |
82 | ||
83 | # Local variables: | |
84 | # mode: python | |
85 | # End: |