Refactored how entry points work.

This commit is contained in:
Anthony Sottile 2014-03-22 18:21:54 -07:00
parent 5db7f2d807
commit 45741545dc
8 changed files with 71 additions and 42 deletions

View File

@ -1,28 +1,20 @@
-
repo: git@github.com:pre-commit/pre-commit-hooks
- repo: git@github.com:pre-commit/pre-commit-hooks
sha: 12794c1c19c001e3d05bcfe316b4f93b414035a7
hooks:
-
id: pyflakes
- id: pyflakes
files: '\.py$'
-
id: debug-statements
- id: debug-statements
files: '\.py$'
-
id: trailing-whitespace
- id: trailing-whitespace
files: '\.(py|sh|yaml)'
-
id: name-tests-test
- id: name-tests-test
files: 'tests/.*\.py'
-
repo: git@github.com:pre-commit/pre-commit
- repo: git@github.com:pre-commit/pre-commit
sha: c77d65d9cbbcf30e2be005f5ba8b63447deedc1e
hooks:
-
id: validate_manifest
- id: validate_manifest
files: /manifest.yaml
-
id: validate_config
- id: validate_config
files: /\.pre-commit-config.yaml

View File

@ -1,25 +1,21 @@
-
id: pyflakes
- id: pyflakes
name: Pyflakes
description: This validator runs pyflakes.
entry: pyflakes
language: python
-
id: debug-statements
- id: debug-statements
name: Debug Statements (Python)
description: This hook checks that debug statements (pdb, ipdb, pudb) are not imported on commit.
entry: debug-statement-hook
language: python
-
id: trailing-whitespace
- id: trailing-whitespace
name: Trim Trailing Whitespace
description: This hook trims trailing whitespace.
entry: trailing-whitespace-fixer
language: python
-
id: name-tests-test
- id: name-tests-test
name: Tests should end in _test.py
description: This verifies that test files are named correctly
entry: name-tests-test
language: python
language: python

View File

@ -4,6 +4,8 @@ import ast
import collections
import sys
from pre_commit_hooks.util import entry
DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb'])
@ -43,6 +45,7 @@ def check_file_for_debug_statements(filename):
return 0
@entry
def debug_statement_hook(argv):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to run')
@ -55,9 +58,5 @@ def debug_statement_hook(argv):
return retv
def entry():
return debug_statement_hook(sys.argv[1:])
if __name__ == '__main__':
sys.exit(entry())
sys.exit(debug_statement_hook())

View File

@ -3,7 +3,10 @@ from __future__ import print_function
import sys
from pre_commit_hooks.util import entry
@entry
def validate_files(argv):
retcode = 0
for filename in argv:
@ -18,9 +21,5 @@ def validate_files(argv):
return retcode
def entry():
return validate_files(sys.argv[1:])
if __name__ == '__main__':
sys.exit(entry())

View File

@ -3,7 +3,10 @@ import argparse
import sys
from plumbum import local
from pre_commit_hooks.util import entry
@entry
def fix_trailing_whitespace(argv):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
@ -22,9 +25,5 @@ def fix_trailing_whitespace(argv):
return 0
def entry():
fix_trailing_whitespace(sys.argv[1:])
if __name__ == '__main__':
sys.exit(entry())
sys.exit(fix_trailing_whitespace())

16
pre_commit_hooks/util.py Normal file
View File

@ -0,0 +1,16 @@
import functools
import sys
def entry(func):
"""Allows a function that has `argv` as an argument to be used as a
commandline entry. This will make the function callable using either
explicitly passed argv or defaulting to sys.argv[1:]
"""
@functools.wraps(func)
def wrapper(argv=None):
if argv is None:
argv = sys.argv[1:]
return func(argv)
return wrapper

View File

@ -13,9 +13,9 @@ setup(
],
entry_points={
'console_scripts': [
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:entry',
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:entry',
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:entry',
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
],
},
)

28
tests/util_test.py Normal file
View File

@ -0,0 +1,28 @@
import mock
import pytest
import sys
from pre_commit.util import entry
@pytest.fixture
def entry_func():
@entry
def func(argv):
return argv
return func
def test_explicitly_passed_argv_are_passed(entry_func):
input = object()
ret = entry_func(input)
assert ret is input
def test_no_arguments_passed_uses_argv(entry_func):
argv = [1, 2, 3, 4]
with mock.patch.object(sys, 'argv', argv):
ret = entry_func()
assert ret == argv[1:]