ExtendedUserModel: ez_setup.py

File ez_setup.py, 8.6 KB (added by anonymous, 19 years ago)
Line 
1#!python
2"""Bootstrap setuptools installation
3
4If you want to use setuptools in your package's setup.py, just include this
5file 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
10If you want to require a specific version of setuptools, set a download
11mirror, or use an alternate download directory, you can do so by supplying
12the appropriate options to ``use_setuptools()``.
13
14This file can also be run as a script to install or upgrade setuptools.
15"""
16import sys
17DEFAULT_VERSION = "0.6a10"
18DEFAULT_URL = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3]
19
20md5_data = {
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.6a10-py2.3.egg': '162d8357f1aff2b0349c6c247ee62987',
26 'setuptools-0.6a10-py2.4.egg': '803a2d8db501c1ac3b5b6fb4e907f788',
27 'setuptools-0.6a10dev_r42346-py2.3.egg': 'a7899272cfceb6aa60094ae8928b8077',
28 'setuptools-0.6a10dev_r42346-py2.4.egg': '5d42a64adca9aedb409f83ecf22156a5',
29 'setuptools-0.6a2-py2.3.egg': 'b98da449da411267c37a738f0ab625ba',
30 'setuptools-0.6a2-py2.4.egg': 'be5b88bc30aed63fdefd2683be135c3b',
31 'setuptools-0.6a3-py2.3.egg': 'ee0e325de78f23aab79d33106dc2a8c8',
32 'setuptools-0.6a3-py2.4.egg': 'd95453d525a456d6c23e7a5eea89a063',
33 'setuptools-0.6a4-py2.3.egg': 'e958cbed4623bbf47dd1f268b99d7784',
34 'setuptools-0.6a4-py2.4.egg': '7f33c3ac2ef1296f0ab4fac1de4767d8',
35 'setuptools-0.6a5-py2.3.egg': '748408389c49bcd2d84f6ae0b01695b1',
36 'setuptools-0.6a5-py2.4.egg': '999bacde623f4284bfb3ea77941d2627',
37 'setuptools-0.6a6-py2.3.egg': '7858139f06ed0600b0d9383f36aca24c',
38 'setuptools-0.6a6-py2.4.egg': 'c10d20d29acebce0dc76219dc578d058',
39 'setuptools-0.6a7-py2.3.egg': 'cfc4125ddb95c07f9500adc5d6abef6f',
40 'setuptools-0.6a7-py2.4.egg': 'c6d62dab4461f71aed943caea89e6f20',
41 'setuptools-0.6a8-py2.3.egg': '2f18eaaa3f544f5543ead4a68f3b2e1a',
42 'setuptools-0.6a8-py2.4.egg': '799018f2894f14c9f8bcb2b34e69b391',
43 'setuptools-0.6a9-py2.3.egg': '8e438ad70438b07b0d8f82cae42b278f',
44 'setuptools-0.6a9-py2.4.egg': '8f6e01fc12fb1cd006dc0d6c04327ec1',
45}
46
47import sys, os
48
49def _validate_md5(egg_name, data):
50 if egg_name in md5_data:
51 from md5 import md5
52 digest = md5(data).hexdigest()
53 if digest != md5_data[egg_name]:
54 print >>sys.stderr, (
55 "md5 validation of %s failed! (Possible download problem?)"
56 % egg_name
57 )
58 sys.exit(2)
59 return data
60
61
62def use_setuptools(
63 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
64 download_delay=15
65):
66 """Automatically find/download setuptools and make it available on sys.path
67
68 `version` should be a valid setuptools version number that is available
69 as an egg for download under the `download_base` URL (which should end with
70 a '/'). `to_dir` is the directory where setuptools will be downloaded, if
71 it is not already available. If `download_delay` is specified, it should
72 be the number of seconds that will be paused before initiating a download,
73 should one be required. If an older version of setuptools is installed,
74 this routine will print a message to ``sys.stderr`` and raise SystemExit in
75 an attempt to abort the calling script.
76 """
77 try:
78 import setuptools
79 if setuptools.__version__ == '0.0.1':
80 print >>sys.stderr, (
81 "You have an obsolete version of setuptools installed. Please\n"
82 "remove it from your system entirely before rerunning this script."
83 )
84 sys.exit(2)
85 except ImportError:
86 egg = download_setuptools(version, download_base, to_dir, download_delay)
87 sys.path.insert(0, egg)
88 import setuptools; setuptools.bootstrap_install_from = egg
89
90 import pkg_resources
91 try:
92 pkg_resources.require("setuptools>="+version)
93
94 except pkg_resources.VersionConflict:
95 # XXX could we install in a subprocess here?
96 print >>sys.stderr, (
97 "The required version of setuptools (>=%s) is not available, and\n"
98 "can't be installed while this script is running. Please install\n"
99 " a more recent version first."
100 ) % version
101 sys.exit(2)
102
103def download_setuptools(
104 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
105 delay = 15
106):
107 """Download setuptools from a specified location and return its filename
108
109 `version` should be a valid setuptools version number that is available
110 as an egg for download under the `download_base` URL (which should end
111 with a '/'). `to_dir` is the directory where the egg will be downloaded.
112 `delay` is the number of seconds to pause before an actual download attempt.
113 """
114 import urllib2, shutil
115 egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
116 url = download_base + egg_name
117 saveto = os.path.join(to_dir, egg_name)
118 src = dst = None
119 if not os.path.exists(saveto): # Avoid repeated downloads
120 try:
121 from distutils import log
122 if delay:
123 log.warn("""
124---------------------------------------------------------------------------
125This script requires setuptools version %s to run (even to display
126help). I will attempt to download it for you (from
127%s), but
128you may need to enable firewall access for this script first.
129I will start the download in %d seconds.
130
131(Note: if this machine does not have network access, please obtain the file
132
133 %s
134
135and place it in this directory before rerunning this script.)
136---------------------------------------------------------------------------""",
137 version, download_base, delay, url
138 ); from time import sleep; sleep(delay)
139 log.warn("Downloading %s", url)
140 src = urllib2.urlopen(url)
141 # Read/write all in one block, so we don't create a corrupt file
142 # if the download is interrupted.
143 data = _validate_md5(egg_name, src.read())
144 dst = open(saveto,"wb"); dst.write(data)
145 finally:
146 if src: src.close()
147 if dst: dst.close()
148 return os.path.realpath(saveto)
149
150def main(argv, version=DEFAULT_VERSION):
151 """Install or upgrade setuptools and EasyInstall"""
152
153 try:
154 import setuptools
155 except ImportError:
156 import tempfile, shutil
157 tmpdir = tempfile.mkdtemp(prefix="easy_install-")
158 try:
159 egg = download_setuptools(version, to_dir=tmpdir, delay=0)
160 sys.path.insert(0,egg)
161 from setuptools.command.easy_install import main
162 main(list(argv)+[egg])
163 finally:
164 shutil.rmtree(tmpdir)
165 else:
166 if setuptools.__version__ == '0.0.1':
167 # tell the user to uninstall obsolete version
168 use_setuptools(version)
169
170 req = "setuptools>="+version
171 import pkg_resources
172 try:
173 pkg_resources.require(req)
174 except pkg_resources.VersionConflict:
175 try:
176 from setuptools.command.easy_install import main
177 except ImportError:
178 from easy_install import main
179 main(list(argv)+[download_setuptools(delay=0)])
180 sys.exit(0) # try to force an exit
181 else:
182 if argv:
183 from setuptools.command.easy_install import main
184 main(argv)
185 else:
186 print "Setuptools version",version,"or greater has been installed."
187 print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
188
189
190
191def update_md5(filenames):
192 """Update our built-in md5 registry"""
193
194 import re
195 from md5 import md5
196
197 for name in filenames:
198 base = os.path.basename(name)
199 f = open(name,'rb')
200 md5_data[base] = md5(f.read()).hexdigest()
201 f.close()
202
203 data = [" %r: %r,\n" % it for it in md5_data.items()]
204 data.sort()
205 repl = "".join(data)
206
207 import inspect
208 srcfile = inspect.getsourcefile(sys.modules[__name__])
209 f = open(srcfile, 'rb'); src = f.read(); f.close()
210
211 match = re.search("\nmd5_data = {\n([^}]+)}", src)
212 if not match:
213 print >>sys.stderr, "Internal error!"
214 sys.exit(2)
215
216 src = src[:match.start(1)] + repl + src[match.end(1):]
217 f = open(srcfile,'w')
218 f.write(src)
219 f.close()
220
221
222if __name__=='__main__':
223 if len(sys.argv)>2 and sys.argv[1]=='--md5update':
224 update_md5(sys.argv[2:])
225 else:
226 main(sys.argv[1:])
227
228
229
230
231
Back to Top