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