From cb60629e817177cfeb6c5c1ff1e0051faebdc5e8 Mon Sep 17 00:00:00 2001 From: Bernard Kwok Date: Wed, 1 Jun 2022 14:18:57 -0400 Subject: [PATCH] Support multiple folders in render comparisons (#973) - Add the ability to have difference source folders for different source languages. This allows for the same or different languages to be compared against different runs. Note that the folder structure must still be the same. e.g. can compare OSL in one build with one renderer vs OSL in another build with another renderer. - Cleanup so that destination files will just try and match source ones to avoid file name mismatch comparisons. --- python/MaterialXTest/tests_to_html.py | 131 +++++++++++++++----------- 1 file changed, 75 insertions(+), 56 deletions(-) diff --git a/python/MaterialXTest/tests_to_html.py b/python/MaterialXTest/tests_to_html.py index b8b3a1a78e..eb65375db1 100644 --- a/python/MaterialXTest/tests_to_html.py +++ b/python/MaterialXTest/tests_to_html.py @@ -4,21 +4,14 @@ import os import datetime import argparse -import itertools try: # Use pip to install Pillow and Image to enable image diffs - from PIL import Image - from PIL import ImageChops + from PIL import Image, ImageChops DIFF_ENABLED = True except Exception: DIFF_ENABLED = False -try: - from itertools import zip_longest -except ImportError: - from itertools import izip_longest as zip_longest - def createDiff(image1Path, image2Path, imageDiffPath): try: image1 = Image.open(image1Path).convert('RGB') @@ -32,6 +25,7 @@ def main(args=None): parser = argparse.ArgumentParser() parser.add_argument('-i', '--inputdir', dest='inputdir', action='store', help='Input directory', default=".") + parser.add_argument('-i2', '--inputdir2', dest='inputdir2', action='store', help='Second input directory', default=".") parser.add_argument('-o', '--outputfile', dest='outputfile', action='store', help='Output file name', default="tests.html") parser.add_argument('-d', '--diff', dest='CREATE_DIFF', action='store_true', help='Perform image diff', default=False) parser.add_argument('-t', '--timestamp', dest='ENABLE_TIMESTAMPS', action='store_true', help='Write image timestamps', default=False) @@ -58,62 +52,87 @@ def main(args=None): fh.write("}") fh.write("") fh.write("\n") + dir1 = os.getcwd() if args.inputdir == "." else args.inputdir + dir2 = os.getcwd() if args.inputdir2 == "." else args.inputdir2 + fh.write("

" + args.sourcelang + " (in: " + dir1 + ") vs "+ args.destlang + " (in: " + dir2 + ")

\n") + + if not DIFF_ENABLED and args.CREATE_DIFF: + print("--diff argument ignored. Diff utility not installed.") - # Iterate over subdirectories + if not args.inputdir2: + args.inputdir2 = args.inputdir + + # Get all source files + sourceFiles = [] + sourcePaths = [] for subdir, _, files in os.walk(args.inputdir): - sourceFiles = [] - destFiles = [] - sourceCount = 0 - destCount = 0 for curFile in files: if curFile.endswith(args.sourcelang + ".png"): sourceFiles.append(curFile) - sourceCount += 1 - # Allow for just one language to be shown if source and dest are the same. - # Otherwise add in equivalent name with dest language replacement - if args.sourcelang != args.destlang: - destFile = curFile.replace(args.sourcelang, args.destlang) - destFiles.append(destFile) - else: - destFiles.append("") - - if sourceFiles: - fh.write("

" + subdir + ":

\n") - fh.write("\n") - for sourceFile, destFile in zip(sourceFiles, destFiles): - fullsourcePath = os.path.join(subdir, sourceFile) if sourceFile else None - fulldestPath = os.path.join(subdir, destFile) if destFile else None - if sourceFile and destFile and DIFF_ENABLED and args.CREATE_DIFF: - diffPath = fullsourcePath[0:-8] + "diff.png" - createDiff(fullsourcePath, fulldestPath, diffPath) - else: - diffPath = None - - fh.write("\n") - if fullsourcePath: - fh.write("\n") - if fulldestPath: - fh.write("\n") - if diffPath: - fh.write("\n") - fh.write("\n") - - fh.write("\n") - if fullsourcePath: + sourcePaths.append(subdir) + + # Get all destination files, matching source files + destFiles = [] + destPaths = [] + postFix = args.sourcelang + ".png" + for sourceFile, sourcePath in zip(sourceFiles, sourcePaths): + # Allow for just one language to be shown if source and dest are the same. + # Otherwise add in equivalent name with dest language replacement if + # pointing to the same directory + if args.inputdir != args.inputdir2 or args.sourcelang != args.destlang: + destFile = sourceFile[:-len(postFix)] + args.destlang + ".png" + destPath = os.path.join(args.inputdir2, sourcePath) + else: + destFile = "" + destPath = None + destFiles.append(destFile) + destPaths.append(destPath) + + if sourceFiles: + curPath = "" + for sourceFile, destFile, sourcePath, destPath in zip(sourceFiles, destFiles, sourcePaths, destPaths): + + fullSourcePath = os.path.join(sourcePath, sourceFile) if sourceFile else None + fullDestPath = os.path.join(destPath, destFile) if destFile else None + + if curPath != sourcePath: + if curPath != "": + fh.write("
\n") + fh.write("

" + sourcePath + ":

\n") + fh.write("\n") + curPath = sourcePath + + if sourceFile and destFile and DIFF_ENABLED and args.CREATE_DIFF: + diffPath = fullSourcePath[0:-8] + "diff.png" + createDiff(fullSourcePath, fullDestPath, diffPath) + else: + diffPath = None + + fh.write("\n") + if fullSourcePath: + fh.write("\n") + if fullDestPath: + fh.write("\n") + if diffPath: + fh.write("\n") + fh.write("\n") + + fh.write("\n") + if fullSourcePath: fh.write("\n") - if fulldestPath: + if args.ENABLE_TIMESTAMPS and os.path.isfile(fullSourcePath): + fh.write("
(" + str(datetime.datetime.fromtimestamp(os.path.getmtime(fullSourcePath))) + ")") + fh.write("\n") + if fullDestPath: fh.write("\n") - if diffPath: - fh.write("\n") - fh.write("\n") - fh.write("
" + sourceFile) - if args.ENABLE_TIMESTAMPS and os.path.isfile(fullsourcePath): - fh.write("
(" + str(datetime.datetime.fromtimestamp(os.path.getmtime(fullsourcePath))) + ")") - fh.write("
" + destFile) - if args.ENABLE_TIMESTAMPS and os.path.isfile(fulldestPath): - fh.write("
(" + str(datetime.datetime.fromtimestamp(os.path.getmtime(fulldestPath))) + ")") - fh.write("
" + sourceFile[0:-8] + "diff.png" + "
\n") + if args.ENABLE_TIMESTAMPS and os.path.isfile(fullDestPath): + fh.write("
(" + str(datetime.datetime.fromtimestamp(os.path.getmtime(fullDestPath))) + ")") + fh.write("\n") + if diffPath: + fh.write("Difference\n") + fh.write("\n") + fh.write("\n") fh.write("\n") fh.write("\n")