mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Replace resources with importlib_resources
This commit is contained in:
parent
ebe5132576
commit
9f60561d6f
17 changed files with 72 additions and 49 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
0
pre_commit/resources/__init__.py
Normal file
0
pre_commit/resources/__init__.py
Normal 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'
|
||||
|
|
|
|||
|
|
@ -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('.'))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue