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