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 CYTHON_VERSION = CYTHON_VERSION.split('+', 1)[0] 133 for s in ('.alpha', 'alpha'): 134 CYTHON_VERSION = CYTHON_VERSION.replace(s, 'a') 135 for s in ('.beta', 'beta', '.rc', 'rc', '.c', 'c'): 136 CYTHON_VERSION = CYTHON_VERSION.replace(s, 'b') 137 if Version(CYTHON_VERSION) < Version(CYTHON_VERSION_REQUIRED): 138 warn("*"*80) 139 warn() 140 warn(" You need to install Cython %s (you have version %s)" 141 % (CYTHON_VERSION_REQUIRED, CYTHON_VERSION)) 142 warn(" Download and install Cython <http://www.cython.org>") 143 warn() 144 warn("*"*80) 145 return False 146 # 147 return True 148 149def run_cython(source): 150 from conf.cythonize import run as cythonize 151 from distutils import log 152 log.set_verbosity(1) 153 log.info("cythonizing '%s' source" % source) 154 cythonize(source) 155 156def main(): 157 CYTHON_VERSION_REQUIRED = '0.13' 158 import sys, os, glob 159 from distutils import dep_util 160 source = os.path.join('src', 'petsc4py.PETSc.pyx') 161 target = os.path.splitext(source)[0]+".c" 162 depends = (glob.glob("src/include/*/*.pxd") + 163 glob.glob("src/*/*.pyx") + 164 glob.glob("src/*/*.pxi")) 165 if ((os.path.isdir('.hg') or os.path.isdir('.git')) and 166 dep_util.newer_group([source]+depends, target)): 167 if not chk_cython(CYTHON_VERSION_REQUIRED): 168 sys.exit(1) 169 run_cython(source) 170 run_setup() 171 172if __name__ == '__main__': 173 main() 174 175# ----------------------------------------------------------------------------- 176