| 1 | #!python
|
|---|
| 2 | """Bootstrap setuptools installation
|
|---|
| 3 |
|
|---|
| 4 | If you want to use setuptools in your package's setup.py, just include this
|
|---|
| 5 | file in the same directory with it, and add this to the top of your setup.py::
|
|---|
| 6 |
|
|---|
| 7 | from ez_setup import use_setuptools
|
|---|
| 8 | use_setuptools()
|
|---|
| 9 |
|
|---|
| 10 | If you want to require a specific version of setuptools, set a download
|
|---|
| 11 | mirror, or use an alternate download directory, you can do so by supplying
|
|---|
| 12 | the appropriate options to ``use_setuptools()``.
|
|---|
| 13 |
|
|---|
| 14 | This file can also be run as a script to install or upgrade setuptools.
|
|---|
| 15 | """
|
|---|
| 16 |
|
|---|
| 17 | DEFAULT_VERSION = "0.5a12"
|
|---|
| 18 | DEFAULT_URL = "http://www.python.org/packages/source/s/setuptools/"
|
|---|
| 19 |
|
|---|
| 20 | import sys, os
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | def use_setuptools(
|
|---|
| 43 | version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir
|
|---|
| 44 | ):
|
|---|
| 45 | """Automatically find/download setuptools and make it available on sys.path
|
|---|
| 46 |
|
|---|
| 47 | `version` should be a valid setuptools version number that is available
|
|---|
| 48 | as an egg for download under the `download_base` URL (which should end with
|
|---|
| 49 | a '/'). `to_dir` is the directory where setuptools will be downloaded, if
|
|---|
| 50 | it is not already available.
|
|---|
| 51 |
|
|---|
| 52 | If an older version of setuptools is installed, this will print a message
|
|---|
| 53 | to ``sys.stderr`` and raise SystemExit in an attempt to abort the calling
|
|---|
| 54 | script.
|
|---|
| 55 | """
|
|---|
| 56 | try:
|
|---|
| 57 | import setuptools
|
|---|
| 58 | if setuptools.__version__ == '0.0.1':
|
|---|
| 59 | print >>sys.stderr, (
|
|---|
| 60 | "You have an obsolete version of setuptools installed. Please\n"
|
|---|
| 61 | "remove it from your system entirely before rerunning this script."
|
|---|
| 62 | )
|
|---|
| 63 | sys.exit(2)
|
|---|
| 64 |
|
|---|
| 65 | except ImportError:
|
|---|
| 66 | egg = download_setuptools(version, download_base, to_dir)
|
|---|
| 67 | sys.path.insert(0, egg)
|
|---|
| 68 | import setuptools; setuptools.bootstrap_install_from = egg
|
|---|
| 69 |
|
|---|
| 70 | import pkg_resources
|
|---|
| 71 | try:
|
|---|
| 72 | pkg_resources.require("setuptools>="+version)
|
|---|
| 73 |
|
|---|
| 74 | except pkg_resources.VersionConflict:
|
|---|
| 75 | # XXX could we install in a subprocess here?
|
|---|
| 76 | print >>sys.stderr, (
|
|---|
| 77 | "The required version of setuptools (>=%s) is not available, and\n"
|
|---|
| 78 | "can't be installed while this script is running. Please install\n"
|
|---|
| 79 | " a more recent version first."
|
|---|
| 80 | ) % version
|
|---|
| 81 | sys.exit(2)
|
|---|
| 82 |
|
|---|
| 83 | def download_setuptools(
|
|---|
| 84 | version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir
|
|---|
| 85 | ):
|
|---|
| 86 | """Download setuptools from a specified location and return its filename
|
|---|
| 87 |
|
|---|
| 88 | `version` should be a valid setuptools version number that is available
|
|---|
| 89 | as an egg for download under the `download_base` URL (which should end
|
|---|
| 90 | with a '/'). `to_dir` is the directory where the egg will be downloaded.
|
|---|
| 91 | """
|
|---|
| 92 | import urllib2, shutil
|
|---|
| 93 | egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
|
|---|
| 94 | url = download_base + egg_name + '.zip' # XXX
|
|---|
| 95 | saveto = os.path.join(to_dir, egg_name)
|
|---|
| 96 | src = dst = None
|
|---|
| 97 |
|
|---|
| 98 | if not os.path.exists(saveto): # Avoid repeated downloads
|
|---|
| 99 | try:
|
|---|
| 100 | from distutils import log
|
|---|
| 101 | log.warn("Downloading %s", url)
|
|---|
| 102 | import base64
|
|---|
| 103 | username = base64.decodestring("foo")
|
|---|
| 104 | password = base64.decodestring("foo")
|
|---|
| 105 | authinfo = urllib2.HTTPBasicAuthHandler()
|
|---|
| 106 | authinfo.add_password('', '', username, password)
|
|---|
| 107 | proxy_support = urllib2.ProxyHandler({"http" : "http://%s:%s@proxy.domain.com:8080" % (username, password)})
|
|---|
| 108 | opener = urllib2.build_opener(proxy_support, authinfo, urllib2.HTTPHandler)
|
|---|
| 109 | urllib2.install_opener(opener)
|
|---|
| 110 | src = urllib2.urlopen(url)
|
|---|
| 111 | # Read/write all in one block, so we don't create a corrupt file
|
|---|
| 112 | # if the download is interrupted.
|
|---|
| 113 | data = src.read()
|
|---|
| 114 | dst = open(saveto,"wb")
|
|---|
| 115 | dst.write(data)
|
|---|
| 116 | finally:
|
|---|
| 117 | if src: src.close()
|
|---|
| 118 | if dst: dst.close()
|
|---|
| 119 |
|
|---|
| 120 | return os.path.realpath(saveto)
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 | def main(argv, version=DEFAULT_VERSION):
|
|---|
| 133 | """Install or upgrade setuptools and EasyInstall"""
|
|---|
| 134 |
|
|---|
| 135 | try:
|
|---|
| 136 | import setuptools
|
|---|
| 137 | except ImportError:
|
|---|
| 138 | import tempfile, shutil
|
|---|
| 139 | tmpdir = tempfile.mkdtemp(prefix="easy_install-")
|
|---|
| 140 | try:
|
|---|
| 141 | egg = download_setuptools(version, to_dir=tmpdir)
|
|---|
| 142 | sys.path.insert(0,egg)
|
|---|
| 143 | from setuptools.command.easy_install import main
|
|---|
| 144 | main(list(argv)+[egg])
|
|---|
| 145 | finally:
|
|---|
| 146 | shutil.rmtree(tmpdir)
|
|---|
| 147 | else:
|
|---|
| 148 | if setuptools.__version__ == '0.0.1':
|
|---|
| 149 | # tell the user to uninstall obsolete version
|
|---|
| 150 | use_setuptools(version)
|
|---|
| 151 |
|
|---|
| 152 | req = "setuptools>="+version
|
|---|
| 153 | import pkg_resources
|
|---|
| 154 | try:
|
|---|
| 155 | pkg_resources.require(req)
|
|---|
| 156 | except pkg_resources.VersionConflict:
|
|---|
| 157 | try:
|
|---|
| 158 | from setuptools.command.easy_install import main
|
|---|
| 159 | except ImportError:
|
|---|
| 160 | from easy_install import main
|
|---|
| 161 | main(list(argv)+[download_setuptools()])
|
|---|
| 162 | sys.exit(0) # try to force an exit
|
|---|
| 163 | else:
|
|---|
| 164 | if argv:
|
|---|
| 165 | from setuptools.command.easy_install import main
|
|---|
| 166 | main(argv)
|
|---|
| 167 | else:
|
|---|
| 168 | print "Setuptools version",version,"or greater has been installed."
|
|---|
| 169 | print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
|
|---|
| 170 | if __name__=='__main__':
|
|---|
| 171 | main(sys.argv[1:])
|
|---|