From 57cc50e0ad9bd2a43ad146f7d1814f31943b342b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 31 Aug 2016 16:24:30 -0700 Subject: [PATCH] Remove tarfile_open (tarfile open in 2.7+) --- pre_commit/languages/ruby.py | 8 ++++---- pre_commit/make_archives.py | 4 ++-- pre_commit/util.py | 11 ----------- tests/make_archives_test.py | 4 ++-- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py index 28ad3771..3b8ddd35 100644 --- a/pre_commit/languages/ruby.py +++ b/pre_commit/languages/ruby.py @@ -4,6 +4,7 @@ import contextlib 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.languages import helpers 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 index ff6d3bda..35bb303c 100644 --- a/pre_commit/make_archives.py +++ b/pre_commit/make_archives.py @@ -3,12 +3,12 @@ from __future__ import print_function 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 index 4b478563..7be7582b 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -8,7 +8,6 @@ import os.path 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 diff --git a/tests/make_archives_test.py b/tests/make_archives_test.py index fc267b63..f3636b53 100644 --- a/tests/make_archives_test.py +++ b/tests/make_archives_test.py @@ -2,6 +2,7 @@ from __future__ import absolute_import from __future__ import unicode_literals import os.path +import tarfile import mock import pytest @@ -9,7 +10,6 @@ import pytest 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