Replace resources with importlib_resources

This commit is contained in:
Anthony Sottile
2018-10-14 13:17:38 -07:00
parent ebe5132576
commit 9f60561d6f
17 changed files with 72 additions and 49 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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(

View File

View File

@@ -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'

View File

@@ -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 (<PY37)
from importlib_resources import open_binary
from importlib_resources import read_text
def mkdirp(path):
try:
@@ -84,10 +91,12 @@ def tmpdir():
rmtree(tempdir)
def resource_filename(*segments):
return pkg_resources.resource_filename(
'pre_commit', os.path.join('resources', *segments),
)
def resource_bytesio(filename):
return open_binary('pre_commit.resources', filename)
def resource_text(filename):
return read_text('pre_commit.resources', filename)
def make_executable(filename):
@@ -195,24 +204,6 @@ def rmtree(path):
shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)
def copy_tree_to_path(src_dir, dest_dir):
"""Copies all of the things inside src_dir to an already existing dest_dir.
This looks eerily similar to shutil.copytree, but copytree has no option
for not creating dest_dir.
"""
names = os.listdir(src_dir)
for name in names:
srcname = os.path.join(src_dir, name)
destname = os.path.join(dest_dir, name)
if os.path.isdir(srcname):
shutil.copytree(srcname, destname)
else:
shutil.copy(srcname, destname)
def parse_version(s):
"""poor man's version comparison"""
return tuple(int(p) for p in s.split('.'))

View File

@@ -29,10 +29,9 @@ setup(
packages=find_packages(exclude=('tests*', 'testing*')),
package_data={
'pre_commit': [
'resources/*-tmpl',
'resources/hook-tmpl',
'resources/*.tar.gz',
'resources/empty_template/*',
'resources/empty_template/.npmignore',
'resources/empty_template_*',
],
},
install_requires=[
@@ -48,6 +47,7 @@ setup(
'toml',
'virtualenv',
],
extras_require={':python_version<"3.7"': ['importlib-resources']},
entry_points={
'console_scripts': [
'pre-commit = pre_commit.main:main',

View File

@@ -4,6 +4,7 @@ from __future__ import unicode_literals
import contextlib
import io
import os.path
import shutil
from collections import OrderedDict
from aspy.yaml import ordered_dump
@@ -16,10 +17,27 @@ from pre_commit import git
from pre_commit.clientlib import CONFIG_SCHEMA
from pre_commit.clientlib import load_manifest
from pre_commit.util import cmd_output
from pre_commit.util import copy_tree_to_path
from testing.util import get_resource_path
def copy_tree_to_path(src_dir, dest_dir):
"""Copies all of the things inside src_dir to an already existing dest_dir.
This looks eerily similar to shutil.copytree, but copytree has no option
for not creating dest_dir.
"""
names = os.listdir(src_dir)
for name in names:
srcname = os.path.join(src_dir, name)
destname = os.path.join(dest_dir, name)
if os.path.isdir(srcname):
shutil.copytree(srcname, destname)
else:
shutil.copy(srcname, destname)
def git_dir(tempdir_factory):
path = tempdir_factory.get()
cmd_output('git', 'init', path)

View File

@@ -22,7 +22,7 @@ from pre_commit.runner import Runner
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
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
from testing.fixtures import remove_config_from_repo
@@ -36,7 +36,7 @@ def test_is_not_script():
def test_is_script():
assert is_our_script(resource_filename('hook-tmpl'))
assert is_our_script('pre_commit/resources/hook-tmpl')
def test_is_previous_pre_commit(tmpdir):
@@ -415,8 +415,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
runner = Runner(path, C.CONFIG_FILE)
# Install a script that looks like our old script
with io.open(resource_filename('hook-tmpl')) as f:
pre_commit_contents = f.read()
pre_commit_contents = resource_text('hook-tmpl')
new_contents = pre_commit_contents.replace(
CURRENT_HASH, PRIOR_HASHES[-1],
)

View File

@@ -153,3 +153,12 @@ def test_require_created_when_directory_exists_but_not_db(store):
os.makedirs(store.directory)
store.require_created()
assert os.path.exists(store.db_path)
def test_local_resources_reflects_reality():
on_disk = {
res[len('empty_template_'):]
for res in os.listdir('pre_commit/resources')
if res.startswith('empty_template_')
}
assert on_disk == set(Store.LOCAL_RESOURCES)