Changed CommonMiddleware so it doesn't assume HTTP_HOST is set.
[fdr-django.git] / ez_setup.py
blobb9d4f2dae0c16c3d65de5f8a23f7bd5fc1f1950a
1 #!python
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
8 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.
15 """
16 import sys
17 DEFAULT_VERSION = "0.6a7"
18 DEFAULT_URL = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3]
20 md5_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.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',
39 import sys, os
41 def _validate_md5(egg_name, data):
42 if egg_name in md5_data:
43 from md5 import md5
44 digest = md5(data).hexdigest()
45 if digest != md5_data[egg_name]:
46 print >>sys.stderr, (
47 "md5 validation of %s failed! (Possible download problem?)"
48 % egg_name
50 sys.exit(2)
51 return data
54 def use_setuptools(
55 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
56 download_delay=15
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.
68 """
69 try:
70 import setuptools
71 if setuptools.__version__ == '0.0.1':
72 print >>sys.stderr, (
73 "You have an obsolete version of setuptools installed. Please\n"
74 "remove it from your system entirely before rerunning this script."
76 sys.exit(2)
77 except ImportError:
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
82 import pkg_resources
83 try:
84 pkg_resources.require("setuptools>="+version)
86 except pkg_resources.VersionConflict:
87 # XXX could we install in a subprocess here?
88 print >>sys.stderr, (
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."
92 ) % version
93 sys.exit(2)
95 def download_setuptools(
96 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
97 delay = 15
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)
110 src = dst = None
111 if not os.path.exists(saveto): # Avoid repeated downloads
112 try:
113 from distutils import log
114 if delay:
115 log.warn("""
116 ---------------------------------------------------------------------------
117 This script requires setuptools version %s to run (even to display
118 help). I will attempt to download it for you (from
119 %s), but
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)
131 finally:
132 if src: src.close()
133 if dst: dst.close()
134 return os.path.realpath(saveto)
136 def main(argv, version=DEFAULT_VERSION):
137 """Install or upgrade setuptools and EasyInstall"""
139 try:
140 import setuptools
141 except ImportError:
142 import tempfile, shutil
143 tmpdir = tempfile.mkdtemp(prefix="easy_install-")
144 try:
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])
149 finally:
150 shutil.rmtree(tmpdir)
151 else:
152 if setuptools.__version__ == '0.0.1':
153 # tell the user to uninstall obsolete version
154 use_setuptools(version)
156 req = "setuptools>="+version
157 import pkg_resources
158 try:
159 pkg_resources.require(req)
160 except pkg_resources.VersionConflict:
161 try:
162 from setuptools.command.easy_install import main
163 except ImportError:
164 from easy_install import main
165 main(list(argv)+[download_setuptools(delay=0)])
166 sys.exit(0) # try to force an exit
167 else:
168 if argv:
169 from setuptools.command.easy_install import main
170 main(argv)
171 else:
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"""
180 import re
181 from md5 import md5
183 for name in filenames:
184 base = os.path.basename(name)
185 f = open(name,'rb')
186 md5_data[base] = md5(f.read()).hexdigest()
187 f.close()
189 data = [" %r: %r,\n" % it for it in md5_data.items()]
190 data.sort()
191 repl = "".join(data)
193 import inspect
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)
198 if not match:
199 print >>sys.stderr, "Internal error!"
200 sys.exit(2)
202 src = src[:match.start(1)] + repl + src[match.end(1):]
203 f = open(srcfile,'w')
204 f.write(src)
205 f.close()
208 if __name__=='__main__':
209 if len(sys.argv)>2 and sys.argv[1]=='--md5update':
210 update_md5(sys.argv[2:])
211 else:
212 main(sys.argv[1:])