bug in the tarval modul ...
[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 import sha
8 import sqlite3
9
10 from optparse import OptionParser
11
12 def ev_create_stmt(heads):
13         create = 'create table if not exists ev (id int, time int, timeev int'
14         for x in heads:
15                 create += (', %s real' % (x,))
16         create += ');'
17         return create
18
19 def ctx_create_stmt(heads):
20         create = 'create table if not exists ctx (id int unique on conflict ignore'
21         for x in heads:
22                 create += (', %s text' % (x,))
23         create += ');'
24         return create
25
26 def find_heads(file):
27         ctx_heads = set()
28         ev_heads = set()
29         for line in fileinput.input(file):
30                 items = re.split('\s+', line)
31                 if items[0] == 'E':
32                         ev_heads.add(items[2])
33                 elif items[0] == 'P':
34                         ctx_heads.add(items[2])
35         return (ev_heads, ctx_heads)
36
37 def fill_tables(conn, file):
38         curr_id  = 0
39         ids      = dict()
40         valstack = []
41         keystack = []
42         for line in fileinput.input(file):
43                 items = re.split('\s+', line)
44                 op    = items[0]
45                 id    = items[1]
46
47                 if op == 'P':
48                         key   = items[2]
49                         val   = items[3]
50                         keystack.append(key)
51                         valstack.append(val)
52
53                         dig = sha.new()
54                         for i in xrange(0,len(keystack)):
55                                 dig.update(".")
56                                 dig.update(keystack[i])
57                                 dig.update("=")
58                                 dig.update(valstack[i])
59
60                         hash = dig.digest()
61                         if hash in ids:
62                                 id = ids[hash]
63                         else:
64                                 id        = curr_id
65                                 ids[hash] = curr_id
66                                 curr_id   = curr_id + 1
67
68                         stmt = 'insert into ctx (id'
69                         for x in keystack:
70                                 stmt += ', ' + x
71                         stmt += ') values (' + str(id)
72                         for x in valstack:
73                                 stmt += ', \'' + x + '\''
74                         stmt += ');'
75                         conn.execute(stmt)
76
77                 elif op == 'O':
78                         keystack.pop()
79                         valstack.pop()
80
81                 elif op == 'E':
82                         key   = items[2]
83                         val   = items[3]
84                         time   = items[4]
85                         timeev = items[5]
86                         t      = (id, val, time, timeev)
87                         stmt   = 'insert into ev (id, %s, time, timeev) values (?, ?, ?, ?)' % (key,)
88                         conn.execute(stmt, (id, val, time, timeev))
89
90
91 def main():
92         parser = OptionParser('usage: %prog [options] <sqlite3  database> <event file>')
93         parser.add_option("-c", "--clean",  action="store_true", help="clean existing data base before adding data")
94         (options, args) = parser.parse_args()
95
96         if len(args) < 2:
97                 parser.print_help()
98                 sys.exit(1)
99
100         db   = args[0]
101         file = args[1]
102
103         if not os.path.isfile(file):
104                 print "cannot find input file %s" % (file, )
105                 sys.exit(2)
106
107         have_to_clean = 0
108         if os.path.isfile(db):
109                 if options.clean:
110                         have_to_clean = 1
111                 else:
112                         print "database %s already exists (use different name or use -c)" % (db, )
113                         sys.exit(3)
114
115         (ev_heads, ctx_heads) = find_heads(file)
116
117         conn = sqlite3.connect(db)
118         if have_to_clean:
119                 conn.execute("drop table ctx")
120                 conn.execute("drop table ev")
121
122         conn.execute(ev_create_stmt(ev_heads))
123         conn.execute(ctx_create_stmt(ctx_heads))
124         fill_tables(conn, file)
125         conn.commit()
126
127 if __name__ == "__main__":
128         main()