Implement pygrep language as a replacement for pcre · precommit/pre-commit@e950930 · GitHub
Skip to content

Commit e950930

Browse files
committed
Implement pygrep language as a replacement for pcre
1 parent 3a7806e commit e950930

8 files changed

Lines changed: 186 additions & 112 deletions

File tree

pre_commit/languages/all.py

Lines changed: 2 additions & 0 deletions

pre_commit/languages/pygrep.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import argparse
5+
import re
6+
import sys
7+
8+
from pre_commit import output
9+
from pre_commit.languages import helpers
10+
from pre_commit.xargs import xargs
11+
12+
13+
ENVIRONMENT_DIR = None
14+
get_default_version = helpers.basic_get_default_version
15+
healthy = helpers.basic_healthy
16+
install_environment = helpers.no_install
17+
18+
19+
def _process_filename_by_line(pattern, filename):
20+
retv = 0
21+
with open(filename, 'rb') as f:
22+
for line_no, line in enumerate(f, start=1):
23+
if pattern.search(line):
24+
retv = 1
25+
output.write('{}:{}:'.format(filename, line_no))
26+
output.write_line(line.rstrip(b'\r\n'))
27+
return retv
28+
29+
30+
def run_hook(repo_cmd_runner, hook, file_args):
31+
exe = (sys.executable, '-m', __name__)
32+
exe += tuple(hook['args']) + (hook['entry'],)
33+
return xargs(exe, file_args)
34+
35+
36+
def main(argv=None):
37+
parser = argparse.ArgumentParser(
38+
description=(
39+
'grep-like finder using python regexes. Unlike grep, this tool '
40+
'returns nonzero when it finds a match and zero otherwise. The '
41+
'idea here being that matches are "problems".'
42+
),
43+
)
44+
parser.add_argument('-i', '--ignore-case', action='store_true')
45+
parser.add_argument('pattern', help='python regex pattern.')
46+
parser.add_argument('filenames', nargs='*')
47+
args = parser.parse_args(argv)
48+
49+
flags = re.IGNORECASE if args.ignore_case else 0
50+
pattern = re.compile(args.pattern.encode(), flags)
51+
52+
retv = 0
53+
for filename in args.filenames:
54+
retv |= _process_filename_by_line(pattern, filename)
55+
return retv
56+
57+
58+
if __name__ == '__main__':
59+
exit(main())

pre_commit/repository.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ class LocalRepository(Repository):
202202
def _cmd_runner_from_deps(self, language_name, deps):
203203
"""local repositories have a cmd runner per hook"""
204204
language = languages[language_name]
205-
# pcre / script / system / docker_image do not have environments so
206-
# they work out of the current directory
205+
# pcre / pygrep / script / system / docker_image do not have
206+
# environments so they work out of the current directory
207207
if language.ENVIRONMENT_DIR is None:
208208
return PrefixedCommandRunner(git.get_root())
209209
else:

testing/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def config_with_local_hooks():
7373
('id', 'do_not_commit'),
7474
('name', 'Block if "DO NOT COMMIT" is found'),
7575
('entry', 'DO NOT COMMIT'),
76-
('language', 'pcre'),
76+
('language', 'pygrep'),
7777
('files', '^(.*)$'),
7878
))],
7979
),

testing/resources/pcre_hooks_repo/.pre-commit-hooks.yaml

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

tests/languages/pygrep_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import pytest
5+
6+
from pre_commit.languages import pygrep
7+
8+
9+
@pytest.fixture
10+
def some_files(tmpdir):
11+
tmpdir.join('f1').write_binary(b'foo\nbar\n')
12+
tmpdir.join('f2').write_binary(b'[INFO] hi\n')
13+
tmpdir.join('f3').write_binary(b"with'quotes\n")
14+
with tmpdir.as_cwd():
15+
yield
16+
17+
18+
@pytest.mark.usefixtures('some_files')
19+
@pytest.mark.parametrize(
20+
('pattern', 'expected_retcode', 'expected_out'),
21+
(
22+
('baz', 0, ''),
23+
('foo', 1, 'f1:1:foo\n'),
24+
('bar', 1, 'f1:2:bar\n'),
25+
(r'(?i)\[info\]', 1, 'f2:1:[INFO] hi\n'),
26+
("h'q", 1, "f3:1:with'quotes\n"),
27+
),
28+
)
29+
def test_main(some_files, cap_out, pattern, expected_retcode, expected_out):
30+
ret = pygrep.main((pattern, 'f1', 'f2', 'f3'))
31+
out = cap_out.get()
32+
assert ret == expected_retcode
33+
assert out == expected_out
34+
35+
36+
def test_ignore_case(some_files, cap_out):
37+
ret = pygrep.main(('--ignore-case', 'info', 'f1', 'f2', 'f3'))
38+
out = cap_out.get()
39+
assert ret == 1
40+
assert out == 'f2:1:[INFO] hi\n'

tests/repository_test.py

Lines changed: 80 additions & 91 deletions

0 commit comments

Comments
 (0)