xref: /petsc/setup.py (revision f33b81999e499544a7d8ebfd7e5ac1a4f0aca700)
1#!/usr/bin/env python
2
3"""
4PETSc for Python
5================
6
7Python bindings for PETSc libraries.
8"""
9
10
11## try:
12##     import setuptools
13## except ImportError:
14##     pass
15
16
17# -----------------------------------------------------------------------------
18# Metadata
19# -----------------------------------------------------------------------------
20
21from conf.metadata import metadata
22
23def version():
24    import os, re
25    data = open(os.path.join('src', '__init__.py')).read()
26    m = re.search(r"__version__\s*=\s*'(.*)'", data)
27    return m.groups()[0]
28
29name     = 'petsc4py'
30version  = version()
31
32url      = 'http://%(name)s.googlecode.com/' % vars()
33download = url + 'files/%(name)s-%(version)s.tar.gz' % vars()
34
35descr    = __doc__.strip().split('\n'); del descr[1:3]
36devstat  = ['Development Status :: 3 - Alpha']
37keywords = ['PETSc', 'MPI']
38
39metadata['name'] = name
40metadata['version'] = version
41metadata['description'] = descr.pop(0)
42metadata['long_description'] = '\n'.join(descr)
43metadata['keywords'] += keywords
44metadata['classifiers'] += devstat
45metadata['url'] = url
46metadata['download_url'] = download
47
48metadata['provides'] = ['petsc4py']
49metadata['requires'] = ['numpy']
50
51# -----------------------------------------------------------------------------
52# Extension modules
53# -----------------------------------------------------------------------------
54
55def get_ext_modules(Extension):
56    from os   import walk, path
57    from glob import glob
58    depends = []
59    for pth, dirs, files in walk('src'):
60        depends += glob(path.join(pth, '*.h'))
61    for pth, dirs, files in walk(path.join('src', 'source')):
62        depends += glob(path.join(pth, '*.h'))
63        depends += glob(path.join(pth, '*.c'))
64    try:
65        import numpy
66        numpy_includes = [numpy.get_include()]
67    except ImportError:
68        numpy_includes = []
69    return [Extension('petsc4py.lib.PETSc',
70                      sources=['src/PETSc.c',
71                               'src/source/libpetsc4py.c',
72                               ],
73                      include_dirs=['src/include',
74                                    'src/source',
75                                    ] + numpy_includes,
76                      depends=depends)]
77
78# -----------------------------------------------------------------------------
79# Setup
80# -----------------------------------------------------------------------------
81
82from conf.petscconf import setup, Extension
83from conf.petscconf import config, build, build_ext
84
85def run_setup():
86    import sys, os
87    if (('distribute' in sys.modules) or
88        ('setuptools' in sys.modules)):
89        metadata['install_requires'] = ['numpy']
90        if not os.environ.get('PETSC_DIR'):
91            metadata['install_requires'].append('petsc')
92    if 'setuptools' in sys.modules:
93        metadata['zip_safe'] = False
94    setup(packages     = ['petsc4py',
95                          'petsc4py.lib',],
96          package_dir  = {'petsc4py'     : 'src',
97                          'petsc4py.lib' : 'src/lib'},
98          package_data = {'petsc4py'     : ['include/petsc4py/*.h',
99                                            'include/petsc4py/*.i',
100                                            'include/petsc4py/*.pxd',
101                                            'include/petsc4py/*.pxi',
102                                            'include/petsc4py/*.pyx',],
103                          'petsc4py.lib' : ['petsc.cfg'],},
104          ext_modules  = get_ext_modules(Extension),
105          cmdclass     = {'config'     : config,
106                          'build'      : build,
107                          'build_ext'  : build_ext},
108          **metadata)
109
110def chk_cython(CYTHON_VERSION_REQUIRED):
111    import sys, os
112    from distutils.version import StrictVersion as Version
113    warn = lambda msg='': sys.stderr.write(msg+'\n')
114    #
115    try:
116        import Cython
117    except ImportError:
118        warn("*"*80)
119        warn()
120        warn(" You need to generate C source files with Cython!!")
121        warn(" Download and install Cython <http://www.cython.org>")
122        warn()
123        warn("*"*80)
124        return False
125    #
126    try:
127        CYTHON_VERSION = Cython.__version__
128    except AttributeError:
129        from Cython.Compiler.Version import version as CYTHON_VERSION
130    if Version(CYTHON_VERSION) < Version(CYTHON_VERSION_REQUIRED):
131        warn("*"*80)
132        warn()
133        warn(" You need to install Cython %s (you have version %s)"
134             % (CYTHON_VERSION_REQUIRED, CYTHON_VERSION))
135        warn(" Download and install Cython <http://www.cython.org>")
136        warn()
137        warn("*"*80)
138        return False
139    #
140    return True
141
142def run_cython(source):
143    from conf.cythonize import run as cythonize
144    from distutils import log
145    log.set_verbosity(1)
146    log.info("cythonizing '%s' source" % source)
147    cythonize(source)
148
149def main():
150    CYTHON_VERSION_REQUIRED = '0.13'
151    import sys, os, glob
152    from distutils import dep_util
153    source = os.path.join('src', 'petsc4py.PETSc.pyx')
154    target = os.path.splitext(source)[0]+".c"
155    depends = (glob.glob("src/include/*/*.pxd") +
156               glob.glob("src/*/*.pyx") +
157               glob.glob("src/*/*.pxi"))
158    if ((os.path.isdir('.hg') or os.path.isdir('.git')) and
159        dep_util.newer_group([source]+depends, target)):
160        if not chk_cython(CYTHON_VERSION_REQUIRED):
161            sys.exit(1)
162        run_cython(source)
163    run_setup()
164
165if __name__ == '__main__':
166    main()
167
168# -----------------------------------------------------------------------------
169