#!/usr/bin/env python
#     Copyright 2015, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#     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.
#

from __future__ import print_function

import difflib
import os
import re
import subprocess
import sys
import tempfile
import time

filename = sys.argv[1]
args     = sys.argv[2:]

def hasArg(arg):
    if arg in args:
        args.remove(arg)
        return True
    else:
        return False

# For output keep it
arguments = list(args)

silent_mode        = hasArg("silent")
ignore_stderr      = hasArg("ignore_stderr")
ignore_warnings    = hasArg("ignore_warnings")
expect_success     = hasArg("expect_success")
expect_failure     = hasArg("expect_failure")
python_debug       = hasArg("python_debug")
module_mode        = hasArg("module_mode")
two_step_execution = hasArg("two_step_execution")
binary_python_path = hasArg("binary_python_path")
keep_python_path   = hasArg("keep_python_path")
trace_command      = hasArg("trace_command")
remove_output      = hasArg("remove_output")
standalone_mode    = hasArg("standalone")
no_site            = hasArg("no_site")
recurse_none       = hasArg("recurse_none")
timing             = hasArg("timing")

assert not standalone_mode or not module_mode

if args:
    sys.exit("Error, non understood mode(s) '%s'," % ",".join(args))

if "PYTHONHASHSEED" not in os.environ:
    os.environ["PYTHONHASHSEED"] = "0"

os.environ["PYTHONWARNINGS"] = "ignore"

if "PYTHON" not in os.environ:
    os.environ["PYTHON"] = sys.executable

extra_options = os.environ.get("NUITKA_EXTRA_OPTIONS", "")

if "--python-debug" in extra_options:
    python_debug = True

if python_debug:
    if os.path.exists(os.path.join("/usr/bin/", os.environ["PYTHON"] + "-dbg")):
        os.environ["PYTHON"] += "-dbg"

if os.environ["PYTHON"].endswith("-dbg"):
    python_debug = True

# Make sure we flush after every print, the "-u" option does more than that
# and this is easy enough.
def my_print(*args, **kwargs):
    print(*args, **kwargs)

    sys.stdout.flush()

my_print(
    """\
Comparing output of '{filename}' using '{python}' with flags {args} ...""".
format(
        filename = filename,
        python   = os.environ["PYTHON"],
        args     = ", ".join(arguments)
    )
)

if not silent_mode:
    my_print("*" * 80)
    my_print("CPython:")
    my_print("*" * 80)

if two_step_execution:
    filename = os.path.abspath(filename)

if module_mode:
    cpython_cmd = [
        os.environ["PYTHON"],
        "-W", "ignore",
        "-c", "import sys; sys.path.append(%s); import %s" % (
            repr(os.path.dirname(filename)),
            os.path.basename(filename)
        )
    ]
else:
    cpython_cmd = [
        os.environ["PYTHON"],
        "-W", "ignore",
        filename
    ]

if no_site:
    cpython_cmd.insert(1, "-S")

if "NUITKA" in os.environ:
    nuitka_call = [os.environ["NUITKA"]]
else:
    nuitka_call = [
        os.environ["PYTHON"],
        os.path.abspath(os.path.join(os.path.dirname(__file__), "nuitka"))
    ]

if python_debug:
    extra_options += " --python-debug"

if remove_output:
    extra_options += " --remove-output"

if binary_python_path:
    if os.name == "nt":
        python_path_sep = ";"
    else:
        python_path_sep = ":"

    python_path = os.environ.get("PYTHONPATH", "")
    os.environ["PYTHONPATH"] = python_path_sep.join(
        python_path.split(python_path_sep) + \
        [os.path.dirname(os.path.abspath(filename))]
    )

if keep_python_path or binary_python_path:
    extra_options += " --keep-pythonpath"

if recurse_none:
    extra_options += " --recurse-none"

