Repository: pre-commit/pre-commit

Issue:
Remove python2.6 tarfile_open compatibility



Discussion:
See #399


Resolving patch:
diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py
--- a/pre_commit/languages/ruby.py
+++ b/pre_commit/languages/ruby.py
@@ -4,6 +4,7 @@
 import io
 import os.path
 import shutil
+import tarfile
 
 from pre_commit.envcontext import envcontext
 from pre_commit.envcontext import Var
@@ -11,7 +12,6 @@
 from pre_commit.util import CalledProcessError
 from pre_commit.util import clean_path_on_failure
 from pre_commit.util import resource_filename
-from pre_commit.util import tarfile_open
 from pre_commit.xargs import xargs
 
 
@@ -46,7 +46,7 @@ def in_env(repo_cmd_runner, language_version):
 def _install_rbenv(repo_cmd_runner, version='default'):
     directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
 
-    with tarfile_open(resource_filename('rbenv.tar.gz')) as tf:
+    with tarfile.open(resource_filename('rbenv.tar.gz')) as tf:
         tf.extractall(repo_cmd_runner.path('.'))
     shutil.move(
         repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory),
@@ -55,11 +55,11 @@ def _install_rbenv(repo_cmd_runner, version='default'):
     # Only install ruby-build if the version is specified
     if version != 'default':
         # ruby-download
-        with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf:
+        with tarfile.open(resource_filename('ruby-download.tar.gz')) as tf:
             tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
 
         # ruby-build
-        with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf:
+        with tarfile.open(resource_filename('ruby-build.tar.gz')) as tf:
             tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
 
     activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')
diff --git a/pre_commit/make_archives.py b/pre_commit/make_archives.py
--- a/pre_commit/make_archives.py
+++ b/pre_commit/make_archives.py
@@ -3,12 +3,12 @@
 from __future__ import unicode_literals
 
 import os.path
+import tarfile
 
 from pre_commit import five
 from pre_commit.util import cmd_output
 from pre_commit.util import cwd
 from pre_commit.util import rmtree
-from pre_commit.util import tarfile_open
 from pre_commit.util import tmpdir
 
 
@@ -53,7 +53,7 @@ def make_archive(name, repo, ref, destdir):
         # runtime
         rmtree(os.path.join(tempdir, '.git'))
 
-        with tarfile_open(five.n(output_path), 'w|gz') as tf:
+        with tarfile.open(five.n(output_path), 'w|gz') as tf:
             tf.add(tempdir, name)
 
     return output_path
diff --git a/pre_commit/util.py b/pre_commit/util.py
--- a/pre_commit/util.py
+++ b/pre_commit/util.py
@@ -8,7 +8,6 @@
 import shutil
 import stat
 import subprocess
-import tarfile
 import tempfile
 
 import pkg_resources
@@ -82,16 +81,6 @@ def no_git_env():
     )
 
 
-@contextlib.contextmanager
-def tarfile_open(*args, **kwargs):
-    """Compatibility layer because python2.6"""
-    tf = tarfile.open(*args, **kwargs)
-    try:
-        yield tf
-    finally:
-        tf.close()
-
-
 @contextlib.contextmanager
 def tmpdir():
     """Contextmanager to create a temporary directory.  It will be cleaned up


Test patch:
diff --git a/tests/make_archives_test.py b/tests/make_archives_test.py
--- a/tests/make_archives_test.py
+++ b/tests/make_archives_test.py
@@ -2,6 +2,7 @@
 from __future__ import unicode_literals
 
 import os.path
+import tarfile
 
 import mock
 import pytest
@@ -9,7 +10,6 @@
 from pre_commit import make_archives
 from pre_commit.util import cmd_output
 from pre_commit.util import cwd
-from pre_commit.util import tarfile_open
 from testing.fixtures import git_dir
 from testing.util import get_head_sha
 from testing.util import skipif_slowtests_false
@@ -41,7 +41,7 @@ def test_make_archive(tempdir_factory):
     extract_dir = tempdir_factory.get()
 
     # Extract the tar
-    with tarfile_open(archive_path) as tf:
+    with tarfile.open(archive_path) as tf:
         tf.extractall(extract_dir)
 
     # Verify the contents of the tar