1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import getopt |
---|
6 | import string |
---|
7 | import re |
---|
8 | |
---|
9 | def fnParse( arg, dirname, files ): |
---|
10 | for file in files: |
---|
11 | for ext in arg: |
---|
12 | if file.rfind( ext, len(file)-len(ext), len(file) ) != -1 and not os.path.isdir( dirname + "/" + file ): |
---|
13 | print "Parsing file " + dirname + "/" + file + "..." |
---|
14 | fo = open( dirname + "/" + file, "r" ) |
---|
15 | lines = fo.readlines() |
---|
16 | fo.close() |
---|
17 | fo = open( dirname + "/" + file, "r" ) |
---|
18 | newlines = fo.readlines() |
---|
19 | changed = 0 |
---|
20 | for i in range(0, len(newlines) - 1): |
---|
21 | newlines[i] = re.sub( "[ \t]+$", "", newlines[i] ) |
---|
22 | changed = 1 |
---|
23 | if changed == 1: |
---|
24 | backup = open( dirname + "/" + file + "~", "w" ) |
---|
25 | backup.writelines( lines ) |
---|
26 | backup.close() |
---|
27 | |
---|
28 | fo.close() |
---|
29 | fo = open( dirname + "/" + file, "w" ) |
---|
30 | fo.writelines( newlines ) |
---|
31 | |
---|
32 | print "File " + dirname + "/" + file + " was changed." |
---|
33 | fo.close() |
---|
34 | |
---|
35 | def fnHelp(): |
---|
36 | helpstr = """ |
---|
37 | This program deletes all the 'dangling' spaces at the end |
---|
38 | of all the lines in all the files with the passed extensions |
---|
39 | within the directory tree from whose root it was called |
---|
40 | (i.e. recurses into sub-directories) |
---|
41 | |
---|
42 | Syntax: |
---|
43 | rem_endspc [ext1] [ext2] [...] |
---|
44 | |
---|
45 | Extensions are given in the form: |
---|
46 | .cpp .h .txt (no asterisk at the beginning) |
---|
47 | |
---|
48 | Copyright (c) 2002 by Adrian Cearnau (cearny@cearny.ro) |
---|
49 | Any use, commercial or not, is allowed |
---|
50 | """ |
---|
51 | print helpstr |
---|
52 | |
---|
53 | if len(sys.argv) < 2: |
---|
54 | fnHelp() |
---|
55 | else: |
---|
56 | exts = sys.argv[1].split() |
---|
57 | currdir = os.getcwd() |
---|
58 | os.path.walk( currdir, fnParse, exts ) |
---|
59 | #print "\nTotally", num_files, "files were patched\n" |
---|
60 | print "done" |
---|