1#!/usr/bin/env python 2# Author: Lisandro Dalcin 3# Contact: dalcinl@gmail.com 4 5""" 6PETSc for Python 7""" 8 9import sys 10import os 11#try: 12# import setuptools 13#except ImportError: 14# pass 15 16 17# -------------------------------------------------------------------- 18# Metadata 19# -------------------------------------------------------------------- 20 21topdir = os.path.abspath(os.path.dirname(__file__)) 22 23from conf.metadata import metadata 24 25def name(): 26 return 'petsc4py' 27 28def version(): 29 import re 30 fh = open(os.path.join('src', '__init__.py')) 31 try: data = fh.read() 32 finally: fh.close() 33 m = re.search(r"__version__\s*=\s*'(.*)'", data) 34 return m.groups()[0] 35 36def description(): 37 f = open(os.path.join(topdir, 'DESCRIPTION.rst')) 38 try: data = f.read() 39 finally: f.close() 40 return data 41 42name = name() 43version = version() 44 45url = 'https://bitbucket.org/petsc/%(name)s/' % vars() 46download = url + 'downloads/%(name)s-%(version)s.tar.gz' % vars() 47 48devstat = ['Development Status :: 5 - Production/Stable'] 49keywords = ['PETSc', 'MPI'] 50 51metadata['name'] = name 52metadata['version'] = version 53metadata['description'] = __doc__.strip() 54metadata['long_description'] = description() 55metadata['keywords'] += keywords 56metadata['classifiers'] += devstat 57metadata['url'] = url 58metadata['download_url'] = download 59 60metadata['provides'] = ['petsc4py'] 61metadata['requires'] = ['numpy'] 62 63# -------------------------------------------------------------------- 64# Extension modules 65# -------------------------------------------------------------------- 66 67def get_ext_modules(Extension): 68 from os import walk, path 69 from glob import glob 70 depends = [] 71 for pth, dirs, files in walk('src'): 72 depends += glob(path.join(pth, '*.h')) 73 depends += glob(path.join(pth, '*.c')) 74 try: 75 import numpy 76 numpy_includes = [numpy.get_include()] 77 except ImportError: 78 numpy_includes = [] 79 return [Extension('petsc4py.lib.PETSc', 80 sources=['src/PETSc.c', 81 'src/libpetsc4py.c', 82 ], 83 include_dirs=['src/include', 84 ] + numpy_includes, 85 depends=depends)] 86 87# -------------------------------------------------------------------- 88# Setup 89# -------------------------------------------------------------------- 90 91from conf.petscconf import setup, Extension 92from conf.petscconf import config, build, build_src, build_ext 93from conf.petscconf import test, sdist 94 95CYTHON = '0.15' 96 97def run_setup(): 98 if ('setuptools' in sys.modules): 99 from os.path import exists, join 100 metadata['zip_safe'] = False 101 metadata['install_requires'] = ['numpy'] 102 if not exists(join('src', 'petsc4py.PETSc.c')): 103 metadata['install_requires'] += ['Cython>='+CYTHON] 104 PETSC_DIR = os.environ.get('PETSC_DIR') 105 if not (PETSC_DIR and os.path.isdir(PETSC_DIR)): 106 vstr = metadata['version'].split('.')[:2] 107 x, y = int(vstr[0]), int(vstr[1]) 108 reqs = ">=%s.%s,<%s.%s" % (x, y, x, y+1) 109 metadata['install_requires'] += ["petsc"+reqs] 110 # 111 setup(packages = ['petsc4py', 112 'petsc4py.lib',], 113 package_dir = {'petsc4py' : 'src', 114 'petsc4py.lib' : 'src/lib'}, 115 package_data = {'petsc4py' : ['include/petsc4py/*.h', 116 'include/petsc4py/*.i', 117 'include/petsc4py/*.pxd', 118 'include/petsc4py/*.pxi', 119 'include/petsc4py/*.pyx',], 120 'petsc4py.lib' : ['petsc.cfg'],}, 121 ext_modules = get_ext_modules(Extension), 122 cmdclass = {'config' : config, 123 'build' : build, 124 'build_src' : build_src, 125 'build_ext' : build_ext, 126 'test' : test, 127 'sdist' : sdist, 128 }, 129 **metadata) 130 131def chk_cython(VERSION): 132 import re 133 from distutils import log 134 from distutils.version import LooseVersion 135 from distutils.version import StrictVersion 136 warn = lambda msg='': sys.stderr.write(msg+'\n') 137 # 138 cython_zip = 'cython.zip' 139 if os.path.isfile(cython_zip): 140 path = os.path.abspath(cython_zip) 141 if sys.path[0] != path: 142 sys.path.insert(0, path) 143 log.info("adding '%s' to sys.path", cython_zip) 144 # 145 try: 146 import Cython 147 except ImportError: 148 warn("*"*80) 149 warn() 150 warn(" You need to generate C source files with Cython!!") 151 warn(" Download and install Cython <http://www.cython.org>") 152 warn() 153 warn("*"*80) 154 return False 155 # 156 try: 157 CYTHON_VERSION = Cython.__version__ 158 except AttributeError: 159 from Cython.Compiler.Version import version as CYTHON_VERSION 160 REQUIRED = VERSION 161 m = re.match(r"(\d+\.\d+(?:\.\d+)?).*", CYTHON_VERSION) 162 if m: 163 Version = StrictVersion 164 AVAILABLE = m.groups()[0] 165 else: 166 Version = LooseVersion 167 AVAILABLE = CYTHON_VERSION 168 if (REQUIRED is not None and 169 Version(AVAILABLE) < Version(REQUIRED)): 170 warn("*"*80) 171 warn() 172 warn(" You need to install Cython %s (you have version %s)" 173 % (REQUIRED, CYTHON_VERSION)) 174 warn(" Download and install Cython <http://www.cython.org>") 175 warn() 176 warn("*"*80) 177 return False 178 # 179 return True 180 181def run_cython(source, depends=(), includes=(), 182 destdir_c=None, destdir_h=None, wdir=None, 183 force=False, VERSION=None): 184 from glob import glob 185 from distutils import log 186 from distutils import dep_util 187 from distutils.errors import DistutilsError 188 target = os.path.splitext(source)[0]+".c" 189 cwd = os.getcwd() 190 try: 191 if wdir: os.chdir(wdir) 192 alldeps = [source] 193 for dep in depends: 194 alldeps += glob(dep) 195 if not (force or dep_util.newer_group(alldeps, target)): 196 log.debug("skipping '%s' -> '%s' (up-to-date)", 197 source, target) 198 return 199 finally: 200 os.chdir(cwd) 201 if not chk_cython(VERSION): 202 raise DistutilsError("requires Cython>=%s" % VERSION) 203 log.info("cythonizing '%s' -> '%s'", source, target) 204 from conf.cythonize import cythonize 205 err = cythonize(source, 206 includes=includes, 207 destdir_c=destdir_c, 208 destdir_h=destdir_h, 209 wdir=wdir) 210 if err: 211 raise DistutilsError( 212 "Cython failure: '%s' -> '%s'" % (source, target)) 213 214def build_sources(cmd): 215 from os.path import exists, isdir, join 216 if (exists(join('src', 'petsc4py.PETSc.c')) and 217 not (isdir('.hg') or isdir('.git')) and 218 not cmd.force): return 219 # petsc4py.PETSc 220 source = 'petsc4py.PETSc.pyx' 221 depends = ("include/*/*.pxd", 222 "PETSc/*.pyx", 223 "PETSc/*.pxi",) 224 includes = ['include'] 225 destdir_h = os.path.join('include', 'petsc4py') 226 run_cython(source, depends, includes, 227 destdir_c=None, destdir_h=destdir_h, wdir='src', 228 force=cmd.force, VERSION=CYTHON) 229 # libpetsc4py 230 source = os.path.join('libpetsc4py', 'libpetsc4py.pyx') 231 depends = ["include/petsc4py/*.pxd", 232 "libpetsc4py/*.pyx", 233 "libpetsc4py/*.pxi"] 234 includes = ['include'] 235 run_cython(source, depends, includes, 236 destdir_c=None, destdir_h=None, wdir='src', 237 force=cmd.force, VERSION=CYTHON) 238 239build_src.run = build_sources 240 241def run_testsuite(cmd): 242 from distutils.errors import DistutilsError 243 sys.path.insert(0, 'test') 244 try: 245 from runtests import main 246 finally: 247 del sys.path[0] 248 err = main(cmd.args or []) 249 if err: 250 raise DistutilsError("test") 251 252test.run = run_testsuite 253 254# -------------------------------------------------------------------- 255 256def main(): 257 run_setup() 258 259if __name__ == '__main__': 260 main() 261 262# -------------------------------------------------------------------- 263