Update ez_setup.py to 4.0.1

This commit is contained in:
Florian Bruhin 2014-06-10 08:24:04 +02:00
parent 6a81710c71
commit 909ae83155

View File

@ -26,12 +26,17 @@ import contextlib
from distutils import log from distutils import log
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
try: try:
from site import USER_SITE from site import USER_SITE
except ImportError: except ImportError:
USER_SITE = None USER_SITE = None
DEFAULT_VERSION = "3.4.4" DEFAULT_VERSION = "4.0.1"
DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/" DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
def _python_cmd(*args): def _python_cmd(*args):
@ -64,17 +69,24 @@ def _build_egg(egg, archive_filename, to_dir):
raise IOError('Could not build the egg.') raise IOError('Could not build the egg.')
def get_zip_class(): class ContextualZipFile(zipfile.ZipFile):
""" """
Supplement ZipFile class to support context manager for Python 2.6 Supplement ZipFile class to support context manager for Python 2.6
""" """
class ContextualZipFile(zipfile.ZipFile):
def __enter__(self): def __enter__(self):
return self return self
def __exit__(self, type, value, traceback):
self.close def __exit__(self, type, value, traceback):
return zipfile.ZipFile if hasattr(zipfile.ZipFile, '__exit__') else \ self.close()
ContextualZipFile
def __new__(cls, *args, **kwargs):
"""
Construct a ZipFile or ContextualZipFile as appropriate
"""
if hasattr(zipfile.ZipFile, '__exit__'):
return zipfile.ZipFile(*args, **kwargs)
return super(ContextualZipFile, cls).__new__(cls)
@contextlib.contextmanager @contextlib.contextmanager
@ -85,7 +97,7 @@ def archive_context(filename):
old_wd = os.getcwd() old_wd = os.getcwd()
try: try:
os.chdir(tmpdir) os.chdir(tmpdir)
with get_zip_class()(filename) as archive: with ContextualZipFile(filename) as archive:
archive.extractall() archive.extractall()
# going in the directory # going in the directory
@ -166,10 +178,16 @@ def download_file_powershell(url, target):
trust). Raise an exception if the command cannot complete. trust). Raise an exception if the command cannot complete.
""" """
target = os.path.abspath(target) target = os.path.abspath(target)
ps_cmd = (
"[System.Net.WebRequest]::DefaultWebProxy.Credentials = "
"[System.Net.CredentialCache]::DefaultCredentials; "
"(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)"
% vars()
)
cmd = [ cmd = [
'powershell', 'powershell',
'-Command', '-Command',
"(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(), ps_cmd,
] ]
_clean_check(cmd, target) _clean_check(cmd, target)
@ -177,14 +195,11 @@ def has_powershell():
if platform.system() != 'Windows': if platform.system() != 'Windows':
return False return False
cmd = ['powershell', '-Command', 'echo test'] cmd = ['powershell', '-Command', 'echo test']
devnull = open(os.path.devnull, 'wb') with open(os.path.devnull, 'wb') as devnull:
try:
try: try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull) subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except Exception: except Exception:
return False return False
finally:
devnull.close()
return True return True
download_file_powershell.viable = has_powershell download_file_powershell.viable = has_powershell
@ -195,14 +210,11 @@ def download_file_curl(url, target):
def has_curl(): def has_curl():
cmd = ['curl', '--version'] cmd = ['curl', '--version']
devnull = open(os.path.devnull, 'wb') with open(os.path.devnull, 'wb') as devnull:
try:
try: try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull) subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except Exception: except Exception:
return False return False
finally:
devnull.close()
return True return True
download_file_curl.viable = has_curl download_file_curl.viable = has_curl
@ -213,14 +225,11 @@ def download_file_wget(url, target):
def has_wget(): def has_wget():
cmd = ['wget', '--version'] cmd = ['wget', '--version']
devnull = open(os.path.devnull, 'wb') with open(os.path.devnull, 'wb') as devnull:
try:
try: try:
subprocess.check_call(cmd, stdout=devnull, stderr=devnull) subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
except Exception: except Exception:
return False return False
finally:
devnull.close()
return True return True
download_file_wget.viable = has_wget download_file_wget.viable = has_wget
@ -230,37 +239,28 @@ def download_file_insecure(url, target):
Use Python to download the file, even though it cannot authenticate the Use Python to download the file, even though it cannot authenticate the
connection. connection.
""" """
src = urlopen(url)
try: try:
from urllib.request import urlopen # Read all the data in one block.
except ImportError:
from urllib2 import urlopen
src = dst = None
try:
src = urlopen(url)
# Read/write all in one block, so we don't create a corrupt file
# if the download is interrupted.
data = src.read() data = src.read()
dst = open(target, "wb")
dst.write(data)
finally: finally:
if src: src.close()
src.close()
if dst: # Write all the data in one block to avoid creating a partial file.
dst.close() with open(target, "wb") as dst:
dst.write(data)
download_file_insecure.viable = lambda: True download_file_insecure.viable = lambda: True
def get_best_downloader(): def get_best_downloader():
downloaders = [ downloaders = (
download_file_powershell, download_file_powershell,
download_file_curl, download_file_curl,
download_file_wget, download_file_wget,
download_file_insecure, download_file_insecure,
] )
viable_downloaders = (dl for dl in downloaders if dl.viable())
for dl in downloaders: return next(viable_downloaders, None)
if dl.viable():
return dl
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 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):