#!/usr/bin/env python

from __future__ import print_function

import os
import subprocess
import sys

import numpy as np
from setuptools import setup, Extension
import Cython.Build


pypkg="pycydemo"

# Try to get the version from git describe
__version__ = None
try:
    print('Trying to get the version from git describe')
    git_describe = subprocess.check_output(["git", "describe", "--tags"])
    version_words = git_describe.decode('utf-8').strip().split('-')
    __version__ = version_words[0]
    if len(version_words) > 1:
        __version__ += '.post' + version_words[1]
    print('Version from git describe: {}'.format(__version__))
except (subprocess.CalledProcessError, OSError):
    pass

# Interact with version.py
fn_version = os.path.join(os.path.dirname(__file__), pypkg, 'version.py')
version_template = """\
\"""Do not edit this file, versioning is governed by ``git describe --tags`` and ``setup.py``.\"""
__version__ = '{}'
"""
if __version__ is None:
    print('Trying to get the version from {}',format(fn_version))
    # Try to load the git version tag from version.py
    try:
        with open(fn_version, 'r') as fh:
            __version__ = fh.read().split('=')[-1].replace('\'', '').strip()
    except IOError:
        print('Could not determine version. Giving up.')
        sys.exit(1)
    print('Version according to {}: {}'.format(fn_version, __version__))
else:
    # Store the git version tag in version.py
    print('Writing version to {}'.format(fn_version))
    with open(fn_version, 'w') as fh:
        fh.write(version_template.format(__version__))


setup(
    name=pypkg,
    version=__version__,
    description='Demo python+cython project',
    author='Toon Verstraelen',
    author_email='Toon.Verstraelen@UGent.be',
    url='https://github.com/tovrstra/python-cython-ci-example',
    cmdclass={'build_ext': Cython.Build.build_ext},
    package_dir = {pypkg: pypkg},
    packages=[pypkg, pypkg + '.tests'],
    ext_modules=[Extension(
        pypkg + ".extension",
        sources=[pypkg + "/extension.pyx"],
        include_dirs=[np.get_include()],
    )],
    zip_safe=False,
    setup_requires=['numpy>=1.0', 'cython>=0.24.1'],
    install_requires=['numpy>=1.0', 'nose>=0.11', 'cython>=0.24.1'],
)
