From 18c9e061d89e5a1386b6beda13399e2e030a7a4d Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 30 Sep 2017 15:15:52 -0700 Subject: [PATCH] Small cleanups --- pre_commit/clientlib.py | 5 ++--- pre_commit/color.py | 5 +---- pre_commit/color_windows.py | 5 ++--- pre_commit/commands/run.py | 6 ++---- pre_commit/constants.py | 1 + pre_commit/envcontext.py | 6 ++---- pre_commit/error_handler.py | 9 +++++---- pre_commit/errors.py | 6 ------ pre_commit/file_lock.py | 3 +++ pre_commit/five.py | 1 + pre_commit/git.py | 5 ++--- pre_commit/logging_handler.py | 4 ++-- pre_commit/make_archives.py | 14 +++++++------- pre_commit/util.py | 8 +++----- tests/error_handler_test.py | 7 +++---- tests/git_test.py | 2 +- tests/make_archives_test.py | 11 +++-------- 17 files changed, 40 insertions(+), 58 deletions(-) delete mode 100644 pre_commit/errors.py diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index c04cf333..11750b74 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -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']), diff --git a/pre_commit/color.py b/pre_commit/color.py index 25fbb256..44917ca0 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -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()) diff --git a/pre_commit/color_windows.py b/pre_commit/color_windows.py index dae41afe..4e193f96 100644 --- a/pre_commit/color_windows.py +++ b/pre_commit/color_windows.py @@ -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) diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 6f695487..74bff891 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -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, diff --git a/pre_commit/constants.py b/pre_commit/constants.py index 8af49184..2fa43552 100644 --- a/pre_commit/constants.py +++ b/pre_commit/constants.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from __future__ import unicode_literals import pkg_resources diff --git a/pre_commit/envcontext.py b/pre_commit/envcontext.py index 8066da3b..82538df2 100644 --- a/pre_commit/envcontext.py +++ b/pre_commit/envcontext.py @@ -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 ) diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py index 76662e97..72067803 100644 --- a/pre_commit/error_handler.py +++ b/pre_commit/error_handler.py @@ -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(), ) diff --git a/pre_commit/errors.py b/pre_commit/errors.py deleted file mode 100644 index 4dedbfc2..00000000 --- a/pre_commit/errors.py +++ /dev/null @@ -1,6 +0,0 @@ -from __future__ import absolute_import -from __future__ import unicode_literals - - -class FatalError(RuntimeError): - pass diff --git a/pre_commit/file_lock.py b/pre_commit/file_lock.py index f33584c3..7c7e8514 100644 --- a/pre_commit/file_lock.py +++ b/pre_commit/file_lock.py @@ -1,3 +1,6 @@ +from __future__ import absolute_import +from __future__ import unicode_literals + import contextlib import errno diff --git a/pre_commit/five.py b/pre_commit/five.py index de017267..3b94a927 100644 --- a/pre_commit/five.py +++ b/pre_commit/five.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from __future__ import unicode_literals import six diff --git a/pre_commit/git.py b/pre_commit/git.py index 1c3191e3..96a5155b 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -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], ), ) diff --git a/pre_commit/logging_handler.py b/pre_commit/logging_handler.py index 7241cd67..c043a8ac 100644 --- a/pre_commit/logging_handler.py +++ b/pre_commit/logging_handler.py @@ -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(), ), ) diff --git a/pre_commit/make_archives.py b/pre_commit/make_archives.py index c672fc18..90809c10 100644 --- a/pre_commit/make_archives.py +++ b/pre_commit/make_archives.py @@ -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__': diff --git a/pre_commit/util.py b/pre_commit/util.py index b0095843..10d78d99 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -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, ) diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py index d6eaf500..0e93298b 100644 --- a/tests/error_handler_test.py +++ b/tests/error_handler_test.py @@ -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() diff --git a/tests/git_test.py b/tests/git_test.py index 8417523f..8f80dcad 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -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 diff --git a/tests/make_archives_test.py b/tests/make_archives_test.py index 5aa303f7..34233424 100644 --- a/tests/make_archives_test.py +++ b/tests/make_archives_test.py @@ -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()