aboutsummaryrefslogtreecommitdiffstats
path: root/tools/scripts/fixWhitespace.py
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-29 19:25:29 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-29 19:25:29 +0100
commitbc524d70253a4ab2fe40c3ca3e5666e267c0a4d1 (patch)
tree1e629e7b46b1d9972a973bc93fd100bcebd395be /tools/scripts/fixWhitespace.py
downloadnihil-vendor/catch2.tar.gz
nihil-vendor/catch2.tar.bz2
Diffstat (limited to 'tools/scripts/fixWhitespace.py')
-rwxr-xr-xtools/scripts/fixWhitespace.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/tools/scripts/fixWhitespace.py b/tools/scripts/fixWhitespace.py
new file mode 100755
index 0000000..5840e79
--- /dev/null
+++ b/tools/scripts/fixWhitespace.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+import os
+from scriptCommon import catchPath
+
+def isSourceFile( path ):
+ return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" )
+
+def fixAllFilesInDir( dir ):
+ changedFiles = 0
+ for f in os.listdir( dir ):
+ path = os.path.join( dir,f )
+ if os.path.isfile( path ):
+ if isSourceFile( path ):
+ if fixFile( path ):
+ changedFiles += 1
+ else:
+ fixAllFilesInDir( path )
+ return changedFiles
+
+def fixFile( path ):
+ f = open( path, 'r' )
+ lines = []
+ changed = 0
+ for line in f:
+ trimmed = line.rstrip() + "\n"
+ trimmed = trimmed.replace('\t', ' ')
+ 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 )
+ return True
+ return False
+
+changedFiles = fixAllFilesInDir(catchPath)
+if changedFiles > 0:
+ print( "Fixed " + str(changedFiles) + " file(s)" )
+else:
+ print( "No trailing whitespace found" )