aboutsummaryrefslogtreecommitdiff
path: root/tools/refactoring/trimall.py
blob: 7a1c458af3fd4d8867d77a3d56f16403545059d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python

import sys
import fileinput
import filemanagement
import p4commands

# Defaults
TABSIZE = 4

extensions = ['.h','.cc','.c','.cpp']

ignore_these = ['my_ignore_header.h']

usage = """
Replaces all TAB characters with %(TABSIZE)d space characters.
In addition, all trailing space characters are removed.
usage: trim directory
""" % vars()

if((len(sys.argv) != 2) and (len(sys.argv) != 3)):
    sys.stderr.write(usage)
    sys.exit(2)

directory = sys.argv[1];
if(not filemanagement.pathexist(directory)):
    sys.stderr.write(usage)
    sys.exit(2)

if((len(sys.argv) == 3) and (sys.argv[2] != '--commit')):
    sys.stderr.write(usage)
    sys.exit(2)

commit = False
if(len(sys.argv) == 3):
    commit = True

files_to_fix = []
for extension in extensions:
    files_to_fix.extend(filemanagement.listallfilesinfolder(directory,\
                                                       extension))

def main():
    if (commit):
        p4commands.checkoutallfiles()
    for path,file_name in files_to_fix:
        full_file_name = path + file_name
        if (not commit):
            print full_file_name + ' will be edited'
            continue
        for line in fileinput.input(full_file_name, inplace=True):
            line = line.replace('\t',' '*TABSIZE);    # replace TABs
            line = line.rstrip(None)  # remove trailing whitespaces
            print line                # modify the file
    if (commit):
        p4commands.revertunchangedfiles()

if __name__ == '__main__':
    main()