Use templates instead of replacing (#292)

* Use templates instead of replacing

* Use Renderer to avoid loading templates ourselves

---------

Co-authored-by: Thomas Phillips <thomas@teknique.com>
This commit is contained in:
Thomas Phillips 2023-11-20 01:55:01 +13:00 committed by GitHub
parent 0b0a9a6d6f
commit dff410d87a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 47 deletions

View file

@ -12,6 +12,7 @@
import colorama import colorama
import html import html
import re import re
from pystache import Renderer
parser = argparse.ArgumentParser(allow_abbrev=False, parser = argparse.ArgumentParser(allow_abbrev=False,
description='Recompilation Compare: compare an original EXE with a recompiled EXE + PDB.') description='Recompilation Compare: compare an original EXE with a recompiled EXE + PDB.')
@ -552,50 +553,38 @@ def can_resolve_register_differences(original_asm, new_asm):
except UnicodeDecodeError: except UnicodeDecodeError:
break break
def gen_html(html_path, data): def gen_html(html_file, data):
templatedata = None output_data = Renderer().render_path(get_file_in_script_dir('template.html'),
with open(get_file_in_script_dir('template.html')) as templatefile: {
templatedata = templatefile.read() "data": ','.join(data)
}
)
templatedata = templatedata.replace('/* INSERT DATA HERE */', ','.join(data), 1) with open(html_file, 'w') as htmlfile:
htmlfile.write(output_data)
with open(html_path, 'w') as htmlfile:
htmlfile.write(templatedata)
def gen_svg(svg, name, icon, implemented_funcs, total_funcs, raw_accuracy): def gen_svg(svg_file, name_svg, icon, svg_implemented_funcs, total_funcs, raw_accuracy):
templatedata = None icon_data = None
with open(get_file_in_script_dir('template.svg')) as templatefile: if icon:
templatedata = templatefile.read() with open(icon, 'rb') as iconfile:
icon_data = base64.b64encode(iconfile.read()).decode('utf-8')
# TODO: Use templating engine (e.g. pystache)
# Replace icon
if args.svg_icon:
with open(args.svg_icon, 'rb') as iconfile:
templatedata = templatedata.replace('{icon}', base64.b64encode(iconfile.read()).decode('utf-8'), 1)
# Replace name
templatedata = templatedata.replace('{name}', name, 1)
# Replace implemented statistic
templatedata = templatedata.replace('{implemented}', f'{(implemented_funcs / total_funcs * 100):.2f}% ({implemented_funcs}/{total_funcs})', 1)
# Replace accuracy statistic
templatedata = templatedata.replace('{accuracy}', f'{(raw_accuracy / implemented_funcs * 100):.2f}%', 1)
# Generate progress bar width
total_statistic = raw_accuracy / total_funcs total_statistic = raw_accuracy / total_funcs
percenttemplate = '{progbar' full_percentbar_width = 127.18422
percentstart = templatedata.index(percenttemplate) output_data = Renderer().render_path(get_file_in_script_dir('template.svg'),
percentend = templatedata.index('}', percentstart) {
progwidth = float(templatedata[percentstart + len(percenttemplate) + 1:percentend]) * total_statistic "name": name_svg,
templatedata = templatedata[0:percentstart] + str(progwidth) + templatedata[percentend + 1:] "icon": icon_data,
"implemented": f'{(svg_implemented_funcs / total_funcs * 100):.2f}% ({svg_implemented_funcs}/{total_funcs})',
"accuracy": f'{(raw_accuracy / svg_implemented_funcs * 100):.2f}%',
"progbar": total_statistic * full_percentbar_width,
"percent": f'{(total_statistic * 100):.2f}%',
}
)
with open(svg_file, 'w') as svgfile:
svgfile.write(output_data)
# Replace percentage statistic
templatedata = templatedata.replace('{percent}', f'{(total_statistic * 100):.2f}%', 2)
with open(svg, 'w') as svgfile:
svgfile.write(templatedata)
if html_path: if html_path:
gen_html(html_path, htmlinsert) gen_html(html_path, htmlinsert)

View file

@ -1,2 +1,3 @@
colorama
capstone capstone
colorama
pystache

View file

@ -75,7 +75,7 @@
} }
</style> </style>
<script> <script>
var data = [/* INSERT DATA HERE */]; var data = [{{{data}}}];
function formatAsm(asm) { function formatAsm(asm) {
var lines = asm.split('\n'); var lines = asm.split('\n');

View file

@ -37,7 +37,7 @@
<clipPath <clipPath
id="progBarCutoff"> id="progBarCutoff">
<rect <rect
width="{progbar:127.18422}" width="{{progbar}}"
height="8.6508904" height="8.6508904"
x="21.118132" x="21.118132"
y="134.05507" y="134.05507"
@ -50,7 +50,7 @@
height="53.066437" height="53.066437"
preserveAspectRatio="none" preserveAspectRatio="none"
style="image-rendering:optimizeSpeed" style="image-rendering:optimizeSpeed"
xlink:href="data:image/png;base64,{icon}" xlink:href="data:image/png;base64,{{icon}}"
id="image1060" id="image1060"
x="58.13345" x="58.13345"
y="51.967873" /><text y="51.967873" /><text
@ -62,7 +62,7 @@
id="tspan738" id="tspan738"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-family:monospace;-inkscape-font-specification:mono;text-align:center;text-anchor:middle;stroke:#000000;stroke-width:1.25161812;stroke-opacity:1;stroke-dasharray:none;paint-order:stroke fill markers" style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-family:monospace;-inkscape-font-specification:mono;text-align:center;text-anchor:middle;stroke:#000000;stroke-width:1.25161812;stroke-opacity:1;stroke-dasharray:none;paint-order:stroke fill markers"
x="84.666656" x="84.666656"
y="118.35877">{name}</tspan></text><g y="118.35877">{{name}}</tspan></text><g
id="g1250" id="g1250"
transform="translate(-0.04358834,8.1397473)"><rect transform="translate(-0.04358834,8.1397473)"><rect
style="display:inline;fill:none;fill-opacity:1;stroke:#000000;stroke-width:2.50324;stroke-dasharray:none;stroke-opacity:1" style="display:inline;fill:none;fill-opacity:1;stroke:#000000;stroke-width:2.50324;stroke-dasharray:none;stroke-opacity:1"
@ -85,7 +85,7 @@
style="font-size:4.23333px;fill:#ffffff;fill-opacity:1;stroke-width:1.05833" style="font-size:4.23333px;fill:#ffffff;fill-opacity:1;stroke-width:1.05833"
x="76.884926" x="76.884926"
y="139.89182" y="139.89182"
id="tspan2150">{percent}</tspan></text><rect id="tspan2150">{{percent}}</tspan></text><rect
style="display:inline;fill:#ffffff;stroke:none;stroke-width:2.6764" style="display:inline;fill:#ffffff;stroke:none;stroke-width:2.6764"
id="rect1169" id="rect1169"
width="127.18422" width="127.18422"
@ -103,7 +103,7 @@
style="font-size:4.23333px;fill:#000000;fill-opacity:1;stroke-width:1.05833" style="font-size:4.23333px;fill:#000000;fill-opacity:1;stroke-width:1.05833"
x="76.884926" x="76.884926"
y="139.89182" y="139.89182"
id="tspan16">{percent}</tspan></text></g><text id="tspan16">{{percent}}</tspan></text></g><text
xml:space="preserve" xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333px;font-family:monospace;-inkscape-font-specification:mono;text-align:start;text-anchor:start;fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:0.83441208;stroke-dasharray:none;stroke-opacity:1;opacity:1;stroke-linejoin:miter;stroke-linecap:butt;paint-order:stroke fill markers" style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333px;font-family:monospace;-inkscape-font-specification:mono;text-align:start;text-anchor:start;fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:0.83441208;stroke-dasharray:none;stroke-opacity:1;opacity:1;stroke-linejoin:miter;stroke-linecap:butt;paint-order:stroke fill markers"
x="46.947659" x="46.947659"
@ -112,8 +112,8 @@
id="tspan1258" id="tspan1258"
style="font-size:4.23333px;stroke-width:0.83441208;stroke:#000000;stroke-opacity:1;stroke-dasharray:none;stroke-linejoin:miter;stroke-linecap:butt;paint-order:stroke fill markers" style="font-size:4.23333px;stroke-width:0.83441208;stroke:#000000;stroke-opacity:1;stroke-dasharray:none;stroke-linejoin:miter;stroke-linecap:butt;paint-order:stroke fill markers"
x="46.947659" x="46.947659"
y="129.67447">Implemented: {implemented}</tspan><tspan y="129.67447">Implemented: {{implemented}}</tspan><tspan
style="font-size:4.23333px;stroke-width:0.83441208;stroke:#000000;stroke-opacity:1;stroke-dasharray:none;stroke-linejoin:miter;stroke-linecap:butt;paint-order:stroke fill markers" style="font-size:4.23333px;stroke-width:0.83441208;stroke:#000000;stroke-opacity:1;stroke-dasharray:none;stroke-linejoin:miter;stroke-linecap:butt;paint-order:stroke fill markers"
x="46.947659" x="46.947659"
y="134.96613" y="134.96613"
id="tspan1262">Accuracy: {accuracy}</tspan></text></g></svg> id="tspan1262">Accuracy: {{accuracy}}</tspan></text></g></svg>

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB