From 1e8a69351238e55eee070c7867cfae42a9412221 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Mon, 5 Nov 2007 21:32:08 -0700
Subject: [PATCH] Makefile: add 'find-trailing-spaces' tool and target

---
 Makefile                     |  2 ++
 misc/find-trailing-spaces.py | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 misc/find-trailing-spaces.py

diff --git a/Makefile b/Makefile
index 793b2040..29b0349c 100644
--- a/Makefile
+++ b/Makefile
@@ -249,6 +249,8 @@ clean:
 	rm -rf support dist
 	rm -rf setuptools*.egg *.pyc
 
+find-trailing-spaces:
+	$(PYTHON) misc/find-trailing-spaces.py -r src
 
 
 # DEBIAN PACKAGING
diff --git a/misc/find-trailing-spaces.py b/misc/find-trailing-spaces.py
new file mode 100644
index 00000000..21b25243
--- /dev/null
+++ b/misc/find-trailing-spaces.py
@@ -0,0 +1,36 @@
+#! /usr/bin/python
+
+import os
+
+from twisted.python import usage
+
+class Options(usage.Options):
+    optFlags = [
+        ("recursive", "r", "Search for .py files recursively"),
+        ]
+    def parseArgs(self, *starting_points):
+        self.starting_points = starting_points
+
+def check(fn):
+    f = open(fn, "r")
+    for i,line in enumerate(f.readlines()):
+        if line == "\n":
+            continue
+        if line[-1] == "\n":
+            line = line[:-1]
+        if line.rstrip() != line:
+            # the %s:%d:%d: lets emacs' compile-mode jump to those locations
+            print "%s:%d:%d: trailing whitespace" % (fn, i+1, len(line)+1)
+    f.close()
+
+o = Options()
+o.parseOptions()
+if o['recursive']:
+    for starting_point in o.starting_points:
+        for root, dirs, files in os.walk(starting_point):
+            for fn in [f for f in files if f.endswith(".py")]:
+                fn = os.path.join(root, fn)
+                check(fn)
+else:
+    for fn in o.starting_points:
+        check(fn)
-- 
2.45.2