setup.py 8.54 KB
Newer Older
1
#! /usr/bin/env python
Frédéric Boyer's avatar
Frédéric Boyer committed
2 3 4 5
#
# Install script
#
#
6

7

8 9 10 11
try:
   from setuptools.core import setup
except ImportError:
   from distutils.core import setup
12
from distutils.extension import Extension
13 14 15 16
from distutils.util import convert_path
from distutils import log
from distutils.dep_util import newer
from distutils import sysconfig
17 18

try:
19
    from Cython.Distutils import build_ext  as ori_build_ext
20 21 22
    from Cython.Compiler import Main
    has_cython=True
except ImportError:
23
    from distutils.command.build_ext import build_ext  as ori_build_ext
24 25
    has_cython=False

26 27 28
from distutils.command.build_scripts import build_scripts as ori_build_scripts
from distutils.command.build_scripts import first_line_re

Eric Coissac's avatar
Eric Coissac committed
29 30 31 32
from distutils.command.install_scripts import install_scripts as ori_install_scripts



33 34
from stat import ST_MODE

Eric Coissac's avatar
Eric Coissac committed
35
import os
36
import os.path
37 38 39 40 41
import re, sys
import glob

from os import path

Eric Coissac's avatar
Eric Coissac committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
class install_scripts(ori_install_scripts):

    def remove_deprecated_script(self):
        for f in DEPRECATED_SCRIPTS:
            try:
                ff = os.path.join(self.install_dir,f)
                os.unlink(ff)
                log.info('Removing deprecated unix command : %s (file : %s)' % (f,ff))
                ff = os.path.join(self.build_dir,f)
                os.unlink(ff)
            except:
                log.info('Unix command %s is not present' % f)
                pass

    def run(self):
        self.remove_deprecated_script()
        ori_install_scripts.run(self)
        
Eric Coissac's avatar
Eric Coissac committed
60 61 62 63 64 65 66 67
class build_ext(ori_build_ext):
    def modifyDocScripts(self):
        print >>open("doc/sphinx/build_dir.txt","w"),self.build_lib
        
    def run(self):
        self.modifyDocScripts()
        ori_build_ext.run(self)
    
Eric Coissac's avatar
Eric Coissac committed
68

69
class build_scripts(ori_build_scripts):
Eric Coissac's avatar
Eric Coissac committed
70
            
71 72 73 74 75
    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.
Eric Coissac's avatar
Eric Coissac committed
76
        """        
77
        self.mkpath(self.build_dir)
78 79 80
        rawbuild_dir = os.path.join(os.path.dirname(self.build_dir),'raw_scripts')
        self.mkpath(rawbuild_dir)
        
81 82 83 84 85
        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])
86
            rawoutfile = os.path.join(rawbuild_dir, os.path.basename(script))
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
            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 ''
112 113 114 115
            
            log.info("Store the raw script %s -> %s", script,rawoutfile)        
            self.copy_file(script, rawoutfile)

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

            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)
    
154 155 156
class build_filters(build_scripts):
    pass

157

158 159 160 161 162 163 164 165 166 167
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
    
168 169 170 171 172
def findCython(root,base=None,pyrexs=None):
    modules=[]
    pyrexs=[]
    #o=dict(Main.default_options)
    pyopt=Main.CompilationOptions(Main.default_options)
173 174
    #print dir(pyopt)
    Main.__dict__['context'] = Main.Context(pyopt.include_path, {})
175 176 177 178
    if base is None:
        base=[]
    for module in (path.basename(path.dirname(x)) 
                   for x in glob.glob(path.join(root,'*','__init__.py'))):
179
                       
180 181 182
                
        for pyrex in glob.glob(path.join(root,module,'*.pyx')):
            pyrexs.append(Extension('.'.join(base+[module,path.splitext(path.basename(pyrex))[0]]),[pyrex]))
183
            pyrexs[-1].sources.extend(glob.glob(os.path.splitext(pyrex)[0]+'.ext.*.c'))
184
            print pyrexs[-1].sources
185 186 187 188 189 190 191 192 193 194 195 196 197 198
            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')):
199 200 201 202 203
            if '.ext.' not in pyrex:
                pyrexs.append(Extension('.'.join(base+[module,path.splitext(path.basename(pyrex))[0]]),[pyrex]))
                pyrexs[-1].sources.extend(glob.glob(os.path.splitext(pyrex)[0]+'.ext.*.c'))
                print pyrexs[-1].sources
       
204
        pyrexs.extend(findC(path.join(root,module),base+[module]))
205 206
    return pyrexs
    
207

208 209 210 211
#sys.path.insert(0,"src")
#from obitools.version import version as obiversion
#sys.path.pop(0)

Eric Coissac's avatar
Eric Coissac committed
212
VERSION =  "1.0.beta"
213
AUTHOR  = 'Eric Coissac'
214 215
EMAIL   = 'eric@coissac.eu'
URL     = 'www.grenoble.prabi.fr/trac/OBITools'
216 217
LICENSE = 'CeCILL-V2'

218 219
SRC       = 'src'
FILTERSRC = 'textwrangler/filter'
220 221

SCRIPTS = glob.glob('%s/*.py' % SRC)
222 223
FILTERS = glob.glob('%s/*.py' % FILTERSRC)

224 225 226
DEPRECATED_SCRIPTS=["fastaComplement", "fastaUniq","fasta2tab","fastaAnnotate",
                    "fastaSample","fastaGrep","fastaCount","fastaLength",
                    "fastaHead","fastaTail","fastaSplit","fastaStrand",
Eric Coissac's avatar
Eric Coissac committed
227
                    "fastaLocate","solexaPairEnd","ecoTag","obijoinpairedend"
228
                   ]
Eric Coissac's avatar
Eric Coissac committed
229

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

233
if  has_cython:
234
    EXTENTION=findCython(SRC)
235 236 237
    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)
238 239 240
else:
    EXTENTION=findC(SRC)
    
241
#SCRIPTS.append('src/fastaComplement')
242 243 244 245 246 247 248 249 250 251

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},
252
      packages=findPackage(SRC),
Eric Coissac's avatar
Eric Coissac committed
253
      cmdclass = {'build_ext': build_ext,'build_scripts':build_scripts, 'install_scripts':install_scripts},
254
      requires=['Cython (>=0.16)'],
255
      zip_safe = False,
256
      ext_modules=EXTENTION)
257