mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
upgrade hooks, pyupgrade pre-commit
This commit is contained in:
parent
764c765d29
commit
30c1e8289f
91 changed files with 176 additions and 437 deletions
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from pre_commit.main import main
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import functools
|
||||
import logging
|
||||
|
|
@ -106,7 +103,7 @@ LOCAL = 'local'
|
|||
META = 'meta'
|
||||
|
||||
|
||||
class MigrateShaToRev(object):
|
||||
class MigrateShaToRev:
|
||||
key = 'rev'
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -202,7 +199,7 @@ META_HOOK_DICT = cfgv.Map(
|
|||
if item.key in {'name', 'language', 'entry'} else
|
||||
item
|
||||
for item in MANIFEST_HOOK_DICT.items
|
||||
])
|
||||
]),
|
||||
)
|
||||
CONFIG_HOOK_DICT = cfgv.Map(
|
||||
'Hook', 'id',
|
||||
|
|
@ -217,7 +214,7 @@ CONFIG_HOOK_DICT = cfgv.Map(
|
|||
cfgv.OptionalNoDefault(item.key, item.check_fn)
|
||||
for item in MANIFEST_HOOK_DICT.items
|
||||
if item.key != 'id'
|
||||
]
|
||||
],
|
||||
)
|
||||
CONFIG_REPO_DICT = cfgv.Map(
|
||||
'Repository', 'repo',
|
||||
|
|
@ -243,7 +240,7 @@ CONFIG_REPO_DICT = cfgv.Map(
|
|||
DEFAULT_LANGUAGE_VERSION = cfgv.Map(
|
||||
'DefaultLanguageVersion', None,
|
||||
cfgv.NoAdditionalKeys(all_languages),
|
||||
*[cfgv.Optional(x, cfgv.check_string, C.DEFAULT) for x in all_languages]
|
||||
*[cfgv.Optional(x, cfgv.check_string, C.DEFAULT) for x in all_languages],
|
||||
)
|
||||
CONFIG_SCHEMA = cfgv.Map(
|
||||
'Config', None,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
|
@ -8,7 +6,7 @@ if os.name == 'nt': # pragma: no cover (windows)
|
|||
from pre_commit.color_windows import enable_virtual_terminal_processing
|
||||
try:
|
||||
enable_virtual_terminal_processing()
|
||||
except WindowsError:
|
||||
except OSError:
|
||||
terminal_supports_color = False
|
||||
|
||||
RED = '\033[41m'
|
||||
|
|
@ -34,7 +32,7 @@ def format_color(text, color, use_color_setting):
|
|||
if not use_color_setting:
|
||||
return text
|
||||
else:
|
||||
return '{}{}{}'.format(color, text, NORMAL)
|
||||
return f'{color}{text}{NORMAL}'
|
||||
|
||||
|
||||
COLOR_CHOICES = ('auto', 'always', 'never')
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from ctypes import POINTER
|
||||
from ctypes import windll
|
||||
from ctypes import WinError
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}`?',
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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.')
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import collections
|
||||
import logging
|
||||
import os.path
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import sys
|
||||
|
||||
if sys.version_info < (3, 8): # pragma: no cover (<PY38)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import collections
|
||||
import contextlib
|
||||
import os
|
||||
|
|
|
|||
|
|
@ -1,14 +1,8 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os.path
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
import six
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import five
|
||||
from pre_commit import output
|
||||
|
|
@ -23,7 +17,7 @@ def _to_bytes(exc):
|
|||
try:
|
||||
return bytes(exc)
|
||||
except Exception:
|
||||
return six.text_type(exc).encode('UTF-8')
|
||||
return str(exc).encode('UTF-8')
|
||||
|
||||
|
||||
def _log_and_exit(msg, exc, formatted):
|
||||
|
|
@ -35,7 +29,7 @@ def _log_and_exit(msg, exc, formatted):
|
|||
output.write_line(error_msg)
|
||||
store = Store()
|
||||
log_path = os.path.join(store.directory, 'pre-commit.log')
|
||||
output.write_line('Check the log at {}'.format(log_path))
|
||||
output.write_line(f'Check the log at {log_path}')
|
||||
|
||||
with open(log_path, 'wb') as log:
|
||||
def _log_line(*s): # type: (*str) -> None
|
||||
|
|
@ -44,13 +38,13 @@ def _log_and_exit(msg, exc, formatted):
|
|||
_log_line('### version information')
|
||||
_log_line()
|
||||
_log_line('```')
|
||||
_log_line('pre-commit version: {}'.format(C.VERSION))
|
||||
_log_line(f'pre-commit version: {C.VERSION}')
|
||||
_log_line('sys.version:')
|
||||
for line in sys.version.splitlines():
|
||||
_log_line(' {}'.format(line))
|
||||
_log_line('sys.executable: {}'.format(sys.executable))
|
||||
_log_line('os.name: {}'.format(os.name))
|
||||
_log_line('sys.platform: {}'.format(sys.platform))
|
||||
_log_line(f' {line}')
|
||||
_log_line(f'sys.executable: {sys.executable}')
|
||||
_log_line(f'os.name: {os.name}')
|
||||
_log_line(f'sys.platform: {sys.platform}')
|
||||
_log_line('```')
|
||||
_log_line()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import errno
|
||||
|
||||
|
|
@ -18,12 +15,12 @@ try: # pragma: no cover (windows)
|
|||
def _locked(fileno, blocked_cb):
|
||||
try:
|
||||
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region)
|
||||
except IOError:
|
||||
except OSError:
|
||||
blocked_cb()
|
||||
while True:
|
||||
try:
|
||||
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)
|
||||
except IOError as e:
|
||||
except OSError as e:
|
||||
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
|
||||
# flag is specified and the file cannot be locked after 10
|
||||
# attempts.
|
||||
|
|
@ -48,7 +45,7 @@ except ImportError: # pragma: windows no cover
|
|||
def _locked(fileno, blocked_cb):
|
||||
try:
|
||||
fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except IOError: # pragma: no cover (tests are single-threaded)
|
||||
except OSError: # pragma: no cover (tests are single-threaded)
|
||||
blocked_cb()
|
||||
fcntl.flock(fileno, fcntl.LOCK_EX)
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import six
|
||||
|
||||
|
||||
def to_text(s):
|
||||
return s if isinstance(s, six.text_type) else s.decode('UTF-8')
|
||||
return s if isinstance(s, str) else s.decode('UTF-8')
|
||||
|
||||
|
||||
def to_bytes(s):
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import logging
|
||||
import os.path
|
||||
import sys
|
||||
|
|
@ -127,7 +125,7 @@ def get_changed_files(new, old):
|
|||
return zsplit(
|
||||
cmd_output(
|
||||
'git', 'diff', '--name-only', '--no-ext-diff', '-z',
|
||||
'{}...{}'.format(old, new),
|
||||
f'{old}...{new}',
|
||||
)[1],
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit.languages import conda
|
||||
from pre_commit.languages import docker
|
||||
from pre_commit.languages import docker_image
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ def install_environment(prefix, version, additional_dependencies):
|
|||
if additional_dependencies:
|
||||
cmd_output_b(
|
||||
'conda', 'install', '-p', env_dir, *additional_dependencies,
|
||||
cwd=prefix.prefix_dir
|
||||
cwd=prefix.prefix_dir,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
|
|
@ -24,7 +21,7 @@ def md5(s): # pragma: windows no cover
|
|||
|
||||
def docker_tag(prefix): # pragma: windows no cover
|
||||
md5sum = md5(os.path.basename(prefix.prefix_dir)).lower()
|
||||
return 'pre-commit-{}'.format(md5sum)
|
||||
return f'pre-commit-{md5sum}'
|
||||
|
||||
|
||||
def docker_is_running(): # pragma: windows no cover
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.languages.docker import assert_docker_available
|
||||
from pre_commit.languages.docker import docker_cmd
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os.path
|
||||
import sys
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import multiprocessing
|
||||
import os
|
||||
import random
|
||||
|
||||
import six
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.util import cmd_output_b
|
||||
from pre_commit.xargs import xargs
|
||||
|
|
@ -21,13 +17,13 @@ def environment_dir(ENVIRONMENT_DIR, language_version):
|
|||
if ENVIRONMENT_DIR is None:
|
||||
return None
|
||||
else:
|
||||
return '{}-{}'.format(ENVIRONMENT_DIR, language_version)
|
||||
return f'{ENVIRONMENT_DIR}-{language_version}'
|
||||
|
||||
|
||||
def assert_version_default(binary, version):
|
||||
if version != C.DEFAULT:
|
||||
raise AssertionError(
|
||||
'For now, pre-commit requires system-installed {}'.format(binary),
|
||||
f'For now, pre-commit requires system-installed {binary}',
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -68,10 +64,7 @@ def target_concurrency(hook):
|
|||
def _shuffled(seq):
|
||||
"""Deterministically shuffle identically under both py2 + py3."""
|
||||
fixed_random = random.Random()
|
||||
if six.PY2: # pragma: no cover (py2)
|
||||
fixed_random.seed(FIXED_RANDOM_SEED)
|
||||
else: # pragma: no cover (py3)
|
||||
fixed_random.seed(FIXED_RANDOM_SEED, version=1)
|
||||
fixed_random.seed(FIXED_RANDOM_SEED, version=1)
|
||||
|
||||
seq = list(seq)
|
||||
random.shuffle(seq, random=fixed_random.random)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import sys
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import sys
|
||||
|
|
@ -22,7 +19,7 @@ def _process_filename_by_line(pattern, filename):
|
|||
for line_no, line in enumerate(f, start=1):
|
||||
if pattern.search(line):
|
||||
retv = 1
|
||||
output.write('{}:{}:'.format(filename, line_no))
|
||||
output.write(f'{filename}:{line_no}:')
|
||||
output.write_line(line.rstrip(b'\r\n'))
|
||||
return retv
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import sys
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.util import CalledProcessError
|
||||
|
|
@ -13,10 +10,7 @@ ENVIRONMENT_DIR = 'py_venv'
|
|||
|
||||
|
||||
def get_default_version(): # pragma: no cover (version specific)
|
||||
if sys.version_info < (3,):
|
||||
return 'python3'
|
||||
else:
|
||||
return python.get_default_version()
|
||||
return python.get_default_version()
|
||||
|
||||
|
||||
def orig_py_exe(exe): # pragma: no cover (platform specific)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import os.path
|
||||
import shutil
|
||||
import tarfile
|
||||
|
|
@ -66,7 +63,7 @@ def _install_rbenv(prefix, version=C.DEFAULT): # pragma: windows no cover
|
|||
_extract_resource('ruby-build.tar.gz', plugins_dir)
|
||||
|
||||
activate_path = prefix.path(directory, 'bin', 'activate')
|
||||
with io.open(activate_path, 'w') as activate_file:
|
||||
with open(activate_path, 'w') as activate_file:
|
||||
# This is similar to how you would install rbenv to your home directory
|
||||
# However we do a couple things to make the executables exposed and
|
||||
# configure it to work in our directory.
|
||||
|
|
@ -86,7 +83,7 @@ def _install_rbenv(prefix, version=C.DEFAULT): # pragma: windows no cover
|
|||
|
||||
# If we aren't using the system ruby, add a version here
|
||||
if version != C.DEFAULT:
|
||||
activate_file.write('export RBENV_VERSION="{}"\n'.format(version))
|
||||
activate_file.write(f'export RBENV_VERSION="{version}"\n')
|
||||
|
||||
|
||||
def _install_ruby(prefix, version): # pragma: windows no cover
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os.path
|
||||
|
||||
|
|
@ -85,7 +83,7 @@ def install_environment(prefix, version, additional_dependencies):
|
|||
for package in packages_to_install:
|
||||
cmd_output_b(
|
||||
'cargo', 'install', '--bins', '--root', directory, *package,
|
||||
cwd=prefix.prefix_dir
|
||||
cwd=prefix.prefix_dir,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
|
||||
|
|
@ -19,14 +17,14 @@ LOG_LEVEL_COLORS = {
|
|||
|
||||
class LoggingHandler(logging.Handler):
|
||||
def __init__(self, use_color):
|
||||
super(LoggingHandler, self).__init__()
|
||||
super().__init__()
|
||||
self.use_color = use_color
|
||||
|
||||
def emit(self, record):
|
||||
output.write_line(
|
||||
'{} {}'.format(
|
||||
color.format_color(
|
||||
'[{}]'.format(record.levelname),
|
||||
f'[{record.levelname}]',
|
||||
LOG_LEVEL_COLORS[record.levelname],
|
||||
self.use_color,
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
|
|
@ -57,7 +55,7 @@ def _add_config_option(parser):
|
|||
|
||||
class AppendReplaceDefault(argparse.Action):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(AppendReplaceDefault, self).__init__(*args, **kwargs)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.appended = False
|
||||
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
|
|
@ -154,7 +152,7 @@ def main(argv=None):
|
|||
parser.add_argument(
|
||||
'-V', '--version',
|
||||
action='version',
|
||||
version='%(prog)s {}'.format(C.VERSION),
|
||||
version=f'%(prog)s {C.VERSION}',
|
||||
)
|
||||
|
||||
subparsers = parser.add_subparsers(dest='command')
|
||||
|
|
@ -254,7 +252,7 @@ def main(argv=None):
|
|||
_add_run_options(run_parser)
|
||||
|
||||
sample_config_parser = subparsers.add_parser(
|
||||
'sample-config', help='Produce a sample {} file'.format(C.CONFIG_FILE),
|
||||
'sample-config', help=f'Produce a sample {C.CONFIG_FILE} file',
|
||||
)
|
||||
_add_color_option(sample_config_parser)
|
||||
_add_config_option(sample_config_parser)
|
||||
|
|
@ -345,11 +343,11 @@ def main(argv=None):
|
|||
return uninstall(hook_types=args.hook_types)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
'Command {} not implemented.'.format(args.command),
|
||||
f'Command {args.command} not implemented.',
|
||||
)
|
||||
|
||||
raise AssertionError(
|
||||
'Command {} failed to exit with a returncode'.format(args.command),
|
||||
f'Command {args.command} failed to exit with a returncode',
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import os.path
|
||||
import tarfile
|
||||
|
|
@ -59,7 +55,7 @@ def main(argv=None):
|
|||
args = parser.parse_args(argv)
|
||||
for archive_name, repo, ref in REPOS:
|
||||
output.write_line(
|
||||
'Making {}.tar.gz for {}@{}'.format(archive_name, repo, ref),
|
||||
f'Making {archive_name}.tar.gz for {repo}@{ref}',
|
||||
)
|
||||
make_archive(archive_name, repo, ref, args.dest)
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ def check_all_hooks_match_files(config_file):
|
|||
if hook.always_run or hook.language == 'fail':
|
||||
continue
|
||||
elif not classifier.filenames_for_hook(hook):
|
||||
print('{} does not apply to this repository'.format(hook.id))
|
||||
print(f'{hook.id} does not apply to this repository')
|
||||
retv = 1
|
||||
|
||||
return retv
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import re
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import sys
|
||||
|
||||
from pre_commit import color
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os.path
|
||||
|
||||
from identify.identify import parse_shebang_from_file
|
||||
|
|
@ -44,7 +41,7 @@ def find_executable(exe, _environ=None):
|
|||
|
||||
def normexe(orig):
|
||||
def _error(msg):
|
||||
raise ExecutableNotFoundError('Executable `{}` {}'.format(orig, msg))
|
||||
raise ExecutableNotFoundError(f'Executable `{orig}` {msg}')
|
||||
|
||||
if os.sep not in orig and (not os.altsep or os.altsep not in orig):
|
||||
exe = find_executable(orig)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import collections
|
||||
import os.path
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import collections
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
|
@ -36,14 +33,14 @@ def _read_state(prefix, venv):
|
|||
if not os.path.exists(filename):
|
||||
return None
|
||||
else:
|
||||
with io.open(filename) as f:
|
||||
with open(filename) as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def _write_state(prefix, venv, state):
|
||||
state_filename = _state_filename(prefix, venv)
|
||||
staging = state_filename + 'staging'
|
||||
with io.open(staging, 'w') as state_file:
|
||||
with open(staging, 'w') as state_file:
|
||||
state_file.write(five.to_text(json.dumps(state)))
|
||||
# Move the file into place atomically to indicate we've installed
|
||||
os.rename(staging, state_filename)
|
||||
|
|
@ -82,7 +79,7 @@ class Hook(collections.namedtuple('Hook', ('src', 'prefix') + _KEYS)):
|
|||
)
|
||||
|
||||
def install(self):
|
||||
logger.info('Installing environment for {}.'.format(self.src))
|
||||
logger.info(f'Installing environment for {self.src}.')
|
||||
logger.info('Once installed this environment will be reused.')
|
||||
logger.info('This may take a few minutes...')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
"""File generated by pre-commit: https://pre-commit.com"""
|
||||
from __future__ import print_function
|
||||
|
||||
import distutils.spawn
|
||||
import os
|
||||
import subprocess
|
||||
|
|
@ -64,7 +62,7 @@ def _run_legacy():
|
|||
else:
|
||||
stdin = None
|
||||
|
||||
legacy_hook = os.path.join(HERE, '{}.legacy'.format(HOOK_TYPE))
|
||||
legacy_hook = os.path.join(HERE, f'{HOOK_TYPE}.legacy')
|
||||
if os.access(legacy_hook, os.X_OK):
|
||||
cmd = _norm_exe(legacy_hook) + (legacy_hook,) + tuple(sys.argv[1:])
|
||||
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE if stdin else None)
|
||||
|
|
@ -136,7 +134,7 @@ def _pre_push(stdin):
|
|||
# ancestors not found in remote
|
||||
ancestors = subprocess.check_output((
|
||||
'git', 'rev-list', local_sha, '--topo-order', '--reverse',
|
||||
'--not', '--remotes={}'.format(remote),
|
||||
'--not', f'--remotes={remote}',
|
||||
)).decode().strip()
|
||||
if not ancestors:
|
||||
continue
|
||||
|
|
@ -148,7 +146,7 @@ def _pre_push(stdin):
|
|||
# pushing the whole tree including root commit
|
||||
opts = ('--all-files',)
|
||||
else:
|
||||
cmd = ('git', 'rev-parse', '{}^'.format(first_ancestor))
|
||||
cmd = ('git', 'rev-parse', f'{first_ancestor}^')
|
||||
source = subprocess.check_output(cmd).decode().strip()
|
||||
opts = ('--origin', local_sha, '--source', source)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import logging
|
||||
import os.path
|
||||
import time
|
||||
|
|
@ -54,11 +51,11 @@ def _unstaged_changes_cleared(patch_dir):
|
|||
patch_filename = os.path.join(patch_dir, patch_filename)
|
||||
logger.warning('Unstaged files detected.')
|
||||
logger.info(
|
||||
'Stashing unstaged files to {}.'.format(patch_filename),
|
||||
f'Stashing unstaged files to {patch_filename}.',
|
||||
)
|
||||
# Save the current unstaged changes as a patch
|
||||
mkdirp(patch_dir)
|
||||
with io.open(patch_filename, 'wb') as patch_file:
|
||||
with open(patch_filename, 'wb') as patch_file:
|
||||
patch_file.write(diff_stdout_binary)
|
||||
|
||||
# Clear the working directory of unstaged changes
|
||||
|
|
@ -79,7 +76,7 @@ def _unstaged_changes_cleared(patch_dir):
|
|||
# Roll back the changes made by hooks.
|
||||
cmd_output_b('git', 'checkout', '--', '.')
|
||||
_git_apply(patch_filename)
|
||||
logger.info('Restored changes from {}.'.format(patch_filename))
|
||||
logger.info(f'Restored changes from {patch_filename}.')
|
||||
else:
|
||||
# There weren't any staged files so we don't need to do anything
|
||||
# special
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import logging
|
||||
import os.path
|
||||
import sqlite3
|
||||
|
|
@ -34,7 +31,7 @@ def _get_default_directory():
|
|||
)
|
||||
|
||||
|
||||
class Store(object):
|
||||
class Store:
|
||||
get_default_directory = staticmethod(_get_default_directory)
|
||||
|
||||
def __init__(self, directory=None):
|
||||
|
|
@ -43,7 +40,7 @@ class Store(object):
|
|||
|
||||
if not os.path.exists(self.directory):
|
||||
mkdirp(self.directory)
|
||||
with io.open(os.path.join(self.directory, 'README'), 'w') as f:
|
||||
with open(os.path.join(self.directory, 'README'), 'w') as f:
|
||||
f.write(
|
||||
'This directory is maintained by the pre-commit project.\n'
|
||||
'Learn more: https://github.com/pre-commit/pre-commit\n',
|
||||
|
|
@ -122,7 +119,7 @@ class Store(object):
|
|||
if result: # pragma: no cover (race)
|
||||
return result
|
||||
|
||||
logger.info('Initializing environment for {}.'.format(repo))
|
||||
logger.info(f'Initializing environment for {repo}.')
|
||||
|
||||
directory = tempfile.mkdtemp(prefix='repo', dir=self.directory)
|
||||
with clean_path_on_failure(directory):
|
||||
|
|
@ -179,8 +176,8 @@ class Store(object):
|
|||
def make_local(self, deps):
|
||||
def make_local_strategy(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:
|
||||
contents = resource_text(f'empty_template_{resource}')
|
||||
with open(os.path.join(directory, resource), 'w') as f:
|
||||
f.write(contents)
|
||||
|
||||
env = git.no_git_env()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import errno
|
||||
import os.path
|
||||
|
|
@ -9,8 +7,6 @@ import subprocess
|
|||
import sys
|
||||
import tempfile
|
||||
|
||||
import six
|
||||
|
||||
from pre_commit import five
|
||||
from pre_commit import parse_shebang
|
||||
|
||||
|
|
@ -75,7 +71,7 @@ def make_executable(filename):
|
|||
|
||||
class CalledProcessError(RuntimeError):
|
||||
def __init__(self, returncode, cmd, expected_returncode, stdout, stderr):
|
||||
super(CalledProcessError, self).__init__(
|
||||
super().__init__(
|
||||
returncode, cmd, expected_returncode, stdout, stderr,
|
||||
)
|
||||
self.returncode = returncode
|
||||
|
|
@ -104,12 +100,8 @@ class CalledProcessError(RuntimeError):
|
|||
def to_text(self):
|
||||
return self.to_bytes().decode('UTF-8')
|
||||
|
||||
if six.PY2: # pragma: no cover (py2)
|
||||
__str__ = to_bytes
|
||||
__unicode__ = to_text
|
||||
else: # pragma: no cover (py3)
|
||||
__bytes__ = to_bytes
|
||||
__str__ = to_text
|
||||
__bytes__ = to_bytes
|
||||
__str__ = to_text
|
||||
|
||||
|
||||
def _cmd_kwargs(*cmd, **kwargs):
|
||||
|
|
@ -154,7 +146,7 @@ if os.name != 'nt': # pragma: windows no cover
|
|||
from os import openpty
|
||||
import termios
|
||||
|
||||
class Pty(object):
|
||||
class Pty:
|
||||
def __init__(self):
|
||||
self.r = self.w = None
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,3 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import concurrent.futures
|
||||
import contextlib
|
||||
import math
|
||||
|
|
@ -9,8 +5,6 @@ import os
|
|||
import subprocess
|
||||
import sys
|
||||
|
||||
import six
|
||||
|
||||
from pre_commit import parse_shebang
|
||||
from pre_commit.util import cmd_output_b
|
||||
from pre_commit.util import cmd_output_p
|
||||
|
|
@ -26,7 +20,7 @@ def _environ_size(_env=None):
|
|||
|
||||
def _get_platform_max_length(): # pragma: no cover (platform specific)
|
||||
if os.name == 'posix':
|
||||
maximum = os.sysconf(str('SC_ARG_MAX')) - 2048 - _environ_size()
|
||||
maximum = os.sysconf('SC_ARG_MAX') - 2048 - _environ_size()
|
||||
maximum = max(min(maximum, 2 ** 17), 2 ** 12)
|
||||
return maximum
|
||||
elif os.name == 'nt':
|
||||
|
|
@ -43,10 +37,7 @@ def _command_length(*cmd):
|
|||
# https://github.com/pre-commit/pre-commit/pull/839
|
||||
if sys.platform == 'win32':
|
||||
# the python2.x apis require bytes, we encode as UTF-8
|
||||
if six.PY2:
|
||||
return len(full_cmd.encode('utf-8'))
|
||||
else:
|
||||
return len(full_cmd.encode('utf-16le')) // 2
|
||||
return len(full_cmd.encode('utf-16le')) // 2
|
||||
else:
|
||||
return len(full_cmd.encode(sys.getfilesystemencoding()))
|
||||
|
||||
|
|
@ -125,7 +116,7 @@ def xargs(cmd, varargs, **kwargs):
|
|||
|
||||
def run_cmd_partition(run_cmd):
|
||||
return cmd_fn(
|
||||
*run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs
|
||||
*run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs,
|
||||
)
|
||||
|
||||
threads = min(len(partitions), target_concurrency)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue