Skip to content

Instantly share code, notes, and snippets.

@nothingface0
Last active December 9, 2022 09:41
Show Gist options
  • Save nothingface0/d38a73c4b8cdef41fdcbbe728a4238c7 to your computer and use it in GitHub Desktop.
Save nothingface0/d38a73c4b8cdef41fdcbbe728a4238c7 to your computer and use it in GitHub Desktop.
KATARAMENO CMSSW
import os
import argparse
import pprint
def load_data_from_debug_file(filename):
data = {}
is_in_block = False
block_name = None
# print(f"Opening '{filename}'")
with open(filename, "r", errors="replace") as f:
for index, line in enumerate(f.readlines()):
if line.startswith("*START*"):
if not is_in_block:
is_in_block = True
s = line.split("*START*")
block_name = "".join(s[1:]).strip()
# print(f"Found start of block '{block_name}'")
data[block_name] = []
else:
raise Exception("Found start of block within block")
elif line.startswith("*END*"):
if is_in_block:
is_in_block = False
s = line.split("*END*")
new_block_name = "".join(s[1:]).strip()
if block_name != new_block_name:
raise Exception(
f"Found END for block '{new_block_name}' while being in '{block_name}'"
)
# print(f"Found end of block '{block_name}'")
block_name = None
else:
raise Exception("Found end of block without being in block")
else:
line = line.strip().split("\t")
if block_name and block_name in data:
data[block_name].append(line)
# print(f"Closed '{filename}'")
return data
def compare_list_of_files(filenames, block_names=None, silent=False):
# print(f"Comparing '{filenames}'")
data = {} # Key: filename, value: dict of k: blocks and v: lines
for filename in filenames:
data[filename] = load_data_from_debug_file(filename)
# Assert number of blocks in each file is equal to the number of blocks in first file
try:
assert all(
(
set(data[list(data.keys())[0]].keys()).difference(set(log_data.keys()))
== set()
for filename, log_data in data.items()
)
)
except AssertionError as exc:
raise Exception("Failed to find same START/END blocks on all files") from exc
files_different = []
total_differences = 0
# Iterate over first file's blocks
first_file = list(data.keys())[0]
for block_name, lines in data[first_file].items():
if block_names and block_name not in block_names:
# print(f"Skipping block {block_name}")
continue
# print(f"Comparing block '{block_name}'")
# Compare each line on file 0 with same line in each other file
for line_index, line in enumerate(lines):
# For each other file, starting from file 1
# compare each line
for other_file in list(data.keys())[1:]:
# print(f"\t Comparing with {other_file}")
# Compare the current line on current block of file 0
# with the same line, in the same block of each other file
try:
other_file_line = set(data[other_file][block_name][line_index])
except IndexError:
# print(
# f"Files '{first_file}' and '{other_file}' have different length"
# )
continue
try:
assert set(line).difference(other_file_line) == set()
except AssertionError as e:
if other_file not in files_different:
files_different.append(other_file)
total_differences += 1
is_file_same = False
if not silent:
print(
"Found line which differs when comparing "
f"File '{first_file}' with file '{other_file}'. "
f"Difference in block '{block_name}', line {line_index}\n"
f"\t{line}\n"
f"\t{data[other_file][block_name][line_index]}\n"
)
if total_differences > 0:
print(
f"Found {total_differences} different lines in {len(files_different)} files"
)
return len(files_different), total_differences
def compare_list_of_directories(directories, block_names=None, silent=False):
if not silent:
print(f"Comparing '{directories}'")
all_files = {}
total_differences = 0
total_files_different = 0
for directory in directories:
for root, dir, files in os.walk(directory):
if len(files) < 1:
raise Exception(f"No log files found in '{directory}'")
all_files[directory] = files
# Compare each file of first dir with every other dir
first_dir = list(all_files.keys())[0]
other_dirs = list(all_files.keys())[1:]
for f in all_files[first_dir]:
for other_dir in other_dirs:
if f in all_files[other_dir]:
num_files_different, num_lines_different = compare_list_of_files(
[os.path.join(first_dir, f), os.path.join(other_dir, f)],
block_names,
silent,
)
total_differences += num_lines_different
total_files_different += num_files_different
else:
# print(f"!!! Could not find file '{os.path.join(other_dir, f)}'")
pass
print(
f"Found {total_differences} different lines in total across {total_files_different} files"
)
return total_differences
def main():
parser = argparse.ArgumentParser(
prog="Compare debug logs", description="AAAAAAAAAA", epilog="BOTTOM TEXT"
)
parser.add_argument(
"filenames", type=str, nargs="+", help="filenames or directories to compare"
)
parser.add_argument(
"--block-names", type=str, nargs="*", help="Only compare given block names"
)
parser.add_argument("--silent", action="store_true", help="Suppress most output")
args = parser.parse_args()
filenames = args.filenames
if len(filenames) < 2:
raise Exception("You must determine at least 2 paths for comparison")
if all([os.path.isdir(filename) for filename in filenames]):
print("!!! comparing directories")
compare_list_of_directories(filenames, args.block_names, args.silent)
elif all([os.path.isfile(filename) for filename in filenames]):
print("!!! comparing files")
compare_list_of_files(filenames, args.block_names, args.silent)
else:
raise Exception("Can only compare dirs/dirs or files/files for now")
if __name__ == "__main__":
main()
@nothingface0
Copy link
Author

nothingface0 commented Dec 7, 2022

Usage:

python3 compare_debug.py <file1> <file2> ... <fileN>

or

python3 compare_debug.py <dir1> <dir2> ... <dirN>

Can also specify to only compare specific blocks with --block-names:

python3 compare_debug.py <file1> <file2> --block-names "Tracks on Host","Hits on Host"

Used for this branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment