[MediaWiki-commits] [Gerrit] Update ez_setup.py to v19.1.1 - change (pywikibot/core)

2016-05-02 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Update ez_setup.py to v19.1.1
..


Update ez_setup.py to v19.1.1

Supports Python 2.6+.
Does not use `cmp`, which is removed in Python 3
causing pyflakes on Python 3 to report it as an error.

Bug: T134063
Change-Id: I08f02ba7f4c6d7ea6c32e3845024a5bd00f58b35
---
M ez_setup.py
1 file changed, 251 insertions(+), 206 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ez_setup.py b/ez_setup.py
index b02f3f1..9715bdc 100644
--- a/ez_setup.py
+++ b/ez_setup.py
@@ -1,66 +1,55 @@
-#!python
-"""Bootstrap setuptools installation
+#!/usr/bin/env python
 
-If you want to use setuptools in your package's setup.py, just include this
-file in the same directory with it, and add this to the top of your setup.py::
-
-from ez_setup import use_setuptools
-use_setuptools()
-
-If you want to require a specific version of setuptools, set a download
-mirror, or use an alternate download directory, you can do so by supplying
-the appropriate options to ``use_setuptools()``.
-
-This file can also be run as a script to install or upgrade setuptools.
 """
+Setuptools bootstrapping installer.
+
+Run this script to install or upgrade setuptools.
+"""
+
 import os
 import shutil
 import sys
 import tempfile
-import tarfile
+import zipfile
 import optparse
 import subprocess
 import platform
+import textwrap
+import contextlib
+import json
+import codecs
 
 from distutils import log
+
+try:
+from urllib.request import urlopen
+except ImportError:
+from urllib2 import urlopen
 
 try:
 from site import USER_SITE
 except ImportError:
 USER_SITE = None
 
-DEFAULT_VERSION = "1.1.6"
+LATEST = object()
+DEFAULT_VERSION = LATEST
 DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/;
+DEFAULT_SAVE_DIR = os.curdir
+
 
 def _python_cmd(*args):
+"""
+Execute a command.
+
+Return True if the command succeeded.
+"""
 args = (sys.executable,) + args
 return subprocess.call(args) == 0
 
-def _check_call_py24(cmd, *args, **kwargs):
-res = subprocess.call(cmd, *args, **kwargs)
-class CalledProcessError(Exception):
-pass
-if not res == 0:
-msg = "Command '%s' return non-zero exit status %d" % (cmd, res)
-raise CalledProcessError(msg)
-vars(subprocess).setdefault('check_call', _check_call_py24)
 
-def _install(tarball, install_args=()):
-# extracting the tarball
-tmpdir = tempfile.mkdtemp()
-log.warn('Extracting in %s', tmpdir)
-old_wd = os.getcwd()
-try:
-os.chdir(tmpdir)
-tar = tarfile.open(tarball)
-_extractall(tar)
-tar.close()
-
-# going in the directory
-subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
-os.chdir(subdir)
-log.warn('Now working in %s', subdir)
-
+def _install(archive_filename, install_args=()):
+"""Install Setuptools."""
+with archive_context(archive_filename):
 # installing
 log.warn('Installing Setuptools')
 if not _python_cmd('setup.py', 'install', *install_args):
@@ -68,198 +57,266 @@
 log.warn('See the error message above.')
 # exitcode will be 2
 return 2
-finally:
-os.chdir(old_wd)
-shutil.rmtree(tmpdir)
 
 
-def _build_egg(egg, tarball, to_dir):
-# extracting the tarball
-tmpdir = tempfile.mkdtemp()
-log.warn('Extracting in %s', tmpdir)
-old_wd = os.getcwd()
-try:
-os.chdir(tmpdir)
-tar = tarfile.open(tarball)
-_extractall(tar)
-tar.close()
-
-# going in the directory
-subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
-os.chdir(subdir)
-log.warn('Now working in %s', subdir)
-
+def _build_egg(egg, archive_filename, to_dir):
+"""Build Setuptools egg."""
+with archive_context(archive_filename):
 # building an egg
 log.warn('Building a Setuptools egg in %s', to_dir)
 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
-
-finally:
-os.chdir(old_wd)
-shutil.rmtree(tmpdir)
 # returning the result
 log.warn(egg)
 if not os.path.exists(egg):
 raise IOError('Could not build the egg.')
 
 
+class ContextualZipFile(zipfile.ZipFile):
+
+"""Supplement ZipFile class to support context manager for Python 2.6."""
+
+def __enter__(self):
+return self
+
+def __exit__(self, type, value, traceback):
+self.close()
+
+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
+def archive_context(filename):
+"""
+Unzip filename to a temporary 

