{# zetup.py
 #
 # Zimmermann's Python package setup.
 #
 # Copyright (C) 2014-2015 Stefan Zimmermann <zimmermann.code@gmail.com>
 #
 # zetup.py is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # zetup.py is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Lesser General Public License for more details.
 #
 # You should have received a copy of the GNU Lesser General Public License
 # along with zetup.py. If not, see <http://www.gnu.org/licenses/>.
 #}

{% block header %}{{ zetup_header }}{% endblock %}

{% block imports %}
import sys
try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup
else:
    from setuptools.dist import Distribution
{% endblock %}

SETUP_REQUIRES = [
  {% block setup_requires %}'zetup',
  {% for req in zfg.SETUP_REQUIRES or [] %}'{{ req }}',
  {% endfor %}
  {% endblock %}]

{% block pre_setup %}
try:
    Distribution
except NameError: # no setuptools
    pass
else:
    #HACK: make sure that setup requirements
    # are always correctly resolved and accessible
    # by pre-processing them one after another
    # and moving any installed eggs to the front of sys.path

    PYTHONPATH = list(sys.path)
    for req in SETUP_REQUIRES:
        Distribution({'setup_requires': req})
        sys.path = list(set(sys.path) - set(PYTHONPATH)) + PYTHONPATH
{% endblock %}

dist = setup(
  {% block setup_keywords %}
  setup_requires=SETUP_REQUIRES,

  use_zetup=True,
  {% endblock %}
  )

{% block post_setup %}{% endblock %}
