#!/usr/bin/python -tt

import os
import sys
from setuptools import setup

{%- if version.major < 6 %}

def detect_bad_upgrade():
    # prevent direct upgrade from 2.9.x or earlier to 2.10 due to pip limitations
    try:
        import ansible
    except ImportError:
        # ansible is not installed
        return False

    try:
        current_version = ansible.__version__.split('.')
        current_filename = ansible.__file__
    except AttributeError:
        # ansible is installed but already broken.  We're probably being reinstalled.
        return False

    try:
        current_version = (int(current_version[0]), int(current_version[1]))
    except Exception as e:
        print("""\n
            ### ERROR ###

            The currently installed ansible found at:

            {0}

{%-  if ansible_core_package_name == 'ansible-core' %}
            is of an unknown version.  Since upgrading directly from ansible-2.x, ansible-3, or
            ansible-base to ansible-4 or newer with pip is known to cause problems, please uninstall
            the old version and install the new version:

                pip uninstall ansible       # if installed
                pip uninstall ansible-base  # if installed
                pip install ansible
{%-  else %}
            is of an unknown version.  Since upgrading directly from ansible-2.9 or less to
            ansible-2.10 with pip is known to cause problems, please uninstall the old version and
            install the new version:

                pip uninstall ansible
                pip install ansible
{%-  endif %}

            If you have a broken installation, perhaps because {{ ansible_core_package_name }} was installed before
            ansible was upgraded, try this to resolve it:

                pip install --force-reinstall ansible {{ ansible_core_package_name }}

            If ansible is installed in a different location than you will be installing it now
            (for example, if the old version is installed by a system package manager to
            /usr/lib/python3.8/site-packages/ansible but you are installing the new version into
            ~/.local/lib/python3.8/site-packages/ansible with `pip install --user ansible`)
            or you want to install anyways and cleanup any breakage afterwards, then you may set
            the ANSIBLE_SKIP_CONFLICT_CHECK environment variable to ignore this check:

                ANSIBLE_SKIP_CONFLICT_CHECK=1 pip install --user ansible

            ### END ERROR ###

            """.format(current_filename))
    else:
{%-  if ansible_core_package_name == 'ansible-core' %}
        if current_version >= (2, 11):
            return False

        if current_version == (2, 10):
            print("""\n
                ### ERROR ###

                Upgrading directly from ansible-2.10, ansible-3, or ansible-base-2.10 to ansible-4
                or greater with pip is known to cause problems.  Please uninstall the old version found at:

                {0}

                and install the new version:

                    pip uninstall ansible       # if installed
                    pip uninstall ansible-base  # if installed
                    pip install ansible

                If you have a broken installation, perhaps because {{ ansible_core_package_name }} was installed before
                ansible was upgraded, try this to resolve it:

                    pip install --force-reinstall ansible {{ ansible_core_package_name }}

                If ansible is installed in a different location than you will be installing it now
                (for example, if the old version is installed by a system package manager to
                /usr/lib/python3.8/site-packages/ansible but you are installing the new version into
                ~/.local/lib/python3.8/site-packages/ansible with `pip install --user ansible`)
                or you want to install anyways and cleanup any breakage afterwards, then you may set
                the ANSIBLE_SKIP_CONFLICT_CHECK environment variable to ignore this check:

                    ANSIBLE_SKIP_CONFLICT_CHECK=1 pip install --user ansible

                ### END ERROR ###

                """.format(current_filename))
{%-  else %}
        if current_version >= (2, 10):
            return False
{%-  endif %}

        print("""\n
            ### ERROR ###

            Upgrading directly from ansible-2.9 or less to ansible-2.10 or greater with pip is
            known to cause problems.  Please uninstall the old version found at:

            {0}

            and install the new version:

                pip uninstall ansible
                pip install ansible

            If you have a broken installation, perhaps because {{ ansible_core_package_name }} was installed before
            ansible was upgraded, try this to resolve it:

                pip install --force-reinstall ansible {{ ansible_core_package_name }}

            If ansible is installed in a different location than you will be installing it now
            (for example, if the old version is installed by a system package manager to
            /usr/lib/python3.8/site-packages/ansible but you are installing the new version into
            ~/.local/lib/python3.8/site-packages/ansible with `pip install --user ansible`)
            or you want to install anyways and cleanup any breakage afterwards, then you may set
            the ANSIBLE_SKIP_CONFLICT_CHECK environment variable to ignore this check:

                ANSIBLE_SKIP_CONFLICT_CHECK=1 pip install --user ansible

            ### END ERROR ###

            """.format(current_filename))

    return True


if not os.environ.get('ANSIBLE_SKIP_CONFLICT_CHECK'):
    if detect_bad_upgrade():
        sys.exit(1)

{%- endif %}

__version__ = '{{ version }}'
__author__ = 'Ansible, Inc.'


with open('README.rst', 'r') as f:
    long_desc = f.read()

setup(
    name='ansible',
    version=__version__,
    description='Radically simple IT automation',
    long_description=long_desc,
    author=__author__,
    author_email='info@ansible.com',
    url='https://ansible.com/',
    project_urls={
        'Bug Tracker': 'https://github.com/ansible/ansible/issues',
        'Code of Conduct': 'https://docs.ansible.com/ansible/latest/community/code_of_conduct.html',
        'Documentation': 'https://docs.ansible.com/ansible/',
        'Mailing lists': 'https://docs.ansible.com/ansible/latest/community/communication.html#mailing-list-information',
        'Source Code': 'https://github.com/ansible/ansible',
    },
    license='GPLv3+',
{%- if version.major < 5 %}
    python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*',
{%- else %}
    python_requires='>=3.8',
{%- endif %}
    packages=['ansible_collections'],
{% if version.major >= 6 and collection_exclude_paths %}
    exclude_package_data={
        'ansible_collections': [
{%-  for path in collection_exclude_paths %}
            '{{ path }}',
{%-  endfor %}
        ],
    },
{%- endif %}
    include_package_data=True,
    install_requires=[
        '{{ ansible_core_package_name }} ~= {{ ansible_core_version }}',{{ collection_deps }}
    ],
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Environment :: Console',
        'Framework :: Ansible',
        'Intended Audience :: Developers',
        'Intended Audience :: Information Technology',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Natural Language :: English',
        'Operating System :: POSIX',
{%- if version.major < 5 %}
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
{%- endif %}
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: 3.10',
        'Topic :: System :: Installation/Setup',
        'Topic :: System :: Systems Administration',
        'Topic :: Utilities',
    ],
    data_files=[],
{%- if version >= PypiVer('6.0.0rc1') %}
    entry_points={
        'console_scripts': [
            'ansible-community=ansible_collections.ansible_community:main'
        ]
    },
{%- endif %}
    # Installing as zip files would break due to references to __file__
    zip_safe=False
)
