# Copyright 2011 OpenStack Foundation
# Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
#    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 unicode_literals

from distutils.command import install as du_install
from distutils import log
import os
import sys

import setuptools
from setuptools.command import egg_info
from setuptools.command import install
from setuptools.command import install_scripts
from setuptools.command import sdist

import pbr._compat.easy_install
import pbr._compat.metadata
import pbr._compat.versions
from pbr import extra_files
from pbr import git
from pbr import options
from pbr import version


if pbr._compat.versions.setuptools_has_develop_command:
    from setuptools.command import develop

    class LocalDevelop(develop.develop):

        command_name = 'develop'

        def install_wrapper_scripts(self, dist):
            if sys.platform == 'win32':
                return develop.develop.install_wrapper_scripts(self, dist)
            if not self.exclude_scripts:
                for (
                    args
                ) in pbr._compat.easy_install.ScriptWriter.get_script_args(
                    dist
                ):
                    self.write_script(*args)


class LocalInstallScripts(install_scripts.install_scripts):
    """Intercepts console scripts entry_points."""

    command_name = 'install_scripts'

    def run(self):
        import distutils.command.install_scripts

        self.run_command("egg_info")
        if self.distribution.scripts:
            # run first to set up self.outfiles
            distutils.command.install_scripts.install_scripts.run(self)
        else:
            self.outfiles = []

        ei_cmd = self.get_finalized_command("egg_info")
        dist = pbr._compat.metadata.dist(
            ei_cmd.egg_base,
            ei_cmd.egg_info,
            ei_cmd.egg_name,
            ei_cmd.egg_version,
        )
        bs_cmd = self.get_finalized_command('build_scripts')
        executable = getattr(
            bs_cmd, 'executable', pbr._compat.easy_install.sys_executable
        )
        if 'bdist_wheel' in self.distribution.have_run:
            # We're building a wheel which has no way of generating mod_wsgi
            # scripts for us. Let's build them.
            # NOTE(sigmavirus24): This needs to happen here because, as the
            # comment below indicates, no_ep is True when building a wheel.

            header = pbr._compat.easy_install.ScriptWriter.get_header(
                "", executable
            )

            wsgi_script_template = pbr._compat.easy_install.ENTRY_POINTS_MAP[
                'wsgi_scripts'
            ]
            wsgi_scripts = pbr._compat.metadata.get_entry_points(
                dist, 'wsgi_scripts'
            )
            for name, ep in wsgi_scripts:
                content = pbr._compat.easy_install.generate_script(
                    'wsgi_scripts', ep, header, wsgi_script_template
                )
                self.write_script(name, content)

        if self.no_ep:
            # no_ep is True if we're installing into an .egg file or building
            # a .whl file, in those cases, we do not want to build all of the
            # entry-points listed for this package.
            return

        if os.name == 'nt':
            executable = '"%s"' % executable

        for args in pbr._compat.easy_install.ScriptWriter.get_script_args(
            dist, executable
        ):
            self.write_script(*args)


class LocalManifestMaker(egg_info.manifest_maker):
    """Add any files that are in git and some standard sensible files."""

    def _add_pbr_defaults(self):
        for template_line in [
            'include AUTHORS',
            'include ChangeLog',
            'exclude .gitignore',
            'exclude .gitreview',
            'global-exclude *.pyc',
        ]:
            self.filelist.process_template_line(template_line)

    def add_defaults(self):
        """Add all the default files to self.filelist:

        Extends the functionality provided by distutils to also included
        additional sane defaults, such as the ``AUTHORS`` and ``ChangeLog``
        files generated by *pbr*.

        Warns if (``README`` or ``README.txt``) or ``setup.py`` are missing;
        everything else is optional.
        """
        option_dict = self.distribution.get_option_dict('pbr')

        sdist.sdist.add_defaults(self)
        self.filelist.append(self.template)
        self.filelist.append(self.manifest)
        self.filelist.extend(extra_files.get_extra_files())
        should_skip = options.get_boolean_option(
            option_dict, 'skip_git_sdist', 'SKIP_GIT_SDIST'
        )
        if not should_skip:
            rcfiles = git._find_git_files()
            if rcfiles:
                self.filelist.extend(rcfiles)
        elif os.path.exists(self.manifest):
            self.read_manifest()
        ei_cmd = self.get_finalized_command('egg_info')
        self._add_pbr_defaults()
        self.filelist.include_pattern("*", prefix=ei_cmd.egg_info)


