initial commit
[public/postgresql-toys.git] / rundb
1 #!/usr/bin/python
2 # Database script runner
3 # Copyright 2007 Svenne Krap
4 # License ASL 2.0
5 #
6 # Usage:
7 #   - ensure you have a file named DATABASE that contains the name of your target db 
8 #       ie "echo project_test > DATABASE"
9 #   - remember to exclude it from your SCM
10 #       ie. "echo DATABASE >> .gitignore" (if using git)
11 #   - if you want to run it in normal mode you need a file called PRODMODE
12 #       ie. "touch PRODMODE"
13 #
14 #   Files needs to be called nnnn_XXXXXXX.sql (or use sqledit)
15 #       i.e. any amount of digits (0-9) followed by one underscore followed by some
16 #       string followed by ".sql"
17 #
18 #   Run the command as rundb nnnn 
19 #   
20 import sys
21 import os
22 import time
23
24 PAGER = os.environ.get('PAGER', 'more')
25 argc = len(sys.argv)
26
27 try:
28         db = open("DATABASE","r").read().strip()
29 except:
30         print "Need database name in file DATABASE"
31         sys.exit(1)
32
33 files = os.listdir('.')
34 files.sort()
35 mode = "unknown"
36 if 'DIAGMODE' in files: mode = "diag"
37 if 'PRODMODE' in files: mode = "prod"
38 if mode=="unknown":
39         print "Unable to determine DIAGMODE or PRODMODE. Please touch appropriate file in run-dir"
40         sys.exit(0);
41
42 try:
43         os.chdir("output")
44 except:
45         os.mkdir("output")
46         os.chdir("output")
47 os.chdir("..")
48
49
50
51 rt = time.localtime()
52 logfile = "output/run_" + time.strftime("%Y-%m-%d-%H-%M-%S",rt)  +  ".log"
53
54
55 if argc < 2 or argc > 3: 
56         print "Syntax: rundb.py <from> [exclude_tags]"
57         print
58         print "dbname = name of database"
59         print "from = integer of first command"
60         print "exclude_tags = comma separated list of strings in filename to exclude"
61         if argc == 1:
62                 print "Log is written to " + logfile
63                 os.system("psql -d %s -e -L %s" % (db,logfile) )
64                 print "Log was written to " + logfile
65                 sys.exit(0)
66         sys.exit(1)
67
68 def db_run_file(db,f,logfile):
69         os.system("echo \"###RESULT### %s\" >>%s 2>&1" % (f,logfile))
70         os.system("psql --single-transaction -d %s -f %s >>%s 2>&1" % (db,f,logfile) )
71         os.system("echo \"###RUN### %s\" >>%s 2>&1" % (f,logfile))
72         os.system("cat %s  >>%s 2>&1" % (f,logfile) )
73
74 if argc >= 3:
75         exclude_tags = sys.argv[2].strip().split(",")
76 else:
77         exclude_tags = []
78
79 if mode=='prod':
80         run_from = int(sys.argv[1])
81         for f in files:
82                 good = True
83                 if not f.endswith(".sql"): continue
84                 pos = f.find("_")
85                 if pos == -1: continue
86                 first = f[:pos]
87                 try:
88                         num = int(first)
89                 except:
90                         num = int(first[:-1])
91                 if num < run_from: continue
92                 for x in exclude_tags:
93                         if not f.find(x) == -1: 
94                                 good = False
95                                 break
96                 if good == False: continue
97                 db_run_file(db,f,logfile)
98 elif mode=='diag': 
99         filename = sys.argv[1] 
100         if os.path.exists(filename) and os.path.isfile(filename):
101                 db_run_file(db,filename,logfile)
102
103 if os.path.exists(logfile):
104         os.system('%s %s' % (PAGER, logfile))