Files
nuitka-mirror/bin/compare_with_xml

113 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
"""Tool to compare XML outputs of two Nuitka versions."""
import os
import shutil
import sys
import tempfile
# Unchanged, running from checkout, use the parent directory, the nuitka
# package ought be there.
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(__file__), "..")))
# isort:start
from nuitka.tools.testing.Common import my_print
from nuitka.utils.Diffs import getUnifiedDiff
from nuitka.utils.Execution import executeProcess
from nuitka.utils.FileOperations import getFileContentByLine
nuitka1 = sys.argv[1]
nuitka2 = sys.argv[2]
filename = sys.argv[3]
my_print(
"Comparing output of '%s' using '%s' <-> '%s' ..." % (filename, nuitka1, nuitka2)
)
temp_dir = tempfile.mkdtemp(prefix="nuitka-compare-xml-")
xml_filenames = {}
exit_code = 0
try:
for label, nuitka in (("nuitka1", nuitka1), ("nuitka2", nuitka2)):
xml_filename = os.path.join(temp_dir, "%s.xml" % label)
xml_filenames[label] = xml_filename
# Only the optimization result is compared, so stop after code
# generation and avoid compiling imported modules.
command = [nuitka, "--generate-c-only", "--nofollow-imports"]
command += [
option for option in os.getenv("NUITKA_EXTRA_OPTIONS", "").split() if option
]
command += [
"--xml=%s" % xml_filename,
"--output-dir=%s" % os.path.join(temp_dir, label),
filename,
]
process_result = executeProcess(command)
if process_result.exit_code != 0:
my_print(
"Command '%s' failed with exit code %d"
% (" ".join(command), process_result.exit_code)
)
if process_result.stdout:
my_print(process_result.stdout)
if process_result.stderr:
my_print(process_result.stderr)
exit_code = 1
if exit_code == 0:
old_lines = getFileContentByLine(xml_filenames["nuitka1"], encoding="utf-8")
new_lines = getFileContentByLine(xml_filenames["nuitka2"], encoding="utf-8")
diff = list(
getUnifiedDiff(
old_lines=old_lines,
new_lines=new_lines,
old_filename="%s (%s)" % ("nuitka1 " + filename, "xml"),
new_filename="%s (%s)" % ("nuitka2 " + filename, "xml"),
num_lines=3,
)
)
if diff:
for line in diff:
my_print(line, end="\n" if not line.startswith("---") else "")
exit_code = 1
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
if exit_code:
sys.exit("Error, outputs differed.")
my_print("OK, same outputs.")
# Part of "Nuitka", an optimizing Python compiler that is compatible and
# integrates with CPython, but also works on its own.
#
# Licensed under the GNU Affero General Public License, Version 3 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gnu.org/licenses/agpl-3.0.txt
#
# See also: "Nuitka Runtime Library Exception, Version 1.0" in file
# "LICENSE-RUNTIME.txt" for additional permissions granted under Section 7.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.