root/postfacto/trunk/ez_setup.py

Revision 7, 4.7 kB (checked in by fumanchu, 16 months ago)

New 'pf' script, plus setup stuff.

  • Property svn:eol-style set to native
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"""
16
17DEFAULT_VERSION = "0.5a13"
18DEFAULT_URL     = "http://www.python.org/packages/source/s/setuptools/"
19
20import sys, os
21
22def use_setuptools(
23    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir
24):
25    """Automatically find/download setuptools and make it available on sys.path
26
27    `version` should be a valid setuptools version number that is available
28    as an egg for download under the `download_base` URL (which should end with
29    a '/').  `to_dir` is the directory where setuptools will be downloaded, if
30    it is not already available.
31
32    If an older version of setuptools is installed, this will print a message
33    to ``sys.stderr`` and raise SystemExit in an attempt to abort the calling
34    script.
35    """
36    try:
37        import setuptools
38        if setuptools.__version__ == '0.0.1':
39            print >>sys.stderr, (
40            "You have an obsolete version of setuptools installed.  Please\n"
41            "remove it from your system entirely before rerunning this script."
42            )
43            sys.exit(2)
44
45    except ImportError:
46        egg = download_setuptools(version, download_base, to_dir)
47        sys.path.insert(0, egg)
48        import setuptools; setuptools.bootstrap_install_from = egg
49
50    import pkg_resources
51    try:
52        pkg_resources.require("setuptools>="+version)
53
54    except pkg_resources.VersionConflict:
55        # XXX could we install in a subprocess here?
56        print >>sys.stderr, (
57            "The required version of setuptools (>=%s) is not available, and\n"
58            "can't be installed while this script is running. Please install\n"
59            " a more recent version first."
60        ) % version
61        sys.exit(2)
62
63def download_setuptools(
64    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir
65):
66    """Download setuptools from a specified location and return its filename
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
70    with a '/'). `to_dir` is the directory where the egg will be downloaded.
71    """
72    import urllib2, shutil
73    egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
74    url = download_base + egg_name + '.zip'  # XXX
75    saveto = os.path.join(to_dir, egg_name)
76    src = dst = None
77
78    if not os.path.exists(saveto):  # Avoid repeated downloads
79        try:
80            from distutils import log
81            log.warn("Downloading %s", url)
82            src = urllib2.urlopen(url)
83            # Read/write all in one block, so we don't create a corrupt file
84            # if the download is interrupted.
85            data = src.read()
86            dst = open(saveto,"wb")
87            dst.write(data)
88        finally:
89            if src: src.close()
90            if dst: dst.close()
91
92    return os.path.realpath(saveto)
93
94def main(argv, version=DEFAULT_VERSION):
95    """Install or upgrade setuptools and EasyInstall"""
96
97    try:
98        import setuptools
99    except ImportError:
100        import tempfile, shutil
101        tmpdir = tempfile.mkdtemp(prefix="easy_install-")
102        try:
103            egg = download_setuptools(version, to_dir=tmpdir)
104            sys.path.insert(0,egg)
105            from setuptools.command.easy_install import main
106            main(list(argv)+[egg])
107        finally:
108            shutil.rmtree(tmpdir)
109    else:
110        if setuptools.__version__ == '0.0.1':
111            # tell the user to uninstall obsolete version
112            use_setuptools(version)
113
114    req = "setuptools>="+version
115    import pkg_resources
116    try:
117        pkg_resources.require(req)
118    except pkg_resources.VersionConflict:
119        try:
120            from setuptools.command.easy_install import main
121        except ImportError:
122            from easy_install import main
123        main(list(argv)+[download_setuptools()])
124        sys.exit(0) # try to force an exit
125    else:
126        if argv:
127            from setuptools.command.easy_install import main
128            main(argv)
129        else:
130            print "Setuptools version",version,"or greater has been installed."
131            print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
132if __name__=='__main__':
133    main(sys.argv[1:])
Note: See TracBrowser for help on using the browser.