source: TI13-DataProvider/trunk/python_installer/ez_setup.py @ 1051

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI13-DataProvider/trunk/python_installer/ez_setup.py@1051
Revision 1051, 7.3 KB checked in by spascoe, 14 years ago (diff)

We will use setuputils, therefore all we need is ez_setup.py to fetch
most dependencies.

The --enable-shared configure option may not be necessary for the DeliveryService
after all therefore it's disabled for now.

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