Commit 6c91d110 by Eric Coissac

Switch to the new version of the setup.py script inspirated by orgasm

parent 5454cebe
......@@ -5,5 +5,5 @@
<path>/OBITools-1.0/textwrangler</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Python-2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Python2.7</pydev_property>
</pydev_project>
include setup.py
recursive-include distutils.ext *.py *.c
recursive-include src *.pyx *.pxd *.c *.h
recursive-include doc/sphinx/source *.txt *.rst *.py
include doc/sphinx/make.bat
include doc/sphinx/Makefile
include README.txt
include requirements.txt
def build_ext(*args,**kargs):
'''
Wrapper over the build_ext class to postpone the import of cython
'''
from build_ext import build_ext as _build_ext
return _build_ext(*args,**kargs)
'''
Created on 20 oct. 2012
@author: coissac
'''
from distutils.command.build import build as ori_build
class build(ori_build):
def has_ctools(self):
return self.distribution.has_ctools()
def has_files(self):
return self.distribution.has_files()
def has_executables(self):
return self.distribution.has_executables()
def has_ext_modules(self):
return self.distribution.has_ext_modules()
def has_littlebigman(self):
return True
sub_commands = [('littlebigman', has_littlebigman),
('build_ctools', has_ctools),
('build_files', has_files),
('build_cexe', has_executables),
('build_ext', has_ext_modules)] \
+ ori_build.sub_commands
'''
Created on 20 oct. 2012
@author: coissac
'''
import os
from obidistutils.command.build_ctools import build_ctools
from distutils.sysconfig import customize_compiler
from distutils.errors import DistutilsSetupError
from distutils import log
class build_cexe(build_ctools):
description = "build C/C++ executable distributed with Python extensions"
def initialize_options(self):
build_ctools.initialize_options(self)
self.built_files = None
def finalize_options(self):
# This might be confusing: both build-cexe and build-temp default
# to build-temp as defined by the "build" command. This is because
# I think that C libraries are really just temporary build
# by-products, at least from the point of view of building Python
# extensions -- but I want to keep my options open.
build_cexe_dir = self.build_cexe
build_ctools.finalize_options(self)
if build_cexe_dir is None:
self.build_cexe=None
self.set_undefined_options('build',
('build_scripts', 'build_cexe'))
self.set_undefined_options('build_files',
('files', 'built_files'))
self.executables = self.distribution.executables
if self.executables:
self.check_executable_list(self.executables)
# XXX same as for build_ext -- what about 'self.define' and
# 'self.undef' ?
def substitute_sources(self,exe_name,sources):
"""
Substitutes source file name starting by an @ by the actual
name of the built file (see --> build_files)
"""
sources = list(sources)
for i in xrange(len(sources)):
print exe_name,sources[i],
if sources[i][0]=='@':
try:
filename = self.built_files[sources[i][1:]]
except KeyError:
raise DistutilsSetupError, \
('The %s filename declared in the source '
'files of the program %s have not been '
'built by the installation process') % (sources[i],
exe_name)
sources[i]=filename
print "changed to ",filename
else:
print " ok"
return sources
'''
Created on 20 oct. 2012
@author: coissac
'''
import os
from obidistutils.command.build_exe import build_exe
from distutils.sysconfig import customize_compiler
from distutils.errors import DistutilsSetupError
from distutils import log
class build_ctools(build_exe):
description = "build C/C++ executable not distributed with Python extensions"
def initialize_options(self):
build_exe.initialize_options(self)
# List of built tools
self.ctools = None
self.littlebigman = None
def finalize_options(self):
# This might be confusing: both build-cexe and build-temp default
# to build-temp as defined by the "build" command. This is because
# I think that C libraries are really just temporary build
# by-products, at least from the point of view of building Python
# extensions -- but I want to keep my options open.
build_exe.finalize_options(self)
self.set_undefined_options('build',
('build_temp', 'build_cexe'))
self.set_undefined_options('littlebigman',
('littlebigman', 'littlebigman'))
self.executables = self.distribution.ctools
self.check_executable_list(self.executables)
if self.littlebigman =='-DLITTLE_END':
if self.define is None:
self.define=[('LITTLE_END',None)]
else:
self.define.append('LITTLE_END',None)
self.ctools = set()
def run(self):
build_exe.run(self)
for e,p in self.executables:
self.ctools.add(e)
'''
Created on 20 oct. 2012
@author: coissac
'''
import os
from distutils.core import Command
from distutils.sysconfig import customize_compiler
from distutils.errors import DistutilsSetupError
from distutils import log
from distutils.ccompiler import show_compilers
import subprocess
class build_exe(Command):
description = "build an executable -- Abstract command "
user_options = [
('build-cexe', 'x',
"directory to build C/C++ libraries to"),
('build-temp', 't',
"directory to put temporary build by-products"),
('debug', 'g',
"compile with debugging information"),
('force', 'f',
"forcibly build everything (ignore file timestamps)"),
('compiler=', 'c',
"specify the compiler type"),
]
boolean_options = ['debug', 'force']
help_options = [
('help-compiler', None,
"list available compilers", show_compilers),
]
def initialize_options(self):
self.build_cexe = None
self.build_temp = None
# List of executables to build
self.executables = None
# Compilation options for all libraries
self.include_dirs = None
self.define = None
self.undef = None
self.extra_compile_args = None
self.debug = None
self.force = 0
self.compiler = None
self.sse = None
self.built_files=None
def finalize_options(self):
# This might be confusing: both build-cexe and build-temp default
# to build-temp as defined by the "build" command. This is because
# I think that C libraries are really just temporary build
# by-products, at least from the point of view of building Python
# extensions -- but I want to keep my options open.
self.set_undefined_options('build',
('build_temp', 'build_temp'),
('compiler', 'compiler'),
('debug', 'debug'),
('force', 'force'))
if self.include_dirs is None:
self.include_dirs = self.distribution.include_dirs or []
if isinstance(self.include_dirs, str):
self.include_dirs = self.include_dirs.split(os.pathsep)
self.sse = self.distribution.sse
if self.sse is not None:
if self.extra_compile_args is None:
self.extra_compile_args=['-m%s' % self.sse]
else:
self.extra_compile_args.append('-m%s' % self.sse)
# XXX same as for build_ext -- what about 'self.define' and
# 'self.undef' ?
def run(self):
if not self.executables:
return
self.mkpath(self.build_cexe)
# Yech -- this is cut 'n pasted from build_ext.py!
from distutils.ccompiler import new_compiler
self.compiler = new_compiler(compiler=self.compiler,
dry_run=self.dry_run,
force=self.force)
customize_compiler(self.compiler)
if self.include_dirs is not None:
self.compiler.set_include_dirs(self.include_dirs)
if self.define is not None:
# 'define' option is a list of (name,value) tuples
for (name,value) in self.define:
self.compiler.define_macro(name, value)
if self.undef is not None:
for macro in self.undef:
self.compiler.undefine_macro(macro)
self.build_executables(self.executables)
def check_executable_list(self, executables):
"""Ensure that the list of executables is valid.
`executable` is presumably provided as a command option 'executables'.
This method checks that it is a list of 2-tuples, where the tuples
are (executable_name, build_info_dict).
Raise DistutilsSetupError if the structure is invalid anywhere;
just returns otherwise.
"""
if not isinstance(executables, list):
raise DistutilsSetupError, \
"'executables' option must be a list of tuples"
for exe in executables:
if not isinstance(exe, tuple) and len(exe) != 2:
raise DistutilsSetupError, \
"each element of 'executables' must a 2-tuple"
name, build_info = exe
if not isinstance(name, str):
raise DistutilsSetupError, \
"first element of each tuple in 'executables' " + \
"must be a string (the executables name)"
if '/' in name or (os.sep != '/' and os.sep in name):
raise DistutilsSetupError, \
("bad executable name '%s': " +
"may not contain directory separators") % \
exe[0]
if not isinstance(build_info, dict):
raise DistutilsSetupError, \
"second element of each tuple in 'executables' " + \
"must be a dictionary (build info)"
def get_executable_names(self):
# Assume the executables list is valid -- 'check_executable_list()' is
# called from 'finalize_options()', so it should be!
if not self.executables:
return None
exe_names = []
for (exe_name, build_info) in self.executables:
exe_names.append(exe_name)
return exe_names
def get_source_files(self):
self.check_executable_list(self.executables)
filenames = []
for (exe_name, build_info) in self.executables:
sources = build_info.get('sources')
if sources is None or not isinstance(sources, (list, tuple)):
raise DistutilsSetupError, \
("in 'executables' option (library '%s'), "
"'sources' must be present and must be "
"a list of source filenames") % exe_name
filenames.extend(sources)
return filenames
def substitute_sources(self,exe_name,sources):
return list(sources)
def build_executables(self, executables):
for (exe_name, build_info) in executables:
sources = build_info.get('sources')
if sources is None or not isinstance(sources, (list, tuple)):
raise DistutilsSetupError, \
("in 'executables' option (library '%s'), " +
"'sources' must be present and must be " +
"a list of source filenames") % exe_name
sources = self.substitute_sources(exe_name,sources)
log.info("building '%s' program", exe_name)
# First, compile the source code to object files in the library
# directory. (This should probably change to putting object
# files in a temporary build directory.)
macros = build_info.get('macros')
include_dirs = build_info.get('include_dirs')
extra_args = self.extra_compile_args or []
objects = self.compiler.compile(sources,
output_dir=self.build_temp,
macros=macros,
include_dirs=include_dirs,
extra_postargs=extra_args,
debug=self.debug)
# Now "link" the object files together into a static library.
# (On Unix at least, this isn't really linking -- it just
# builds an archive. Whatever.)
self.compiler.link_executable(objects, exe_name,
output_dir=self.build_cexe,
debug=self.debug)
'''
Created on 13 fevr. 2014
@author: coissac
'''
from Cython.Distutils import build_ext as ori_build_ext # @UnresolvedImport
from distutils.errors import DistutilsSetupError
import os
class build_ext(ori_build_ext):
def modifyDocScripts(self):
print >>open("doc/sphinx/build_dir.txt","w"),self.build_lib
def initialize_options(self):
ori_build_ext.initialize_options(self)
self.littlebigman = None
self.built_files = None
def finalize_options(self):
ori_build_ext.finalize_options(self)
self.set_undefined_options('littlebigman',
('littlebigman', 'littlebigman'))
self.set_undefined_options('build_files',
('files', 'built_files'))
self.cython_c_in_temp = 1
if self.littlebigman =='-DLITTLE_END':
if self.define is None:
self.define=[('LITTLE_END',None)]
else:
self.define.append('LITTLE_END',None)
def substitute_sources(self,exe_name,sources):
"""
Substitutes source file name starting by an @ by the actual
name of the built file (see --> build_files)
"""
sources = list(sources)
for i in xrange(len(sources)):
print exe_name,sources[i],
if sources[i][0]=='@':
try:
filename = self.built_files[sources[i][1:]]
except KeyError:
tmpfilename = os.path.join(self.build_temp,sources[i][1:])
if os.path.isfile (tmpfilename):
filename = tmpfilename
else:
raise DistutilsSetupError, \
('The %s filename declared in the source '
'files of the program %s have not been '
'built by the installation process') % (sources[i],
exe_name)
sources[i]=filename
print "changed to ",filename
else:
print " ok"
return sources
def build_extensions(self):
# First, sanity-check the 'extensions' list
for ext in self.extensions:
ext.sources = self.substitute_sources(ext.name,ext.sources)
self.check_extensions_list(self.extensions)
for ext in self.extensions:
print "#####>",ext.sources
ext.sources = self.cython_sources(ext.sources, ext)
self.build_extension(ext)
def run(self):
self.modifyDocScripts()
ori_build_ext.run(self)
\ No newline at end of file
'''
Created on 20 oct. 2012
@author: coissac
'''
import os.path
from distutils.core import Command
from distutils.util import convert_path
from distutils import log, sysconfig
from distutils.dep_util import newer
from distutils import log
class build_files(Command):
def initialize_options(self):
self.files=None
self.ctools=None
self.build_temp=None
self.build_cexe=None
def finalize_options(self):
self.set_undefined_options('build_ctools',
('ctools', 'ctools'),
('build_temp','build_temp'),
('build_cexe','build_cexe'),
)
self.files = {}
def run(self):
for dest,prog,command in self.distribution.files:
destfile = os.path.join(self.build_temp,dest)
if prog in self.ctools:
progfile = os.path.join(self.build_cexe,prog)
else:
progfile = prog
log.info("Building file : %s" % dest)
commandline = command % {'prog' : progfile,
'dest' : destfile}
log.info(" --> %s" % commandline)
os.system(commandline)
self.files[dest]=destfile
log.info("Done.\n")
'''
Created on 20 oct. 2012
@author: coissac
'''
from obidistutils.command.build_scripts import build_scripts
class build_filters(build_scripts):
pass
'''
Created on 20 oct. 2012
@author: coissac
'''
import os.path
from distutils.command.build_scripts import build_scripts as ori_build_scripts,\
first_line_re
from distutils.util import convert_path
from distutils import log, sysconfig
from distutils.dep_util import newer
from stat import ST_MODE
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)
rawbuild_dir = os.path.join(os.path.dirname(self.build_dir),'raw_scripts')
self.mkpath(rawbuild_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])
rawoutfile = os.path.join(rawbuild_dir, os.path.basename(script))
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 ''
log.info("Store the raw script %s -> %s", script,rawoutfile)
self.copy_file(script, rawoutfile)
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 F in outfiles:
if self.dry_run:
log.info("changing mode of %s", F)
else:
oldmode = os.stat(F)[ST_MODE] & 07777
newmode = (oldmode | 0555) & 07777
if newmode != oldmode:
log.info("changing mode of %s from %o to %o",
F, oldmode, newmode)
os.chmod(F, newmode)
'''
Created on 6 oct. 2014
@author: coissac
'''
from distutils.command.install import install as install_ori
class install(install_ori):
def __init__(self,dist):
install_ori.__init__(self, dist)