diff options
author | tarek <none@none> | 2009-09-10 20:57:46 +0200 |
---|---|---|
committer | tarek <none@none> | 2009-09-10 20:57:46 +0200 |
commit | 364308e9ce78818d74e88987459e3c08f8e80d11 (patch) | |
tree | 787c6c5aa8e340ba9eb84a75707ac644cc602c5d | |
parent | b863dd62d8defc800fcbff6fa07eb73d4351acb9 (diff) | |
download | external_python_setuptools-364308e9ce78818d74e88987459e3c08f8e80d11.tar.gz external_python_setuptools-364308e9ce78818d74e88987459e3c08f8e80d11.tar.bz2 external_python_setuptools-364308e9ce78818d74e88987459e3c08f8e80d11.zip |
reintroduced use_setuptools in distribute_setup.py
--HG--
branch : distribute
extra : rebase_source : 3f0d0f085964feffdb07e97f25d508b7aaa88ffb
-rw-r--r-- | distribute_setup.py | 129 | ||||
-rw-r--r-- | tests/test_distribute_setup.py | 61 |
2 files changed, 164 insertions, 26 deletions
diff --git a/distribute_setup.py b/distribute_setup.py index a57fd2a8..83b56d4d 100644 --- a/distribute_setup.py +++ b/distribute_setup.py @@ -23,17 +23,115 @@ import os import time import fnmatch from distutils import log +import subprocess -is_jython = sys.platform.startswith('java') -if is_jython: - import subprocess +IS_JYTHON = sys.platform.startswith('java') +DEFAULT_VERSION = "0.6.2" +DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/" +def quote(c): + if sys.platform == 'win32': + if ' ' in c: + return '"%s"' % c + return c -DEFAULT_VERSION = "0.6.1" -DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/" +def python_cmd(cmd): + python = quote(sys.executable) + cmd = quote(cmd) + if IS_JYTHON: + return subprocess.Popen([python, cmd]).wait() == 0 + args = [os.P_WAIT, python, python] + cmd.split() + [os.environ] + return os.spawnle(*args) == 0 + +def _install(tarball): + # extracting the tarball + tmpdir = tempfile.mkdtemp() + log.warn('Extracting in %s' % tmpdir) + old_wd = os.getcwd() + try: + os.chdir(tmpdir) + tar = tarfile.open(tarball) + extractall(tar) + tar.close() + + # going in the directory + subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) + os.chdir(subdir) + log.warn('Now working in %s' % subdir) + + # installing + assert python_cmd('setup.py -q install') + finally: + os.chdir(old_wd) + +def _build_egg(tarball, to_dir=os.curdir): + # extracting the tarball + tmpdir = tempfile.mkdtemp() + log.warn('Extracting in %s' % tmpdir) + old_wd = os.getcwd() + try: + os.chdir(tmpdir) + tar = tarfile.open(tarball) + extractall(tar) + tar.close() + + # going in the directory + subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) + os.chdir(subdir) + log.warn('Now working in %s' % subdir) + + # building an egg + python_cmd('setup.py -v -q bdist_egg --dist-dir %s' % 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) + +def _do_download(version=DEFAULT_VERSION, download_base=DEFAULT_URL, + to_dir=os.curdir, download_delay=15): + tarball = download_setuptools(version, download_base, + to_dir, download_delay) + egg = _build_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 +): + was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules + try: + import pkg_resources + if not hasattr(pkg_resources, '_distribute'): + 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: + if was_imported: + print >>sys.stderr, ( + "The required version of distribute (>=%s) is not available, and\n" + "can't be installed while this script is running. Please install\n" + " a more recent version first, using 'easy_install -U distribute'." + "\n\n(Currently using %r)" + ) % (version, e.args[0]) + sys.exit(2) + else: + del pkg_resources, sys.modules['pkg_resources'] # reload ok + return _do_download(version, download_base, to_dir, download_delay) + except pkg_resources.DistributionNotFound: + return _do_download(version, download_base, to_dir, download_delay) def download_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + delay=15, ): """Download distribute from a specified location and return its filename @@ -304,27 +402,6 @@ def extractall(self, path=".", members=None): else: self._dbg(1, "tarfile: %s" % e) -def _install(tarball): - # extracting the tarball - tmpdir = tempfile.mkdtemp() - log.warn('Extracting in %s' % tmpdir) - old_wd = os.getcwd() - try: - os.chdir(tmpdir) - tar = tarfile.open(tarball) - extractall(tar) - tar.close() - - # going in the directory - subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) - os.chdir(subdir) - log.warn('Now working in %s' % subdir) - - # installing distribute - os.system('%s setup.py install' % sys.executable) - finally: - os.chdir(old_wd) - def main(argv, version=DEFAULT_VERSION): """Install or upgrade setuptools and EasyInstall""" tarball = download_setuptools() diff --git a/tests/test_distribute_setup.py b/tests/test_distribute_setup.py new file mode 100644 index 00000000..6c004bd7 --- /dev/null +++ b/tests/test_distribute_setup.py @@ -0,0 +1,61 @@ +import sys +import os +import tempfile +import unittest +import shutil +import copy + +CURDIR = os.path.abspath(os.path.dirname(__file__)) +TOPDIR = os.path.split(CURDIR)[0] +sys.path.insert(0, TOPDIR) + +from distribute_setup import (use_setuptools, _build_egg, python_cmd, + _do_download, _install) +import distribute_setup + +class TestSetup(unittest.TestCase): + + def urlopen(self, url): + return open(self.tarball) + + def setUp(self): + self.old_sys_path = copy.copy(sys.path) + self.cwd = os.getcwd() + self.tmpdir = tempfile.mkdtemp() + os.chdir(TOPDIR) + python_cmd("setup.py -q egg_info -RDb '' sdist --dist-dir %s" % \ + self.tmpdir) + tarball = os.listdir(self.tmpdir)[0] + self.tarball = os.path.join(self.tmpdir, tarball) + import urllib2 + urllib2.urlopen = self.urlopen + + def tearDown(self): + shutil.rmtree(self.tmpdir) + os.chdir(self.cwd) + sys.path = copy.copy(self.old_sys_path) + + def test_build_egg(self): + # making it an egg + egg = _build_egg(self.tarball, self.tmpdir) + + # now trying to import it + sys.path[0] = egg + import setuptools + self.assert_(setuptools.__file__.startswith(egg)) + + def test_do_download(self): + + tmpdir = tempfile.mkdtemp() + _do_download(to_dir=tmpdir) + import setuptools + self.assert_(setuptools.bootstrap_install_from.startswith(tmpdir)) + + def test_install(self): + def _faked(*args): + return True + distribute_setup.python_cmd = _faked + _install(self.tarball) + +if __name__ == '__main__': + unittest.main() |