home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / scripts / xxci.py < prev   
Text File  |  1994-01-07  |  2KB  |  118 lines

  1. #! /usr/local/bin/python
  2.  
  3. # xxci
  4. #
  5. # check in files for which rcsdiff returns nonzero exit status
  6.  
  7. import sys
  8. import os
  9. from stat import *
  10. import commands
  11. import fnmatch
  12. import string
  13.  
  14. EXECMAGIC = '\001\140\000\010'
  15.  
  16. MAXSIZE = 200*1024 # Files this big must be binaries and are skipped.
  17.  
  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
  35.  
  36. def getmtime(file):
  37.     try:
  38.         st = os.stat(file)
  39.         return st[ST_MTIME]
  40.     except os.error:
  41.         return -1
  42.  
  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 = []
  49.  
  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())
  61.  
  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
  80.  
  81. def badprefix(file):
  82.     for bad in badprefixes:
  83.         if file[:len(bad)] == bad: return 1
  84.     return 0
  85.  
  86. def badsuffix(file):
  87.     for bad in badsuffixes:
  88.         if file[-len(bad):] == bad: return 1
  89.     return 0
  90.  
  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)
  99.  
  100. def differing(file):
  101.     cmd = 'co -p ' + file + ' 2>/dev/null | cmp -s - ' + file
  102.     sts = os.system(cmd)
  103.     return sts != 0
  104.  
  105. def showdiffs(file):
  106.     cmd = 'rcsdiff ' + file + ' 2>&1 | ${PAGER-more}'
  107.     sts = os.system(cmd)
  108.  
  109. def askyesno(prompt):
  110.     s = raw_input(prompt)
  111.     return s in ['y', 'yes']
  112.  
  113. try:
  114.     setup()
  115.     go(getargs())
  116. except KeyboardInterrupt:
  117.     print '[Intr]'
  118.