blob: 6f37c5399f8b51fd418a761b017df66c32bdb723 [file] [log] [blame]
Martin Hořeňovský81159832017-01-20 12:28:40 +01001#!/usr/bin/env python
2
Phil Nash8c32b492015-11-04 07:33:39 +00003from __future__ import print_function
4import os
5from scriptCommon import catchPath
6
7changedFiles = 0
8
9def isSourceFile( path ):
10 return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" )
11
12def fixAllFilesInDir( dir ):
13 for f in os.listdir( dir ):
14 path = os.path.join( dir,f )
15 if os.path.isfile( path ):
Phil Nash315c83a2015-11-04 18:49:19 +000016 if isSourceFile( path ):
Phil Nash8c32b492015-11-04 07:33:39 +000017 fixFile( path )
18 else:
19 fixAllFilesInDir( path )
20
21def 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ýc390c4c2017-01-26 23:13:12 +010027 trimmed = trimmed.replace('\t', ' ')
Phil Nash8c32b492015-11-04 07:33:39 +000028 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
45fixAllFilesInDir(catchPath)
46if changedFiles > 0:
47 print( "Fixed " + str(changedFiles) + " file(s)" )
48else:
49 print( "No trailing whitespace found" )