from __future__ import absolute_import
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _build_ext
from distutils.errors import CompileError
from warnings import warn
import os

# use cython if it is importable and the environment has USE_CYTHON
try:
    from Cython.Distutils import build_ext as _build_ext
except ImportError:
    use_cython = False
else:
    use_cython = os.getenv('USE_CYTHON', False)

# subclass the build_ext command to handle numpy include and build failures
class build_ext(_build_ext):
    # see http://stackoverflow.com/q/19919905 for explanation
    def finalize_options(self):
        _build_ext.finalize_options(self)
        __builtins__.__NUMPY_SETUP__ = False
        import numpy as np
        self.include_dirs.append(np.get_include())

    # if optional extension modules fail to build, keep going anyway
    def run(self):
        try:
            _build_ext.run(self)
        except CompileError:
            warn('Failed to compile optional extension modules')

# list the extension files to build
extensions = [
    Extension(
        'autograd.numpy.linalg_extra', ['autograd/numpy/linalg_extra.c'],
        extra_compile_args=['-w','-Ofast']),
]

# if using cython, regenerate the extension files from the .pyx sources
if use_cython:
    from Cython.Build import cythonize
    try:
        extensions = cythonize('**/*.pyx')
    except:
        warn('Failed to generate extension module code from Cython files')

setup(
    name='autograd',
    version='1.1.1',
    description='Efficiently computes derivatives of numpy code.',
    author='Dougal Maclaurin and David Duvenaud and Matthew Johnson',
    author_email="maclaurin@physics.harvard.edu, dduvenaud@seas.harvard.edu, mattjj@csail.mit.edu",
    packages=['autograd', 'autograd.numpy', 'autograd.scipy', 'autograd.scipy.stats'],
    install_requires=['numpy>=1.9', 'future'],
    setup_requires=['numpy>=1.9'],
    keywords=['Automatic differentiation', 'backpropagation', 'gradients',
              'machine learning', 'optimization', 'neural networks',
              'Python', 'Numpy', 'Scipy'],
    url='https://github.com/HIPS/autograd',
    license='MIT',
    classifiers=['Development Status :: 4 - Beta',
                 'License :: OSI Approved :: MIT License',
                 'Programming Language :: Python :: 2.7',
                 'Programming Language :: Python :: 3.4'],
    ext_modules=extensions,
    cmdclass={'build_ext': build_ext},
)

@@ -137,16 +137,69 @@
 ild_ext%0A
+from setuptools.command.sdist import sdist as _sdist%0A
 from dis
@@ -232,16 +232,16 @@
 leError%0A
-
 from war
@@ -1091,16 +1091,444 @@
 ules')%0A%0A
+# when building a source distribution (only for autograd maintainers), wrap the%0A# sdist command to try to generate extension module sources%0Aclass sdist(_sdist):%0A    def run(self):%0A        try:%0A            from Cython.Build import cythonize%0A            cythonize(os.path.join('**','*.pyx'))%0A        except:%0A            warn('Failed to generate extension files from Cython sources')%0A        finally:%0A            _sdist.run(self)%0A%0A
 # list t
@@ -1877,19 +1877,35 @@
 ize(
-'**/
+os.path.join('**','
 *.pyx')
+)
 %0A   
@@ -2034,17 +2034,17 @@
 on='1.1.
-1
+2
 ',%0A    d
@@ -2949,16 +2949,16 @@
 nsions,%0A
-
     cmdc
@@ -2985,13 +2985,29 @@
 uild_ext
+, 'sdist': sdist
 %7D,%0A)%0A

add an sdist command to setup that tries to generate extension modules