2
"""Bootstrap setuptools installation
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::
7
from ez_setup import use_setuptools
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()``.
14
This file can also be run as a script to install or upgrade setuptools.
17
DEFAULT_VERSION = "0.5a13"
18
DEFAULT_URL = "http://www.python.org/packages/source/s/setuptools/"
23
version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir
25
"""Automatically find/download setuptools and make it available on sys.path
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.
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
38
if setuptools.__version__ == '0.0.1':
40
"You have an obsolete version of setuptools installed. Please\n"
41
"remove it from your system entirely before rerunning this script."
46
egg = download_setuptools(version, download_base, to_dir)
47
sys.path.insert(0, egg)
48
import setuptools; setuptools.bootstrap_install_from = egg
52
pkg_resources.require("setuptools>="+version)
54
except pkg_resources.VersionConflict:
55
# XXX could we install in a subprocess here?
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."
63
def download_setuptools(
64
version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir
66
"""Download setuptools from a specified location and return its filename
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.
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)
78
if not os.path.exists(saveto): # Avoid repeated downloads
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.
86
dst = open(saveto,"wb")
92
return os.path.realpath(saveto)
94
def main(argv, version=DEFAULT_VERSION):
95
"""Install or upgrade setuptools and EasyInstall"""
100
import tempfile, shutil
101
tmpdir = tempfile.mkdtemp(prefix="easy_install-")
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])
108
shutil.rmtree(tmpdir)
110
if setuptools.__version__ == '0.0.1':
111
# tell the user to uninstall obsolete version
112
use_setuptools(version)
114
req = "setuptools>="+version
117
pkg_resources.require(req)
118
except pkg_resources.VersionConflict:
120
from setuptools.command.easy_install import main
122
from easy_install import main
123
main(list(argv)+[download_setuptools()])
124
sys.exit(0) # try to force an exit
127
from setuptools.command.easy_install import main
130
print "Setuptools version",version,"or greater has been installed."
131
print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
132
if __name__=='__main__':