00001
00002 """Bootstrap setuptools installation
00003
00004 If you want to use setuptools in your package's setup.py, just include this
00005 file in the same directory with it, and add this to the top of your setup.py::
00006
00007 from ez_setup import use_setuptools
00008 use_setuptools()
00009
00010 If you want to require a specific version of setuptools, set a download
00011 mirror, or use an alternate download directory, you can do so by supplying
00012 the appropriate options to ``use_setuptools()``.
00013
00014 This file can also be run as a script to install or upgrade setuptools.
00015 """
00016 import sys
00017 DEFAULT_VERSION = "0.6c11"
00018 DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]
00019
00020 md5_data = {
00021 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
00022 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
00023 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
00024 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
00025 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
00026 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
00027 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
00028 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
00029 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
00030 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
00031 'setuptools-0.6c10-py2.3.egg': 'ce1e2ab5d3a0256456d9fc13800a7090',
00032 'setuptools-0.6c10-py2.4.egg': '57d6d9d6e9b80772c59a53a8433a5dd4',
00033 'setuptools-0.6c10-py2.5.egg': 'de46ac8b1c97c895572e5e8596aeb8c7',
00034 'setuptools-0.6c10-py2.6.egg': '58ea40aef06da02ce641495523a0b7f5',
00035 'setuptools-0.6c11-py2.3.egg': '2baeac6e13d414a9d28e7ba5b5a596de',
00036 'setuptools-0.6c11-py2.4.egg': 'bd639f9b0eac4c42497034dec2ec0c2b',
00037 'setuptools-0.6c11-py2.5.egg': '64c94f3bf7a72a13ec83e0b24f2749b2',
00038 'setuptools-0.6c11-py2.6.egg': 'bfa92100bd772d5a213eedd356d64086',
00039 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
00040 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
00041 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
00042 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
00043 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
00044 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
00045 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
00046 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
00047 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
00048 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
00049 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
00050 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20',
00051 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab',
00052 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53',
00053 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2',
00054 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e',
00055 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372',
00056 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902',
00057 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de',
00058 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b',
00059 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03',
00060 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a',
00061 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6',
00062 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a',
00063 }
00064
00065 import sys, os
00066 try: from hashlib import md5
00067 except ImportError: from md5 import md5
00068
00069 def _validate_md5(egg_name, data):
00070 if egg_name in md5_data:
00071 digest = md5(data).hexdigest()
00072 if digest != md5_data[egg_name]:
00073 print >>sys.stderr, (
00074 "md5 validation of %s failed! (Possible download problem?)"
00075 % egg_name
00076 )
00077 sys.exit(2)
00078 return data
00079
00080 def use_setuptools(
00081 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
00082 download_delay=15
00083 ):
00084 """Automatically find/download setuptools and make it available on sys.path
00085
00086 `version` should be a valid setuptools version number that is available
00087 as an egg for download under the `download_base` URL (which should end with
00088 a '/'). `to_dir` is the directory where setuptools will be downloaded, if
00089 it is not already available. If `download_delay` is specified, it should
00090 be the number of seconds that will be paused before initiating a download,
00091 should one be required. If an older version of setuptools is installed,
00092 this routine will print a message to ``sys.stderr`` and raise SystemExit in
00093 an attempt to abort the calling script.
00094 """
00095 was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules
00096 def do_download():
00097 egg = download_setuptools(version, download_base, to_dir, download_delay)
00098 sys.path.insert(0, egg)
00099 import setuptools; setuptools.bootstrap_install_from = egg
00100 try:
00101 import pkg_resources
00102 except ImportError:
00103 return do_download()
00104 try:
00105 pkg_resources.require("setuptools>="+version); return
00106 except pkg_resources.VersionConflict, e:
00107 if was_imported:
00108 print >>sys.stderr, (
00109 "The required version of setuptools (>=%s) is not available, and\n"
00110 "can't be installed while this script is running. Please install\n"
00111 " a more recent version first, using 'easy_install -U setuptools'."
00112 "\n\n(Currently using %r)"
00113 ) % (version, e.args[0])
00114 sys.exit(2)
00115 except pkg_resources.DistributionNotFound:
00116 pass
00117
00118 del pkg_resources, sys.modules['pkg_resources']
00119 return do_download()
00120
00121 def download_setuptools(
00122 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
00123 delay = 15
00124 ):
00125 """Download setuptools from a specified location and return its filename
00126
00127 `version` should be a valid setuptools version number that is available
00128 as an egg for download under the `download_base` URL (which should end
00129 with a '/'). `to_dir` is the directory where the egg will be downloaded.
00130 `delay` is the number of seconds to pause before an actual download attempt.
00131 """
00132 import urllib2, shutil
00133 egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
00134 url = download_base + egg_name
00135 saveto = os.path.join(to_dir, egg_name)
00136 src = dst = None
00137 if not os.path.exists(saveto):
00138 try:
00139 from distutils import log
00140 if delay:
00141 log.warn("""
00142 ---------------------------------------------------------------------------
00143 This script requires setuptools version %s to run (even to display
00144 help). I will attempt to download it for you (from
00145 %s), but
00146 you may need to enable firewall access for this script first.
00147 I will start the download in %d seconds.
00148
00149 (Note: if this machine does not have network access, please obtain the file
00150
00151 %s
00152
00153 and place it in this directory before rerunning this script.)
00154 ---------------------------------------------------------------------------""",
00155 version, download_base, delay, url
00156 ); from time import sleep; sleep(delay)
00157 log.warn("Downloading %s", url)
00158 src = urllib2.urlopen(url)
00159
00160
00161 data = _validate_md5(egg_name, src.read())
00162 dst = open(saveto,"wb"); dst.write(data)
00163 finally:
00164 if src: src.close()
00165 if dst: dst.close()
00166 return os.path.realpath(saveto)
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 def main(argv, version=DEFAULT_VERSION):
00204 """Install or upgrade setuptools and EasyInstall"""
00205 try:
00206 import setuptools
00207 except ImportError:
00208 egg = None
00209 try:
00210 egg = download_setuptools(version, delay=0)
00211 sys.path.insert(0,egg)
00212 from setuptools.command.easy_install import main
00213 return main(list(argv)+[egg])
00214 finally:
00215 if egg and os.path.exists(egg):
00216 os.unlink(egg)
00217 else:
00218 if setuptools.__version__ == '0.0.1':
00219 print >>sys.stderr, (
00220 "You have an obsolete version of setuptools installed. Please\n"
00221 "remove it from your system entirely before rerunning this script."
00222 )
00223 sys.exit(2)
00224
00225 req = "setuptools>="+version
00226 import pkg_resources
00227 try:
00228 pkg_resources.require(req)
00229 except pkg_resources.VersionConflict:
00230 try:
00231 from setuptools.command.easy_install import main
00232 except ImportError:
00233 from easy_install import main
00234 main(list(argv)+[download_setuptools(delay=0)])
00235 sys.exit(0)
00236 else:
00237 if argv:
00238 from setuptools.command.easy_install import main
00239 main(argv)
00240 else:
00241 print "Setuptools version",version,"or greater has been installed."
00242 print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
00243
00244 def update_md5(filenames):
00245 """Update our built-in md5 registry"""
00246
00247 import re
00248
00249 for name in filenames:
00250 base = os.path.basename(name)
00251 f = open(name,'rb')
00252 md5_data[base] = md5(f.read()).hexdigest()
00253 f.close()
00254
00255 data = [" %r: %r,\n" % it for it in md5_data.items()]
00256 data.sort()
00257 repl = "".join(data)
00258
00259 import inspect
00260 srcfile = inspect.getsourcefile(sys.modules[__name__])
00261 f = open(srcfile, 'rb'); src = f.read(); f.close()
00262
00263 match = re.search("\nmd5_data = {\n([^}]+)}", src)
00264 if not match:
00265 print >>sys.stderr, "Internal error!"
00266 sys.exit(2)
00267
00268 src = src[:match.start(1)] + repl + src[match.end(1):]
00269 f = open(srcfile,'w')
00270 f.write(src)
00271 f.close()
00272
00273
00274 if __name__=='__main__':
00275 if len(sys.argv)>2 and sys.argv[1]=='--md5update':
00276 update_md5(sys.argv[2:])
00277 else:
00278 main(sys.argv[1:])
00279
00280
00281
00282
00283
00284