Make entry points simpler · precommit/pre-commit@04b4219 · GitHub
Skip to content

Commit 04b4219

Browse files
committed
Make entry points simpler
1 parent 6d1a464 commit 04b4219

8 files changed

Lines changed: 59 additions & 31 deletions

File tree

pre_commit/clientlib/validate_config.py

Lines changed: 6 additions & 0 deletions

pre_commit/clientlib/validate_manifest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pre_commit.constants as C
77
from pre_commit.clientlib.validate_base import get_validator
8+
from pre_commit.util import entry
89

910

1011
class InvalidManifestError(ValueError): pass
@@ -52,6 +53,7 @@ def additional_manifest_check(obj):
5253
)
5354

5455

56+
@entry
5557
def run(argv):
5658
parser = argparse.ArgumentParser()
5759
parser.add_argument(
@@ -73,4 +75,8 @@ def run(argv):
7375
print(str(e.args[1]))
7476
return 1
7577

76-
return 0
78+
return 0
79+
80+
81+
if __name__ == '__main__':
82+
run()

pre_commit/entry_points.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

pre_commit/run.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import argparse
33
import os.path
44
import subprocess
5-
import sys
65

76
from pre_commit import git
87
from pre_commit.clientlib.validate_config import validate_config
98
from pre_commit.repository import Repository
9+
from pre_commit.util import entry
1010

1111

1212
RED = '\033[41m'
@@ -90,6 +90,7 @@ def run_single_hook(hook_id, configs=None, run_all_the_things=False):
9090
return 1
9191

9292

93+
@entry
9394
def run(argv):
9495
parser = argparse.ArgumentParser()
9596

@@ -130,4 +131,4 @@ def run(argv):
130131

131132

132133
if __name__ == '__main__':
133-
run(sys.argv[1:])
134+
run()

pre_commit/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import functools
33
import os
4+
import sys
45

56

67
class cached_property(object):
@@ -35,3 +36,16 @@ def wrapper(*args):
3536
wrapper._cache = {}
3637

3738
return wrapper
39+
40+
41+
def entry(func):
42+
"""Allows a function that has `argv` as an argument to be used as a
43+
commandline entry. This will make the function callable using either
44+
explicitly passed argv or defaulting to sys.argv[1:]
45+
"""
46+
@functools.wraps(func)
47+
def wrapper(argv=None):
48+
if argv is None:
49+
argv = sys.argv[1:]
50+
return func(argv)
51+
return wrapper

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
],
2020
entry_points={
2121
'console_scripts': [
22-
'pre-commit = pre_commit.entry_points:pre_commit_func',
23-
'validate-config = pre_commit.entry_points:validate_config_func',
24-
'validate-manifest = pre_commit.entry_points:validate_manifest_func',
22+
'pre-commit = pre_commit.run:run',
23+
'validate-config = pre_commit.clientlib.validate_config:run',
24+
'validate-manifest = pre_commit.clientlib.validate_manifest:run',
2525
],
2626
},
2727
scripts=[

tests/repository_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test_run_a_hook_lots_of_files(config_for_python_pre_commit_git_repo):
6565
os.environ.get('slowtests', None) == 'false',
6666
reason="TODO: make this test not super slow",
6767
)
68+
@pytest.mark.integration
6869
def test_run_a_node_hook(config_for_node_pre_commit_git_repo):
6970
repo = Repository(config_for_node_pre_commit_git_repo)
7071
repo.install()

tests/util_test.py

Lines changed: 25 additions & 0 deletions

0 commit comments

Comments
 (0)