scripts: add a script to compare compilation outputs

This commit is contained in:
Tyler Wilding 2024-05-24 22:57:22 -04:00
parent 0e4b6f2c11
commit e67a3ea4c3
No known key found for this signature in database
GPG key ID: BF7B068C2FEFD7EF
2 changed files with 55 additions and 2 deletions

4
.vscode/launch.json vendored
View file

@ -8,10 +8,10 @@
"name": "run python script", "name": "run python script",
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/scripts/gsrc/copy-common-naming.py", "program": "${workspaceFolder}/scripts/gsrc/compare-compilation-outputs.py",
"console": "integratedTerminal", "console": "integratedTerminal",
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"args": ["--update-names-from-refs", "--decompiler", "./out/build/Release/bin/decompiler"] "args": []
}, },
] ]
} }

View file

@ -0,0 +1,53 @@
# Simple script that compares every file in `out/game/obj` with a base directory
# This is useful for when you expect your compilation output to be identical, ie. when you've just made formatting only changes
# If every file matches...you should be able to be confident that you have broken nothing!
import os
import hashlib
def hash_file(filepath):
"""Returns the MD5 hash of the file."""
hasher = hashlib.md5()
with open(filepath, 'rb') as f:
buf = f.read()
hasher.update(buf)
return hasher.hexdigest()
def compare_directories(base_dir, compare_dir):
"""Compares files in two directories based on their MD5 hash."""
mismatched_files = []
missing_files = []
# Iterate through files in the base directory
for root, _, files in os.walk(base_dir):
for file in files:
base_file_path = os.path.join(root, file)
relative_path = os.path.relpath(base_file_path, base_dir)
compare_file_path = os.path.join(compare_dir, relative_path)
if os.path.exists(compare_file_path):
base_file_hash = hash_file(base_file_path)
compare_file_hash = hash_file(compare_file_path)
if base_file_hash != compare_file_hash:
mismatched_files.append(relative_path)
else:
missing_files.append(relative_path)
# Report results
if not mismatched_files and not missing_files:
print("All files matched successfully.")
else:
if mismatched_files:
print("Mismatched files:")
for file in mismatched_files:
print(f" - {file}")
if missing_files:
print("Missing files:")
for file in missing_files:
print(f" - {file}")
# Usage example
base_directory = './out/jak1/obj_before'
compare_directory = './out/jak1/obj'
print(f'Comparing {base_directory} with {compare_directory}')
compare_directories(base_directory, compare_directory)