| 
									
										
										
										
											1999-02-03 12:07:14 +00:00
										 |  |  | """findgremlins - Search through a folder and subfolders for
 | 
					
						
							|  |  |  | text files that have characters with bit 8 set, and print | 
					
						
							|  |  |  | the filename and a bit of context. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | By Just, with a little glue by Jack"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-26 21:40:00 +00:00
										 |  |  | import EasyDialogs | 
					
						
							| 
									
										
										
										
											2003-02-05 23:10:46 +00:00
										 |  |  | import MacOS | 
					
						
							| 
									
										
										
										
											1999-02-03 12:07:14 +00:00
										 |  |  | import re | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import string | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | xpat = re.compile(r"[\200-\377]") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def walk(top, recurse=1): | 
					
						
							|  |  |  | 	if os.path.isdir(top): | 
					
						
							|  |  |  | 		if recurse: | 
					
						
							|  |  |  | 			for name in os.listdir(top): | 
					
						
							|  |  |  | 				path = os.path.join(top, name) | 
					
						
							|  |  |  | 				walk(path) | 
					
						
							|  |  |  | 	else: | 
					
						
							| 
									
										
										
										
											2003-02-05 23:10:46 +00:00
										 |  |  | 		cr, tp = MacOS.GetCreatorAndType(top) | 
					
						
							|  |  |  | 		if tp in ('TEXT', '\0\0\0\0') and top[-4:] <> ".hqx": | 
					
						
							| 
									
										
										
										
											1999-02-03 12:07:14 +00:00
										 |  |  | 			data = open(top).read() | 
					
						
							|  |  |  | 			badcount = 0 | 
					
						
							|  |  |  | 			for ch in data[:256]: | 
					
						
							|  |  |  | 				if ord(ch) == 0 or ord(ch) >= 0200: | 
					
						
							|  |  |  | 					badcount = badcount + 1 | 
					
						
							|  |  |  | 			if badcount > 16: | 
					
						
							|  |  |  | 				print `top`, 'appears to be a binary file' | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			pos = 0 | 
					
						
							|  |  |  | 			gotone = 0 | 
					
						
							|  |  |  | 			while 1: | 
					
						
							|  |  |  | 				m = xpat.search(data, pos) | 
					
						
							|  |  |  | 				if m is None: | 
					
						
							|  |  |  | 					break | 
					
						
							|  |  |  | 				if not gotone: | 
					
						
							|  |  |  | 					print `top` | 
					
						
							|  |  |  | 					gotone = 1 | 
					
						
							|  |  |  | 				[(i, j)] = m.regs | 
					
						
							|  |  |  | 				print "     ", string.replace(data[i-15:j+15], '\n', ' ') | 
					
						
							|  |  |  | 				pos = j | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2003-02-05 23:10:46 +00:00
										 |  |  | 	if sys.argv[1:]: | 
					
						
							|  |  |  | 		for pathname in sys.argv[1:]: | 
					
						
							|  |  |  | 			walk(pathname) | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		pathname = EasyDialogs.AskFolder() | 
					
						
							|  |  |  | 		if pathname: | 
					
						
							|  |  |  | 			walk(pathname) | 
					
						
							| 
									
										
										
										
											1999-02-03 12:07:14 +00:00
										 |  |  | 		 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  | 	main() | 
					
						
							|  |  |  | 	 |