home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / scripts / tabpolice.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.1 KB  |  105 lines

  1. #! /usr/bin/env python
  2.  
  3. """The Tab Police watches for possibly inconsistent indentation."""
  4.  
  5. import os
  6. import sys
  7. import getopt
  8. import tokenize
  9.  
  10. verbose = 0
  11. quiet = 0
  12.  
  13. def main():
  14.     global verbose, quiet
  15.     try:
  16.         opts, args = getopt.getopt(sys.argv[1:], "qv")
  17.     except getopt.error, msg:
  18.         print msg
  19.         return
  20.     for o, a in opts:
  21.         if o == '-v':
  22.             verbose = verbose + 1
  23.             quiet = 0
  24.         if o == '-q':
  25.             quiet = 1
  26.             verbose = 0
  27.     if not args:
  28.         print "Usage:", sys.argv[0], "file_or_directory ..."
  29.         return
  30.     for arg in args:
  31.         check(arg)
  32.  
  33. def check(file):
  34.     if os.path.isdir(file) and not os.path.islink(file):
  35.         if verbose:
  36.             print "%s: listing directory" % `file`
  37.         names = os.listdir(file)
  38.         for name in names:
  39.             fullname = os.path.join(file, name)
  40.             if (os.path.isdir(fullname) and
  41.                 not os.path.islink(fullname) or
  42.                 os.path.normcase(name[-3:]) == ".py"):
  43.                 check(fullname)
  44.         return
  45.  
  46.     try:
  47.         f = open(file)
  48.     except IOError, msg:
  49.         print "%s: I/O Error: %s" % (`file`, str(msg))
  50.         return
  51.  
  52.     if verbose > 1:
  53.         print "checking", `file`, "with tabsize 8..."
  54.     tokens = []
  55.     tokenize.tabsize = 8
  56.     try:
  57.         tokenize.tokenize(f.readline, tokens.append)
  58.     except tokenize.TokenError, msg:
  59.         print "%s: Token Error: %s" % (`file`, str(msg))
  60.  
  61.     if verbose > 1:
  62.         print "checking", `file`, "with tabsize 1..."
  63.     f.seek(0)
  64.     alttokens = []
  65.     tokenize.tabsize = 1
  66.     try:
  67.         tokenize.tokenize(f.readline, alttokens.append)
  68.     except tokenize.TokenError, msg:
  69.         print "%s: Token Error: %s" % (`file`, str(msg))
  70.     f.close()
  71.  
  72.     if tokens == alttokens:
  73.         if verbose:
  74.             print "%s: Clean bill of health." % `file`
  75.     elif quiet:
  76.         print file
  77.     else:
  78.         badline = 0
  79.         n, altn = len(tokens), len(alttokens)
  80.         for i in range(max(n, altn)):
  81.             if i >= n:
  82.                 x = None
  83.             else:
  84.                 x = tokens[i]
  85.             if i >= altn:
  86.                 y = None
  87.             else:
  88.                 y = alttokens[i]
  89.             if x != y:
  90.                 if x:
  91.                     kind, token, start, end, line = x
  92.                 else:
  93.                     kind, token, start, end, line = y
  94.                 badline = start[0]
  95.                 break
  96.         if verbose:
  97.             print "%s: *** Line %d: trouble in tab city! ***" % (
  98.                 `file`, badline)
  99.             print "offending line:", `line`
  100.         else:
  101.             print file, badline, `line`
  102.  
  103. if __name__ == '__main__':
  104.     main()
  105.