root/branches/mk/ez_setup.py

Revision 11, 8.7 kB (checked in by grig, 7 years ago)

Added ez_setup.py so that users are forced to upgrade to new version of setuptools.

Changed installation to alternate directory to use --root instead of --home.

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