if not two_step_execution:
    if module_mode:
        nuitka_cmd = nuitka_call + extra_options.split() + \
          ["--execute", "--python-flag=no_warnings", "--module", filename]
    elif standalone_mode:
        nuitka_cmd = nuitka_call + extra_options.split() + \
          ["--execute", "--python-flag=no_warnings", "--standalone", filename]
    else:
        nuitka_cmd = nuitka_call + extra_options.split() + \
          ["--execute", "--python-flag=no_warnings", filename]

    if no_site:
        nuitka_cmd.insert(len(nuitka_cmd) - 1, "--python-flag=-S")

else:
    if module_mode:
        nuitka_cmd1 = nuitka_call + extra_options.split() + \
          ["--python-flag=no_warnings", "--module", os.path.abspath(filename)]
    elif standalone_mode:
        nuitka_cmd1 = nuitka_call + extra_options.split() + \
          ["--python-flag=no_warnings", "--standalone", filename]
    else:
        nuitka_cmd1 = nuitka_call + extra_options.split() + \
          ["--python-flag=no_warnings", filename]

    if no_site:
        nuitka_cmd1.insert(len(nuitka_cmd1) - 1, "--python-flag=-S")


dir_match = re.search(r"--output-dir=(.*?)(\s|$)", extra_options)

if dir_match:
    output_dir = dir_match.group(1)
else:
    output_dir = "."

if module_mode:
    nuitka_cmd2 = [
        os.environ["PYTHON"],
        "-W", "ignore",
        "-c", "import %s" % os.path.basename(filename)
    ]
else:
    exe_filename = os.path.basename(filename)

    if filename.endswith(".py"):
        exe_filename = exe_filename[:-3]

    exe_filename = exe_filename.replace(")", "").replace("(", "")
    exe_filename += ".exe"

    nuitka_cmd2 = [
        os.path.join(output_dir, exe_filename)
    ]

if trace_command:
    my_print("CPython command:", *cpython_cmd)

start_time = time.time()

process = subprocess.Popen(
    args   = cpython_cmd,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE
)

stdout_cpython, stderr_cpython = process.communicate()
exit_cpython = process.returncode

cpython_time = time.time() - start_time

def displayOutput(stdout, stderr):
    if type(stdout) is not str:
        stdout = stdout.decode("utf-8" if os.name != "nt" else "cp850")
        stderr = stderr.decode("utf-8" if os.name != "nt" else "cp850")

    my_print(stdout, end=' ')
    if stderr:
        my_print(stderr)

if not silent_mode:
    displayOutput(stdout_cpython, stderr_cpython)

if not silent_mode:
    my_print("*" * 80)
    my_print("Nuitka:")
    my_print("*" * 80)

if two_step_execution:
    if output_dir:
        os.chdir(output_dir)
    else:
        tmp_dir = tempfile.gettempdir()

        # Try to avoid RAM disk /tmp and use the disk one instead.
        if tmp_dir == "/tmp" and os.path.exists("/var/tmp"):
            tmp_dir = "/var/tmp"

        os.chdir(tmp_dir)

    if trace_command:
        my_print("Going to output directory", os.getcwd())

start_time = time.time()

if not two_step_execution:
    if trace_command:
        my_print("Nuitka command:", nuitka_cmd)
    process = subprocess.Popen(
        args   = nuitka_cmd,
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )

    stdout_nuitka, stderr_nuitka = process.communicate()
    exit_nuitka = process.returncode
else:
    if trace_command:
        my_print("Nuitka command 1:", nuitka_cmd1)

    process = subprocess.Popen(
        args   = nuitka_cmd1,
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )

    stdout_nuitka1, stderr_nuitka1 = process.communicate()
    exit_nuitka1 = process.returncode

    if exit_nuitka1 != 0:
        exit_nuitka = exit_nuitka1
        stdout_nuitka, stderr_nuitka = stdout_nuitka1, stderr_nuitka1
    else:
        if trace_command:
            my_print("Nuitka command 2:", nuitka_cmd2)

        process = subprocess.Popen(
            args   = nuitka_cmd2,
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE
        )

        stdout_nuitka2, stderr_nuitka2 = process.communicate()
        stdout_nuitka = stdout_nuitka1 + stdout_nuitka2
        stderr_nuitka = stderr_nuitka1 + stderr_nuitka2
        exit_nuitka = process.returncode

