#!/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 sys

# Unchanged, running from checkout, use the parent directory, the nuitka
# package ought to 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

nuitka1 = sys.argv[1]
nuitka2 = sys.argv[2]
filename = sys.argv[3]

my_print(
    """\
Comparing output of '{filename}' using '{nuitka1}' <-> '{nuitka2}' ...""".format(
        filename=filename, nuitka1=nuitka1, nuitka2=nuitka2
    )
)

extra_options = os.getenv("NUITKA_EXTRA_OPTIONS", "")

nuitka1_cmd = "{nuitka1} --xml {filename}".format(nuitka1=nuitka1, filename=filename)
nuitka2_cmd = "{nuitka2} --xml {filename}".format(nuitka2=nuitka2, filename=filename)

process_result1 = executeProcess(
    nuitka1_cmd,
    shell=True,
)

process_result2 = executeProcess(
    nuitka2_cmd,
    shell=True,
)


def makeDiffable(output):
    result = []

    for line in output.split(b"\n"):
        line = str(line)
        result.append(line)

    return result


def compareOutput(kind, out1, out2):
    diff = getUnifiedDiff(
        old_lines=makeDiffable(out1),
        new_lines=makeDiffable(out2),
        old_filename="%s (%s)" % ("nuitka1 " + filename, kind),
        new_filename="%s (%s)" % ("nuitka2 " + filename, kind),
        num_lines=3,
    )

    result = list(diff)

    if result:
        for line in result:
            my_print(line, end="\n" if not line.startswith("---") else "")
        return 1
    else:
        return 0


exit_code_stdout = compareOutput(
    "stdout", process_result1.stdout, process_result2.stdout
)
exit_code_return = process_result1.exit_code != process_result2.exit_code

if exit_code_return:
    my_print(
        """\
Exit codes {exit_nuitka1:d} ({nuitka1}) != {exit_nuitka2:d} ({nuitka2})""".format(
            exit_nuitka1=process_result1.exit_code,
            nuitka1=nuitka1,
            exit_nuitka2=process_result2.exit_code,
            nuitka2=nuitka2,
        )
    )

exit_code = exit_code_stdout or exit_code_return

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.
