2963
|
1 |
#!/usr/bin/env python
|
3617
|
2 |
#Copyright ReportLab Europe Ltd. 2000-2012
|
2963
|
3 |
#see license.txt for license details
|
|
4 |
"""This tests for things in source files. Initially, absence of tabs :-)
|
|
5 |
"""
|
2987
|
6 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, SecureTestCase, GlobDirectoryWalker, printLocation
|
2984
|
7 |
setOutDir(__name__)
|
2987
|
8 |
from reportlab.lib.testutils import RL_HOME,testsFolder
|
2966
|
9 |
__version__=''' $Id$ '''
|
3794
|
10 |
import os, sys, glob, re
|
2963
|
11 |
from types import ModuleType, ClassType, MethodType, FunctionType
|
|
12 |
import reportlab
|
2966
|
13 |
import unittest
|
2963
|
14 |
from reportlab.lib.utils import open_and_read
|
|
15 |
|
|
16 |
|
|
17 |
class SourceTester(SecureTestCase):
|
|
18 |
def setUp(self):
|
|
19 |
SecureTestCase.setUp(self)
|
|
20 |
try:
|
|
21 |
fn = __file__
|
|
22 |
except:
|
|
23 |
fn = sys.argv[0]
|
|
24 |
|
|
25 |
self.output = open(outputfile(os.path.splitext(os.path.basename(fn))[0]+'.txt'),'w')
|
|
26 |
|
|
27 |
def checkFileForTabs(self, filename):
|
|
28 |
txt = open_and_read(filename, 'r')
|
3794
|
29 |
chunks = txt.split('\t')
|
2963
|
30 |
tabCount = len(chunks) - 1
|
|
31 |
if tabCount:
|
|
32 |
#raise Exception, "File %s contains %d tab characters!" % (filename, tabCount)
|
|
33 |
self.output.write("file %s contains %d tab characters!\n" % (filename, tabCount))
|
|
34 |
|
|
35 |
def checkFileForTrailingSpaces(self, filename):
|
|
36 |
txt = open_and_read(filename, 'r')
|
|
37 |
initSize = len(txt)
|
|
38 |
badLines = 0
|
|
39 |
badChars = 0
|
3794
|
40 |
for line in txt.split('\n'):
|
|
41 |
stripped = line.rstrip()
|
2963
|
42 |
spaces = len(line) - len(stripped) # OK, so they might be trailing tabs, who cares?
|
|
43 |
if spaces:
|
|
44 |
badLines = badLines + 1
|
|
45 |
badChars = badChars + spaces
|
|
46 |
|
3326
|
47 |
if badChars != 0:
|
2963
|
48 |
self.output.write("file %s contains %d trailing spaces, or %0.2f%% wastage\n" % (filename, badChars, 100.0*badChars/initSize))
|
|
49 |
|
|
50 |
def testFiles(self):
|
2966
|
51 |
w = GlobDirectoryWalker(RL_HOME, '*.py')
|
2963
|
52 |
for filename in w:
|
|
53 |
self.checkFileForTabs(filename)
|
|
54 |
self.checkFileForTrailingSpaces(filename)
|
|
55 |
|
|
56 |
def zapTrailingWhitespace(dirname):
|
|
57 |
"""Eliminates trailing spaces IN PLACE. Use with extreme care
|
|
58 |
and only after a backup or with version-controlled code."""
|
|
59 |
assert os.path.isdir(dirname), "Directory not found!"
|
3721
|
60 |
print("This will eliminate all trailing spaces in py files under %s." % dirname)
|
|
61 |
ok = input("Shall I proceed? type YES > ")
|
3326
|
62 |
if ok != 'YES':
|
3721
|
63 |
print('aborted by user')
|
2963
|
64 |
return
|
|
65 |
w = GlobDirectoryWalker(dirname, '*.py')
|
|
66 |
for filename in w:
|
|
67 |
# trim off final newline and detect real changes
|
|
68 |
txt = open(filename, 'r').read()
|
|
69 |
badChars = 0
|
|
70 |
cleaned = []
|
3794
|
71 |
for line in txt.split('\n'):
|
|
72 |
stripped = line.rstrip()
|
2963
|
73 |
cleaned.append(stripped)
|
|
74 |
spaces = len(line) - len(stripped) # OK, so they might be trailing tabs, who cares?
|
|
75 |
if spaces:
|
|
76 |
badChars = badChars + spaces
|
|
77 |
|
3326
|
78 |
if badChars != 0:
|
3794
|
79 |
open(filename, 'w').write('\n'.join(cleaned))
|
3721
|
80 |
print("file %s contained %d trailing spaces, FIXED" % (filename, badChars))
|
|
81 |
print('done')
|
2963
|
82 |
|
|
83 |
def makeSuite():
|
|
84 |
return makeSuiteForClasses(SourceTester)
|
|
85 |
|
|
86 |
|
|
87 |
#noruntests
|
|
88 |
if __name__ == "__main__":
|
|
89 |
if len(sys.argv) == 3 and sys.argv[1] == 'zap' and os.path.isdir(sys.argv[2]):
|
|
90 |
zapTrailingWhitespace(sys.argv[2])
|
|
91 |
else:
|
|
92 |
unittest.TextTestRunner().run(makeSuite())
|
|
93 |
printLocation()
|