setup.py 6.5 KB
Newer Older
1 2 3
#! /usr/bin/env python

from distutils.core import setup
4
from distutils.extension import Extension
5 6 7 8
from distutils.util import convert_path
from distutils import log
from distutils.dep_util import newer
from distutils import sysconfig
9 10 11 12 13 14

try:
    from Cython.Distutils import build_ext
    from Cython.Compiler import Main
    has_cython=True
except ImportError:
15
    from distutils.command.build_ext import build_ext
16 17
    has_cython=False

18 19 20 21 22 23
from distutils.command.build_scripts import build_scripts as ori_build_scripts
from distutils.command.build_scripts import first_line_re

from stat import ST_MODE

import os.path
24 25 26 27 28
import re, sys
import glob

from os import path

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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
class build_scripts(ori_build_scripts):
    def copy_scripts (self):
        """Copy each script listed in 'self.scripts'; if it's marked as a
        Python script in the Unix way (first line matches 'first_line_re',
        ie. starts with "\#!" and contains "python"), then adjust the first
        line to refer to the current Python interpreter as we copy.
        """
        self.mkpath(self.build_dir)
        outfiles = []
        for script in self.scripts:
            adjust = 0
            script = convert_path(script)
            outfile = os.path.join(self.build_dir, os.path.splitext(os.path.basename(script))[0])
            outfiles.append(outfile)

            if not self.force and not newer(script, outfile):
                log.debug("not copying %s (up-to-date)", script)
                continue

            # Always open the file, but ignore failures in dry-run mode --
            # that way, we'll get accurate feedback if we can read the
            # script.
            try:
                f = open(script, "r")
            except IOError:
                if not self.dry_run:
                    raise
                f = None
            else:
                first_line = f.readline()
                if not first_line:
                    self.warn("%s is an empty file (skipping)" % script)
                    continue

                match = first_line_re.match(first_line)
                if match:
                    adjust = 1
                    post_interp = match.group(1) or ''

            if adjust:
                log.info("copying and adjusting %s -> %s", script,
                         self.build_dir)
                if not self.dry_run:
                    outf = open(outfile, "w")
                    if not sysconfig.python_build:
                        outf.write("#!%s%s\n" %
                                   (self.executable,
                                    post_interp))
                    else:
                        outf.write("#!%s%s\n" %
                                   (os.path.join(
                            sysconfig.get_config_var("BINDIR"),
                           "python%s%s" % (sysconfig.get_config_var("VERSION"),
                                           sysconfig.get_config_var("EXE"))),
                                    post_interp))
                    outf.writelines(f.readlines())
                    outf.close()
                if f:
                    f.close()
            else:
                if f:
                    f.close()
                self.copy_file(script, outfile)

        if os.name == 'posix':
            for file in outfiles:
                if self.dry_run:
                    log.info("changing mode of %s", file)
                else:
                    oldmode = os.stat(file)[ST_MODE] & 07777
                    newmode = (oldmode | 0555) & 07777
                    if newmode != oldmode:
                        log.info("changing mode of %s from %o to %o",
                                 file, oldmode, newmode)
                        os.chmod(file, newmode)
    
105 106 107
class build_filters(build_scripts):
    pass

108

109 110 111 112 113 114 115 116 117 118
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
    
119 120 121 122 123
def findCython(root,base=None,pyrexs=None):
    modules=[]
    pyrexs=[]
    #o=dict(Main.default_options)
    pyopt=Main.CompilationOptions(Main.default_options)
124 125
    #print dir(pyopt)
    Main.__dict__['context'] = Main.Context(pyopt.include_path, {})
126 127 128 129 130 131 132
    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]))
133 134
            pyrexs[-1].sources.extend(glob.glob(os.path.splitext(pyrex)[0]+'.*.c'))
            print pyrexs[-1].sources
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            Main.compile([pyrex],timestamps=True,recursion=True)
            
        pyrexs.extend(findCython(path.join(root,module),base+[module]))
    return pyrexs
    
def findC(root,base=None,pyrexs=None):
    modules=[]
    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')):
            pyrexs.append(Extension('.'.join(base+[module,path.splitext(path.basename(pyrex))[0]]),[pyrex]))
        
151
        pyrexs.extend(findC(path.join(root,module),base+[module]))
152 153
    return pyrexs
    
154

155
VERSION = '0.2.000'
156
AUTHOR  = 'Eric Coissac'
157 158
EMAIL   = 'eric@coissac.eu'
URL     = 'www.grenoble.prabi.fr/trac/OBITools'
159 160
LICENSE = 'CeCILL-V2'

161 162
SRC       = 'src'
FILTERSRC = 'textwrangler/filter'
163 164

SCRIPTS = glob.glob('%s/*.py' % SRC)
165 166 167 168
FILTERS = glob.glob('%s/*.py' % FILTERSRC)

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

170 171
if has_cython:
    EXTENTION=findCython(SRC)
172 173 174
    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)
175 176 177
else:
    EXTENTION=findC(SRC)
    
178
#SCRIPTS.append('src/fastaComplement')
179 180 181 182 183 184 185 186 187 188

setup(name="OBITools",
      description="Scripts and library for sequence analysis",
      version=VERSION,
      author=AUTHOR,
      author_email=EMAIL,
      license=LICENSE,
      url=URL,
      scripts=SCRIPTS,
      package_dir = {'': SRC},
189
      packages=findPackage(SRC),
190 191
      cmdclass = {'build_ext': build_ext,'build_scripts':build_scripts},
      requires=['Cython (>=0.12)'],
192
      zip_safe = False,
193
      ext_modules=EXTENTION)
194