nuitka_time = time.time() - start_time

if not silent_mode:
    displayOutput(stdout_nuitka, stderr_nuitka)

ran_tests_re                 = re.compile(r"^(Ran \d+ tests? in )\d+\.\d+s$")
instance_re                  = re.compile(r"at (?:0x)?[0-9a-fA-F]+")
thread_re                    = re.compile(r"Thread 0x[0-9a-fA-F]+")
compiled_function_re         = re.compile(r"\<compiled function")
compiled_frame_re            = re.compile(r"\<compiled_frame")
compiled_genexpr_re          = re.compile(
    r"\<compiled generator object \<(.*?)\>"
)
compiled_generator_re        = re.compile(
    r"\<compiled generator object (.*?) at"
)
unbound_method_re            = re.compile(r"bound compiled_method ")
compiled_type_re             = re.compile(r"type 'compiled_")
compiled_generator_object_re = re.compile(r"'compiled_generator' object")
module_repr_re               = re.compile(r"(\<module '.*?' from ').*?('\>)")

global_name_error_re         = re.compile(
    r"global (name ')(.*?)(' is not defined)"
)
non_ascii_error_rt           = re.compile(
    r"(SyntaxError: Non-ASCII character.*? on line) \d+"
)
python_win_lib_re            = re.compile(
    r"[a-zA-Z]:\\\\?[Pp]ython(.*\\\\?)[Ll]ib"
)


def makeDiffable(output):
    result = []

    # Fix import "readline" because output sometimes starts with "\x1b[?1034h"
    m = re.match(b'\\x1b\\[[^h]+h', output)
    if m:
        output = output[len(m.group()):]

    for line in output.split(b"\n"):
        if type(line) is not str:
            line = line.decode("utf-8" if os.name != "nt" else "cp850")

        if line.endswith("\r"):
            line = line[:-1]

        if line.startswith("REFCOUNTS"):
            first_value = line[line.find("[")+1:line.find(",")]
            last_value = line[line.rfind(" ")+1:line.rfind("]")]
            line = line.\
              replace(first_value, "xxxxx").\
              replace(last_value, "xxxxx")

        if line.startswith("[") and line.endswith("refs]"):
            continue

        if ignore_warnings and line.startswith("Nuitka:WARNING"):
            continue

        if line.startswith("Nuitka:WARNING:Cannot recurse to import"):
            continue

        line = instance_re.sub(r"at 0xxxxxxxxx", line)
        line = thread_re.sub(r"Thread 0xXXXXXXXX", line)
        line = compiled_function_re.sub(r"<function", line)
        line = compiled_frame_re.sub(r"<frame", line)
        line = compiled_genexpr_re.sub(r"<generator object <\1>", line)
        line = compiled_generator_re.sub(r"<generator object \1 at", line)
        line = unbound_method_re.sub(r"bound method ", line)
        line = compiled_type_re.sub(r"type '", line)
        line = compiled_generator_object_re.sub(r"'generator' object", line)
        line = global_name_error_re.sub(r"\1\2\3", line)
        line = module_repr_re.sub(r"\1xxxxx\2", line)
        line = line.replace("'compiled_module'", "'module'")
        line = line.replace("'compiled_function'", "'function'")
        line = non_ascii_error_rt.sub(r"\1 xxxx", line)

        # Windows has a different "os.path", update according to it.
        line = line.replace("ntpath", "posixpath")

        line = line.replace(
            "must be a mapping, not compiled_function",
            "must be a mapping, not function"
        )
        line = line.replace(
            "must be a sequence, not compiled_function",
            "must be a sequence, not function"
        )
        line = line.replace(
            "compiled_function object is not an iterator",
            "function object is not an iterator"
        )
        line = line.replace(
            "AttributeError: 'compiled_frame' object has no attribute 'clear'",
            "AttributeError: 'frame' object has no attribute 'clear'",
        )

        # This URL is updated, and Nuitka outputs the new one, but we test
        # against versions that don't have that.
        line = line.replace(
            "http://www.python.org/peps/pep-0263.html",
            "http://python.org/dev/peps/pep-0263/",
        )

        line = ran_tests_re.sub(r"\1x.xxxs", line)

        # This is a bug potentially, occurs only for CPython when re-directed,
        # we are going to ignore the issue as Nuitka is fine.
        if line == "Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.AttributeError'> ignored":
            continue

        # This is also a bug potentially, but only visible under
        # CPython
        line = python_win_lib_re.sub(r"C:\Python\1Lib", line)

        # This is a bug with clang potentially, can't find out why it says that.
        if line == "/usr/bin/ld: warning: .init_array section has zero size":
            continue

        # This is for NetBSD and OpenBSD, which seems to build "libpython" so
        # that it gives such warnings.
        if "() possibly used unsafely" in line or \
           "() is almost always misused" in line:
            continue

        # This is for CentOS5, where the linker says this, and it's hard to
        # disable
        if "skipping incompatible /usr/lib/libpython2.6.so" in line:
            continue

        # This is for self compiled Python with default options, gives this
        # harmless option for every time we link to "libpython".
        if "is dangerous, better use `mkstemp'" in line or \
           "In function `posix_tempnam'" in line or \
           "In function `posix_tmpnam'" in line:
            continue

        result.append(line)

    return result


