From c944824817f5628b74f50facfdca989f25807e9a Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Fri, 17 Sep 2010 15:23:13 -0400 Subject: [PATCH] Update aksetup. --- aksetup_helper.py | 172 ++++++++++++++++++++++---------------------- distribute_setup.py | 121 ++++++++++++++++++++----------- 2 files changed, 167 insertions(+), 126 deletions(-) diff --git a/aksetup_helper.py b/aksetup_helper.py index 2e9d7a88..252fd3f9 100644 --- a/aksetup_helper.py +++ b/aksetup_helper.py @@ -5,20 +5,20 @@ distribute_setup.use_setuptools() import setuptools from setuptools import Extension -if 'distribute' not in setuptools.__file__: - print "-------------------------------------------------------------------------" - print "Setuptools conflict detected." - print "-------------------------------------------------------------------------" - print "When I imported setuptools, I did not get the distribute version of" - print "setuptools, which is troubling--this package really wants to be used" - print "with distribute rather than the old setuptools package. More than likely," - print "you have both distribute and setuptools installed, which is bad." - print - print "See this page for more information:" - print "http://wiki.tiker.net/DistributeVsSetuptools" - print "-------------------------------------------------------------------------" - print "I will continue after a short while, fingers crossed." - print "-------------------------------------------------------------------------" +if not hasattr(setuptools, "_distribute"): + print("-------------------------------------------------------------------------") + print("Setuptools conflict detected.") + print("-------------------------------------------------------------------------") + print("When I imported setuptools, I did not get the distribute version of") + print("setuptools, which is troubling--this package really wants to be used") + print("with distribute rather than the old setuptools package. More than likely,") + print("you have both distribute and setuptools installed, which is bad.") + print("") + print("See this page for more information:") + print("http://wiki.tiker.net/DistributeVsSetuptools") + print("-------------------------------------------------------------------------") + print("I will continue after a short while, fingers crossed.") + print("-------------------------------------------------------------------------") delay = 10 @@ -40,16 +40,16 @@ def setup(*args, **kwargs): except SystemExit: raise except: - print "----------------------------------------------------------------------------" - print "Sorry, your build failed. Try rerunning configure.py with different options." - print "----------------------------------------------------------------------------" + print ("----------------------------------------------------------------------------") + print ("Sorry, your build failed. Try rerunning configure.py with different options.") + print ("----------------------------------------------------------------------------") raise class NumpyExtension(Extension): - # nicked from + # nicked from # http://mail.python.org/pipermail/distutils-sig/2007-September/008253.html # solution by Michael Hoffmann def __init__(self, *args, **kwargs): @@ -109,7 +109,7 @@ class HedgeExtension(PyUblasExtension): # tools ----------------------------------------------------------------------- def flatten(list): - """For an iterable of sub-iterables, generate each member of each + """For an iterable of sub-iterables, generate each member of each sub-iterable in turn, i.e. a flattened version of that super-iterable. Example: Turn [[a,b,c],[d,e,f]] into [a,b,c,d,e,f]. @@ -136,20 +136,20 @@ def get_config(schema=None, warn_about_no_config=True): if (not schema.have_config() and not schema.have_global_config() and warn_about_no_config): - print "*************************************************************" - print "*** I have detected that you have not run configure.py." - print "*************************************************************" - print "*** Additionally, no global config files were found." - print "*** I will go ahead with the default configuration." - print "*** In all likelihood, this will not work out." - print "*** " - print "*** See README_SETUP.txt for more information." - print "*** " - print "*** If the build does fail, just re-run configure.py with the" - print "*** correct arguments, and then retry. Good luck!" - print "*************************************************************" - print "*** HIT Ctrl-C NOW IF THIS IS NOT WHAT YOU WANT" - print "*************************************************************" + print("*************************************************************") + print("*** I have detected that you have not run configure.py.") + print("*************************************************************") + print("*** Additionally, no global config files were found.") + print("*** I will go ahead with the default configuration.") + print("*** In all likelihood, this will not work out.") + print("*** ") + print("*** See README_SETUP.txt for more information.") + print("*** ") + print("*** If the build does fail, just re-run configure.py with the") + print("*** correct arguments, and then retry. Good luck!") + print("*************************************************************") + print("*** HIT Ctrl-C NOW IF THIS IS NOT WHAT YOU WANT") + print("*************************************************************") delay = 10 @@ -230,7 +230,7 @@ def expand_str(s, options): return re.subn(r"\$\{([a-zA-Z0-9_]+)\}", my_repl, s)[0] def expand_value(v, options): - if isinstance(v, (str, unicode)): + if isinstance(v, str): return expand_str(v, options) elif isinstance(v, list): return [expand_value(i, options) for i in v] @@ -272,15 +272,15 @@ class ConfigSchema: self.conf_dir = conf_dir def get_default_config(self): - return dict((opt.name, opt.default) + return dict((opt.name, opt.default) for opt in self.options) - + def read_config_from_pyfile(self, filename): result = {} filevars = {} - execfile(filename, filevars) + exec(compile(open(filename, "r").read(), filename, "exec"), filevars) - for key, value in filevars.iteritems(): + for key, value in filevars.items(): if key in self.optdict: result[key] = value @@ -291,13 +291,13 @@ class ConfigSchema: filevars = {} try: - execfile(filename, filevars) + exec(compile(open(filename, "r").read(), filename, "exec"), filevars) except IOError: pass del filevars["__builtins__"] - for key, value in config.iteritems(): + for key, value in config.items(): if value is not None: filevars[key] = value @@ -322,7 +322,7 @@ class ConfigSchema: result = self.get_default_config() import os - + confignames = [] if self.global_conf_file is not None: confignames.append(self.global_conf_file) @@ -354,16 +354,16 @@ class ConfigSchema: result = self.get_default_config_with_files() if os.access(cfile, os.R_OK): filevars = {} - execfile(cfile, filevars) + exec(compile(open(cfile, "r").read(), cfile, "exec"), filevars) - for key, value in filevars.iteritems(): + for key, value in filevars.items(): if key in self.optdict: result[key] = value elif key == "__builtins__": pass else: - raise KeyError, "invalid config key in %s: %s" % ( - cfile, key) + raise KeyError("invalid config key in %s: %s" % ( + cfile, key)) expand_options(result) @@ -425,13 +425,13 @@ class Option(object): return result def value_to_str(self, default): - return default + return default def add_to_configparser(self, parser, default=None): default = default_or(default, self.default) default_str = self.value_to_str(default) parser.add_option( - "--" + self.as_option(), dest=self.name, + "--" + self.as_option(), dest=self.name, default=default_str, metavar=self.metavar(), help=self.get_help(default)) @@ -443,7 +443,7 @@ class Switch(Option): option = self.as_option() if not isinstance(self.default, bool): - raise ValueError, "Switch options must have a default" + raise ValueError("Switch options must have a default") if default is None: default = self.default @@ -452,11 +452,11 @@ class Switch(Option): action = "store_false" else: action = "store_true" - + parser.add_option( - "--" + self.as_option(), - dest=self.name, - help=self.get_help(default), + "--" + self.as_option(), + dest=self.name, + help=self.get_help(default), default=default, action=action) @@ -484,7 +484,7 @@ class StringListOption(Option): class IncludeDir(StringListOption): def __init__(self, lib_name, default=None, human_name=None, help=None): StringListOption.__init__(self, "%s_INC_DIR" % lib_name, default, - help=help or ("Include directories for %s" + help=help or ("Include directories for %s" % (human_name or humanize(lib_name)))) class LibraryDir(StringListOption): @@ -496,14 +496,14 @@ class LibraryDir(StringListOption): class Libraries(StringListOption): def __init__(self, lib_name, default=None, human_name=None, help=None): StringListOption.__init__(self, "%s_LIBNAME" % lib_name, default, - help=help or ("Library names for %s (without lib or .so)" + help=help or ("Library names for %s (without lib or .so)" % (human_name or humanize(lib_name)))) class BoostLibraries(Libraries): def __init__(self, lib_base_name): - Libraries.__init__(self, "BOOST_%s" % lib_base_name.upper(), + Libraries.__init__(self, "BOOST_%s" % lib_base_name.upper(), ["boost_%s-${BOOST_COMPILER}-mt" % lib_base_name], - help="Library names for Boost C++ %s library (without lib or .so)" + help="Library names for Boost C++ %s library (without lib or .so)" % humanize(lib_base_name)) def set_up_shipped_boost_if_requested(conf): @@ -517,22 +517,22 @@ def set_up_shipped_boost_if_requested(conf): if conf["USE_SHIPPED_BOOST"]: if not exists("bpl-subset/bpl_subset/boost/version.hpp"): - print >>sys.stderr, "------------------------------------------------------------------------" - print >>sys.stderr, "The shipped Boost library was not found, but USE_SHIPPED_BOOST is True." - print >>sys.stderr, "(The files should be under bpl-subset/.)" - print >>sys.stderr, "------------------------------------------------------------------------" - print >>sys.stderr, "If you got this package from git, you probably want to do" - print >>sys.stderr, "" - print >>sys.stderr, " $ git submodule init" - print >>sys.stderr, " $ git submodule update" - print >>sys.stderr, "" - print >>sys.stderr, "to fetch what you are presently missing. If you got this from" - print >>sys.stderr, "a distributed package on the net, that package is broken and" - print >>sys.stderr, "should be fixed. For now, I will turn off 'USE_SHIPPED_BOOST'" - print >>sys.stderr, "to try and see if the build succeeds that way, but in the long" - print >>sys.stderr, "run you might want to either get the missing bits or turn" - print >>sys.stderr, "'USE_SHIPPED_BOOST' off." - print >>sys.stderr, "------------------------------------------------------------------------" + print("------------------------------------------------------------------------") + print("The shipped Boost library was not found, but USE_SHIPPED_BOOST is True.") + print("(The files should be under bpl-subset/.)") + print("------------------------------------------------------------------------") + print("If you got this package from git, you probably want to do") + print("") + print(" $ git submodule init") + print(" $ git submodule update") + print("") + print("to fetch what you are presently missing. If you got this from") + print("a distributed package on the net, that package is broken and") + print("should be fixed. For now, I will turn off 'USE_SHIPPED_BOOST'") + print("to try and see if the build succeeds that way, but in the long") + print("run you might want to either get the missing bits or turn") + print("'USE_SHIPPED_BOOST' off.") + print("------------------------------------------------------------------------") conf["USE_SHIPPED_BOOST"] = False delay = 10 @@ -567,7 +567,7 @@ def set_up_shipped_boost_if_requested(conf): source_files += glob( "bpl-subset/bpl_subset/libs/thread/src/pthread/*.cpp") - return (source_files, + return (source_files, {"BOOST_MULTI_INDEX_DISABLE_SERIALIZATION": 1} ) else: @@ -578,7 +578,7 @@ def make_boost_base_options(): return [ IncludeDir("BOOST", []), LibraryDir("BOOST", []), - Option("BOOST_COMPILER", default="gcc43", + Option("BOOST_COMPILER", default="gcc43", help="The compiler with which Boost C++ was compiled, e.g. gcc43"), ] @@ -594,29 +594,29 @@ def configure_frontend(): from setup import get_config_schema schema = get_config_schema() if schema.have_config(): - print "************************************************************" - print "*** I have detected that you have already run configure." - print "*** I'm taking the configured values as defaults for this" - print "*** configure run. If you don't want this, delete the file" - print "*** %s." % schema.get_conf_file() - print "************************************************************" + print("************************************************************") + print("*** I have detected that you have already run configure.") + print("*** I'm taking the configured values as defaults for this") + print("*** configure run. If you don't want this, delete the file") + print("*** %s." % schema.get_conf_file()) + print("************************************************************") import sys description = "generate a configuration file for this software package" parser = OptionParser(description=description) parser.add_option( - "--python-exe", dest="python_exe", default=sys.executable, - help="Which Python interpreter to use", metavar="PATH") + "--python-exe", dest="python_exe", default=sys.executable, + help="Which Python interpreter to use", metavar="PATH") parser.add_option("--prefix", default=None, - help="Ignored") + help="Ignored") parser.add_option("--enable-shared", help="Ignored", action="store_false") parser.add_option("--disable-static", help="Ignored", action="store_false") - parser.add_option("--update-user", help="Update user config file (%s)" % schema.user_conf_file, + parser.add_option("--update-user", help="Update user config file (%s)" % schema.user_conf_file, action="store_true") - parser.add_option("--update-global", - help="Update global config file (%s)" % schema.global_conf_file, + parser.add_option("--update-global", + help="Update global config file (%s)" % schema.global_conf_file, action="store_true") schema.add_to_configparser(parser, schema.read_config()) diff --git a/distribute_setup.py b/distribute_setup.py index 59424ccc..3ea2e667 100644 --- a/distribute_setup.py +++ b/distribute_setup.py @@ -46,19 +46,21 @@ except ImportError: args = [quote(arg) for arg in args] return os.spawnl(os.P_WAIT, sys.executable, *args) == 0 -DEFAULT_VERSION = "0.6.4" +DEFAULT_VERSION = "0.6.14" DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/" +SETUPTOOLS_FAKED_VERSION = "0.6c11" + SETUPTOOLS_PKG_INFO = """\ Metadata-Version: 1.0 Name: setuptools -Version: 0.6c9 +Version: %s Summary: xxxx Home-page: xxx Author: xxx Author-email: xxx License: xxx Description: xxx -""" +""" % SETUPTOOLS_FAKED_VERSION def _install(tarball): @@ -79,12 +81,14 @@ def _install(tarball): # installing log.warn('Installing Distribute') - assert _python_cmd('setup.py', 'install') + if not _python_cmd('setup.py', 'install'): + log.warn('Something went wrong during the installation.') + log.warn('See the error message above.') finally: os.chdir(old_wd) -def _build_egg(tarball, to_dir): +def _build_egg(egg, tarball, to_dir): # extracting the tarball tmpdir = tempfile.mkdtemp() log.warn('Extracting in %s', tmpdir) @@ -104,27 +108,28 @@ def _build_egg(tarball, to_dir): log.warn('Building a Distribute egg in %s', to_dir) _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir) - # returning the result - for file in os.listdir(to_dir): - if fnmatch.fnmatch(file, 'distribute-%s*.egg' % DEFAULT_VERSION): - return os.path.join(to_dir, file) - - raise IOError('Could not build the egg.') finally: os.chdir(old_wd) + # returning the result + log.warn(egg) + if not os.path.exists(egg): + raise IOError('Could not build the egg.') def _do_download(version, download_base, to_dir, download_delay): - tarball = download_setuptools(version, download_base, - to_dir, download_delay) - egg = _build_egg(tarball, to_dir) + egg = os.path.join(to_dir, 'distribute-%s-py%d.%d.egg' + % (version, sys.version_info[0], sys.version_info[1])) + if not os.path.exists(egg): + tarball = download_setuptools(version, download_base, + to_dir, download_delay) + _build_egg(egg, tarball, to_dir) sys.path.insert(0, egg) import setuptools setuptools.bootstrap_install_from = egg def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, - to_dir=os.curdir, download_delay=15): + to_dir=os.curdir, download_delay=15, no_fake=True): # making sure we use the absolute path to_dir = os.path.abspath(to_dir) was_imported = 'pkg_resources' in sys.modules or \ @@ -133,21 +138,23 @@ def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, try: import pkg_resources if not hasattr(pkg_resources, '_distribute'): - fake_setuptools() + if not no_fake: + _fake_setuptools() raise ImportError except ImportError: return _do_download(version, download_base, to_dir, download_delay) try: pkg_resources.require("distribute>="+version) return - except pkg_resources.VersionConflict, e: + except pkg_resources.VersionConflict: + e = sys.exc_info()[1] if was_imported: - print >>sys.stderr, ( + sys.stderr.write( "The required version of distribute (>=%s) is not available,\n" "and can't be installed while this script is running. Please\n" "install a more recent version first, using\n" "'easy_install -U distribute'." - "\n\n(Currently using %r)") % (version, e.args[0]) + "\n\n(Currently using %r)\n" % (version, e.args[0])) sys.exit(2) else: del pkg_resources, sys.modules['pkg_resources'] # reload ok @@ -157,7 +164,8 @@ def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, return _do_download(version, download_base, to_dir, download_delay) finally: - _create_fake_setuptools_pkg_info(to_dir) + if not no_fake: + _create_fake_setuptools_pkg_info(to_dir) def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, delay=15): @@ -171,7 +179,10 @@ def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, """ # making sure we use the absolute path to_dir = os.path.abspath(to_dir) - import urllib2 + try: + from urllib.request import urlopen + except ImportError: + from urllib2 import urlopen tgz_name = "distribute-%s.tar.gz" % version url = download_base + tgz_name saveto = os.path.join(to_dir, tgz_name) @@ -179,7 +190,7 @@ def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, if not os.path.exists(saveto): # Avoid repeated downloads try: log.warn("Downloading %s", url) - src = urllib2.urlopen(url) + src = urlopen(url) # Read/write all in one block, so we don't create a corrupt file # if the download is interrupted. data = src.read() @@ -192,6 +203,29 @@ def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, dst.close() return os.path.realpath(saveto) +def _no_sandbox(function): + def __no_sandbox(*args, **kw): + try: + from setuptools.sandbox import DirectorySandbox + if not hasattr(DirectorySandbox, '_old'): + def violation(*args): + pass + DirectorySandbox._old = DirectorySandbox._violation + DirectorySandbox._violation = violation + patched = True + else: + patched = False + except ImportError: + patched = False + + try: + return function(*args, **kw) + finally: + if patched: + DirectorySandbox._violation = DirectorySandbox._old + del DirectorySandbox._old + + return __no_sandbox def _patch_file(path, content): """Will backup the file then patch it""" @@ -209,26 +243,17 @@ def _patch_file(path, content): f.close() return True +_patch_file = _no_sandbox(_patch_file) def _same_content(path, content): return open(path).read() == content - def _rename_path(path): new_name = path + '.OLD.%s' % time.time() log.warn('Renaming %s into %s', path, new_name) - try: - from setuptools.sandbox import DirectorySandbox - def _violation(*args): - pass - DirectorySandbox._violation = _violation - except ImportError: - pass - os.rename(path, new_name) return new_name - def _remove_flat_installation(placeholder): if not os.path.isdir(placeholder): log.warn('Unkown installation at %s', placeholder) @@ -262,6 +287,7 @@ def _remove_flat_installation(placeholder): 'Setuptools distribution', element) return True +_remove_flat_installation = _no_sandbox(_remove_flat_installation) def _after_install(dist): log.warn('After install bootstrap.') @@ -273,17 +299,20 @@ def _create_fake_setuptools_pkg_info(placeholder): log.warn('Could not find the install location') return pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1]) - setuptools_file = 'setuptools-0.6c9-py%s.egg-info' % pyver + setuptools_file = 'setuptools-%s-py%s.egg-info' % \ + (SETUPTOOLS_FAKED_VERSION, pyver) pkg_info = os.path.join(placeholder, setuptools_file) if os.path.exists(pkg_info): log.warn('%s already exists', pkg_info) return + log.warn('Creating %s', pkg_info) f = open(pkg_info, 'w') try: f.write(SETUPTOOLS_PKG_INFO) finally: f.close() + pth_file = os.path.join(placeholder, 'setuptools.pth') log.warn('Creating %s', pth_file) f = open(pth_file, 'w') @@ -292,6 +321,7 @@ def _create_fake_setuptools_pkg_info(placeholder): finally: f.close() +_create_fake_setuptools_pkg_info = _no_sandbox(_create_fake_setuptools_pkg_info) def _patch_egg_dir(path): # let's check if it's already patched @@ -311,10 +341,11 @@ def _patch_egg_dir(path): f.close() return True +_patch_egg_dir = _no_sandbox(_patch_egg_dir) def _before_install(): log.warn('Before install bootstrap.') - fake_setuptools() + _fake_setuptools() def _under_prefix(location): @@ -330,12 +361,12 @@ def _under_prefix(location): if len(args) > index: top_dir = args[index+1] return location.startswith(top_dir) - elif option == '--user' and USER_SITE is not None: - return location.startswith(USER_SITE) + if arg == '--user' and USER_SITE is not None: + return location.startswith(USER_SITE) return True -def fake_setuptools(): +def _fake_setuptools(): log.warn('Scanning installed packages') try: import pkg_resources @@ -344,7 +375,13 @@ def fake_setuptools(): log.warn('Setuptools or Distribute does not seem to be installed.') return ws = pkg_resources.working_set - setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools')) + try: + setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools', + replacement=False)) + except TypeError: + # old distribute API + setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools')) + if setuptools_dist is None: log.warn('No setuptools distribution found') return @@ -384,6 +421,9 @@ def fake_setuptools(): def _relaunch(): log.warn('Relaunching...') # we have to relaunch the process + # pip marker to avoid a relaunch bug + if sys.argv[:3] == ['-c', 'install', '--single-version-externally-managed']: + sys.argv[0] = 'setup.py' args = [sys.executable] + sys.argv sys.exit(subprocess.call(args)) @@ -408,7 +448,7 @@ def _extractall(self, path=".", members=None): # Extract directories with a safe mode. directories.append(tarinfo) tarinfo = copy.copy(tarinfo) - tarinfo.mode = 0700 + tarinfo.mode = 448 # decimal for oct 0700 self.extract(tarinfo, path) # Reverse sort directories. @@ -427,7 +467,8 @@ def _extractall(self, path=".", members=None): self.chown(tarinfo, dirpath) self.utime(tarinfo, dirpath) self.chmod(tarinfo, dirpath) - except ExtractError, e: + except ExtractError: + e = sys.exc_info()[1] if self.errorlevel > 1: raise else: -- GitLab