Skip to content
Myr

ez_setup.py

This file

Path

ez_setup.py

215 lines · 7.5 KiB · Python

Last changed by

[svn] some small updates to make jinja performing betterArmin Ronacherbefore adoptionde478f6

History of this file

Take it

1#!python2"""Bootstrap setuptools installation34If you want to use setuptools in your package's setup.py, just include this5file in the same directory with it, and add this to the top of your setup.py::67    from ez_setup import use_setuptools8    use_setuptools()910If you want to require a specific version of setuptools, set a download11mirror, or use an alternate download directory, you can do so by supplying12the appropriate options to ``use_setuptools()``.1314This file can also be run as a script to install or upgrade setuptools.15"""16import sys17DEFAULT_VERSION = "0.6b3"18DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3]1920md5_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.6b3dev_r46791-py2.3.egg': 'e765a29566575ffac5d81cdf0c6f8db9',28    'setuptools-0.6b3dev_r46791-py2.4.egg': 'd249c022ed029ad60d134bd998adc880',29}3031import sys, os3233def _validate_md5(egg_name, data):34    if egg_name in md5_data:35        from md5 import md536        digest = md5(data).hexdigest()37        if digest != md5_data[egg_name]:38            print >>sys.stderr, (39                "md5 validation of %s failed!  (Possible download problem?)"40                % egg_name41            )42            sys.exit(2)43    return data444546def use_setuptools(47    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,48    download_delay=1549):50    """Automatically find/download setuptools and make it available on sys.path5152    `version` should be a valid setuptools version number that is available53    as an egg for download under the `download_base` URL (which should end with54    a '/').  `to_dir` is the directory where setuptools will be downloaded, if55    it is not already available.  If `download_delay` is specified, it should56    be the number of seconds that will be paused before initiating a download,57    should one be required.  If an older version of setuptools is installed,58    this routine will print a message to ``sys.stderr`` and raise SystemExit in59    an attempt to abort the calling script.60    """61    try:62        import setuptools63        if setuptools.__version__ == '0.0.1':64            print >>sys.stderr, (65            "You have an obsolete version of setuptools installed.  Please\n"66            "remove it from your system entirely before rerunning this script."67            )68            sys.exit(2)69    except ImportError:70        egg = download_setuptools(version, download_base, to_dir, download_delay)71        sys.path.insert(0, egg)72        import setuptools; setuptools.bootstrap_install_from = egg7374    import pkg_resources75    try:76        pkg_resources.require("setuptools>="+version)7778    except pkg_resources.VersionConflict:79        # XXX could we install in a subprocess here?80        print >>sys.stderr, (81            "The required version of setuptools (>=%s) is not available, and\n"82            "can't be installed while this script is running. Please install\n"83            " a more recent version first."84        ) % version85        sys.exit(2)8687def download_setuptools(88    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,89    delay = 1590):91    """Download setuptools from a specified location and return its filename9293    `version` should be a valid setuptools version number that is available94    as an egg for download under the `download_base` URL (which should end95    with a '/'). `to_dir` is the directory where the egg will be downloaded.96    `delay` is the number of seconds to pause before an actual download attempt.97    """98    import urllib2, shutil99    egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])100    url = download_base + egg_name101    saveto = os.path.join(to_dir, egg_name)102    src = dst = None103    if not os.path.exists(saveto):  # Avoid repeated downloads104        try:105            from distutils import log106            if delay:107                log.warn("""108---------------------------------------------------------------------------109This script requires setuptools version %s to run (even to display110help).  I will attempt to download it for you (from111%s), but112you may need to enable firewall access for this script first.113I will start the download in %d seconds.114115(Note: if this machine does not have network access, please obtain the file116117   %s118119and place it in this directory before rerunning this script.)120---------------------------------------------------------------------------""",121                    version, download_base, delay, url122                ); from time import sleep; sleep(delay)123            log.warn("Downloading %s", url)124            src = urllib2.urlopen(url)125            # Read/write all in one block, so we don't create a corrupt file126            # if the download is interrupted.127            data = _validate_md5(egg_name, src.read())128            dst = open(saveto,"wb"); dst.write(data)129        finally:130            if src: src.close()131            if dst: dst.close()132    return os.path.realpath(saveto)133134def main(argv, version=DEFAULT_VERSION):135    """Install or upgrade setuptools and EasyInstall"""136137    try:138        import setuptools139    except ImportError:140        import tempfile, shutil141        tmpdir = tempfile.mkdtemp(prefix="easy_install-")142        try:143            egg = download_setuptools(version, to_dir=tmpdir, delay=0)144            sys.path.insert(0,egg)145            from setuptools.command.easy_install import main146            return main(list(argv)+[egg])   # we're done here147        finally:148            shutil.rmtree(tmpdir)149    else:150        if setuptools.__version__ == '0.0.1':151            # tell the user to uninstall obsolete version152            use_setuptools(version)153154    req = "setuptools>="+version155    import pkg_resources156    try:157        pkg_resources.require(req)158    except pkg_resources.VersionConflict:159        try:160            from setuptools.command.easy_install import main161        except ImportError:162            from easy_install import main163        main(list(argv)+[download_setuptools(delay=0)])164        sys.exit(0) # try to force an exit165    else:166        if argv:167            from setuptools.command.easy_install import main168            main(argv)169        else:170            print "Setuptools version",version,"or greater has been installed."171            print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'172173174175def update_md5(filenames):176    """Update our built-in md5 registry"""177178    import re179    from md5 import md5180181    for name in filenames:182        base = os.path.basename(name)183        f = open(name,'rb')184        md5_data[base] = md5(f.read()).hexdigest()185        f.close()186187    data = ["    %r: %r,\n" % it for it in md5_data.items()]188    data.sort()189    repl = "".join(data)190191    import inspect192    srcfile = inspect.getsourcefile(sys.modules[__name__])193    f = open(srcfile, 'rb'); src = f.read(); f.close()194195    match = re.search("\nmd5_data = {\n([^}]+)}", src)196    if not match:197        print >>sys.stderr, "Internal error!"198        sys.exit(2)199200    src = src[:match.start(1)] + repl + src[match.end(1):]201    f = open(srcfile,'w')202    f.write(src)203    f.close()204205206if __name__=='__main__':207    if len(sys.argv)>2 and sys.argv[1]=='--md5update':208        update_md5(sys.argv[2:])209    else:210        main(sys.argv[1:])211212213214215