core.py 6.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
'''
Created on 20 oct. 2012

@author: coissac
'''

from os import path
import os.path
import glob
import sys
Eric Coissac committed
11
from obidistutils.command.sdist import sdist
12 13


14 15
try:
    from setuptools import setup as ori_setup
Eric Coissac committed
16
    from setuptools.command.egg_info import egg_info
17 18 19 20
    has_setuptools = True
except ImportError:
    from distutils.core import setup as ori_setup
    has_setuptools = False
21 22 23 24 25 26

from distutils.extension import Extension

from obidistutils.command.build import build
from obidistutils.command.littlebigman import littlebigman
from obidistutils.command.build_cexe import build_cexe
27
from obidistutils.command.build_sphinx import build_sphinx
28 29 30 31 32
from obidistutils.command import build_ext
from obidistutils.command.build_ctools import build_ctools
from obidistutils.command.build_files import build_files
from obidistutils.command.build_scripts import build_scripts
from obidistutils.command.install_scripts import install_scripts
33
from obidistutils.command.install_sphinx import install_sphinx
34
from obidistutils.command.install import install
Eric Coissac committed
35
from obidistutils.command.pidname import pidname
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

from obidistutils.dist import Distribution


def findPackage(root,base=None):
    modules=[]
    if base is None:
        base=[]
    for module in (path.basename(path.dirname(x)) 
                   for x in glob.glob(path.join(root,'*','__init__.py'))):
        modules.append('.'.join(base+[module]))
        modules.extend(findPackage(path.join(root,module),base+[module]))
    return modules
    
def findCython(root,base=None,pyrexs=None):
    setupdir = os.path.dirname(sys.argv[0])
    pyrexs=[]
Eric Coissac committed
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    if base is None:
        base=[]
    for module in (path.basename(path.dirname(x)) 
                   for x in glob.glob(path.join(root,'*','__init__.py'))):
                       
                
        for pyrex in glob.glob(path.join(root,module,'*.pyx')):
            pyrexs.append(Extension('.'.join(base+[module,path.splitext(path.basename(pyrex))[0]]),[pyrex]))
            try:
                cfiles = os.path.splitext(pyrex)[0]+".cfiles"
                cfilesdir = os.path.dirname(cfiles)
                cfiles = open(cfiles)
                cfiles = [os.path.relpath(os.path.join(cfilesdir,y),setupdir).strip() 
                          if y[0] !='@' else y.strip()
                          for y in cfiles]
                
                print "@@@@>",cfiles
                incdir = set(os.path.dirname(x) for x in cfiles if x[-2:]==".h")
                cfiles = [x for x in cfiles if x[-2:]==".c"]                
                pyrexs[-1].sources.extend(cfiles)
                pyrexs[-1].include_dirs.extend(incdir)
                pyrexs[-1].extra_compile_args.extend(['-msse2'])
                
            except IOError:
                pass
            pyrexs[-1].sources.extend(glob.glob(os.path.splitext(pyrex)[0]+'.ext.*.c'))
            print pyrexs[-1].sources
            # Main.compile([pyrex],timestamps=True)
            
        pyrexs.extend(findCython(path.join(root,module),base+[module]))
    return pyrexs
    
def findC(root,base=None,pyrexs=None):
    setupdir = os.path.dirname(sys.argv[0])
    pyrexs=[]
    if base is None:
        base=[]
    for module in (path.basename(path.dirname(x)) 
                   for x in glob.glob(path.join(root,'*','__init__.py'))):
                
        for pyrex in glob.glob(path.join(root,module,'*.c')):
            if '.ext.' not in pyrex:
                pyrexs.append(Extension('.'.join(base+[module,path.splitext(path.basename(pyrex))[0]]),[pyrex]))
                try:
                    cfiles = os.path.splitext(pyrex)[0]+".cfiles"
                    cfilesdir = os.path.dirname(cfiles)
                    cfiles = open(cfiles)
                    cfiles = [os.path.relpath(os.path.join(cfilesdir,y),setupdir).strip() 
                              if y[0] !='@' else y.strip()
                              for y in cfiles]
                    incdir = set(os.path.dirname(x) for x in cfiles if x[-2:]==".h")
                    cfiles = [x for x in cfiles if x[-2:]==".c"]                
                    pyrexs[-1].sources.extend(cfiles)
                    pyrexs[-1].include_dirs.extend(incdir)
                    pyrexs[-1].extra_compile_args.extend(['-msse2'])
                except IOError:
                    pass
                pyrexs[-1].sources.extend(glob.glob(os.path.splitext(pyrex)[0]+'.ext.*.c'))
                print pyrexs[-1].sources
       
        pyrexs.extend(findC(path.join(root,module),base+[module]))
    return pyrexs

def rootname(x):
    return os.path.splitext(x.sources[0])[0]

COMMANDS = {'build':build,
            'littlebigman':littlebigman,
Eric Coissac committed
122
            'pidname':pidname,
123 124 125 126 127
            'build_ctools':build_ctools, 
            'build_files':build_files,
            'build_cexe':build_cexe, 
            'build_ext': build_ext,
            'build_scripts':build_scripts, 
128
            'build_sphinx':build_sphinx, 
129
            'install_scripts':install_scripts,
130
            'install_sphinx':install_sphinx,
Eric Coissac committed
131 132
            'install':install,
            'sdist':sdist}
133

Eric Coissac committed
134 135 136 137
if has_setuptools:
    COMMANDS['egg_info']=egg_info
    

138 139 140 141 142 143
CTOOLS =[]
CEXES  =[]
FILES  =[]

def setup(**attrs):
    
144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if has_setuptools:
        try:
             
            requirements = open('requirements.txt').readlines()
            requirements = [x.strip() for x in requirements]
            requirements = [x for x in requirements if x[0]!='-']
         
            if 'install_requires' not in attrs:
                attrs['install_requires']=requirements
            else:
                attrs['install_requires'].extend(requirements)
        except IOError:
            pass

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    if 'distclass' not in attrs:
        attrs['distclass']=Distribution

    if 'python_src' not in attrs:
        SRC = 'src'
    else:
        SRC = attrs['python_src']
        del(attrs['python_src'])
    
    if 'scripts' not in attrs:
        attrs['scripts'] = glob.glob('%s/*.py' % SRC)

    if 'package_dir' not in attrs:
        attrs['package_dir'] = {'': SRC}

    if 'packages' not in attrs:
        attrs['packages'] = findPackage(SRC)
    
    if 'cmdclass' not in attrs:
        attrs['cmdclass'] = COMMANDS

    if 'ctools' not in attrs:
        attrs['ctools'] = CTOOLS
    
    if 'executables' not in attrs:
        attrs['executables'] = CEXES
        
    if 'files' not in attrs:
        attrs['files'] = FILES
        
    if 'sse' not in attrs:
        attrs['sse']=None
191 192 193
        
    if 'serenity' not in attrs:
        attrs['serenity']=False
194 195 196 197 198 199 200 201 202 203
    

    EXTENTION=findCython(SRC)
    CEXTENTION=findC(SRC)
    cython_ext = set(rootname(x) for x in EXTENTION)
    EXTENTION.extend(x for x in CEXTENTION 
                     if rootname(x) not in cython_ext)
    
    if 'ext_modules' not in attrs:
        attrs['ext_modules'] = EXTENTION
204
            
205
    ori_setup(**attrs)