niklase@google.com | 47bdc46 | 2011-05-30 11:42:35 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import fileinput |
| 5 | import filemanagement |
| 6 | import p4commands |
| 7 | |
| 8 | # Defaults |
| 9 | TABSIZE = 4 |
| 10 | |
| 11 | extensions = ['.h','.cc','.c','.cpp'] |
| 12 | |
| 13 | ignore_these = ['my_ignore_header.h'] |
| 14 | |
| 15 | usage = """ |
| 16 | Replaces all TAB characters with %(TABSIZE)d space characters. |
| 17 | In addition, all trailing space characters are removed. |
| 18 | usage: trim directory |
| 19 | """ % vars() |
| 20 | |
| 21 | if((len(sys.argv) != 2) and (len(sys.argv) != 3)): |
| 22 | sys.stderr.write(usage) |
| 23 | sys.exit(2) |
| 24 | |
| 25 | directory = sys.argv[1]; |
| 26 | if(not filemanagement.pathexist(directory)): |
| 27 | sys.stderr.write(usage) |
| 28 | sys.exit(2) |
| 29 | |
| 30 | if((len(sys.argv) == 3) and (sys.argv[2] != '--commit')): |
| 31 | sys.stderr.write(usage) |
| 32 | sys.exit(2) |
| 33 | |
| 34 | commit = False |
| 35 | if(len(sys.argv) == 3): |
| 36 | commit = True |
| 37 | |
| 38 | files_to_fix = [] |
| 39 | for extension in extensions: |
| 40 | files_to_fix.extend(filemanagement.listallfilesinfolder(directory,\ |
| 41 | extension)) |
| 42 | |
| 43 | def main(): |
| 44 | if (commit): |
| 45 | p4commands.checkoutallfiles() |
| 46 | for path,file_name in files_to_fix: |
| 47 | full_file_name = path + file_name |
| 48 | if (not commit): |
| 49 | print full_file_name + ' will be edited' |
| 50 | continue |
| 51 | for line in fileinput.input(full_file_name, inplace=True): |
| 52 | line = line.replace('\t',' '*TABSIZE); # replace TABs |
| 53 | line = line.rstrip(None) # remove trailing whitespaces |
| 54 | print line # modify the file |
| 55 | if (commit): |
| 56 | p4commands.revertunchangedfiles() |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | main() |