From 9f60561d6f5038cf58d3cd1dd6f2baccfc630f0d Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 14 Oct 2018 13:17:38 -0700 Subject: [PATCH] Replace resources with importlib_resources --- pre_commit/commands/install_uninstall.py | 5 +-- pre_commit/languages/ruby.py | 21 ++++++----- pre_commit/make_archives.py | 3 +- pre_commit/resources/__init__.py | 0 .../.npmignore => empty_template_.npmignore} | 0 .../Cargo.toml => empty_template_Cargo.toml} | 0 .../main.go => empty_template_main.go} | 0 .../main.rs => empty_template_main.rs} | 0 ...ckage.json => empty_template_package.json} | 0 ...template_pre_commit_dummy_package.gemspec} | 0 .../setup.py => empty_template_setup.py} | 0 pre_commit/store.py | 13 +++++-- pre_commit/util.py | 37 +++++++------------ setup.py | 6 +-- testing/fixtures.py | 20 +++++++++- tests/commands/install_uninstall_test.py | 7 ++-- tests/store_test.py | 9 +++++ 17 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 pre_commit/resources/__init__.py rename pre_commit/resources/{empty_template/.npmignore => empty_template_.npmignore} (100%) rename pre_commit/resources/{empty_template/Cargo.toml => empty_template_Cargo.toml} (100%) rename pre_commit/resources/{empty_template/main.go => empty_template_main.go} (100%) rename pre_commit/resources/{empty_template/main.rs => empty_template_main.rs} (100%) rename pre_commit/resources/{empty_template/package.json => empty_template_package.json} (100%) rename pre_commit/resources/{empty_template/pre_commit_dummy_package.gemspec => empty_template_pre_commit_dummy_package.gemspec} (100%) rename pre_commit/resources/{empty_template/setup.py => empty_template_setup.py} (100%) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index d76a6c1a..d3133060 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -12,7 +12,7 @@ from pre_commit.repository import repositories from pre_commit.util import cmd_output from pre_commit.util import make_executable from pre_commit.util import mkdirp -from pre_commit.util import resource_filename +from pre_commit.util import resource_text logger = logging.getLogger(__name__) @@ -80,8 +80,7 @@ def install( } with io.open(hook_path, 'w') as hook_file: - with io.open(resource_filename('hook-tmpl')) as f: - contents = f.read() + contents = resource_text('hook-tmpl') before, rest = contents.split(TEMPLATE_START) to_template, after = rest.split(TEMPLATE_END) diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py index 3bd7130d..bef3fe38 100644 --- a/pre_commit/languages/ruby.py +++ b/pre_commit/languages/ruby.py @@ -11,7 +11,7 @@ from pre_commit.envcontext import Var 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 resource_bytesio from pre_commit.xargs import xargs @@ -47,22 +47,23 @@ def in_env(prefix, language_version): # pragma: windows no cover yield +def _extract_resource(filename, dest): + with resource_bytesio(filename) as bio: + with tarfile.open(fileobj=bio) as tf: + tf.extractall(dest) + + def _install_rbenv(prefix, version='default'): # pragma: windows no cover directory = helpers.environment_dir(ENVIRONMENT_DIR, version) - with tarfile.open(resource_filename('rbenv.tar.gz')) as tf: - tf.extractall(prefix.path('.')) + _extract_resource('rbenv.tar.gz', prefix.path('.')) shutil.move(prefix.path('rbenv'), prefix.path(directory)) # 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: - tf.extractall(prefix.path(directory, 'plugins')) - - # ruby-build - with tarfile.open(resource_filename('ruby-build.tar.gz')) as tf: - tf.extractall(prefix.path(directory, 'plugins')) + plugins_dir = prefix.path(directory, 'plugins') + _extract_resource('ruby-download.tar.gz', plugins_dir) + _extract_resource('ruby-build.tar.gz', plugins_dir) activate_path = prefix.path(directory, 'bin', 'activate') with io.open(activate_path, 'w') as activate_file: diff --git a/pre_commit/make_archives.py b/pre_commit/make_archives.py index e85a8f4a..865ef061 100644 --- a/pre_commit/make_archives.py +++ b/pre_commit/make_archives.py @@ -8,7 +8,6 @@ import tarfile from pre_commit import output from pre_commit.util import cmd_output -from pre_commit.util import resource_filename from pre_commit.util import rmtree from pre_commit.util import tmpdir @@ -56,7 +55,7 @@ def make_archive(name, repo, ref, destdir): def main(argv=None): parser = argparse.ArgumentParser() - parser.add_argument('--dest', default=resource_filename()) + parser.add_argument('--dest', default='pre_commit/resources') args = parser.parse_args(argv) for archive_name, repo, ref in REPOS: output.write_line('Making {}.tar.gz for {}@{}'.format( diff --git a/pre_commit/resources/__init__.py b/pre_commit/resources/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pre_commit/resources/empty_template/.npmignore b/pre_commit/resources/empty_template_.npmignore similarity index 100% rename from pre_commit/resources/empty_template/.npmignore rename to pre_commit/resources/empty_template_.npmignore diff --git a/pre_commit/resources/empty_template/Cargo.toml b/pre_commit/resources/empty_template_Cargo.toml similarity index 100% rename from pre_commit/resources/empty_template/Cargo.toml rename to pre_commit/resources/empty_template_Cargo.toml diff --git a/pre_commit/resources/empty_template/main.go b/pre_commit/resources/empty_template_main.go similarity index 100% rename from pre_commit/resources/empty_template/main.go rename to pre_commit/resources/empty_template_main.go diff --git a/pre_commit/resources/empty_template/main.rs b/pre_commit/resources/empty_template_main.rs similarity index 100% rename from pre_commit/resources/empty_template/main.rs rename to pre_commit/resources/empty_template_main.rs diff --git a/pre_commit/resources/empty_template/package.json b/pre_commit/resources/empty_template_package.json similarity index 100% rename from pre_commit/resources/empty_template/package.json rename to pre_commit/resources/empty_template_package.json diff --git a/pre_commit/resources/empty_template/pre_commit_dummy_package.gemspec b/pre_commit/resources/empty_template_pre_commit_dummy_package.gemspec similarity index 100% rename from pre_commit/resources/empty_template/pre_commit_dummy_package.gemspec rename to pre_commit/resources/empty_template_pre_commit_dummy_package.gemspec diff --git a/pre_commit/resources/empty_template/setup.py b/pre_commit/resources/empty_template_setup.py similarity index 100% rename from pre_commit/resources/empty_template/setup.py rename to pre_commit/resources/empty_template_setup.py diff --git a/pre_commit/store.py b/pre_commit/store.py index 07702fb5..f3096fcd 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -11,9 +11,8 @@ import pre_commit.constants as C from pre_commit import file_lock from pre_commit.util import clean_path_on_failure from pre_commit.util import cmd_output -from pre_commit.util import copy_tree_to_path from pre_commit.util import no_git_env -from pre_commit.util import resource_filename +from pre_commit.util import resource_text logger = logging.getLogger('pre_commit') @@ -149,9 +148,17 @@ class Store(object): return self._new_repo(repo, ref, deps, clone_strategy) + LOCAL_RESOURCES = ( + 'Cargo.toml', 'main.go', 'main.rs', '.npmignore', 'package.json', + 'pre_commit_dummy_package.gemspec', 'setup.py', + ) + def make_local(self, deps): def make_local_strategy(directory): - copy_tree_to_path(resource_filename('empty_template'), directory) + for resource in self.LOCAL_RESOURCES: + contents = resource_text('empty_template_{}'.format(resource)) + with io.open(os.path.join(directory, resource), 'w') as f: + f.write(contents) env = no_git_env() name, email = 'pre-commit', 'asottile+pre-commit@umich.edu' diff --git a/pre_commit/util.py b/pre_commit/util.py index 55210f10..963461d1 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -7,14 +7,21 @@ import os.path import shutil import stat import subprocess +import sys import tempfile -import pkg_resources import six from pre_commit import five from pre_commit import parse_shebang +if sys.version_info >= (3, 7): # pragma: no cover (PY37+) + from importlib.resources import open_binary + from importlib.resources import read_text +else: # pragma: no cover (