clean_test_logs.py (2518B)
1 #!/usr/bin/python3 2 # Clean testbench logs directory to only keep unique and useful files 3 4 import hashlib 5 from pathlib import Path 6 from string import whitespace 7 8 DIR = Path("test") 9 10 11 def rmtree(p: Path): 12 """Recursively delete file or directory""" 13 if p.exists(): 14 if p.is_dir(): 15 for child in p.iterdir(): 16 rmtree(child) 17 p.rmdir() 18 else: 19 p.unlink() 20 21 22 def remove(p: Path, reason: str): 23 """Announce and recursively remove file or directory""" 24 print(f"rm {reason} {p}") 25 rmtree(p) 26 27 28 content_hashes = set() 29 30 31 def rm_if_similar(p: Path, content: str): 32 """Delete file if another file has the same content""" 33 # Remove whitespace from file 34 normalized = content.translate(str.maketrans("", "", whitespace)) 35 # Hash their content 36 hash = hashlib.blake2b(normalized.encode(), usedforsecurity=False).hexdigest() 37 if hash in content_hashes: 38 remove(p, "similar") 39 else: 40 content_hashes.add(hash) 41 42 43 for platform in DIR.iterdir(): 44 if not platform.is_dir(): 45 continue 46 for date in platform.iterdir(): 47 if not date.is_dir(): 48 continue 49 for request in date.iterdir(): 50 payload_file_path = request.joinpath("payload.xml") 51 payload_dir_path = request.joinpath("payload") 52 53 if payload_file_path.exists(): 54 content = payload_file_path.read_text() 55 if "HAC" in request.name and "ORDER_HAC_FINAL_NEG" not in content: 56 remove(request, "simple hac") 57 elif "HAA" in request.name and "<Service>" not in content: 58 remove(request, "empty haa") 59 elif "wssparam" in request.name: 60 remove(request, "wssparam") 61 else: 62 rm_if_similar(payload_file_path, content) 63 elif payload_dir_path.exists(): 64 for file in payload_dir_path.iterdir(): 65 content = file.read_text() 66 rm_if_similar(file, content) 67 if not any(payload_dir_path.iterdir()): 68 remove(request, "empty request") 69 elif ( 70 request.name != "fetch" 71 and request.name != "submit" 72 and not payload_file_path.exists() 73 and not payload_dir_path.exists() 74 ): 75 remove(request, "empty request") 76 77 if not any(date.iterdir()): 78 remove(date, "empty dir")