Debian Codegraph @

3rdparty/catch2/tools/scripts/fixWhitespace.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import os
4 from scriptCommon import catchPath
5
6 def isSourceFile( path ):
7 return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" )
8
9 def fixAllFilesInDir( dir ):
10 changedFiles = 0
11 for f in os.listdir( dir ):
12 path = os.path.join( dir,f )
13 if os.path.isfile( path ):
14 if isSourceFile( path ):
15 if fixFile( path ):
16 changedFiles += 1
17 else:
18 fixAllFilesInDir( path )
19 return changedFiles
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"
27 trimmed = trimmed.replace('\t', ' ')
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 return True
45 return False
46
47 changedFiles = fixAllFilesInDir(catchPath)
48 if changedFiles > 0:
49 print( "Fixed " + str(changedFiles) + " file(s)" )
50 else:
51 print( "No trailing whitespace found" )