]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/coding_tools/find-trailing-spaces.py
setup: organize misc/ scripts and tools and remove obsolete ones
[tahoe-lafs/tahoe-lafs.git] / misc / coding_tools / find-trailing-spaces.py
1 #!/usr/bin/env python
2
3 import os, sys
4
5 from twisted.python import usage
6
7 class Options(usage.Options):
8     optFlags = [
9         ("recursive", "r", "Search for .py files recursively"),
10         ]
11     def parseArgs(self, *starting_points):
12         self.starting_points = starting_points
13
14 found = [False]
15
16 def check(fn):
17     f = open(fn, "r")
18     for i,line in enumerate(f.readlines()):
19         if line == "\n":
20             continue
21         if line[-1] == "\n":
22             line = line[:-1]
23         if line.rstrip() != line:
24             # the %s:%d:%d: lets emacs' compile-mode jump to those locations
25             print "%s:%d:%d: trailing whitespace" % (fn, i+1, len(line)+1)
26             found[0] = True
27     f.close()
28
29 o = Options()
30 o.parseOptions()
31 if o['recursive']:
32     for starting_point in o.starting_points:
33         for root, dirs, files in os.walk(starting_point):
34             for fn in [f for f in files if f.endswith(".py")]:
35                 fn = os.path.join(root, fn)
36                 check(fn)
37 else:
38     for fn in o.starting_points:
39         check(fn)
40 if found[0]:
41     sys.exit(1)
42 sys.exit(0)