import os

try:
    from setuptools import setup, Extension
    use_setuptools = True
    print("setuptools is used.")
except ImportError:
    from distutils.core import setup, Extension
    use_setuptools = False
    print("distutils is used.")

try:
    from numpy.distutils.misc_util import get_numpy_include_dirs
except ImportError:
    print("numpy.distutils.misc_util cannot be imported. Please install "
          "numpy first before installing spglib...")
    sys.exit(1)

# Workaround Python issue 21121
import sysconfig
config_var = sysconfig.get_config_var("CFLAGS")
if config_var is not None and "-Werror=declaration-after-statement" in config_var:
    os.environ['CFLAGS'] = config_var.replace(
        "-Werror=declaration-after-statement", "")    

sources = ['arithmetic.c',
           'cell.c',
           'delaunay.c',
           'hall_symbol.c',
           'kgrid.c',
           'kpoint.c',
           'mathfunc.c',
           'niggli.c',
           'pointgroup.c',
           'primitive.c',
           'refinement.c',
           'sitesym_database.c',
           'site_symmetry.c',
           'spacegroup.c',
           'spin.c',
           'spg_database.c',
           'spglib.c',
           'symmetry.c']

if os.path.exists('src'):
    source_dir = "src"
else:
    source_dir = "../src"
include_dirs = [source_dir,]
for i,s in enumerate(sources):
    sources[i] = "%s/%s" % (source_dir, s) 

extra_compile_args = []
extra_link_args = []
define_macros = []

## Uncomment to activate OpenMP support for gcc
# extra_compile_args += ['-fopenmp']
# extra_link_args += ['-lgomp']

## For debugging
# define_macros = [('SPGWARNING', None),
#                  ('SPGDEBUG', None)]

extension = Extension('spglib._spglib',
                      include_dirs=include_dirs + get_numpy_include_dirs(),
                      sources=['_spglib.c'] + sources,
                      extra_compile_args=extra_compile_args,
                      extra_link_args=extra_link_args,
                      define_macros=define_macros)

version_nums = [None, None, None]
with open("%s/version.h" % source_dir) as w:
    for line in w:
        for i, chars in enumerate(("MAJOR", "MINOR", "MICRO")):
            if chars in line:
                version_nums[i] = int(line.split()[2])

# To deploy to pypi by travis-CI
nanoversion = 0
if os.path.isfile("__nanoversion__.txt"):
    with open('__nanoversion__.txt') as nv:
        try :
            for line in nv:
                nanoversion = int(line.strip())
                break
        except ValueError :
            nanoversion = 0
        if nanoversion:
            version_nums.append(nanoversion)

if None in version_nums:
    print("Failed to get version number in setup.py.")
    raise

version = ".".join(["%d" % n for n in version_nums])
if use_setuptools:
    setup(name='spglib',
          version=version,
          description='This is the spglib module.',
          author='Atsushi Togo',
          author_email='atz.togo@gmail.com',
          url='http://atztogo.github.io/spglib/',
          packages=['spglib'],
          install_requires=['numpy'],
          provides=['spglib'],
          platforms=['all'],
          ext_modules=[extension])
else:
    setup(name='spglib',
          version=version,
          description='This is the spglib module.',
          author='Atsushi Togo',
          author_email='atz.togo@gmail.com',
          url='http://atztogo.github.io/spglib/',
          packages=['spglib'],
          requires=['numpy'],
          provides=['spglib'],
          platforms=['all'],
          ext_modules=[extension])

@@ -4,25 +4,16 @@
 ort os%0A%0A
-try:%0A    
 from set
@@ -40,32 +40,29 @@
 , Extension%0A
-    use_
+from 
 setuptools =
@@ -63,409 +63,355 @@
 ools
- = True%0A    print(%22setuptools is used.%22)%0Aexcept ImportError:%0A    from distutils.core import setup, Extension%0A    use_setuptools = False%0A    print(%22distutils is used.%22)%0A%0Atry:%0A    from numpy.distutils.misc_u
+.command.build_ext import build_ext as _build_ext%0A%0A%0Aclass build_ext(_build_ext):%0A    def finalize_options(self):%0A        _build_ext.finalize_options(self)%0A        # Prevent numpy from thinking it is s
 til
+l
  i
-mport get_numpy_include_dirs%0Aexcept ImportError
+n its setup process
 :%0A    
+ 
-print(%22numpy.distutils.misc_util cannot be imported. Please install %22%0A          %22numpy first before installing spglib...%22)%0A    sys.exit(1)
+   __builtins__.__NUMPY_SETUP__ = False%0A        import numpy%0A        self.include_dirs.append(numpy.get_include())%0A
 %0A%0A# 
@@ -1665,84 +1665,8 @@
 b',%0A
-                      include_dirs=include_dirs + get_numpy_include_dirs(),%0A
     
@@ -2653,31 +2653,9 @@
 s%5D)%0A
-if use_setuptools:%0A    
+%0A
 setu
@@ -2669,36 +2669,32 @@
 'spglib',%0A      
-    
 version=version,
@@ -2704,408 +2704,95 @@
     
- 
-   description='This is the spglib module.',%0A          author='Atsushi Togo',%0A          author_email='atz.togo@gmail.com',%0A          url='http://atztogo.github.io/spglib/',%0A          packages=%5B'spglib'%5D,%0A          install_requires=%5B'numpy'%5D,%0A          provides=%5B'spglib'%5D,%0A          platforms=%5B'all'%5D,%0A          ext_modules=%5Bextension%5D)%0Aelse:%0A    setup(name='spglib',%0A          version=version,%0A    
+cmdclass=%7B'build_ext': build_ext%7D,%0A      setup_requires=%5B'numpy', 'setuptools%3E=18.0'%5D,%0A
     
@@ -2837,28 +2837,24 @@
 le.',%0A      
- 
-   
 author='Atsu
@@ -2864,20 +2864,16 @@
  Togo',%0A
-    
       au
@@ -2911,20 +2911,16 @@
 ,%0A      
-    
 url='htt
@@ -2957,20 +2957,16 @@
 ,%0A      
-  
-  
 packages
@@ -2984,20 +2984,24 @@
 ,%0A      
-    
+install_
 requires
@@ -3008,28 +3008,24 @@
 =%5B'numpy'%5D,%0A
-    
       provid
@@ -3045,20 +3045,16 @@
 ,%0A      
-  
-  
 platform
@@ -3064,20 +3064,16 @@
 'all'%5D,%0A
-    
       ex

Modify setup.py to dynamically install numpy before building spglib.