root/ez_setup.py

Revision 65:b1ecc2e91912, 8.6 kB (checked in by Allan Saddi <allan@saddi.com>, 1 year ago)

Update ez_setup.py

Line 
1 #!python
2 """Bootstrap setuptools installation
3
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::
6
7     from ez_setup import use_setuptools
8     use_setuptools()
9
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()``.
13
14 This file can also be run as a script to install or upgrade setuptools.
15 """
16 import sys
17 DEFAULT_VERSION = "0.6c6"
18 DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3]
19
20 md5_data = {
21     'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
22     'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
23     'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
24     'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
25     'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
26     'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
27     'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
28     'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
29     'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
30     'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
31     'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
32     'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
33     'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
34     'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
35     'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
36     'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
37     'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
38     'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
39     'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
40     'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
41     'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
42     'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20',
43     'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab',
44     'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53',
45 }
46
47 import sys, os
48
49 def _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
62 def 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, e:
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.\n\n(Currently using %r)"
100         ) % (version, e.args[0])
101         sys.exit(2)
102
103 def 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 ---------------------------------------------------------------------------
125 This script requires setuptools version %s to run (even to display
126 help).  I will attempt to download it for you (from
127 %s), but
128 you may need to enable firewall access for this script first.
129 I 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
135 and 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
150 def main(argv, version=DEFAULT_VERSION):
151     """Install or upgrade setuptools and EasyInstall"""
152
153     try:
154         import setuptools
155     except ImportError:
156         egg = None
157         try:
158             egg = download_setuptools(version, delay=0)
159             sys.path.insert(0,egg)
160             from setuptools.command.easy_install import main
161             return main(list(argv)+[egg])   # we're done here
162         finally:
163             if egg and os.path.exists(egg):
164                 os.unlink(egg)
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
191 def 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
222 if __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
Note: See TracBrowser for help on using the browser.