Merge pull request #632 from pre-commit/small_cleanups

Small cleanups
This commit is contained in:
Anthony Sottile
2017-09-30 17:20:07 -07:00
committed by GitHub
17 changed files with 40 additions and 58 deletions

View File

@@ -10,7 +10,7 @@ from identify.identify import ALL_TAGS
import pre_commit.constants as C
from pre_commit import schema
from pre_commit.errors import FatalError
from pre_commit.error_handler import FatalError
from pre_commit.languages.all import all_languages
@@ -51,8 +51,7 @@ MANIFEST_HOOK_DICT = schema.Map(
'',
),
schema.Optional(
'exclude',
schema.check_and(schema.check_string, schema.check_regex),
'exclude', schema.check_and(schema.check_string, schema.check_regex),
'^$',
),
schema.Optional('types', schema.check_array(check_type_tag), ['file']),

View File

@@ -47,7 +47,4 @@ def use_color(setting):
if setting not in COLOR_CHOICES:
raise InvalidColorSetting(setting)
return (
setting == 'always' or
(setting == 'auto' and sys.stdout.isatty())
)
return setting == 'always' or (setting == 'auto' and sys.stdout.isatty())

View File

@@ -1,3 +1,4 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from ctypes import POINTER
@@ -19,8 +20,7 @@ def bool_errcheck(result, func, args):
GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(
("GetStdHandle", windll.kernel32),
((1, "nStdHandle"), ),
("GetStdHandle", windll.kernel32), ((1, "nStdHandle"),),
)
GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(
@@ -42,7 +42,6 @@ def enable_virtual_terminal_processing():
More info on the escape sequences supported:
https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
"""
stdout = GetStdHandle(STD_OUTPUT_HANDLE)
flags = GetConsoleMode(stdout)

View File

@@ -32,8 +32,7 @@ def _get_skips(environ):
def _hook_msg_start(hook, verbose):
return '{}{}'.format(
'[{}] '.format(hook['id']) if verbose else '',
hook['name'],
'[{}] '.format(hook['id']) if verbose else '', hook['name'],
)
@@ -99,8 +98,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
'git', 'diff', '--no-ext-diff', retcode=None, encoding=None,
)
retcode, stdout, stderr = repo.run_hook(
hook,
tuple(filenames) if hook['pass_filenames'] else (),
hook, tuple(filenames) if hook['pass_filenames'] else (),
)
diff_after = cmd_output(
'git', 'diff', '--no-ext-diff', retcode=None, encoding=None,

View File

@@ -1,3 +1,4 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import pkg_resources

View File

@@ -10,14 +10,12 @@ UNSET = collections.namedtuple('UNSET', ())()
Var = collections.namedtuple('Var', ('name', 'default'))
setattr(Var.__new__, '__defaults__', ('',))
Var.__new__.__defaults__ = ('',)
def format_env(parts, env):
return ''.join(
env.get(part.name, part.default)
if isinstance(part, Var)
else part
env.get(part.name, part.default) if isinstance(part, Var) else part
for part in parts
)

View File

@@ -10,10 +10,13 @@ import six
from pre_commit import five
from pre_commit import output
from pre_commit.errors import FatalError
from pre_commit.store import Store
class FatalError(RuntimeError):
pass
def _to_bytes(exc):
try:
return bytes(exc)
@@ -46,7 +49,5 @@ def error_handler():
_log_and_exit('An error has occurred', e, traceback.format_exc())
except Exception as e:
_log_and_exit(
'An unexpected error has occurred',
e,
traceback.format_exc(),
'An unexpected error has occurred', e, traceback.format_exc(),
)

View File

@@ -1,6 +0,0 @@
from __future__ import absolute_import
from __future__ import unicode_literals
class FatalError(RuntimeError):
pass

View File

@@ -1,3 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import contextlib
import errno

View File

@@ -1,3 +1,4 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import six

View File

@@ -4,7 +4,7 @@ import logging
import os.path
import sys
from pre_commit.errors import FatalError
from pre_commit.error_handler import FatalError
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
@@ -114,7 +114,6 @@ def check_for_cygwin_mismatch():
'These can be installed through the cygwin installer.\n'
' - python {}\n'
' - git {}\n'.format(
exe_type[is_cygwin_python],
exe_type[is_cygwin_git],
exe_type[is_cygwin_python], exe_type[is_cygwin_git],
),
)

View File

@@ -23,12 +23,12 @@ class LoggingHandler(logging.Handler):
def emit(self, record):
output.write_line(
'{}{}'.format(
'{} {}'.format(
color.format_color(
'[{}]'.format(record.levelname),
LOG_LEVEL_COLORS[record.levelname],
self.use_color,
) + ' ',
),
record.getMessage(),
),
)

View File

@@ -2,12 +2,14 @@ from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import os.path
import tarfile
from pre_commit import output
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import resource_filename
from pre_commit.util import rmtree
from pre_commit.util import tmpdir
@@ -27,11 +29,6 @@ REPOS = (
)
RESOURCES_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'resources'),
)
def make_archive(name, repo, ref, destdir):
"""Makes an archive of a repository in the given destdir.
@@ -59,12 +56,15 @@ def make_archive(name, repo, ref, destdir):
return output_path
def main():
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('--dest', default=resource_filename())
args = parser.parse_args(argv)
for archive_name, repo, ref in REPOS:
output.write_line('Making {}.tar.gz for {}@{}'.format(
archive_name, repo, ref,
))
make_archive(archive_name, repo, ref, RESOURCES_DIR)
make_archive(archive_name, repo, ref, args.dest)
if __name__ == '__main__':

View File

@@ -93,18 +93,16 @@ def tmpdir():
rmtree(tempdir)
def resource_filename(filename):
def resource_filename(*segments):
return pkg_resources.resource_filename(
'pre_commit',
os.path.join('resources', filename),
'pre_commit', os.path.join('resources', *segments),
)
def make_executable(filename):
original_mode = os.stat(filename).st_mode
os.chmod(
filename,
original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
filename, original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
)

View File

@@ -11,7 +11,6 @@ import mock
import pytest
from pre_commit import error_handler
from pre_commit.errors import FatalError
from testing.util import cmd_output_mocked_pre_commit_home
@@ -28,7 +27,7 @@ def test_error_handler_no_exception(mocked_log_and_exit):
def test_error_handler_fatal_error(mocked_log_and_exit):
exc = FatalError('just a test')
exc = error_handler.FatalError('just a test')
with error_handler.error_handler():
raise exc
@@ -46,7 +45,7 @@ def test_error_handler_fatal_error(mocked_log_and_exit):
r' File ".+tests.error_handler_test.py", line \d+, '
r'in test_error_handler_fatal_error\n'
r' raise exc\n'
r'(pre_commit\.errors\.)?FatalError: just a test\n',
r'(pre_commit\.error_handler\.)?FatalError: just a test\n',
mocked_log_and_exit.call_args[0][2],
)
@@ -77,7 +76,7 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
def test_log_and_exit(cap_out, mock_out_store_directory):
with pytest.raises(SystemExit):
error_handler._log_and_exit(
'msg', FatalError('hai'), "I'm a stacktrace",
'msg', error_handler.FatalError('hai'), "I'm a stacktrace",
)
printed = cap_out.get()

View File

@@ -7,7 +7,7 @@ import os.path
import pytest
from pre_commit import git
from pre_commit.errors import FatalError
from pre_commit.error_handler import FatalError
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from testing.fixtures import git_dir

View File

@@ -4,7 +4,6 @@ from __future__ import unicode_literals
import os.path
import tarfile
import mock
import pytest
from pre_commit import make_archives
@@ -53,12 +52,8 @@ def test_make_archive(tempdir_factory):
@skipif_slowtests_false
@pytest.mark.integration
def test_main(tempdir_factory):
path = tempdir_factory.get()
# Don't actually want to make these in the current repo
with mock.patch.object(make_archives, 'RESOURCES_DIR', path):
make_archives.main()
def test_main(tmpdir):
make_archives.main(('--dest', tmpdir.strpath))
for archive, _, _ in make_archives.REPOS:
assert os.path.exists(os.path.join(path, archive + '.tar.gz'))
assert tmpdir.join('{}.tar.gz'.format(archive)).exists()