class LocalEggInfo(egg_info.egg_info):
    """Override the egg_info command to regenerate SOURCES.txt sensibly."""

    command_name = 'egg_info'

    def find_sources(self):
        """Generate SOURCES.txt only if there isn't one already.

        If we are in an sdist command, then we always want to update
        SOURCES.txt. If we are not in an sdist command, then it doesn't
        matter one flip, and is actually destructive.
        However, if we're in a git context, it's always the right thing to do
        to recreate SOURCES.txt
        """
        manifest_filename = os.path.join(self.egg_info, "SOURCES.txt")
        if (
            not os.path.exists(manifest_filename)
            or os.path.exists('.git')
            or 'sdist' in sys.argv
        ):
            log.info("[pbr] Processing SOURCES.txt")
            mm = LocalManifestMaker(self.distribution)
            mm.manifest = manifest_filename
            mm.run()
            self.filelist = mm.filelist
        else:
            log.info("[pbr] Reusing existing SOURCES.txt")
            self.filelist = egg_info.FileList()
            with open(manifest_filename, 'r') as fil:
                for entry in fil.read().split('\n'):
                    self.filelist.append(entry)


def _from_git(distribution):
    option_dict = distribution.get_option_dict('pbr')
    changelog = git._iter_log_oneline()
    if changelog:
        changelog = git._iter_changelog(changelog)
    git.write_git_changelog(option_dict=option_dict, changelog=changelog)
    git.generate_authors(option_dict=option_dict)


class InstallWithGit(install.install):
    """Extracts ChangeLog and AUTHORS from git then installs.

    This is useful for e.g. readthedocs where the package is
    installed and then docs built.
    """

    command_name = 'install'

    def run(self):
        _from_git(self.distribution)
        return install.install.run(self)


class LocalInstall(install.install):
    """Runs python setup.py install in a sensible manner.

    Force a non-egg installed in the manner of
    single-version-externally-managed, which allows us to install manpages
    and config files.
    """

    command_name = 'install'

    def run(self):
        _from_git(self.distribution)
        return du_install.install.run(self)


class LocalSDist(sdist.sdist):
    """Builds the ChangeLog and Authors files from VC first."""

    command_name = 'sdist'

    def checking_reno(self):
        """Ensure reno is installed and configured.

        We can't run reno-based commands if reno isn't installed/available, and
        don't want to if the user isn't using it.
        """
        if hasattr(self, '_has_reno'):
            return self._has_reno

        option_dict = self.distribution.get_option_dict('pbr')
        should_skip = options.get_boolean_option(
            option_dict, 'skip_reno', 'SKIP_GENERATE_RENO'
        )
        if should_skip:
            self._has_reno = False
            return False

        try:
            # versions of reno witout this module will not have the required
            # feature, hence the import
            from reno import setup_command  # noqa
        except ImportError:
            log.info(
                '[pbr] reno was not found or is too old. Skipping '
                'release notes'
            )
            self._has_reno = False
            return False

        conf, output_file, cache_file = setup_command.load_config(
            self.distribution
        )

        if not os.path.exists(os.path.join(conf.reporoot, conf.notespath)):
            log.info(
                '[pbr] reno does not appear to be configured. Skipping '
                'release notes'
            )
            self._has_reno = False
            return False

        self._files = [output_file, cache_file]

        log.info('[pbr] Generating release notes')
        self._has_reno = True

        return True

    sub_commands = [('build_reno', checking_reno)] + sdist.sdist.sub_commands

    def run(self):
        _from_git(self.distribution)
        # sdist.sdist is an old style class, can't use super()
        sdist.sdist.run(self)

    def make_distribution(self):
        # This is included in make_distribution because setuptools doesn't use
        # 'get_file_list'. As such, this is the only hook point that runs after
        # the commands in 'sub_commands'
        if self.checking_reno():
            self.filelist.extend(self._files)
            self.filelist.sort()
        sdist.sdist.make_distribution(self)


class LocalRPMVersion(setuptools.Command):
    __doc__ = """Output the rpm *compatible* version string of this package"""
    description = __doc__

    user_options = []
    command_name = "rpm_version"

    def run(self):
        log.info("[pbr] Extracting rpm version")
        name = self.distribution.get_name()
        print(version.VersionInfo(name).semantic_version().rpm_string())

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass


class LocalDebVersion(setuptools.Command):
    __doc__ = """Output the deb *compatible* version string of this package"""
    description = __doc__

    user_options = []
    command_name = "deb_version"

    def run(self):
        log.info("[pbr] Extracting deb version")
        name = self.distribution.get_name()
        print(version.VersionInfo(name).semantic_version().debian_string())

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass
