X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=ez_setup.py;h=6771f3619ea038497cd1de28c319017fc6b6ed4e;hb=326ca99184b896d75266737466516846df29b447;hp=a693849f934d04e9ea90fc590273e20807376ee8;hpb=2d131cd335b35914133d3dd8d1d8f5741fc38eb0;p=yardstick.git diff --git a/ez_setup.py b/ez_setup.py index a693849f9..6771f3619 100644 --- a/ez_setup.py +++ b/ez_setup.py @@ -13,6 +13,7 @@ the appropriate options to ``use_setuptools()``. 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 @@ -21,7 +22,6 @@ import zipfile import optparse import subprocess import platform -import textwrap import contextlib from distutils import log @@ -29,7 +29,7 @@ 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 @@ -39,6 +39,7 @@ except ImportError: DEFAULT_VERSION = "6.1" DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/" + def _python_cmd(*args): """ Return True if the command succeeded. @@ -130,7 +131,7 @@ def _do_download(version, download_base, to_dir, download_delay): 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) @@ -145,14 +146,14 @@ def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 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) @@ -160,6 +161,7 @@ def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 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 @@ -172,6 +174,7 @@ def _clean_check(cmd, target): os.unlink(target) raise + def download_file_powershell(url, target): """ Download the file at url to target using Powershell (which will validate @@ -191,6 +194,7 @@ def download_file_powershell(url, target): ] _clean_check(cmd, target) + def has_powershell(): if platform.system() != 'Windows': return False @@ -202,12 +206,15 @@ def has_powershell(): 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: @@ -217,12 +224,15 @@ def has_curl(): 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: @@ -232,8 +242,10 @@ def has_wget(): 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 @@ -250,8 +262,10 @@ def download_file_insecure(url, target): with open(target, "wb") as dst: dst.write(data) + download_file_insecure.viable = lambda: True + def get_best_downloader(): downloaders = ( download_file_powershell, @@ -262,8 +276,10 @@ def get_best_downloader(): 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 @@ -287,12 +303,14 @@ def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 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 @@ -318,6 +336,7 @@ def _parse_args(): # positional arguments are ignored return options + def main(): """Install or upgrade setuptools and EasyInstall""" options = _parse_args() @@ -328,5 +347,6 @@ def main(): ) return _install(archive, _build_install_args(options)) + if __name__ == '__main__': sys.exit(main())