Nicified the options
[libfirm] / ir / be / test / statev_sql.py
1 #! /usr/bin/env python
2
3 import sys
4 import fileinput
5 import os
6 import re
7
8 from optparse import OptionParser
9
10 def ev_create_stmt(heads):
11         create = 'create table if not exists ev (id int, time int, timeev int'
12         for x in heads:
13                 create += (', %s real' % (x,))
14         create += ');'
15         return create
16
17 def ctx_create_stmt(heads):
18         create = 'create table if not exists ctx (id int unique on conflict ignore'
19         for x in heads:
20                 create += (', %s text' % (x,))
21         create += ');'
22         return create
23
24 def find_heads(file):
25         ctx_heads = set()
26         ev_heads = set()
27         for line in fileinput.input(file):
28                 items = re.split('\s+', line)
29                 if items[0] == 'E':
30                         ev_heads.add(items[2])
31                 elif items[0] == 'P':
32                         ctx_heads.add(items[2])
33         return (ev_heads, ctx_heads)
34
35 def fill_tables(file):
36         keystack = []
37         valstack = []
38         for line in fileinput.input(file):
39                 items = re.split('\s+', line)
40                 op    = items[0]
41                 id    = items[1]
42                 if op == 'P':
43                         key   = items[2]
44                         val   = items[3]
45                         keystack.append(key)
46                         valstack.append(val)
47                         stmt = 'insert into ctx (id'
48                         for x in keystack:
49                                 stmt += ', ' + x
50                         stmt += ') values (' + str(int(id, 16))
51                         for x in valstack:
52                                 stmt += ', \'' + x + '\''
53                         stmt += ');'
54                 elif op == 'O':
55                         keystack.pop()
56                         valstack.pop()
57                         stmt = ''
58                 elif op == 'E':
59                         key   = items[2]
60                         val   = items[3]
61                         time   = items[4]
62                         timeev = items[5]
63                         t      = (id, val, time, timeev)
64                         stmt   = 'insert into ev (id, %s, time, timeev) values (%d, %s, %s, %s);' % (key, int(id, 16), val, time, timeev)
65
66                 if stmt:
67                         print stmt
68
69
70 def main():
71         parser = OptionParser('usage: %prog [options] input')
72         parser.add_option("-s", "--schema", action="store_true", help="emit only the schema definitions")
73         parser.add_option("-e", "--events", action="store_true", help="emit only the event information")
74         (options, args) = parser.parse_args()
75
76         if len(args) < 1:
77                 parser.print_help()
78                 sys.exit(1)
79
80         file = args[0]
81
82         if not os.path.isfile(file):
83                 print "cannot find input file %s" % (file, )
84                 sys.exit(2)
85
86         (ev_heads, ctx_heads) = find_heads(file)
87
88         if not options.schema and not options.events:
89                 options.schema = 1
90                 options.events = 1
91
92         if options.schema:
93                 print ev_create_stmt(ev_heads)
94                 print ctx_create_stmt(ctx_heads)
95
96         if options.events:
97                 print 'begin transaction;'
98                 fill_tables(file)
99                 print 'commit;'
100
101
102 if __name__ == "__main__":
103         main()