Create common print_diff function

This commit is contained in:
tntexplosivesltd 2023-11-22 21:47:21 +13:00
parent 14f05ea823
commit 2d05e256a1
4 changed files with 37 additions and 45 deletions

View File

@ -3,7 +3,7 @@
def is_file_cpp(filename: str) -> bool: def is_file_cpp(filename: str) -> bool:
(basefile, ext) = os.path.splitext(filename) (_, ext) = os.path.splitext(filename)
return ext.lower() in ('.h', '.cpp') return ext.lower() in ('.h', '.cpp')
@ -12,7 +12,7 @@ def walk_source_dir(source: str, recursive: bool = True) -> Iterator[str]:
any C++ files found.""" any C++ files found."""
source = os.path.abspath(source) source = os.path.abspath(source)
for subdir, dirs, files in os.walk(source): for subdir, _, files in os.walk(source):
for file in files: for file in files:
if is_file_cpp(file): if is_file_cpp(file):
yield os.path.join(subdir, file) yield os.path.join(subdir, file)

View File

@ -0,0 +1,22 @@
import colorama
def print_diff(udiff, plain):
has_diff = False
for line in udiff:
has_diff = True
color = ""
if line.startswith("++") or line.startswith("@@") or line.startswith("--"):
# Skip unneeded parts of the diff for the brief view
continue
# Work out color if we are printing color
if not plain:
if line.startswith("+"):
color = colorama.Fore.GREEN
elif line.startswith("-"):
color = colorama.Fore.RED
print(color + line)
# Reset color if we're printing in color
if not plain:
print(colorama.Style.RESET_ALL, end="")
return has_diff

View File

@ -2,18 +2,21 @@
import argparse import argparse
import base64 import base64
from capstone import *
import difflib import difflib
import struct import json
import subprocess
import logging import logging
import os import os
import sys
import colorama
import json
import re import re
import struct
import subprocess
import sys
from isledecomp.dir import walk_source_dir from isledecomp.dir import walk_source_dir
from isledecomp.parser import find_code_blocks from isledecomp.parser import find_code_blocks
from isledecomp.utils import print_diff
from capstone import Cs, CS_ARCH_X86, CS_MODE_32
import colorama
from pystache import Renderer from pystache import Renderer
parser = argparse.ArgumentParser(allow_abbrev=False, parser = argparse.ArgumentParser(allow_abbrev=False,
@ -503,24 +506,7 @@ def can_resolve_register_differences(original_asm, new_asm):
else: else:
print(f'{addrs}: {recinfo.name} Effective 100%% match. (Differs in register allocation only)\n\n{ok_text} (still differs in register allocation)\n\n') print(f'{addrs}: {recinfo.name} Effective 100%% match. (Differs in register allocation only)\n\n{ok_text} (still differs in register allocation)\n\n')
else: else:
for line in udiff: print_diff(udiff, plain)
if line.startswith('++') or line.startswith('@@') or line.startswith('--'):
# Skip unneeded parts of the diff for the brief view
pass
elif line.startswith('+'):
if plain:
print(line)
else:
print(colorama.Fore.GREEN + line)
elif line.startswith('-'):
if plain:
print(line)
else:
print(colorama.Fore.RED + line)
else:
print(line)
if not plain:
print(colorama.Style.RESET_ALL, end='')
print(f'\n{recinfo.name} is only {percenttext} similar to the original, diff above') print(f'\n{recinfo.name} is only {percenttext} similar to the original, diff above')

View File

@ -1,12 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import colorama
import difflib import difflib
import subprocess import subprocess
import os import os
import sys import sys
from isledecomp.utils import print_diff
parser = argparse.ArgumentParser(allow_abbrev=False, parser = argparse.ArgumentParser(allow_abbrev=False,
description='Verify Exports: Compare the exports of two DLLs.') description='Verify Exports: Compare the exports of two DLLs.')
parser.add_argument('original', metavar='original-binary', help='The original binary') parser.add_argument('original', metavar='original-binary', help='The original binary')
@ -54,23 +55,6 @@ def get_exports(file):
re_exp = get_exports(args.recompiled) re_exp = get_exports(args.recompiled)
udiff = difflib.unified_diff(og_exp, re_exp) udiff = difflib.unified_diff(og_exp, re_exp)
has_diff = False has_diff = print_diff(udiff, args.no_color)
for line in udiff:
has_diff = True
color = ''
if line.startswith('++') or line.startswith('@@') or line.startswith('--'):
# Skip unneeded parts of the diff for the brief view
continue
# Work out color if we are printing color
if not args.no_color:
if line.startswith('+'):
color = colorama.Fore.GREEN
elif line.startswith('-'):
color = colorama.Fore.RED
print(color + line)
# Reset color if we're printing in color
if not args.no_color:
print(colorama.Style.RESET_ALL, end='')
sys.exit(1 if has_diff else 0) sys.exit(1 if has_diff else 0)