forked from catchorg/Catch2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to strip trailing whitespace from source files
- Loading branch information
1 parent
ece529a
commit 8c32b49
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import print_function | ||
import os | ||
from scriptCommon import catchPath | ||
|
||
changedFiles = 0 | ||
|
||
def isSourceFile( path ): | ||
return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" ) | ||
|
||
def fixAllFilesInDir( dir ): | ||
for f in os.listdir( dir ): | ||
path = os.path.join( dir,f ) | ||
if os.path.isfile( path ): | ||
if( isSourceFile( path ) ): | ||
fixFile( path ) | ||
else: | ||
fixAllFilesInDir( path ) | ||
|
||
def fixFile( path ): | ||
f = open( path, 'r' ) | ||
lines = [] | ||
changed = 0 | ||
for line in f: | ||
trimmed = line.rstrip() + "\n" | ||
if trimmed != line: | ||
changed = changed +1 | ||
lines.append( trimmed ) | ||
f.close() | ||
if changed > 0: | ||
global changedFiles | ||
changedFiles = changedFiles + 1 | ||
print( path + ":" ) | ||
print( " - fixed " + str(changed) + " line(s)" ) | ||
altPath = path + ".backup" | ||
os.rename( path, altPath ) | ||
f2 = open( path, 'w' ) | ||
for line in lines: | ||
f2.write( line ) | ||
f2.close() | ||
os.remove( altPath ) | ||
|
||
fixAllFilesInDir(catchPath) | ||
if changedFiles > 0: | ||
print( "Fixed " + str(changedFiles) + " file(s)" ) | ||
else: | ||
print( "No trailing whitespace found" ) |