Apparently the code to forestall Tk eating events was too aggressive (Tk user input...
[python/dscho.git] / Tools / scripts / xxci.py
blob2567bc508b624560ffb67201922cf247874fe74f
1 #! /usr/bin/env python
3 # xxci
5 # check in files for which rcsdiff returns nonzero exit status
7 import sys
8 import os
9 from stat import *
10 import commands
11 import fnmatch
12 import string
14 EXECMAGIC = '\001\140\000\010'
16 MAXSIZE = 200*1024 # Files this big must be binaries and are skipped.
18 def getargs():
19 args = sys.argv[1:]
20 if args:
21 return args
22 print 'No arguments, checking almost *, in "ls -t" order'
23 list = []
24 for file in os.listdir(os.curdir):
25 if not skipfile(file):
26 list.append((getmtime(file), file))
27 list.sort()
28 if not list:
29 print 'Nothing to do -- exit 1'
30 sys.exit(1)
31 list.sort()
32 list.reverse()
33 for mtime, file in list: args.append(file)
34 return args
36 def getmtime(file):
37 try:
38 st = os.stat(file)
39 return st[ST_MTIME]
40 except os.error:
41 return -1
43 badnames = ['tags', 'TAGS', 'xyzzy', 'nohup.out', 'core']
44 badprefixes = ['.', ',', '@', '#', 'o.']
45 badsuffixes = \
46 ['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \
47 '.pyc', '.fdc', '.rgb', '.elc', ',v']
48 ignore = []
50 def setup():
51 ignore[:] = badnames
52 for p in badprefixes:
53 ignore.append(p + '*')
54 for p in badsuffixes:
55 ignore.append('*' + p)
56 try:
57 f = open('.xxcign', 'r')
58 except IOError:
59 return
60 ignore[:] = ignore + string.split(f.read())
62 def skipfile(file):
63 for p in ignore:
64 if fnmatch.fnmatch(file, p): return 1
65 try:
66 st = os.lstat(file)
67 except os.error:
68 return 1 # Doesn't exist -- skip it
69 # Skip non-plain files.
70 if not S_ISREG(st[ST_MODE]): return 1
71 # Skip huge files -- probably binaries.
72 if st[ST_SIZE] >= MAXSIZE: return 1
73 # Skip executables
74 try:
75 data = open(file, 'r').read(len(EXECMAGIC))
76 if data == EXECMAGIC: return 1
77 except:
78 pass
79 return 0
81 def badprefix(file):
82 for bad in badprefixes:
83 if file[:len(bad)] == bad: return 1
84 return 0
86 def badsuffix(file):
87 for bad in badsuffixes:
88 if file[-len(bad):] == bad: return 1
89 return 0
91 def go(args):
92 for file in args:
93 print file + ':'
94 if differing(file):
95 showdiffs(file)
96 if askyesno('Check in ' + file + ' ? '):
97 sts = os.system('rcs -l ' + file) # ignored
98 sts = os.system('ci -l ' + file)
100 def differing(file):
101 cmd = 'co -p ' + file + ' 2>/dev/null | cmp -s - ' + file
102 sts = os.system(cmd)
103 return sts != 0
105 def showdiffs(file):
106 cmd = 'rcsdiff ' + file + ' 2>&1 | ${PAGER-more}'
107 sts = os.system(cmd)
109 def askyesno(prompt):
110 s = raw_input(prompt)
111 return s in ['y', 'yes']
113 try:
114 setup()
115 go(getargs())
116 except KeyboardInterrupt:
117 print '[Intr]'