This file can also be run as a script to install or upgrade setuptools.
"""
+from __future__ import absolute_import
import os
import shutil
import sys
import optparse
import subprocess
import platform
-import textwrap
import contextlib
from distutils import log
try:
from urllib.request import urlopen
except ImportError:
- from urllib2 import urlopen
+ from six.moves.urllib import urlopen
try:
from site import USER_SITE
DEFAULT_VERSION = "6.1"
DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
+
def _python_cmd(*args):
"""
Return True if the command succeeded.
def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
- to_dir=os.curdir, download_delay=15):
+ to_dir=os.curdir, download_delay=15):
to_dir = os.path.abspath(to_dir)
rep_modules = 'pkg_resources', 'setuptools'
imported = set(sys.modules).intersection(rep_modules)
return _do_download(version, download_base, to_dir, download_delay)
except pkg_resources.VersionConflict as VC_err:
if imported:
- msg = textwrap.dedent("""
- The required version of setuptools (>={version}) is not available,
- and can't be installed while this script is running. Please
- install a more recent version first, using
- 'easy_install -U setuptools'.
-
- (Currently using {VC_err.args[0]!r})
- """).format(VC_err=VC_err, version=version)
+ msg = """\
+The required version of setuptools (>={version}) is not available,
+and can't be installed while this script is running. Please
+install a more recent version first, using
+'easy_install -U setuptools'.
+
+(Currently using {VC_err.args[0]!r})
+""".format(VC_err=VC_err, version=version)
sys.stderr.write(msg)
sys.exit(2)
del pkg_resources, sys.modules['pkg_resources']
return _do_download(version, download_base, to_dir, download_delay)
+
def _clean_check(cmd, target):
"""
Run the command to download target. If the command fails, clean up before
os.unlink(target)
raise
+
def download_file_powershell(url, target):
"""
Download the file at url to target using Powershell (which will validate
]
_clean_check(cmd, target)
+
def has_powershell():
if platform.system() != 'Windows':
return False
return False
return True
+
download_file_powershell.viable = has_powershell
+
def download_file_curl(url, target):
cmd = ['curl', url, '--silent', '--output', target]
_clean_check(cmd, target)
+
def has_curl():
cmd = ['curl', '--version']
with open(os.path.devnull, 'wb') as devnull:
return False
return True
+
download_file_curl.viable = has_curl
+
def download_file_wget(url, target):
cmd = ['wget', url, '--quiet', '--output-document', target]
_clean_check(cmd, target)
+
def has_wget():
cmd = ['wget', '--version']
with open(os.path.devnull, 'wb') as devnull:
return False
return True
+
download_file_wget.viable = has_wget
+
def download_file_insecure(url, target):
"""
Use Python to download the file, even though it cannot authenticate the
with open(target, "wb") as dst:
dst.write(data)
+
download_file_insecure.viable = lambda: True
+
def get_best_downloader():
downloaders = (
download_file_powershell,
viable_downloaders = (dl for dl in downloaders if dl.viable())
return next(viable_downloaders, None)
+
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
- to_dir=os.curdir, delay=15, downloader_factory=get_best_downloader):
+ to_dir=os.curdir, delay=15,
+ downloader_factory=get_best_downloader):
"""
Download setuptools from a specified location and return its filename
downloader(url, saveto)
return os.path.realpath(saveto)
+
def _build_install_args(options):
"""
Build the arguments to 'python setup.py install' on the setuptools package
"""
return ['--user'] if options.user_install else []
+
def _parse_args():
"""
Parse the command line for options
# positional arguments are ignored
return options
+
def main():
"""Install or upgrade setuptools and EasyInstall"""
options = _parse_args()
)
return _install(archive, _build_install_args(options))
+
if __name__ == '__main__':
sys.exit(main())