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