initial commit
[public/postgresql-toys.git] / sqledit
1 #!/usr/bin/python
2 import sys
3 import os
4 import md5
5 import os.path
6
7 EDITOR = os.environ.get('EDITOR', 'vim')
8 def md5file(filename):
9         try:
10                 return md5.new(file(filename,'r').read()).hexdigest()
11         except:
12                 return ""
13 if len(sys.argv) < 2:
14         print "need command"
15         sys.exit(1)
16
17 mode = "unknown"
18 if os.path.exists('DIAGMODE') : mode = "diag"
19 if os.path.exists('PRODMODE'): mode = "prod"
20 if os.path.exists('.hg'): 
21         merc = 1
22 else :
23         merc = 0
24
25 if mode=="unknown":
26         print "Unable to determine DIAGMODE or PRODMODE. Please touch appropriate file in run-dir"
27         sys.exit(0);
28
29 cmd = sys.argv[0]
30 files = os.listdir('.')
31 files.sort()
32 lastfile=""
33 new = 0
34
35 for i in range(1,len(files) + 1):
36         j = -1 * i
37         f = files[j]
38         if f.endswith(".sql"):
39                 lastfile = f
40                 break;
41
42 if sys.argv[1] in ["current","c"]:
43         # no last file!
44         if len(lastfile) == 0: 
45                 print "No last file"
46                 sys.exit(0)
47         editfile =lastfile
48 elif sys.argv[1] in ["next","n"]:
49         new = 1
50         if len(sys.argv) == 3:
51                 input = sys.argv[2]
52         else:
53                 print "I need description as third argument"
54                 sys.exit(1)
55
56         if len(input):
57                 if len(lastfile):
58                         first = lastfile[:lastfile.find("_")]
59                         try:
60                                 number = int(first)
61                         except:
62                                 number = int(first[:-1])
63                         number += 1
64                 else:
65                         number = 1
66                 editfile =  "%05u_%s.sql" % (number,input)
67 else:
68         editfile = sys.argv[1].strip()
69         if not (os.path.exists(editfile) and os.path.isfile(editfile) ):
70                 print "no real file"
71                 editfile = ""
72
73 if editfile.find("_") == -1:
74         print "Malformed filename"
75         editfile = ""
76 else:
77         first = editfile[:editfile.find("_")]
78         try:
79                 number = int(first)
80         except:
81                 try:
82                         number = int(first[:-1])
83                 except:
84                         number = -1
85 print number
86 if number < 1: 
87         editfile = ""
88         print "No number"
89
90 if len(editfile) ==0: sys.exit(0)
91
92 before_md5 = md5file(editfile)
93 exitcode = os.system('%s %s' % (EDITOR, editfile))
94 after_md5 = md5file(editfile)
95
96 if (after_md5 != "" and before_md5 != after_md5):
97         if mode == 'prod':
98                 os.system("rundb %s" % (number,))
99         elif mode == 'diag':
100                 os.system("rundb %s" % (editfile,))
101         if merc == 1:
102                 if new:
103                         os.system("hg add " + editfile)
104                 print "Run mercurial ? (Yes [A]uto, Yes [M]anual, No)"
105                 c = ""
106                 while c not in ['a','m','n']:
107                         c = sys.stdin.read(1).lower()   
108                 if c == 'n':
109                         sys.exit()
110                 if c == 'a':
111                         os.system("hg ci -m `date +auto%Y%m%d%H%M%S`")
112                         sys.exit()
113                 if c == 'm':
114                         os.system("hg ci")
115                         sys.exit()
116