| 
									
										
										
										
											2020-03-19 12:36:30 +01:00
										 |  |  | #!/usr/bin/env python3 | 
					
						
							| 
									
										
										
										
											2017-01-20 12:28:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  | from  __future__ import  print_function | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | from scriptCommon import catchPath | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def isSourceFile( path ): | 
					
						
							|  |  |  |     return path.endswith( ".cpp" ) or path.endswith( ".h" ) or path.endswith( ".hpp" ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fixAllFilesInDir( dir ): | 
					
						
							| 
									
										
										
										
											2018-04-19 15:02:15 +02:00
										 |  |  |     changedFiles = 0 | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  |     for f in os.listdir( dir ): | 
					
						
							|  |  |  |         path = os.path.join( dir,f ) | 
					
						
							|  |  |  |         if os.path.isfile( path ): | 
					
						
							| 
									
										
										
										
											2015-11-04 18:49:19 +00:00
										 |  |  |             if isSourceFile( path ): | 
					
						
							| 
									
										
										
										
											2018-04-19 15:02:15 +02:00
										 |  |  |                 if fixFile( path ): | 
					
						
							|  |  |  |                     changedFiles += 1 | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             fixAllFilesInDir( path ) | 
					
						
							| 
									
										
										
										
											2018-04-19 15:02:15 +02:00
										 |  |  |     return changedFiles | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def fixFile( path ): | 
					
						
							|  |  |  |     f = open( path, 'r' ) | 
					
						
							|  |  |  |     lines = [] | 
					
						
							|  |  |  |     changed = 0 | 
					
						
							|  |  |  |     for line in f: | 
					
						
							|  |  |  |         trimmed = line.rstrip() + "\n" | 
					
						
							| 
									
										
										
										
											2017-01-26 23:13:12 +01:00
										 |  |  |         trimmed = trimmed.replace('\t', '    ') | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  |         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 ) | 
					
						
							| 
									
										
										
										
											2018-04-19 15:02:15 +02:00
										 |  |  |         return True | 
					
						
							|  |  |  |     return False | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 15:02:15 +02:00
										 |  |  | changedFiles = fixAllFilesInDir(catchPath) | 
					
						
							| 
									
										
										
										
											2015-11-04 07:33:39 +00:00
										 |  |  | if changedFiles > 0: | 
					
						
							|  |  |  |     print( "Fixed " + str(changedFiles) + " file(s)" ) | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     print( "No trailing whitespace found" ) |