Skip to content

Commit 9f60561

Browse files
committed
Replace resources with importlib_resources
1 parent ebe5132 commit 9f60561

17 files changed

+72
-49
lines changed

pre_commit/commands/install_uninstall.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pre_commit.util import cmd_output
1313
from pre_commit.util import make_executable
1414
from pre_commit.util import mkdirp
15-
from pre_commit.util import resource_filename
15+
from pre_commit.util import resource_text
1616

1717

1818
logger = logging.getLogger(__name__)
@@ -80,8 +80,7 @@ def install(
8080
}
8181

8282
with io.open(hook_path, 'w') as hook_file:
83-
with io.open(resource_filename('hook-tmpl')) as f:
84-
contents = f.read()
83+
contents = resource_text('hook-tmpl')
8584
before, rest = contents.split(TEMPLATE_START)
8685
to_template, after = rest.split(TEMPLATE_END)
8786

pre_commit/languages/ruby.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pre_commit.languages import helpers
1212
from pre_commit.util import CalledProcessError
1313
from pre_commit.util import clean_path_on_failure
14-
from pre_commit.util import resource_filename
14+
from pre_commit.util import resource_bytesio
1515
from pre_commit.xargs import xargs
1616

1717

@@ -47,22 +47,23 @@ def in_env(prefix, language_version): # pragma: windows no cover
4747
yield
4848

4949

50+
def _extract_resource(filename, dest):
51+
with resource_bytesio(filename) as bio:
52+
with tarfile.open(fileobj=bio) as tf:
53+
tf.extractall(dest)
54+
55+
5056
def _install_rbenv(prefix, version='default'): # pragma: windows no cover
5157
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
5258

53-
with tarfile.open(resource_filename('rbenv.tar.gz')) as tf:
54-
tf.extractall(prefix.path('.'))
59+
_extract_resource('rbenv.tar.gz', prefix.path('.'))
5560
shutil.move(prefix.path('rbenv'), prefix.path(directory))
5661

5762
# Only install ruby-build if the version is specified
5863
if version != 'default':
59-
# ruby-download
60-
with tarfile.open(resource_filename('ruby-download.tar.gz')) as tf:
61-
tf.extractall(prefix.path(directory, 'plugins'))
62-
63-
# ruby-build
64-
with tarfile.open(resource_filename('ruby-build.tar.gz')) as tf:
65-
tf.extractall(prefix.path(directory, 'plugins'))
64+
plugins_dir = prefix.path(directory, 'plugins')
65+
_extract_resource('ruby-download.tar.gz', plugins_dir)
66+
_extract_resource('ruby-build.tar.gz', plugins_dir)
6667

6768
activate_path = prefix.path(directory, 'bin', 'activate')
6869
with io.open(activate_path, 'w') as activate_file:

pre_commit/make_archives.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from pre_commit import output
1010
from pre_commit.util import cmd_output
11-
from pre_commit.util import resource_filename
1211
from pre_commit.util import rmtree
1312
from pre_commit.util import tmpdir
1413

@@ -56,7 +55,7 @@ def make_archive(name, repo, ref, destdir):
5655

5756
def main(argv=None):
5857
parser = argparse.ArgumentParser()
59-
parser.add_argument('--dest', default=resource_filename())
58+
parser.add_argument('--dest', default='pre_commit/resources')
6059
args = parser.parse_args(argv)
6160
for archive_name, repo, ref in REPOS:
6261
output.write_line('Making {}.tar.gz for {}@{}'.format(

pre_commit/resources/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

pre_commit/resources/empty_template/pre_commit_dummy_package.gemspec renamed to pre_commit/resources/empty_template_pre_commit_dummy_package.gemspec

File renamed without changes.

0 commit comments

Comments
 (0)