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