Skip to content
Snippets Groups Projects
Commit ea91f54f authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Enable line-wise pycompare option for big files.

parent 61b07d95
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
# The script execution requires python3. # The script execution requires python3.
import re import re
import os
from math import log10 from math import log10
from sys import argv from sys import argv
...@@ -99,51 +100,98 @@ def compare_files(config): ...@@ -99,51 +100,98 @@ def compare_files(config):
fortran_file = open(config['fortran_file_name'], 'r') fortran_file = open(config['fortran_file_name'], 'r')
c_file = open(config['c_file_name'], 'r') c_file = open(config['c_file_name'], 'r')
l_file = None l_file = None
f_lines = fortran_file.readlines() f_lines = []
c_lines = c_file.readlines() c_lines = []
fortran_file.close() line_count = 0
c_file.close() if (not config['linewise']):
if (len(f_lines) == len(c_lines)): f_lines = fortran_file.readlines()
c_lines = c_file.readlines()
line_count = len(f_lines) line_count = len(f_lines)
num_len = 1 fortran_file.close()
if (line_count > 0): c_file.close()
num_len = max(4, int(log10(line_count)) + 1) else: # line-wise comparison mode
if (config['log_html']): f_lines = [fortran_file.readline()]
l_file = open(config['html_output'], 'w') c_lines = [c_file.readline()]
l_file.write("<!DOCTYPE html>\n") print("INFO: using line-wise mode")
l_file.write("<html xmnls=\"http://www.w3.org/1999/xhtml\">\n") print("INFO: counting result lines...")
l_file.write(" <header>\n") while (f_lines[0] != ''):
l_file.write( if (c_lines[0] != ''):
" <h1>Comparison between {0:s} and {1:s}</h1>\n".format( line_count += 1
config['fortran_file_name'], config['c_file_name'] else:
) print("ERROR: C++ file is shorter than FORTRAN file.")
fortran_file.close()
c_file.close()
mismatch_count['errors'] = line_count
return mismatch_count
f_lines[0] = fortran_file.readline()
c_lines[0] = c_file.readline()
if (c_lines[0] != ''):
print("ERROR: C++ file is longer than FORTRAN file.")
fortran_file.close()
c_file.close()
mismatch_count['errors'] = line_count
return mismatch_count
fortran_file.close()
c_file.close()
print("INFO: the output files have %d lines"%line_count)
fortran_file = open(config['fortran_file_name'], 'r')
c_file = open(config['c_file_name'], 'r')
num_read_lines = 0
# LOG FILE INITIALIZATION #
if (config['log_html']):
l_file = open(config['html_output'], 'w')
l_file.write("<!DOCTYPE html>\n")
l_file.write("<html xmnls=\"http://www.w3.org/1999/xhtml\">\n")
l_file.write(" <header>\n")
l_file.write(
" <h1>Comparison between {0:s} and {1:s}</h1>\n".format(
config['fortran_file_name'], config['c_file_name']
) )
l_file.write(" </header>\n") )
l_file.write(" <body>\n") l_file.write(" </header>\n")
l_file.write(" <div>Numeric noise is marked <span style=\"font-weight: bold; color: rgb(0,185,0)\">" l_file.write(" <body>\n")
+ "GREEN</span>, warnings are marked <span style=\"font-weight: bold; color: rgb(0,0,255)\">" l_file.write(" <div>Numeric noise is marked <span style=\"font-weight: bold; color: rgb(0,185,0)\">"
+ "BLUE</span> and errors are marked <span style=\"font-weight: bold; color: rgb(255,0,0)\">" + "GREEN</span>, warnings are marked <span style=\"font-weight: bold; color: rgb(0,0,255)\">"
+ "RED</span>.</div>\n") + "BLUE</span> and errors are marked <span style=\"font-weight: bold; color: rgb(255,0,0)\">"
for li in range(line_count): + "RED</span>.</div>\n")
line_result = compare_lines(f_lines[li], c_lines[li], config, li + 1, num_len, l_file) # END LOG FILE INITIALIZATION #
mismatch_count['errors'] += line_result[0] line_loop = True
mismatch_count['warnings'] += line_result[1] num_len = 1
mismatch_count['noisy'] += line_result[2] if (line_count > 0):
if (mismatch_count['errors'] > 0 and not config['check_all']): num_len = max(4, int(log10(line_count)) + 1)
print("INFO: mismatch found at line %d"%(li + 1)) print("INFO: checking file contents...")
break while (line_loop):
if l_file is not None: if (not config['linewise']):
l_file.write(" </body>\n") line_loop = False
l_file.write("</html>\n") else:
l_file.close() f_lines = [fortran_file.readline()]
else: c_lines = [c_file.readline()]
mismatch_count['errors'] = len(c_lines) num_read_lines += 1
print("ERROR: {0:s} and {1:s} have different numbers of lines!".format( # Start here the comparison loop
config['fortran_file_name'], config['c_file_name'] if (len(f_lines) == len(c_lines)):
)) for li in range(len(f_lines)):
if (config['log_html']): line_result = compare_lines(f_lines[li], c_lines[li], config, li + 1, num_len, l_file)
print("Different file sizes. No log produced.") mismatch_count['errors'] += line_result[0]
config['log_html'] = False mismatch_count['warnings'] += line_result[1]
mismatch_count['noisy'] += line_result[2]
if (mismatch_count['errors'] > 0 and not config['check_all']):
print("INFO: mismatch found at line %d"%(li + 1))
break
else:
mismatch_count['errors'] = len(c_lines)
print("ERROR: {0:s} and {1:s} have different numbers of lines!".format(
config['fortran_file_name'], config['c_file_name']
))
if (config['log_html']):
print("Different file sizes. No log produced.")
config['log_html'] = False
if (num_read_lines >= line_count):
line_loop = False
#End line loop
if l_file is not None:
l_file.write(" </body>\n")
l_file.write("</html>\n")
l_file.close()
return mismatch_count return mismatch_count
## \brief Perform the comparison of two file lines. ## \brief Perform the comparison of two file lines.
...@@ -381,6 +429,7 @@ def parse_arguments(): ...@@ -381,6 +429,7 @@ def parse_arguments():
'fortran_file_name': '', 'fortran_file_name': '',
'c_file_name': '', 'c_file_name': '',
'full_log': False, 'full_log': False,
'linewise': False,
'log_html': False, 'log_html': False,
'html_output': 'pycompare.html', 'html_output': 'pycompare.html',
'warning_threshold': 0.005, 'warning_threshold': 0.005,
...@@ -403,6 +452,8 @@ def parse_arguments(): ...@@ -403,6 +452,8 @@ def parse_arguments():
config['warning_threshold'] = float(split_arg[1]) config['warning_threshold'] = float(split_arg[1])
elif (arg.startswith("--help")): elif (arg.startswith("--help")):
config['help_mode'] = True config['help_mode'] = True
elif (arg.startswith("--linewise")):
config['linewise'] = True
elif (arg.startswith("--quick")): elif (arg.startswith("--quick")):
config['check_all'] = False config['check_all'] = False
else: else:
...@@ -424,8 +475,9 @@ def print_help(): ...@@ -424,8 +475,9 @@ def print_help():
print("--full Print all lines to log file (default prints only mismatches).") print("--full Print all lines to log file (default prints only mismatches).")
print("--help Print this help and exit.") print("--help Print this help and exit.")
print("--html[=OPT_OUTPUT_NAME] Enable logging to HTML file (default logs to \"pycompare.html\").") print("--html[=OPT_OUTPUT_NAME] Enable logging to HTML file (default logs to \"pycompare.html\").")
print("--linewise Load only one line at a time. Useful to compare big files (false by default).")
print("--quick Stop on first mismatch (default is to perform a full check).") print("--quick Stop on first mismatch (default is to perform a full check).")
print("--warn Set a fractional threshold for numeric warning (default=0.005).") print("--warn Set a fractional threshold for numeric warning (default = 0.005).")
print(" ") print(" ")
## \brief Add summary information to the HTML log file ## \brief Add summary information to the HTML log file
...@@ -440,20 +492,25 @@ def print_help(): ...@@ -440,20 +492,25 @@ def print_help():
# \param noisy: `int` The number of noisy values detected by the comparison. # \param noisy: `int` The number of noisy values detected by the comparison.
def reformat_log(config, errors, warnings, noisy): def reformat_log(config, errors, warnings, noisy):
log_file = open(config['html_output'], 'r') log_file = open(config['html_output'], 'r')
log_lines = log_file.readlines() new_file = open("PYCOMPARE_TEMPORARY_LOG.html", 'w')
log_file.close() for hi in range(7):
log_file = open(config['html_output'], 'w') log_line = log_file.readline()
for i in range(7): log_file.write(log_lines[i] + "\n") new_file.write(log_line)
str_errors = "error" if errors == 1 else "errors" str_errors = "error" if errors == 1 else "errors"
str_warnings = "warning" if warnings == 1 else "warnings" str_warnings = "warning" if warnings == 1 else "warnings"
str_noisy = "noisy value" if noisy == 1 else "noisy values" str_noisy = "noisy value" if noisy == 1 else "noisy values"
summary = " <div>Comparison yielded %d %s"%(errors, str_errors) summary = " <div>Comparison yielded %d %s"%(errors, str_errors)
summary = summary + ", %d %s"%(warnings, str_warnings) summary = summary + ", %d %s"%(warnings, str_warnings)
summary = summary + " and %d %s.</div>\n"%(noisy, str_noisy) summary = summary + " and %d %s.</div>\n"%(noisy, str_noisy)
log_file.write(summary) new_file.write(summary)
for i in range(7, len(log_lines)): log_file.write(log_lines[i] + "\n") log_line = log_file.readline()
while (log_line != ''):
new_file.write(log_line)
log_line = log_file.readline()
log_file.close() log_file.close()
new_file.close()
os.remove(config['html_output'])
os.rename("PYCOMPARE_TEMPORARY_LOG.html", config['html_output'])
# ### PROGRAM EXECUTION ### # ### PROGRAM EXECUTION ###
## \cond ## \cond
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment