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

"""

This is the main program of Nuitka, it checks the options and then translates
one or more modules to a C++ source code using Python C/API in a build
directory compiles it to either an executable or an extension module that can
contain other modules.

"""

# Import as little as possible initially, because we might be re-executing
# soon.
import logging
import os
import sys
import warnings

# LIBDIR trick start (marker for removal on platforms that don't need it)
libdir = '@LIBDIR@'

# Two cases:
if libdir != '@' 'LIBDIR' '@':
    # Changed by our "distutils" hook, then use the given path.

    if not os.path.isabs(libdir):
        libdir = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            libdir
        )
        libdir = os.path.abspath(libdir)

    sys.path.insert(
        0,
        libdir
    )
else:
    # 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__),
                ".."
            )
        )
    )
# LIBDIR trick end (marker for removal on platforms that don't need it)

from nuitka import Options, Utils   # isort:skip

logging.basicConfig(format = 'Nuitka:%(levelname)s:%(message)s')

# We don't care, and these are triggered by run time calculations of "range" and
# others, while on python2.7 they are disabled by default.

warnings.simplefilter("ignore", DeprecationWarning)

# We will run with the Python configuration as specified by the user, if it does
# not match, we restart ourselves with matching configuration.
needs_reexec = False

current_version = "%d.%d" % (sys.version_info[0], sys.version_info[1])

# We support to execute with a specified version.
intended_version = Options.getIntendedPythonVersion()
if intended_version is None:
    intended_version = current_version

# If it's a different version, we find it by guessing it, otherwise we use the
# one previously used.
if current_version != intended_version:
    if Utils.getOS() == "Windows":
        python_binary = r"C:\Python%s\python.exe" % \
          intended_version.replace( ".", "" )
    else:
        python_binary = "/usr/bin/python" + intended_version

    needs_reexec = True
else:
    python_binary = sys.executable

python_flags = Options.getPythonFlags()

if sys.flags.no_site == 0 and "no_site" in python_flags:
    needs_reexec = True

# The hash randomization totally changes the created source code created,
# changing it every single time Nuitka is run. This kills any attempt at
# caching it, and comparing generated source code. While the created binary
# actually may still use it, during compilation we don't want to. So lets
# disable it.
if os.environ.get("PYTHONHASHSEED", "-1") != "0":
    os.environ["PYTHONHASHSEED"] = "0"
    needs_reexec = True

# In case we need to re-execute.
if needs_reexec:
    # Execute with full path as the process name, so it can find itself and its
    # libraries.
    args = [
        python_binary,
        python_binary,
    ]

    # Potentially give Python command line flags as necessary.
    if "no_site" in python_flags:
        args.append("-S")

    # Same arguments as before.
    args += sys.argv + list(Options.getMainArgs())

    if current_version == intended_version:
        os.environ["NUITKA_PYTHONPATH"] = repr(sys.path)

    Utils.callExec(args)

# Inform the user about potential issues.
if current_version not in Options.getSupportedPythonVersions():

    # Do not disturb run of automatic tests, detected from the presence of
    # that environment variable.
    if "PYTHON" not in os.environ:
        logging.warning(
            "The version '%s' is not currently supported. Expect problems.",
            current_version
        )

if "NUITKA_PYTHONPATH" in os.environ:
    # Restore the PYTHONPATH gained from the site module, that we chose not
    # to have imported. pylint: disable=W0123
    sys.path = eval(os.environ["NUITKA_PYTHONPATH"])
    del os.environ["NUITKA_PYTHONPATH"]

# Now the main program.
from nuitka import MainControl  # isort:skip
MainControl.main()
