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