blob: 9236b711f41cbca36739c16e24b65c6dadedf435 [file] [log] [blame]
Guido van Rossum6f73c1a1998-03-20 19:23:04 +00001#! /usr/bin/env python
2
3"""The Tab Police watches for possibly inconsistent indentation."""
4
5import os
6import sys
7import getopt
8import string
9import tokenize
10
11verbose = 0
12
13def main():
14 global verbose
15 try:
16 opts, args = getopt.getopt(sys.argv[1:], "v")
17 except getopt.error, msg:
18 print msg
19 for o, a in opts:
20 if o == '-v':
21 verbose = verbose + 1
22 for arg in args:
23 check(arg)
24
25def check(file):
26 if os.path.isdir(file) and not os.path.islink(file):
27 if verbose:
28 print "%s: listing directory" % `file`
29 names = os.listdir(file)
30 for name in names:
31 fullname = os.path.join(file, name)
32 if (os.path.isdir(fullname) and
33 not os.path.islink(fullname) or
34 os.path.normcase(name[-3:]) == ".py"):
35 check(fullname)
36 return
37
38 try:
39 f = open(file)
40 except IOError, msg:
41 print "%s: I/O Error: %s" % (`file`, str(msg))
42 return
43
44 if verbose > 1:
45 print "checking", `file`, "with tabsize 8..."
46 tokens = []
47 tokenize.tabsize = 8
48 try:
49 tokenize.tokenize(f.readline, tokens.append)
50 except tokenize.TokenError, msg:
51 print "%s: Token Error: %s" % (`file`, str(msg))
52
53 if verbose > 1:
54 print "checking", `file`, "with tabsize 4..."
55 f.seek(0)
56 alttokens = []
57 tokenize.tabsize = 4
58 try:
59 tokenize.tokenize(f.readline, alttokens.append)
60 except tokenize.TokenError, msg:
61 print "%s: Token Error: %s" % (`file`, str(msg))
62 f.close()
63
64 if tokens != alttokens:
65 if verbose:
66 print "%s: *** Trouble in tab city! ***" % `file`
67 else:
68 print file
69 else:
70 if verbose:
71 print "%s: Clean bill of health." % `file`
72
73if __name__ == '__main__':
74 main()