forked from domodwyer/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo-tags.py
executable file
·56 lines (44 loc) · 1.51 KB
/
todo-tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3 -s
# Searches passed files for TODO comments.
#
# Prints out (and exits non-zero) if any TODO comments are not in the form:
#
# TODO(DEV-1234): some text
#
# Ensuring all TODOs are either complete when opening the PR, or have a JIRA
# ticket to track the future change.
import argparse
import re
import sys
DEFAULT_TAG = "DEV"
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--tag',
type=str,
help="A JIRA-like tag (i.e. DEV) to search for",
default=DEFAULT_TAG,
dest='tag')
parser.add_argument('-r', '--regex',
type=str,
help="Specify the regex to match inner in TODO(inner)",
dest='regex')
parser.add_argument('files', metavar='FILES', type=str, nargs='+',
help='Files to search')
args = parser.parse_args()
# Do not allow specifying a tag and --regex
if args.regex and args.tag != DEFAULT_TAG:
sys.exit("cannot provide tag with --regex")
# Figure out what regex to use
tag = args.tag + "-[0-9]+"
if args.regex:
tag = args.regex
# This regex matches all TODO comments (prefixed by // or #) that are not
# immediately followed by "($TAG)"
pattern = re.compile(
r"(\/\/|#)+\s?TODO((?!\("+tag+r"\)).)*$", re.IGNORECASE)
ret = 0
for f in args.files:
for i, line in enumerate(open(f)):
for match in re.finditer(pattern, line):
print('%s:%s %s' % (f, i+1, match.string.strip()))
ret += 1
exit(ret)