|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 #!python |
|
17 """Bootstrap setuptools installation |
|
18 |
|
19 If you want to use setuptools in your package's setup.py, just include this |
|
20 file in the same directory with it, and add this to the top of your setup.py:: |
|
21 |
|
22 from ez_setup import use_setuptools |
|
23 use_setuptools() |
|
24 |
|
25 If you want to require a specific version of setuptools, set a download |
|
26 mirror, or use an alternate download directory, you can do so by supplying |
|
27 the appropriate options to ``use_setuptools()``. |
|
28 |
|
29 This file can also be run as a script to install or upgrade setuptools. |
|
30 """ |
|
31 import sys |
|
32 DEFAULT_VERSION = "0.6c9" |
|
33 DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] |
|
34 |
|
35 md5_data = { |
|
36 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca', |
|
37 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb', |
|
38 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b', |
|
39 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a', |
|
40 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618', |
|
41 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac', |
|
42 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5', |
|
43 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4', |
|
44 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c', |
|
45 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b', |
|
46 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27', |
|
47 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277', |
|
48 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa', |
|
49 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e', |
|
50 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e', |
|
51 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f', |
|
52 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2', |
|
53 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc', |
|
54 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167', |
|
55 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64', |
|
56 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d', |
|
57 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20', |
|
58 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab', |
|
59 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53', |
|
60 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2', |
|
61 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', |
|
62 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', |
|
63 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', |
|
64 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', |
|
65 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', |
|
66 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03', |
|
67 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a', |
|
68 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6', |
|
69 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a', |
|
70 } |
|
71 |
|
72 import sys, os |
|
73 try: from hashlib import md5 |
|
74 except ImportError: from md5 import md5 |
|
75 |
|
76 def _validate_md5(egg_name, data): |
|
77 if egg_name in md5_data: |
|
78 digest = md5(data).hexdigest() |
|
79 if digest != md5_data[egg_name]: |
|
80 print >>sys.stderr, ( |
|
81 "md5 validation of %s failed! (Possible download problem?)" |
|
82 % egg_name |
|
83 ) |
|
84 sys.exit(2) |
|
85 return data |
|
86 |
|
87 def use_setuptools( |
|
88 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, |
|
89 download_delay=15 |
|
90 ): |
|
91 """Automatically find/download setuptools and make it available on sys.path |
|
92 |
|
93 `version` should be a valid setuptools version number that is available |
|
94 as an egg for download under the `download_base` URL (which should end with |
|
95 a '/'). `to_dir` is the directory where setuptools will be downloaded, if |
|
96 it is not already available. If `download_delay` is specified, it should |
|
97 be the number of seconds that will be paused before initiating a download, |
|
98 should one be required. If an older version of setuptools is installed, |
|
99 this routine will print a message to ``sys.stderr`` and raise SystemExit in |
|
100 an attempt to abort the calling script. |
|
101 """ |
|
102 was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules |
|
103 def do_download(): |
|
104 egg = download_setuptools(version, download_base, to_dir, download_delay) |
|
105 sys.path.insert(0, egg) |
|
106 import setuptools; setuptools.bootstrap_install_from = egg |
|
107 try: |
|
108 import pkg_resources |
|
109 except ImportError: |
|
110 return do_download() |
|
111 try: |
|
112 pkg_resources.require("setuptools>="+version); return |
|
113 except pkg_resources.VersionConflict, e: |
|
114 if was_imported: |
|
115 print >>sys.stderr, ( |
|
116 "The required version of setuptools (>=%s) is not available, and\n" |
|
117 "can't be installed while this script is running. Please install\n" |
|
118 " a more recent version first, using 'easy_install -U setuptools'." |
|
119 "\n\n(Currently using %r)" |
|
120 ) % (version, e.args[0]) |
|
121 sys.exit(2) |
|
122 else: |
|
123 del pkg_resources, sys.modules['pkg_resources'] # reload ok |
|
124 return do_download() |
|
125 except pkg_resources.DistributionNotFound: |
|
126 return do_download() |
|
127 |
|
128 def download_setuptools( |
|
129 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, |
|
130 delay = 15 |
|
131 ): |
|
132 """Download setuptools from a specified location and return its filename |
|
133 |
|
134 `version` should be a valid setuptools version number that is available |
|
135 as an egg for download under the `download_base` URL (which should end |
|
136 with a '/'). `to_dir` is the directory where the egg will be downloaded. |
|
137 `delay` is the number of seconds to pause before an actual download attempt. |
|
138 """ |
|
139 import urllib2, shutil |
|
140 egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) |
|
141 url = download_base + egg_name |
|
142 saveto = os.path.join(to_dir, egg_name) |
|
143 src = dst = None |
|
144 if not os.path.exists(saveto): # Avoid repeated downloads |
|
145 try: |
|
146 from distutils import log |
|
147 if delay: |
|
148 log.warn(""" |
|
149 --------------------------------------------------------------------------- |
|
150 This script requires setuptools version %s to run (even to display |
|
151 help). I will attempt to download it for you (from |
|
152 %s), but |
|
153 you may need to enable firewall access for this script first. |
|
154 I will start the download in %d seconds. |
|
155 |
|
156 (Note: if this machine does not have network access, please obtain the file |
|
157 |
|
158 %s |
|
159 |
|
160 and place it in this directory before rerunning this script.) |
|
161 ---------------------------------------------------------------------------""", |
|
162 version, download_base, delay, url |
|
163 ); from time import sleep; sleep(delay) |
|
164 log.warn("Downloading %s", url) |
|
165 src = urllib2.urlopen(url) |
|
166 # Read/write all in one block, so we don't create a corrupt file |
|
167 # if the download is interrupted. |
|
168 data = _validate_md5(egg_name, src.read()) |
|
169 dst = open(saveto,"wb"); dst.write(data) |
|
170 finally: |
|
171 if src: src.close() |
|
172 if dst: dst.close() |
|
173 return os.path.realpath(saveto) |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 def main(argv, version=DEFAULT_VERSION): |
|
211 """Install or upgrade setuptools and EasyInstall""" |
|
212 try: |
|
213 import setuptools |
|
214 except ImportError: |
|
215 egg = None |
|
216 try: |
|
217 egg = download_setuptools(version, delay=0) |
|
218 sys.path.insert(0,egg) |
|
219 from setuptools.command.easy_install import main |
|
220 return main(list(argv)+[egg]) # we're done here |
|
221 finally: |
|
222 if egg and os.path.exists(egg): |
|
223 os.unlink(egg) |
|
224 else: |
|
225 if setuptools.__version__ == '0.0.1': |
|
226 print >>sys.stderr, ( |
|
227 "You have an obsolete version of setuptools installed. Please\n" |
|
228 "remove it from your system entirely before rerunning this script." |
|
229 ) |
|
230 sys.exit(2) |
|
231 |
|
232 req = "setuptools>="+version |
|
233 import pkg_resources |
|
234 try: |
|
235 pkg_resources.require(req) |
|
236 except pkg_resources.VersionConflict: |
|
237 try: |
|
238 from setuptools.command.easy_install import main |
|
239 except ImportError: |
|
240 from easy_install import main |
|
241 main(list(argv)+[download_setuptools(delay=0)]) |
|
242 sys.exit(0) # try to force an exit |
|
243 else: |
|
244 if argv: |
|
245 from setuptools.command.easy_install import main |
|
246 main(argv) |
|
247 else: |
|
248 print "Setuptools version",version,"or greater has been installed." |
|
249 print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' |
|
250 |
|
251 def update_md5(filenames): |
|
252 """Update our built-in md5 registry""" |
|
253 |
|
254 import re |
|
255 |
|
256 for name in filenames: |
|
257 base = os.path.basename(name) |
|
258 f = open(name,'rb') |
|
259 md5_data[base] = md5(f.read()).hexdigest() |
|
260 f.close() |
|
261 |
|
262 data = [" %r: %r,\n" % it for it in md5_data.items()] |
|
263 data.sort() |
|
264 repl = "".join(data) |
|
265 |
|
266 import inspect |
|
267 srcfile = inspect.getsourcefile(sys.modules[__name__]) |
|
268 f = open(srcfile, 'rb'); src = f.read(); f.close() |
|
269 |
|
270 match = re.search("\nmd5_data = {\n([^}]+)}", src) |
|
271 if not match: |
|
272 print >>sys.stderr, "Internal error!" |
|
273 sys.exit(2) |
|
274 |
|
275 src = src[:match.start(1)] + repl + src[match.end(1):] |
|
276 f = open(srcfile,'w') |
|
277 f.write(src) |
|
278 f.close() |
|
279 |
|
280 |
|
281 if __name__=='__main__': |
|
282 if len(sys.argv)>2 and sys.argv[1]=='--md5update': |
|
283 update_md5(sys.argv[2:]) |
|
284 else: |
|
285 main(sys.argv[1:]) |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |