2 """Bootstrap setuptools installation
4 To use setuptools in your package's setup.py, include this
5 file in the same directory and add this to the top of your setup.py::
7 from ez_setup import use_setuptools
10 To require a specific version of setuptools, set a download
11 mirror, or use an alternate download directory, simply supply
12 the appropriate options to ``use_setuptools()``.
14 This file can also be run as a script to install or upgrade setuptools.
16 from __future__ import absolute_import
27 from distutils import log
30 from urllib.request import urlopen
32 from six.moves.urllib import urlopen
35 from site import USER_SITE
39 DEFAULT_VERSION = "6.1"
40 DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
43 def _python_cmd(*args):
45 Return True if the command succeeded.
47 args = (sys.executable,) + args
48 return subprocess.call(args) == 0
51 def _install(archive_filename, install_args=()):
52 with archive_context(archive_filename):
54 log.warn('Installing Setuptools')
55 if not _python_cmd('setup.py', 'install', *install_args):
56 log.warn('Something went wrong during the installation.')
57 log.warn('See the error message above.')
62 def _build_egg(egg, archive_filename, to_dir):
63 with archive_context(archive_filename):
65 log.warn('Building a Setuptools egg in %s', to_dir)
66 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
67 # returning the result
69 if not os.path.exists(egg):
70 raise IOError('Could not build the egg.')
73 class ContextualZipFile(zipfile.ZipFile):
75 Supplement ZipFile class to support context manager for Python 2.6
81 def __exit__(self, type, value, traceback):
84 def __new__(cls, *args, **kwargs):
86 Construct a ZipFile or ContextualZipFile as appropriate
88 if hasattr(zipfile.ZipFile, '__exit__'):
89 return zipfile.ZipFile(*args, **kwargs)
90 return super(ContextualZipFile, cls).__new__(cls)
93 @contextlib.contextmanager
94 def archive_context(filename):
95 # extracting the archive
96 tmpdir = tempfile.mkdtemp()
97 log.warn('Extracting in %s', tmpdir)
101 with ContextualZipFile(filename) as archive:
104 # going in the directory
105 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
107 log.warn('Now working in %s', subdir)
112 shutil.rmtree(tmpdir)
115 def _do_download(version, download_base, to_dir, download_delay):
116 egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
117 % (version, sys.version_info[0], sys.version_info[1]))
118 if not os.path.exists(egg):
119 archive = download_setuptools(version, download_base,
120 to_dir, download_delay)
121 _build_egg(egg, archive, to_dir)
122 sys.path.insert(0, egg)
124 # Remove previously-imported pkg_resources if present (see
125 # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
126 if 'pkg_resources' in sys.modules:
127 del sys.modules['pkg_resources']
130 setuptools.bootstrap_install_from = egg
133 def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
134 to_dir=os.curdir, download_delay=15):
135 to_dir = os.path.abspath(to_dir)
136 rep_modules = 'pkg_resources', 'setuptools'
137 imported = set(sys.modules).intersection(rep_modules)
141 return _do_download(version, download_base, to_dir, download_delay)
143 pkg_resources.require("setuptools>=" + version)
145 except pkg_resources.DistributionNotFound:
146 return _do_download(version, download_base, to_dir, download_delay)
147 except pkg_resources.VersionConflict as VC_err:
150 The required version of setuptools (>={version}) is not available,
151 and can't be installed while this script is running. Please
152 install a more recent version first, using
153 'easy_install -U setuptools'.
155 (Currently using {VC_err.args[0]!r})
156 """.format(VC_err=VC_err, version=version)
157 sys.stderr.write(msg)
160 # otherwise, reload ok
161 del pkg_resources, sys.modules['pkg_resources']
162 return _do_download(version, download_base, to_dir, download_delay)
165 def _clean_check(cmd, target):
167 Run the command to download target. If the command fails, clean up before
168 re-raising the error.
171 subprocess.check_call(cmd)
172 except subprocess.CalledProcessError:
173 if os.access(target, os.F_OK):
178 def download_file_powershell(url, target):
180 Download the file at url to target using Powershell (which will validate
181 trust). Raise an exception if the command cannot complete.
183 target = os.path.abspath(target)
185 "[System.Net.WebRequest]::DefaultWebProxy.Credentials = "
186 "[System.Net.CredentialCache]::DefaultCredentials; "
187 "(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)"
195 _clean_check(cmd, target)
198 def has_powershell():
199 if platform.system() != 'Windows':
201 cmd = ['powershell', '-Command', 'echo test']
202 with open(os.path.devnull, 'wb') as devnull:
204 subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
210 download_file_powershell.viable = has_powershell
213 def download_file_curl(url, target):
214 cmd = ['curl', url, '--silent', '--output', target]
215 _clean_check(cmd, target)
219 cmd = ['curl', '--version']
220 with open(os.path.devnull, 'wb') as devnull:
222 subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
228 download_file_curl.viable = has_curl
231 def download_file_wget(url, target):
232 cmd = ['wget', url, '--quiet', '--output-document', target]
233 _clean_check(cmd, target)
237 cmd = ['wget', '--version']
238 with open(os.path.devnull, 'wb') as devnull:
240 subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
246 download_file_wget.viable = has_wget
249 def download_file_insecure(url, target):
251 Use Python to download the file, even though it cannot authenticate the
256 # Read all the data in one block.
261 # Write all the data in one block to avoid creating a partial file.
262 with open(target, "wb") as dst:
266 download_file_insecure.viable = lambda: True
269 def get_best_downloader():
271 download_file_powershell,
274 download_file_insecure,
276 viable_downloaders = (dl for dl in downloaders if dl.viable())
277 return next(viable_downloaders, None)
280 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
281 to_dir=os.curdir, delay=15,
282 downloader_factory=get_best_downloader):
284 Download setuptools from a specified location and return its filename
286 `version` should be a valid setuptools version number that is available
287 as an sdist for download under the `download_base` URL (which should end
288 with a '/'). `to_dir` is the directory where the egg will be downloaded.
289 `delay` is the number of seconds to pause before an actual download
292 ``downloader_factory`` should be a function taking no arguments and
293 returning a function for downloading a URL to a target.
295 # making sure we use the absolute path
296 to_dir = os.path.abspath(to_dir)
297 zip_name = "setuptools-%s.zip" % version
298 url = download_base + zip_name
299 saveto = os.path.join(to_dir, zip_name)
300 if not os.path.exists(saveto): # Avoid repeated downloads
301 log.warn("Downloading %s", url)
302 downloader = downloader_factory()
303 downloader(url, saveto)
304 return os.path.realpath(saveto)
307 def _build_install_args(options):
309 Build the arguments to 'python setup.py install' on the setuptools package
311 return ['--user'] if options.user_install else []
316 Parse the command line for options
318 parser = optparse.OptionParser()
320 '--user', dest='user_install', action='store_true', default=False,
321 help='install in user site package (requires Python 2.6 or later)')
323 '--download-base', dest='download_base', metavar="URL",
325 help='alternative URL from where to download the setuptools package')
327 '--insecure', dest='downloader_factory', action='store_const',
328 const=lambda: download_file_insecure, default=get_best_downloader,
329 help='Use internal, non-validating downloader'
332 '--version', help="Specify which version to download",
333 default=DEFAULT_VERSION,
335 options, args = parser.parse_args()
336 # positional arguments are ignored
341 """Install or upgrade setuptools and EasyInstall"""
342 options = _parse_args()
343 archive = download_setuptools(
344 version=options.version,
345 download_base=options.download_base,
346 downloader_factory=options.downloader_factory,
348 return _install(archive, _build_install_args(options))
351 if __name__ == '__main__':