core.py 6.74 KB
Newer Older
Eric Coissac committed
1 2 3 4 5 6 7 8 9
'''
Created on 20 oct. 2012

@author: coissac
'''

from os import path
import os.path
import glob
10
import sys
Eric Coissac committed
11

12 13 14 15
try:
    from setuptools.extension import Extension
except ImportError:
    from distutils.extension import Extension
16

17
# from distutils.extension import Extension
Eric Coissac committed
18

19 20 21 22 23 24 25
from obidistutils.serenity.checkpackage import  install_requirements,\
                                                check_requirements, \
                                                RequirementError
                                                
from obidistutils.serenity.rerun import enforce_good_python
from obidistutils.serenity.rerun import rerun_with_anothe_python
from distutils import log
26

Eric Coissac committed
27
from obidistutils.dist import Distribution
28
from obidistutils.serenity import is_serenity
29 30
 
 
Eric Coissac committed
31 32 33 34 35 36 37 38 39 40 41 42 43
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=[]
44

Eric Coissac committed
45 46 47 48 49 50 51
    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')):
52 53 54 55
            pyrexs.append(Extension('.'.join(base+[module,path.splitext(path.basename(pyrex))[0]]),
                                    [pyrex]
                                    )
                          )
Eric Coissac committed
56 57 58 59
            try:
                cfiles = os.path.splitext(pyrex)[0]+".cfiles"
                cfilesdir = os.path.dirname(cfiles)
                cfiles = open(cfiles)
60
                cfiles = [os.path.relpath(os.path.join(cfilesdir,y),setupdir).strip() 
61
                          if y[0] !='@' else y.strip()
62
                          for y in cfiles]
63
                
64
                log.info("Cython module : %s",cfiles)   
Eric Coissac committed
65 66 67 68
                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)
Eric Coissac committed
69 70 71 72
                pyrexs[-1].extra_compile_args.extend(['-msse2',
                                                      '-Wno-unused-function',
                                                      '-Wmissing-braces',
                                                      '-Wchar-subscripts'])
Eric Coissac committed
73 74 75 76 77 78 79 80 81 82 83
                
            except IOError:
                pass
            
        pyrexs.extend(findCython(path.join(root,module),base+[module]))
    return pyrexs
    

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

84 85 86 87

def prepare_commands():
    from obidistutils.command.build import build
    from obidistutils.command.littlebigman import littlebigman
88
#    from obidistutils.command.serenity import serenity
89 90 91 92 93 94 95 96 97 98
    from obidistutils.command.build_cexe import build_cexe
    from obidistutils.command.build_ext 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
    from obidistutils.command.install_sphinx import install_sphinx
    from obidistutils.command.install import install
    from obidistutils.command.pidname import pidname
    from obidistutils.command.sdist import sdist
99 100 101
    
        
    
102
    COMMANDS = {'build':build,
103
#                'serenity':serenity,
104 105 106 107 108 109 110 111 112 113 114 115
                'littlebigman':littlebigman,
                'pidname':pidname,
                'build_ctools':build_ctools, 
                'build_files':build_files,
                'build_cexe':build_cexe, 
                'build_ext': build_ext,
                'build_scripts':build_scripts, 
                'install_scripts':install_scripts,
                'install_sphinx':install_sphinx,
                'install':install,
                'sdist':sdist}
    
116 117 118 119 120 121 122 123 124 125 126
#     try:
#         from setuptools.commands import egg_info
#         COMMANDS['egg_info']=egg_info
#     except ImportError:
#         pass
    try:
        from obidistutils.command.build_sphinx import build_sphinx
        COMMANDS['build_sphinx']=build_sphinx
    except ImportError:
        pass

127 128
    return COMMANDS

Eric Coissac committed
129 130 131

CTOOLS =[]
CEXES  =[]
132
FILES  =[]
Eric Coissac committed
133 134

def setup(**attrs):
135
    
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    log.set_threshold(log.INFO)
    
    minversion      = attrs.get("pythonmin",'3.4')
    maxversion      = attrs.get('pythonmax',None)    
    fork            = attrs.get('fork',False)
    requirementfile = attrs.get('requirements','requirements.txt')

    try:
        del attrs['pythonmin']
    except KeyError:
        pass

    try:
        del attrs['pythonmax']
    except KeyError:
        pass

    try:
        del attrs['fork']
    except KeyError:
        pass

    try:
        del attrs['requirements']
    except KeyError:
        pass
    
163
    if is_serenity():
164
    
165 166 167 168 169
        
        enforce_good_python(minversion, maxversion, fork)
        
        if (install_requirements(requirementfile)):
            rerun_with_anothe_python(sys.executable,minversion,maxversion,fork)
170 171
        
    
172 173 174 175 176
        try:
            check_requirements(requirementfile)
        except RequirementError as e :
            log.error(e)                                   
            sys.exit(1)
177
        
Eric Coissac committed
178 179 180 181
    if 'distclass' not in attrs:
        attrs['distclass']=Distribution

    if 'python_src' not in attrs:
182
        SRC = 'python'
Eric Coissac committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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:
197
        attrs['cmdclass'] = prepare_commands()
Eric Coissac committed
198 199 200 201 202 203

    if 'ctools' not in attrs:
        attrs['ctools'] = CTOOLS
    
    if 'executables' not in attrs:
        attrs['executables'] = CEXES
204 205 206 207 208 209
        
    if 'files' not in attrs:
        attrs['files'] = FILES
        
    if 'sse' not in attrs:
        attrs['sse']=None
210 211 212
        
    if 'serenity' not in attrs:
        attrs['serenity']=False
Eric Coissac committed
213
    
214
    EXTENTION=findCython(SRC)
Eric Coissac committed
215 216 217
    
    if 'ext_modules' not in attrs:
        attrs['ext_modules'] = EXTENTION
218 219 220 221 222 223 224
        
#     try:
#         from setuptools.core import setup as ori_setup
#     except ImportError:
#         from distutils.core import setup as ori_setup

    from distutils.core import setup as ori_setup
Eric Coissac committed
225 226
    
    ori_setup(**attrs)