upgrade hooks, pyupgrade pre-commit

This commit is contained in:
Anthony Sottile 2020-01-08 20:49:09 -08:00
parent 764c765d29
commit 30c1e8289f
91 changed files with 176 additions and 437 deletions

View file

@ -1,11 +1,7 @@
from __future__ import print_function
from __future__ import unicode_literals
import collections
import os.path
import re
import six
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
@ -64,7 +60,7 @@ def _check_hooks_still_exist_at_rev(repo_config, info, store):
path = store.clone(repo_config['repo'], info.rev)
manifest = load_manifest(os.path.join(path, C.MANIFEST_FILE))
except InvalidManifestError as e:
raise RepositoryCannotBeUpdatedError(six.text_type(e))
raise RepositoryCannotBeUpdatedError(str(e))
# See if any of our hooks were deleted with the new commits
hooks = {hook['id'] for hook in repo_config['hooks']}
@ -108,7 +104,7 @@ def _write_new_config(path, rev_infos):
new_rev_s = ordered_dump({'rev': rev_info.rev}, **C.YAML_DUMP_KWARGS)
new_rev = new_rev_s.split(':', 1)[1].strip()
if rev_info.frozen is not None:
comment = ' # frozen: {}'.format(rev_info.frozen)
comment = f' # frozen: {rev_info.frozen}'
elif match.group(4).strip().startswith('# frozen:'):
comment = ''
else:
@ -138,7 +134,7 @@ def autoupdate(config_file, store, tags_only, freeze, repos=()):
rev_infos.append(None)
continue
output.write('Updating {} ... '.format(info.repo))
output.write(f'Updating {info.repo} ... ')
new_info = info.update(tags_only=tags_only, freeze=freeze)
try:
_check_hooks_still_exist_at_rev(repo_config, new_info, store)
@ -151,10 +147,10 @@ def autoupdate(config_file, store, tags_only, freeze, repos=()):
if new_info.rev != info.rev:
changed = True
if new_info.frozen:
updated_to = '{} (frozen)'.format(new_info.frozen)
updated_to = f'{new_info.frozen} (frozen)'
else:
updated_to = new_info.rev
msg = 'updating {} -> {}.'.format(info.rev, updated_to)
msg = f'updating {info.rev} -> {updated_to}.'
output.write_line(msg)
rev_infos.append(new_info)
else:

View file

@ -1,6 +1,3 @@
from __future__ import print_function
from __future__ import unicode_literals
import os.path
from pre_commit import output
@ -12,5 +9,5 @@ def clean(store):
for directory in (store.directory, legacy_path):
if os.path.exists(directory):
rmtree(directory)
output.write_line('Cleaned {}.'.format(directory))
output.write_line(f'Cleaned {directory}.')
return 0

View file

@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import os.path
import pre_commit.constants as C
@ -79,5 +76,5 @@ def _gc_repos(store):
def gc(store):
with store.exclusive_lock():
repos_removed = _gc_repos(store)
output.write_line('{} repo(s) removed.'.format(repos_removed))
output.write_line(f'{repos_removed} repo(s) removed.')
return 0

View file

@ -23,5 +23,5 @@ def init_templatedir(config_file, store, directory, hook_types):
if configured_path != dest:
logger.warning('`init.templateDir` not set to the target directory')
logger.warning(
'maybe `git config --global init.templateDir {}`?'.format(dest),
f'maybe `git config --global init.templateDir {dest}`?',
)

View file

@ -1,7 +1,3 @@
from __future__ import print_function
from __future__ import unicode_literals
import io
import itertools
import logging
import os.path
@ -36,13 +32,13 @@ TEMPLATE_END = '# end templated\n'
def _hook_paths(hook_type, git_dir=None):
git_dir = git_dir if git_dir is not None else git.get_git_dir()
pth = os.path.join(git_dir, 'hooks', hook_type)
return pth, '{}.legacy'.format(pth)
return pth, f'{pth}.legacy'
def is_our_script(filename):
if not os.path.exists(filename): # pragma: windows no cover (symlink)
return False
with io.open(filename) as f:
with open(filename) as f:
contents = f.read()
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
@ -63,7 +59,7 @@ def shebang():
break
else:
py = 'python'
return '#!/usr/bin/env {}'.format(py)
return f'#!/usr/bin/env {py}'
def _install_hook_script(
@ -94,7 +90,7 @@ def _install_hook_script(
'SKIP_ON_MISSING_CONFIG': skip_on_missing_config,
}
with io.open(hook_path, 'w') as hook_file:
with open(hook_path, 'w') as hook_file:
contents = resource_text('hook-tmpl')
before, rest = contents.split(TEMPLATE_START)
to_template, after = rest.split(TEMPLATE_END)
@ -108,7 +104,7 @@ def _install_hook_script(
hook_file.write(TEMPLATE_END + after)
make_executable(hook_path)
output.write_line('pre-commit installed at {}'.format(hook_path))
output.write_line(f'pre-commit installed at {hook_path}')
def install(
@ -149,11 +145,11 @@ def _uninstall_hook_script(hook_type): # type: (str) -> None
return
os.remove(hook_path)
output.write_line('{} uninstalled'.format(hook_type))
output.write_line(f'{hook_type} uninstalled')
if os.path.exists(legacy_path):
os.rename(legacy_path, hook_path)
output.write_line('Restored previous hooks to {}'.format(hook_path))
output.write_line(f'Restored previous hooks to {hook_path}')
def uninstall(hook_types):

View file

@ -1,7 +1,3 @@
from __future__ import print_function
from __future__ import unicode_literals
import io
import re
import yaml
@ -47,14 +43,14 @@ def _migrate_sha_to_rev(contents):
def migrate_config(config_file, quiet=False):
with io.open(config_file) as f:
with open(config_file) as f:
orig_contents = contents = f.read()
contents = _migrate_map(contents)
contents = _migrate_sha_to_rev(contents)
if contents != orig_contents:
with io.open(config_file, 'w') as f:
with open(config_file, 'w') as f:
f.write(contents)
print('Configuration has been migrated.')

View file

@ -1,5 +1,3 @@
from __future__ import unicode_literals
import logging
import os
import re
@ -32,7 +30,7 @@ def filter_by_include_exclude(names, include, exclude):
]
class Classifier(object):
class Classifier:
def __init__(self, filenames):
# on windows we normalize all filenames to use forward slashes
# this makes it easier to filter using the `files:` regex
@ -136,13 +134,13 @@ def _run_single_hook(classifier, hook, skips, cols, verbose, use_color):
output.write_line(color.format_color(status, print_color, use_color))
if verbose or hook.verbose or retcode or files_modified:
_subtle_line('- hook id: {}'.format(hook.id), use_color)
_subtle_line(f'- hook id: {hook.id}', use_color)
if (verbose or hook.verbose) and duration is not None:
_subtle_line('- duration: {}s'.format(duration), use_color)
_subtle_line(f'- duration: {duration}s', use_color)
if retcode:
_subtle_line('- exit code: {}'.format(retcode), use_color)
_subtle_line(f'- exit code: {retcode}', use_color)
# Print a message if failing due to file modifications
if files_modified:

View file

@ -1,8 +1,3 @@
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
# TODO: maybe `git ls-remote git://github.com/pre-commit/pre-commit-hooks` to
# determine the latest revision? This adds ~200ms from my tests (and is
# significantly faster than https:// or http://). For now, periodically

View file

@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import collections
import logging
import os.path