1#!/usr/bin/env python 2 3from __future__ import print_function 4import os 5from scriptCommon import catchPath 6 7def isSourceFile( path ): 8 return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" ) 9 10def fixAllFilesInDir( dir ): 11 changedFiles = 0 12 for f in os.listdir( dir ): 13 path = os.path.join( dir,f ) 14 if os.path.isfile( path ): 15 if isSourceFile( path ): 16 if fixFile( path ): 17 changedFiles += 1 18 else: 19 fixAllFilesInDir( path ) 20 return changedFiles 21 22def fixFile( path ): 23 f = open( path, 'r' ) 24 lines = [] 25 changed = 0 26 for line in f: 27 trimmed = line.rstrip() + "\n" 28 trimmed = trimmed.replace('\t', ' ') 29 if trimmed != line: 30 changed = changed +1 31 lines.append( trimmed ) 32 f.close() 33 if changed > 0: 34 global changedFiles 35 changedFiles = changedFiles + 1 36 print( path + ":" ) 37 print( " - fixed " + str(changed) + " line(s)" ) 38 altPath = path + ".backup" 39 os.rename( path, altPath ) 40 f2 = open( path, 'w' ) 41 for line in lines: 42 f2.write( line ) 43 f2.close() 44 os.remove( altPath ) 45 return True 46 return False 47 48changedFiles = fixAllFilesInDir(catchPath) 49if changedFiles > 0: 50 print( "Fixed " + str(changedFiles) + " file(s)" ) 51else: 52 print( "No trailing whitespace found" ) 53