-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbutil.py
executable file
·66 lines (55 loc) · 1.79 KB
/
dbutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
import sys
import sqlite3
import hcidemux.pipelineparams as params
TAB="\t"
def Dump( conn ):
c = conn.cursor()
c.execute("select id, dirname, state from run order by id")
recs = c.fetchall()
for rec in recs:
print TAB.join(list(rec))
def AddRun( conn, run_id, dirname ):
cursor = conn.cursor()
t = ( run_id, dirname, 'a_new' )
try:
retval1 = cursor.execute("insert into run (id,dirname,state) values ('%s','%s','%s')" % t )
retval2 = conn.commit()
except sqlite3.IntegrityError:
print "Run %s is already in the database." % run_id
return
print "retval1: %s, retval2: %s" % ( retval1, retval2)
def ChangeState( conn, run_id, new_state ):
cursor = conn.cursor()
t = ( new_state, run_id )
retval1=cursor.execute("update run set state = '%s' where id = '%s'" % t )
retval2=conn.commit()
print "retval1: %s, retval2: %s" % ( retval1, retval2)
def DeleteRun( conn, run_id ):
cursor = conn.cursor()
retval1=cursor.execute("delete from run where id = '%s'" % run_id )
retval2=conn.commit()
print "retval1: %s, retval2: %s" % ( retval1, retval2)
def usage():
print "Use: %s --help | --state <run_id> <state_name> | --delete <run_id> | --add <run_id> <dirname> | --dump" % sys.argv[0]
def main():
# If no args or incorrect args, print the usage message.
if len(sys.argv) < 2 or sys.argv[1] not in ['--help','--state','--delete','--add','--dump']:
usage()
sys.exit(1)
if sys.argv[1] == '--help':
usage()
sys.exit(0)
conn = sqlite3.connect(params.db_file)
if sys.argv[1] == '--delete':
DeleteRun( conn, sys.argv[2] )
elif sys.argv[1] == '--add':
AddRun( conn, sys.argv[2], sys.argv[3] )
elif sys.argv[1] == '--dump':
Dump( conn )
elif sys.argv[1] == '--state':
ChangeState( conn, sys.argv[2], sys.argv[3] )
else:
usage()
if __name__ == "__main__":
main()