2 """Bootstrap setuptools installation
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::
7 from ez_setup import use_setuptools
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()``.
14 This file can also be run as a script to install or upgrade setuptools.
17 DEFAULT_VERSION
= "0.6a7"
18 DEFAULT_URL
= "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys
.version
[:3]
21 'setuptools-0.5a13-py2.3.egg': '85edcf0ef39bab66e130d3f38f578c86',
22 'setuptools-0.5a13-py2.4.egg': 'ede4be600e3890e06d4ee5e0148e092a',
23 'setuptools-0.6a1-py2.3.egg': 'ee819a13b924d9696b0d6ca6d1c5833d',
24 'setuptools-0.6a1-py2.4.egg': '8256b5f1cd9e348ea6877b5ddd56257d',
25 'setuptools-0.6a2-py2.3.egg': 'b98da449da411267c37a738f0ab625ba',
26 'setuptools-0.6a2-py2.4.egg': 'be5b88bc30aed63fdefd2683be135c3b',
27 'setuptools-0.6a3-py2.3.egg': 'ee0e325de78f23aab79d33106dc2a8c8',
28 'setuptools-0.6a3-py2.4.egg': 'd95453d525a456d6c23e7a5eea89a063',
29 'setuptools-0.6a4-py2.3.egg': 'e958cbed4623bbf47dd1f268b99d7784',
30 'setuptools-0.6a4-py2.4.egg': '7f33c3ac2ef1296f0ab4fac1de4767d8',
31 'setuptools-0.6a5-py2.3.egg': '748408389c49bcd2d84f6ae0b01695b1',
32 'setuptools-0.6a5-py2.4.egg': '999bacde623f4284bfb3ea77941d2627',
33 'setuptools-0.6a6-py2.3.egg': '7858139f06ed0600b0d9383f36aca24c',
34 'setuptools-0.6a6-py2.4.egg': 'c10d20d29acebce0dc76219dc578d058',
35 'setuptools-0.6a7-py2.3.egg': 'cfc4125ddb95c07f9500adc5d6abef6f',
36 'setuptools-0.6a7-py2.4.egg': 'c6d62dab4461f71aed943caea89e6f20',
41 def _validate_md5(egg_name
, data
):
42 if egg_name
in md5_data
:
44 digest
= md5(data
).hexdigest()
45 if digest
!= md5_data
[egg_name
]:
47 "md5 validation of %s failed! (Possible download problem?)"
55 version
=DEFAULT_VERSION
, download_base
=DEFAULT_URL
, to_dir
=os
.curdir
,
58 """Automatically find/download setuptools and make it available on sys.path
60 `version` should be a valid setuptools version number that is available
61 as an egg for download under the `download_base` URL (which should end with
62 a '/'). `to_dir` is the directory where setuptools will be downloaded, if
63 it is not already available. If `download_delay` is specified, it should
64 be the number of seconds that will be paused before initiating a download,
65 should one be required. If an older version of setuptools is installed,
66 this routine will print a message to ``sys.stderr`` and raise SystemExit in
67 an attempt to abort the calling script.
71 if setuptools
.__version
__ == '0.0.1':
73 "You have an obsolete version of setuptools installed. Please\n"
74 "remove it from your system entirely before rerunning this script."
78 egg
= download_setuptools(version
, download_base
, to_dir
, download_delay
)
79 sys
.path
.insert(0, egg
)
80 import setuptools
; setuptools
.bootstrap_install_from
= egg
84 pkg_resources
.require("setuptools>="+version
)
86 except pkg_resources
.VersionConflict
:
87 # XXX could we install in a subprocess here?
89 "The required version of setuptools (>=%s) is not available, and\n"
90 "can't be installed while this script is running. Please install\n"
91 " a more recent version first."
95 def download_setuptools(
96 version
=DEFAULT_VERSION
, download_base
=DEFAULT_URL
, to_dir
=os
.curdir
,
99 """Download setuptools from a specified location and return its filename
101 `version` should be a valid setuptools version number that is available
102 as an egg for download under the `download_base` URL (which should end
103 with a '/'). `to_dir` is the directory where the egg will be downloaded.
104 `delay` is the number of seconds to pause before an actual download attempt.
106 import urllib2
, shutil
107 egg_name
= "setuptools-%s-py%s.egg" % (version
,sys
.version
[:3])
108 url
= download_base
+ egg_name
109 saveto
= os
.path
.join(to_dir
, egg_name
)
111 if not os
.path
.exists(saveto
): # Avoid repeated downloads
113 from distutils
import log
116 ---------------------------------------------------------------------------
117 This script requires setuptools version %s to run (even to display
118 help). I will attempt to download it for you (from
120 you may need to enable firewall access for this script first.
121 I will start the download in %d seconds.
122 ---------------------------------------------------------------------------""",
123 version
, download_base
, delay
124 ); from time
import sleep
; sleep(delay
)
125 log
.warn("Downloading %s", url
)
126 src
= urllib2
.urlopen(url
)
127 # Read/write all in one block, so we don't create a corrupt file
128 # if the download is interrupted.
129 data
= _validate_md5(egg_name
, src
.read())
130 dst
= open(saveto
,"wb"); dst
.write(data
)
134 return os
.path
.realpath(saveto
)
136 def main(argv
, version
=DEFAULT_VERSION
):
137 """Install or upgrade setuptools and EasyInstall"""
142 import tempfile
, shutil
143 tmpdir
= tempfile
.mkdtemp(prefix
="easy_install-")
145 egg
= download_setuptools(version
, to_dir
=tmpdir
, delay
=0)
146 sys
.path
.insert(0,egg
)
147 from setuptools
.command
.easy_install
import main
148 main(list(argv
)+[egg
])
150 shutil
.rmtree(tmpdir
)
152 if setuptools
.__version
__ == '0.0.1':
153 # tell the user to uninstall obsolete version
154 use_setuptools(version
)
156 req
= "setuptools>="+version
159 pkg_resources
.require(req
)
160 except pkg_resources
.VersionConflict
:
162 from setuptools
.command
.easy_install
import main
164 from easy_install
import main
165 main(list(argv
)+[download_setuptools(delay
=0)])
166 sys
.exit(0) # try to force an exit
169 from setuptools
.command
.easy_install
import main
172 print "Setuptools version",version
,"or greater has been installed."
173 print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
177 def update_md5(filenames
):
178 """Update our built-in md5 registry"""
183 for name
in filenames
:
184 base
= os
.path
.basename(name
)
186 md5_data
[base
] = md5(f
.read()).hexdigest()
189 data
= [" %r: %r,\n" % it
for it
in md5_data
.items()]
194 srcfile
= inspect
.getsourcefile(sys
.modules
[__name__
])
195 f
= open(srcfile
, 'rb'); src
= f
.read(); f
.close()
197 match
= re
.search("\nmd5_data = {\n([^}]+)}", src
)
199 print >>sys
.stderr
, "Internal error!"
202 src
= src
[:match
.start(1)] + repl
+ src
[match
.end(1):]
203 f
= open(srcfile
,'w')
208 if __name__
=='__main__':
209 if len(sys
.argv
)>2 and sys
.argv
[1]=='--md5update':
210 update_md5(sys
.argv
[2:])