annotate contrib/pep8/DeleteTrailingSpaces.py @ 1396:86e998d808e7

pep8 whitespace fixes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 24 Jun 2012 14:42:26 +0200
parents 6f785bab3b97
children f7604105f64a
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
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
8 - blank line at file end
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
9 - DOS line endings on .bat files, unix line endings everywhere else
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
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
15 import sys, os
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
16 import warnings
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
17 warnings.simplefilter("once")
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
18
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
19 # file types to be processed
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
20 selected_suffixes = "py bat html css js".split()
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
21
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
22 # directories to ignore
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
23 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
24 """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
25 # 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
26 ignore_dirs = []
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
27 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
28 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
29 ignore_dirs.append((starting_dir, dir))
1396
86e998d808e7 pep8 whitespace fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1366
diff changeset
30 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
31 return ignore_dirs
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
32
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
33
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
34 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
35 """Delete trailing blanks,
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
36 force blank line at file end,
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
37 force line ending to be \r\n for bat files and \n for all others."""
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
38 if suffix.lower() == "bat":
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
39 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
40 else:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
41 line_end = "\n"
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
42
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
43 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
44 lines = f.readlines()
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
45
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
46 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
47 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
48 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
49 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
50 # if line was changed, issue warning once for each type of change
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
51 if suffix == "bat" and not line.endswith("\r\n"):
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
52 warnings.warn("%s was changed to DOS line endings" % filename)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
53 elif suffix != "bat" and line.endswith("\r\n"):
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
54 warnings.warn("%s was changed to Unix line endings" % filename)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
55 elif pep8_line != line:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
56 warnings.warn("%s was changed to remove trailing blanks" % filename)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
57
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
58 # add blank line at end of file if needed
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
59 if lines and pep8_line != line_end:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
60 f.write(line_end)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
61 warnings.warn("%s was changed to add blank line to end of file" % filename)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
62
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
63 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
64 """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
65 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
66
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
67 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
68 # 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
69 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
70 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
71 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
72 # 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
73 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
74 suffix = file.split(".")[-1]
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
75 if suffix in selected_suffixes:
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
76 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
77 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
78
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
79 if __name__ == "__main__":
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
80 starting_dir = os.path.abspath(os.path.dirname(__file__))
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
81 starting_dir = starting_dir.split(os.sep + 'contrib')[0]
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
82 warnings.warn("%s is starting directory" % starting_dir)
6f785bab3b97 mass update part 3 - add script to delete trailing spaces to contrib
Roger Haase <crosseyedpenguin@yahoo.com>
parents:
diff changeset
83 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
84