3 from datetime import date
13 def printEdge(tableName, type, baseType, label):
14 if baseType.ref_table_name:
18 elif type.n_max == sys.maxint:
21 arity = "{,%d}" % type.n_max
25 elif type.n_max == sys.maxint:
28 arity = "{1,%d}" % type.n_max
31 options['label'] = '"%s%s"' % (label, arity)
32 if baseType.ref_type == 'weak':
33 options['constraint'] = 'false'
34 options['style'] = 'dotted'
35 print "\t%s -> %s [%s];" % (
37 baseType.ref_table_name,
38 ', '.join(['%s=%s' % (k,v) for k,v in options.items()]))
40 def schemaToDot(schemaFile, arrows):
41 schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schemaFile))
43 print "digraph %s {" % schema.name
45 print '\tsize="6.5,4";'
47 print "\tnode [shape=box];"
49 print "\tedge [dir=none, arrowhead=none, arrowtail=none];"
50 for tableName, table in schema.tables.iteritems():
53 options['style'] = 'bold'
54 print "\t%s [%s];" % (
56 ', '.join(['%s=%s' % (k,v) for k,v in options.items()]))
57 for columnName, column in table.columns.iteritems():
59 printEdge(tableName, column.type, column.type.key, "%s key" % columnName)
60 printEdge(tableName, column.type, column.type.value, "%s value" % columnName)
62 printEdge(tableName, column.type, column.type.key, columnName)
67 %(argv0)s: compiles ovsdb schemas to graphviz format
68 Prints a .dot file that "dot" can render to an entity-relationship diagram
69 usage: %(argv0)s [OPTIONS] SCHEMA
70 where SCHEMA is an OVSDB schema in JSON format
72 The following options are also available:
73 --no-arrows omit arrows from diagram
74 -h, --help display this help message
75 -V, --version display version information\
76 """ % {'argv0': argv0}
79 if __name__ == "__main__":
82 options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
85 except getopt.GetoptError, geo:
86 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
90 for key, value in options:
91 if key == '--no-arrows':
93 elif key in ['-h', '--help']:
95 elif key in ['-V', '--version']:
96 print "ovsdb-dot (Open vSwitch) @VERSION@"
101 sys.stderr.write("%s: exactly 1 non-option argument required "
102 "(use --help for help)\n" % argv0)
105 schemaToDot(args[0], arrows)
107 except ovs.db.error.Error, e:
108 sys.stderr.write("%s: %s\n" % (argv0, e.msg))