source: TI05-delivery/trunk/ez_setup.py @ 1439

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/ez_setup.py@1441
Revision 1439, 7.6 KB checked in by spascoe, 13 years ago (diff)

Continuing transition to use setuptools. Eggs will now build but
examples and test cases are broken.

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.6c1"
18DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3]
19
20md5_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}
32
33import sys, os
34
35def _validate_md5(egg_name, data):
36    if egg_name in md5_data:
37        from md5 import md5
38        digest = md5(data).hexdigest()
39        if digest != md5_data[egg_name]:
40            print >>sys.stderr, (
41                "md5 validation of %s failed!  (Possible download problem?)"
42                % egg_name
43            )
44            sys.exit(2)
45    return data
46
47
48def use_setuptools(
49    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
50    download_delay=15
51):
52    """Automatically find/download setuptools and make it available on sys.path
53
54    `version` should be a valid setuptools version number that is available
55    as an egg for download under the `download_base` URL (which should end with
56    a '/').  `to_dir` is the directory where setuptools will be downloaded, if
57    it is not already available.  If `download_delay` is specified, it should
58    be the number of seconds that will be paused before initiating a download,
59    should one be required.  If an older version of setuptools is installed,
60    this routine will print a message to ``sys.stderr`` and raise SystemExit in
61    an attempt to abort the calling script.
62    """
63    try:
64        import setuptools
65        if setuptools.__version__ == '0.0.1':
66            print >>sys.stderr, (
67            "You have an obsolete version of setuptools installed.  Please\n"
68            "remove it from your system entirely before rerunning this script."
69            )
70            sys.exit(2)
71    except ImportError:
72        egg = download_setuptools(version, download_base, to_dir, download_delay)
73        sys.path.insert(0, egg)
74        import setuptools; setuptools.bootstrap_install_from = egg
75
76    import pkg_resources
77    try:
78        pkg_resources.require("setuptools>="+version)
79
80    except pkg_resources.VersionConflict:
81        # XXX could we install in a subprocess here?
82        print >>sys.stderr, (
83            "The required version of setuptools (>=%s) is not available, and\n"
84            "can't be installed while this script is running. Please install\n"
85            " a more recent version first."
86        ) % version
87        sys.exit(2)
88
89def download_setuptools(
90    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
91    delay = 15
92):
93    """Download setuptools from a specified location and return its filename
94
95    `version` should be a valid setuptools version number that is available
96    as an egg for download under the `download_base` URL (which should end
97    with a '/'). `to_dir` is the directory where the egg will be downloaded.
98    `delay` is the number of seconds to pause before an actual download attempt.
99    """
100    import urllib2, shutil
101    egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
102    url = download_base + egg_name
103    saveto = os.path.join(to_dir, egg_name)
104    src = dst = None
105    if not os.path.exists(saveto):  # Avoid repeated downloads
106        try:
107            from distutils import log
108            if delay:
109                log.warn("""
110---------------------------------------------------------------------------
111This script requires setuptools version %s to run (even to display
112help).  I will attempt to download it for you (from
113%s), but
114you may need to enable firewall access for this script first.
115I will start the download in %d seconds.
116
117(Note: if this machine does not have network access, please obtain the file
118
119   %s
120
121and place it in this directory before rerunning this script.)
122---------------------------------------------------------------------------""",
123                    version, download_base, delay, url
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)
135
136def main(argv, version=DEFAULT_VERSION):
137    """Install or upgrade setuptools and EasyInstall"""
138
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            return main(list(argv)+[egg])   # we're done here
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)
155
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.)'
174
175
176
177def update_md5(filenames):
178    """Update our built-in md5 registry"""
179
180    import re
181    from md5 import md5
182
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()
188
189    data = ["    %r: %r,\n" % it for it in md5_data.items()]
190    data.sort()
191    repl = "".join(data)
192
193    import inspect
194    srcfile = inspect.getsourcefile(sys.modules[__name__])
195    f = open(srcfile, 'rb'); src = f.read(); f.close()
196
197    match = re.search("\nmd5_data = {\n([^}]+)}", src)
198    if not match:
199        print >>sys.stderr, "Internal error!"
200        sys.exit(2)
201
202    src = src[:match.start(1)] + repl + src[match.end(1):]
203    f = open(srcfile,'w')
204    f.write(src)
205    f.close()
206
207
208if __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:])
213
214
215
216
217
Note: See TracBrowser for help on using the repository browser.