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