]>
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 | ||
d5a59e7e | 13 | def printEdge(tableName, type, baseType, label): |
7cba02e4 | 14 | if baseType.ref_table_name: |
d5a59e7e BP |
15 | if type.n_min == 0: |
16 | if type.n_max == 1: | |
17 | arity = "?" | |
bff7da4e | 18 | elif type.n_max == sys.maxsize: |
d5a59e7e BP |
19 | arity = "*" |
20 | else: | |
21 | arity = "{,%d}" % type.n_max | |
22 | elif type.n_min == 1: | |
23 | if type.n_max == 1: | |
24 | arity = "" | |
bff7da4e | 25 | elif type.n_max == sys.maxsize: |
d5a59e7e BP |
26 | arity = "+" |
27 | else: | |
28 | arity = "{1,%d}" % type.n_max | |
29 | ||
f8d739a9 | 30 | options = {} |
d5a59e7e | 31 | options['label'] = '"%s%s"' % (label, arity) |
a5eef57e | 32 | if baseType.ref_type == 'weak': |
c5f341ab | 33 | options['style'] = 'dotted' |
6add8d71 | 34 | print ("\t%s -> %s [%s];" % ( |
f8d739a9 | 35 | tableName, |
7cba02e4 | 36 | baseType.ref_table_name, |
6add8d71 | 37 | ', '.join(['%s=%s' % (k,v) for k,v in options.items()]))) |
f8d739a9 | 38 | |
af4e1a4a | 39 | def schemaToDot(schemaFile, arrows): |
a5eef57e | 40 | schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schemaFile)) |
f8d739a9 | 41 | |
6add8d71 MC |
42 | print ("digraph %s {" % schema.name) |
43 | print ('\trankdir=LR;') | |
44 | print ('\tsize="6.5,4";') | |
45 | print ('\tmargin="0";') | |
46 | print ("\tnode [shape=box];") | |
af4e1a4a | 47 | if not arrows: |
6add8d71 | 48 | print ("\tedge [dir=none, arrowhead=none, arrowtail=none];") |
81068700 | 49 | for tableName, table in schema.tables.items(): |
c5f341ab BP |
50 | options = {} |
51 | if table.is_root: | |
52 | options['style'] = 'bold' | |
6add8d71 | 53 | print ("\t%s [%s];" % ( |
c5f341ab | 54 | tableName, |
6add8d71 | 55 | ', '.join(['%s=%s' % (k,v) for k,v in options.items()]))) |
81068700 | 56 | for columnName, column in table.columns.items(): |
f8d739a9 | 57 | if column.type.value: |
d5a59e7e BP |
58 | printEdge(tableName, column.type, column.type.key, "%s key" % columnName) |
59 | printEdge(tableName, column.type, column.type.value, "%s value" % columnName) | |
f8d739a9 | 60 | else: |
d5a59e7e | 61 | printEdge(tableName, column.type, column.type.key, columnName) |
6add8d71 | 62 | print ("}"); |
f8d739a9 BP |
63 | |
64 | def usage(): | |
6add8d71 | 65 | print ("""\ |
f8d739a9 BP |
66 | %(argv0)s: compiles ovsdb schemas to graphviz format |
67 | Prints a .dot file that "dot" can render to an entity-relationship diagram | |
68 | usage: %(argv0)s [OPTIONS] SCHEMA | |
69 | where SCHEMA is an OVSDB schema in JSON format | |
70 | ||
71 | The following options are also available: | |
af4e1a4a | 72 | --no-arrows omit arrows from diagram |
f8d739a9 BP |
73 | -h, --help display this help message |
74 | -V, --version display version information\ | |
6add8d71 | 75 | """ % {'argv0': argv0}) |
f8d739a9 BP |
76 | sys.exit(0) |
77 | ||
78 | if __name__ == "__main__": | |
79 | try: | |
80 | try: | |
81 | options, args = getopt.gnu_getopt(sys.argv[1:], 'hV', | |
af4e1a4a BP |
82 | ['no-arrows', |
83 | 'help', 'version',]) | |
9a0dbc7b | 84 | except getopt.GetoptError as geo: |
f8d739a9 BP |
85 | sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) |
86 | sys.exit(1) | |
87 | ||
af4e1a4a | 88 | arrows = True |
f8d739a9 | 89 | for key, value in options: |
af4e1a4a BP |
90 | if key == '--no-arrows': |
91 | arrows = False | |
92 | elif key in ['-h', '--help']: | |
f8d739a9 BP |
93 | usage() |
94 | elif key in ['-V', '--version']: | |
6add8d71 | 95 | print ("ovsdb-dot (Open vSwitch) @VERSION@") |
f8d739a9 BP |
96 | else: |
97 | sys.exit(0) | |
c5c7c7c5 | 98 | |
f8d739a9 BP |
99 | if len(args) != 1: |
100 | sys.stderr.write("%s: exactly 1 non-option argument required " | |
101 | "(use --help for help)\n" % argv0) | |
102 | sys.exit(1) | |
103 | ||
af4e1a4a | 104 | schemaToDot(args[0], arrows) |
c5c7c7c5 | 105 | |
9a0dbc7b | 106 | except ovs.db.error.Error as e: |
f8d739a9 BP |
107 | sys.stderr.write("%s: %s\n" % (argv0, e.msg)) |
108 | sys.exit(1) | |
109 | ||
110 | # Local variables: | |
111 | # mode: python | |
112 | # End: |