docs: Implement our own dot->pic translator.
[sliver-openvswitch.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 "\tedge [dir=none, arrowhead=none, arrowtail=none];"
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         
79     except ovs.db.error.Error, e:
80         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
81         sys.exit(1)
82
83 # Local variables:
84 # mode: python
85 # End: