# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import errno
import itertools
import logging
import os
import time
from abc import ABCMeta, abstractmethod
from contextlib import contextmanager
from pathlib import Path

import mozpack.path as mozpath
from mach.mixin.logging import LoggingMixin

from mozbuild.base import ExecutionSummary

from ..frontend.data import ContextDerived
from ..frontend.reader import EmptyConfig
from ..preprocessor import Preprocessor
from ..pythonutil import iter_modules_in_path
from ..util import FileAvoidWrite, simple_diff
from .configenvironment import ConfigEnvironment


class BuildBackend(LoggingMixin):
    """Abstract base class for build backends.

    A build backend is merely a consumer of the build configuration (the output
    of the frontend processing). It does something with said data. What exactly
    is the discretion of the specific implementation.
    """

    __metaclass__ = ABCMeta

    def __init__(self, environment):
        assert isinstance(environment, (ConfigEnvironment, EmptyConfig))
        self.populate_logger()

        self.environment = environment

        # Files whose modification should cause a new read and backend
        # generation.
        self.backend_input_files = set()

        # Files generated by the backend.
        self._backend_output_files = set()

        self._environments = {}
        self._environments[environment.topobjdir] = environment

        # The number of backend files created.
        self._created_count = 0

        # The number of backend files updated.
        self._updated_count = 0

        # The number of unchanged backend files.
        self._unchanged_count = 0

        # The number of deleted backend files.
        self._deleted_count = 0

        # The total wall time spent in the backend. This counts the time the
        # backend writes out files, etc.
        self._execution_time = 0.0

        # Mapping of changed file paths to diffs of the changes.
        self.file_diffs = {}

        self.dry_run = False

        self._init()

    def summary(self):
        return ExecutionSummary(
            self.__class__.__name__.replace("Backend", "")
            + " backend executed in {execution_time:.2f}s\n  "
            "{total:d} total backend files; "
            "{created:d} created; "
            "{updated:d} updated; "
            "{unchanged:d} unchanged; "
            "{deleted:d} deleted",
            execution_time=self._execution_time,
            total=self._created_count + self._updated_count + self._unchanged_count,
            created=self._created_count,
            updated=self._updated_count,
            unchanged=self._unchanged_count,
            deleted=self._deleted_count,
        )

    def _init(self):
        """Hook point for child classes to perform actions during __init__.

        This exists so child classes don't need to implement __init__.
        """

    def consume(self, objs):
        """Consume a stream of TreeMetadata instances.

        This is the main method of the interface. This is what takes the
        frontend output and does something with it.

        Child classes are not expected to implement this method. Instead, the
        base class consumes objects and calls methods (possibly) implemented by
        child classes.
        """

        # Previously generated files.
        list_file = mozpath.join(
            self.environment.topobjdir, "backend.%s" % self.__class__.__name__
        )
        backend_output_list = set()
        if os.path.exists(list_file):
            with open(list_file) as fh:
                backend_output_list.update(
                    mozpath.normsep(p) for p in fh.read().splitlines()
                )

        for obj in objs:
            obj_start = time.monotonic()
            if not self.consume_object(obj) and not isinstance(self, PartialBackend):
                raise Exception("Unhandled object of type %s" % type(obj))
            self._execution_time += time.monotonic() - obj_start

            if isinstance(obj, ContextDerived) and not isinstance(self, PartialBackend):
                self.backend_input_files |= obj.context_all_paths

        # Pull in all loaded Python as dependencies so any Python changes that
        # could influence our output result in a rescan.
        self.backend_input_files |= set(
            iter_modules_in_path(self.environment.topsrcdir, self.environment.topobjdir)
        )

        finished_start = time.monotonic()
        self.consume_finished()
        self._execution_time += time.monotonic() - finished_start

        # Purge backend files created in previous run, but not created anymore
        delete_files = backend_output_list - self._backend_output_files
        for path in delete_files:
            full_path = mozpath.join(self.environment.topobjdir, path)
            try:
                with open(full_path, encoding="utf-8") as existing:
                    old_content = existing.read()
                    if old_content:
                        self.file_diffs[full_path] = simple_diff(
                            full_path, old_content.splitlines(), None
                        )
            except OSError:
                pass
            try:
                if not self.dry_run:
                    os.unlink(full_path)
                self._deleted_count += 1
            except OSError:
                pass
        # Remove now empty directories
        for dir in set(mozpath.dirname(d) for d in delete_files):
            try:
                os.removedirs(dir)
            except OSError:
                pass

        # Write out the list of backend files generated, if it changed.
        if backend_output_list != self._backend_output_files:
            with self._write_file(list_file) as fh:
                fh.write("\n".join(sorted(self._backend_output_files)))
        else:
            # Always update its mtime if we're not in dry-run mode.
            if not self.dry_run:
                with open(list_file, "a"):
                    os.utime(list_file, None)

        # Write out the list of input files for the backend
        with self._write_file("%s.in" % list_file) as fh:
            fh.write(
                "\n".join(sorted(mozpath.normsep(f) for f in self.backend_input_files))
            )

    @abstractmethod
    def consume_object(self, obj):
        """Consumes an individual TreeMetadata instance.

        This is the main method used by child classes to react to build
        metadata.
        """

    def consume_finished(self):
        """Called when consume() has completed handling all objects."""

    def build(self, config, output, jobs, verbose, what=None):
        """Called when 'mach build' is executed.

        This should return the status value of a subprocess, where 0 denotes
        success and any other value is an error code. A return value of None
        indicates that the default 'make -f client.mk' should run.
        """
        return None

    def _write_purgecaches(self, config):
        """Write .purgecaches sentinels.

        The purgecaches mechanism exists to allow the platform to
        invalidate the XUL cache (which includes some JS) at application
        startup-time.  The application checks for .purgecaches in the
        application directory, which varies according to
        --enable-application/--enable-project.  There's a further wrinkle on
        macOS, where the real application directory is part of a Cocoa bundle
        produced from the regular application directory by the build
        system.  In this case, we write to both locations, since the
        build system recreates the Cocoa bundle from the contents of the
        regular application directory and might remove a sentinel
        created here.
        """

        app = config.substs["MOZ_BUILD_APP"]
        if app == "mobile/android":
            # In order to take effect, .purgecaches sentinels would need to be
            # written to the Android device file system.
            return

        root = mozpath.join(config.topobjdir, "dist", "bin")

        if app == "browser":
            root = mozpath.join(config.topobjdir, "dist", "bin", "browser")

        purgecaches_dirs = [root]
        if app == "browser" and "cocoa" == config.substs["MOZ_WIDGET_TOOLKIT"]:
            bundledir = mozpath.join(
                config.topobjdir,
                "dist",
                config.substs["MOZ_MACBUNDLE_NAME"],
                "Contents",
                "Resources",
                "browser",
            )
            purgecaches_dirs.append(bundledir)

        for dir in purgecaches_dirs:
            os.makedirs(dir, exist_ok=True)
            with open(mozpath.join(dir, ".purgecaches"), "w") as f:
                f.write("\n")

    def _setup_tor_browser_environment(self, config):
        app = config.substs["MOZ_BUILD_APP"]

        noscript_target_filename = "{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi"
        noscript_location = config.substs.get("NOSCRIPT")

        def _infallible_symlink(src, dst):
            try:
                os.symlink(src, dst)
            except OSError as e:
                if e.errno == errno.EEXIST:
                    # If the symlink already exists, remove it and try again.
                    os.remove(dst)
                    os.symlink(src, dst)
                else:
                    self.log(
                        logging.ERROR,
                        "_setup_tor_browser_environment",
                        {},
                        "Error creating symlink.",
                    )
                    return

        if app == "mobile/android":
            # Set up NoScript extension
            # We put it in the srcdir... It will be moved to the APK in the gradle build.
            if noscript_location:
                noscript_target = (
                    Path(config.topsrcdir)
                    / "mobile/android/fenix/app/src/main/assets/extensions"
                    / noscript_target_filename
                )
                self.log(
                    logging.INFO,
                    "_setup_tor_browser_environment",
                    {
                        "noscript_location": noscript_location,
                        "noscript_target": str(noscript_target),
                    },
                    "Creating symlink for NoScript from {noscript_location} to {noscript_target}",
                )

                _infallible_symlink(noscript_location, noscript_target)

        if app == "browser":
            tbdir = Path(config.topobjdir) / "dist" / "bin"

            if config.substs.get("OS_TARGET") == "Darwin":
                tbdir = next(tbdir.glob("*.app"))
                paths = {
                    "docs": tbdir / "Contents/Resources/TorBrowser/Docs",
                    "exts": tbdir / "Contents/Resources/distribution/extensions",
                    "tor_bin": tbdir / "Contents/MacOS/tor",
                    "tor_config": tbdir / "Contents/Resources/TorBrowser/Tor",
                    "fonts": tbdir / "Resources/fonts",
                }
            else:
                paths = {
                    "docs": tbdir / "TorBrowser/Docs",
                    "exts": tbdir / "distribution/extensions",
                    "tor_bin": tbdir / "TorBrowser/Tor",
                    "tor_config": tbdir / "TorBrowser/Data/Tor",
                    "fonts": tbdir / "fonts",
                }

            fonts_location = config.substs.get("TOR_BROWSER_FONTS")
            if fonts_location:
                self.log(
                    logging.INFO,
                    "_setup_tor_browser_environment",
                    {
                        "fonts_location": fonts_location,
                        "fonts_target": str(paths["fonts"]),
                    },
                    "Creating symlink for fonts files from {fonts_location} to {fonts_target}",
                )

                for file in Path(fonts_location).iterdir():
                    target = paths["fonts"] / file.name
                    _infallible_symlink(file, target)

            # Set up NoScript extension
            if noscript_location:
                noscript_target = paths["exts"] / noscript_target_filename
                self.log(
                    logging.INFO,
                    "_setup_tor_browser_environment",
                    {
                        "noscript_location": noscript_location,
                        "noscript_target": str(noscript_target),
                    },
                    "Creating symlink for NoScript from {noscript_location} to {noscript_target}",
                )

                paths["exts"].mkdir(parents=True, exist_ok=True)
                _infallible_symlink(noscript_location, noscript_target)

            expert_bundle_location = config.substs.get("TOR_EXPERT_BUNDLE")
            if expert_bundle_location:
                expert_bundle_location = Path(expert_bundle_location)
                if not expert_bundle_location.is_dir():
                    return

                self.log(
                    logging.INFO,
                    "_setup_tor_browser_environment",
                    {
                        "expert_bundle_location": str(expert_bundle_location),
                    },
                    "Setting up tor-expert-bundle resources from {expert_bundle_location}",
                )

                # Set up Tor configuration files
                paths["tor_config"].mkdir(parents=True, exist_ok=True)
                for file in ["geoip", "geoip6", "torrc-defaults"]:
                    target = paths["tor_config"] / file
                    _infallible_symlink(expert_bundle_location / "data" / file, target)

                # Set up Conjure documentation
                conjust_docs_location = paths["docs"] / "conjure"
                conjust_docs_location.mkdir(parents=True, exist_ok=True)
                conjure_readme = conjust_docs_location / "README.CONJURE.md"
                _infallible_symlink(
                    expert_bundle_location
                    / "tor/pluggable_transports/README.CONJURE.md",
                    conjure_readme,
                )

                # Set up pluggable transports
                paths["tor_bin"].mkdir(parents=True, exist_ok=True)
                pluggable_transports_location = (
                    expert_bundle_location / "tor/pluggable_transports"
                )
                pluggable_transports_target = paths["tor_bin"] / "PluggableTransports"
                pluggable_transports_target.mkdir(parents=True, exist_ok=True)
                for file in pluggable_transports_location.iterdir():
                    # We only want the PT executables.
                    if os.access(file, os.X_OK) or file.suffix.lower() == ".exe":
                        target = pluggable_transports_target / file.name
                        _infallible_symlink(file, target)

                # Setup Tor binary
                for item in Path(expert_bundle_location / "tor").iterdir():
                    target = paths["tor_bin"] / item.name

                    if item.is_file():
                        _infallible_symlink(item, target)

                # Set up licenses
                licenses_location = paths["docs"] / "Licenses"
                licenses_location.mkdir(parents=True, exist_ok=True)
                for item in (expert_bundle_location / "docs").iterdir():
                    target = licenses_location / item.name
                    _infallible_symlink(item, target)

    def post_build(self, config, output, jobs, verbose, status):
        """Called late during 'mach build' execution, after `build(...)` has finished.

        `status` is the status value returned from `build(...)`.

        In the case where `build` returns `None`, this is called after
        the default `make` command has completed, with the status of
        that command.

        This should return the status value from `build(...)`, or the
        status value of a subprocess, where 0 denotes success and any
        other value is an error code.

        If an exception is raised, ``mach build`` will fail with a
        non-zero exit code.
        """
        self._write_purgecaches(config)

        if status == 0:
            self._setup_tor_browser_environment(config)

        return status

    @contextmanager
    def _write_file(self, path=None, fh=None, readmode="r"):
        """Context manager to write a file.

        This is a glorified wrapper around FileAvoidWrite with integration to
        update the summary data on this instance.

        Example usage:

            with self._write_file('foo.txt') as fh:
                fh.write('hello world')
        """

        if path is not None:
            assert fh is None
            fh = FileAvoidWrite(
                path, capture_diff=True, dry_run=self.dry_run, readmode=readmode
            )
        else:
            assert fh is not None

        dirname = mozpath.dirname(fh.name)
        os.makedirs(dirname, exist_ok=True)

        yield fh

        self._backend_output_files.add(
            mozpath.relpath(fh.name, self.environment.topobjdir)
        )
        existed, updated = fh.close()
        if fh.diff:
            self.file_diffs[fh.name] = fh.diff
        if not existed:
            self._created_count += 1
        elif updated:
            self._updated_count += 1
        else:
            self._unchanged_count += 1

    @contextmanager
    def _get_preprocessor(self, obj):
        """Returns a preprocessor with a few predefined values depending on
        the given BaseConfigSubstitution(-like) object, and all the substs
        in the current environment."""
        pp = Preprocessor()
        srcdir = mozpath.dirname(obj.input_path)
        pp.context.update(
            {
                k: " ".join(v) if isinstance(v, list) else v
                for k, v in obj.config.substs.items()
            }
        )
        pp.context.update(
            top_srcdir=obj.topsrcdir,
            topobjdir=obj.topobjdir,
            srcdir=srcdir,
            srcdir_rel=mozpath.relpath(srcdir, mozpath.dirname(obj.output_path)),
            relativesrcdir=mozpath.relpath(srcdir, obj.topsrcdir) or ".",
            DEPTH=mozpath.relpath(obj.topobjdir, mozpath.dirname(obj.output_path))
            or ".",
        )
        pp.do_filter("attemptSubstitution")
        pp.setMarker(None)
        with self._write_file(obj.output_path) as fh:
            pp.out = fh
            yield pp


class PartialBackend(BuildBackend):
    """A PartialBackend is a BuildBackend declaring that its consume_object
    method may not handle all build configuration objects it's passed, and
    that it's fine."""


def HybridBackend(*backends):
    """A HybridBackend is the combination of one or more PartialBackends
    with a non-partial BuildBackend.

    Build configuration objects are passed to each backend, stopping at the
    first of them that declares having handled them.
    """
    assert len(backends) >= 2
    assert all(issubclass(b, PartialBackend) for b in backends[:-1])
    assert not (issubclass(backends[-1], PartialBackend))
    assert all(issubclass(b, BuildBackend) for b in backends)

    class TheHybridBackend(BuildBackend):
        def __init__(self, environment):
            self._backends = [b(environment) for b in backends]
            super(TheHybridBackend, self).__init__(environment)

        def consume_object(self, obj):
            return any(b.consume_object(obj) for b in self._backends)

        def consume_finished(self):
            for backend in self._backends:
                backend.consume_finished()

            for attr in (
                "_execution_time",
                "_created_count",
                "_updated_count",
                "_unchanged_count",
                "_deleted_count",
            ):
                setattr(self, attr, sum(getattr(b, attr) for b in self._backends))

            for b in self._backends:
                self.file_diffs.update(b.file_diffs)
                for attr in ("backend_input_files", "_backend_output_files"):
                    files = getattr(self, attr)
                    files |= getattr(b, attr)

    name = "+".join(
        itertools.chain(
            (b.__name__.replace("Backend", "") for b in backends[:-1]),
            (b.__name__ for b in backends[-1:]),
        )
    )

    return type(str(name), (TheHybridBackend,), {})
