annotate contrib/pep8/DeleteTrailingSpaces.py @ 1975:384555088cab

lots of pep8 fixes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 11 Feb 2013 04:49:04 +0100
parents 22c5d8b59163
children
rev   line source
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
1 #!/usr/bin/env python
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
2 # Copyright: 2012 by MoinMoin:RogerHaase
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
3 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
4
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
5 """
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
6 Detect and correct violations of the moin2 coding standards:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
7 - no trailing blanks
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
8 - exactly one linefeed at file end, see PEP8
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
9 - DOS line endings on .bat and .cmd files, unix line endings everywhere else
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
10
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
11 Execute this script from the root directory of the moin2 repository or
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
12 from anywhere within the contrib path.
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
13 """
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
14
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1757
diff changeset
15 import sys
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1757
diff changeset
16 import os
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
17 import warnings
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
18 warnings.simplefilter("once")
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
19
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
20 # file types to be processed
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
21 SELECTED_SUFFIXES = set("py bat cmd html css js styl".split())
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
22
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
23 # stuff considered DOS/WIN
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
24 WIN_SUFFIXES = set("bat cmd".split())
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
25
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
26
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
27 def directories_to_ignore(starting_dir):
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
28 """Return a list of directories that will not be processed."""
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
29 # list format: [(fully qualified directory name, sub-directory name), ... ]
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
30 ignore_dirs = []
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
31 level2_dirs = ".hg contrib dlc docs env moin.egg-info wiki".split()
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
32 for dir in level2_dirs:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
33 ignore_dirs.append((starting_dir, dir))
1396
86e998d808e7 pep8 whitespace fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1366
diff changeset
34 ignore_dirs.append((starting_dir + os.sep + "MoinMoin", "translations"))
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
35 return ignore_dirs
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
36
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
37
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
38 def check_files(filename, suffix):
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
39 """Delete trailing blanks,
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
40 force a single linefeed at file end,
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
41 force line ending to be \r\n for bat files and \n for all others."""
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
42 suffix = suffix.lower()
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
43 if suffix in WIN_SUFFIXES:
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
44 line_end = "\r\n"
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
45 else:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
46 line_end = "\n"
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
47
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
48 with open(filename, "rb") as f:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
49 lines = f.readlines()
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
50
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
51 # now look at file end and get rid of all whitespace-only lines there:
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
52 while lines:
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
53 if not lines[-1].strip():
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
54 del lines[-1]
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
55 warnings.warn(u"%s was changed to remove empty lines at eof" % filename)
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
56 else:
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
57 break
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
58
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
59 with open(filename, "wb") as f:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
60 for line in lines:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
61 pep8_line = line.rstrip() + line_end
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
62 f.write(pep8_line)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
63 # if line was changed, issue warning once for each type of change
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
64 if suffix in WIN_SUFFIXES and not line.endswith("\r\n"):
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
65 warnings.warn(u"%s was changed to DOS line endings" % filename)
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
66 elif suffix not in WIN_SUFFIXES and line.endswith("\r\n"):
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
67 warnings.warn(u"%s was changed to Unix line endings" % filename)
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
68 elif pep8_line != line:
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
69 warnings.warn(u"%s was changed to remove trailing blanks" % filename)
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
70
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
71
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
72 def file_picker(starting_dir):
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
73 """Select target files and pass each to file checker."""
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
74 ignore_dirs = directories_to_ignore(starting_dir)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
75
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
76 for root, dirs, files in os.walk(starting_dir):
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
77 # delete directories in ignore list
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
78 for mama_dir, baby_dir in ignore_dirs:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
79 if mama_dir == root and baby_dir in dirs:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
80 dirs.remove(baby_dir)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
81 # check files with selected suffixes
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
82 for file in files:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
83 suffix = file.split(".")[-1]
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
84 if suffix in SELECTED_SUFFIXES:
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
85 filename = os.path.join(root, file)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
86 check_files(filename, suffix)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
87
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
88
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
89 if __name__ == "__main__":
1398
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
90 if len(sys.argv) > 1:
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
91 starting_dir = os.path.abspath(sys.argv[1])
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
92 else:
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
93 starting_dir = os.path.abspath(os.path.dirname(__file__))
f7604105f64a DeleteTrailingSpaces.py: work conforming to pep8, add starting_dir commandline param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1396
diff changeset
94 starting_dir = starting_dir.split(os.sep + 'contrib')[0]
1757
22c5d8b59163 add .styl to list of file suffixes checked; use unicode strings for messages
Roger Haase <crosseyedpenguin@yahoo.com>
parents: 1398
diff changeset
95 warnings.warn(u"%s is starting directory" % starting_dir)
1366
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
96 file_picker(starting_dir)