[MediaWiki-commits] [Gerrit] Update ez_setup.py to v19.1.1 - change (pywikibot/core)

2015-12-24 Thread John Vandenberg (Code Review)
John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/260908

Change subject: Update ez_setup.py to v19.1.1
..

Update ez_setup.py to v19.1.1

Supports Python 2.6+.
Does not use `cmp`, which is removed in Python 3
causing pyflakes on Python 3 to report it as an error.

Change-Id: I08f02ba7f4c6d7ea6c32e3845024a5bd00f58b35
---
M ez_setup.py
1 file changed, 251 insertions(+), 206 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/08/260908/1

diff --git a/ez_setup.py b/ez_setup.py
index b02f3f1..9715bdc 100644
--- a/ez_setup.py
+++ b/ez_setup.py
@@ -1,66 +1,55 @@
-#!python
-"""Bootstrap setuptools installation
+#!/usr/bin/env python
 
-If you want to use setuptools in your package's setup.py, just include this
-file in the same directory with it, and add this to the top of your setup.py::
-
-from ez_setup import use_setuptools
-use_setuptools()
-
-If you want to require a specific version of setuptools, set a download
-mirror, or use an alternate download directory, you can do so by supplying
-the appropriate options to ``use_setuptools()``.
-
-This file can also be run as a script to install or upgrade setuptools.
 """
+Setuptools bootstrapping installer.
+
+Run this script to install or upgrade setuptools.
+"""
+
 import os
 import shutil
 import sys
 import tempfile
-import tarfile
+import zipfile
 import optparse
 import subprocess
 import platform
+import textwrap
+import contextlib
+import json
+import codecs
 
 from distutils import log
+
+try:
+from urllib.request import urlopen
+except ImportError:
+from urllib2 import urlopen
 
 try:
 from site import USER_SITE
 except ImportError:
 USER_SITE = None
 
-DEFAULT_VERSION = "1.1.6"
+LATEST = object()
+DEFAULT_VERSION = LATEST
 DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/;
+DEFAULT_SAVE_DIR = os.curdir
+
 
 def _python_cmd(*args):
+"""
+Execute a command.
+
+Return True if the command succeeded.
+"""
 args = (sys.executable,) + args
 return subprocess.call(args) == 0
 
-def _check_call_py24(cmd, *args, **kwargs):
-res = subprocess.call(cmd, *args, **kwargs)
-class CalledProcessError(Exception):
-pass
-if not res == 0:
-msg = "Command '%s' return non-zero exit status %d" % (cmd, res)
-raise CalledProcessError(msg)
-vars(subprocess).setdefault('check_call', _check_call_py24)
 
-def _install(tarball, install_args=()):
-# extracting the tarball
-tmpdir = tempfile.mkdtemp()
-log.warn('Extracting in %s', tmpdir)
-old_wd = os.getcwd()
-try:
-os.chdir(tmpdir)
-tar = tarfile.open(tarball)
-_extractall(tar)
-tar.close()
-
-# going in the directory
-subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
-os.chdir(subdir)
-log.warn('Now working in %s', subdir)
-
+def _install(archive_filename, install_args=()):
+"""Install Setuptools."""
+with archive_context(archive_filename):
 # installing
 log.warn('Installing Setuptools')
 if not _python_cmd('setup.py', 'install', *install_args):
@@ -68,198 +57,266 @@
 log.warn('See the error message above.')
 # exitcode will be 2
 return 2
-finally:
-os.chdir(old_wd)
-shutil.rmtree(tmpdir)
 
 
-def _build_egg(egg, tarball, to_dir):
-# extracting the tarball
-tmpdir = tempfile.mkdtemp()
-log.warn('Extracting in %s', tmpdir)
-old_wd = os.getcwd()
-try:
-os.chdir(tmpdir)
-tar = tarfile.open(tarball)
-_extractall(tar)
-tar.close()
-
-# going in the directory
-subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
-os.chdir(subdir)
-log.warn('Now working in %s', subdir)
-
+def _build_egg(egg, archive_filename, to_dir):
+"""Build Setuptools egg."""
+with archive_context(archive_filename):
 # building an egg
 log.warn('Building a Setuptools egg in %s', to_dir)
 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
-
-finally:
-os.chdir(old_wd)
-shutil.rmtree(tmpdir)
 # returning the result
 log.warn(egg)
 if not os.path.exists(egg):
 raise IOError('Could not build the egg.')
 
 
+class ContextualZipFile(zipfile.ZipFile):
+
+"""Supplement ZipFile class to support context manager for Python 2.6."""
+
+def __enter__(self):
+return self
+
+def __exit__(self, type, value, traceback):
+self.close()
+
+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
+def archive_context(filename):
+"""