def compareOutput(kind, out_cpython, out_nuitka):
    fromdate = None
    todate = None

    diff = difflib.unified_diff(
        makeDiffable(out_cpython),
        makeDiffable(out_nuitka),
        "{program} ({detail})".format(
            program = os.environ["PYTHON"],
            detail  = kind
        ),
        "{program} ({detail})".format(
            program = "nuitka",
            detail  = kind
        ),
        fromdate,
        todate,
        n=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", stdout_cpython, stdout_nuitka)

if ignore_stderr:
    exit_code_stderr = 0
else:
    exit_code_stderr = compareOutput("stderr", stderr_cpython, stderr_nuitka)

exit_code_return = exit_cpython != exit_nuitka

if exit_code_return:
    my_print(
        """\
Exit codes {exit_cpython:d} (CPython) != {exit_nuitka:d} (Nuitka)""".format(
            exit_cpython = exit_cpython,
            exit_nuitka  = exit_nuitka
        )
    )

exit_code = exit_code_stdout or exit_code_stderr or exit_code_return

if exit_code:
    sys.exit("Error, outputs differed.")

if expect_success and exit_cpython != 0:
    if silent_mode:
        displayOutput(stdout_cpython, stderr_cpython)

    sys.exit("Unexpected error exit from CPython.")

if expect_failure and exit_cpython == 0:
    sys.exit("Unexpected success exit from CPython.")

if remove_output:
    if not module_mode:
        if os.path.exists(nuitka_cmd2[0]):
            if os.name == "nt":
                # It appears there is a tiny lock race that we randomly cause,
                # likely because --run spawns a subprocess that might still
                # be doing the cleanup work.
                os.rename(nuitka_cmd2[0], nuitka_cmd2[0]+".away")
                for i in range(10):
                    try:
                        os.unlink(nuitka_cmd2[0]+".away")
                    except OSError:
                        time.sleep(2)
                        continue
                    else:
                        break

                assert not os.path.exists(nuitka_cmd2[0]+".away")

            else:
                os.unlink(nuitka_cmd2[0])
    else:
        if os.name == "nt":
            module_filename = os.path.basename(filename) + ".pyd"
        else:
            module_filename = os.path.basename(filename) + ".so"

        if os.path.exists(module_filename):
            os.unlink(module_filename)


if timing:
    my_print(
        "CPython took %.2fs vs %0.2fs Nuitka." % (
            cpython_time,
            nuitka_time
        )
    )

if not silent_mode:
    my_print("OK, same outputs.")
