-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.py
58 lines (47 loc) · 1.65 KB
/
filter.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
57
58
#!/usr/bin/env python3
#
# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
#
# SPDX-License-Identifier: BSD-2-Clause
#
import argparse
import fnmatch
import re
import sys
# never run the tools on anything git
DEFAULT_FILTERS = ["*.git/*"]
# support comments alone and trailing (comment with #)
COMMENT = re.compile(r'^\s*#')
TRAILING_COMMENT = re.compile(r'\s+#.*$')
def parse_filter(line: str):
"""Parse a filter from a line of a filter file"""
line = line.strip()
if line and not COMMENT.search(line):
return TRAILING_COMMENT.sub('', line)
return None
def parse_filters(filters_file: str):
"""Parse the filter file, returning a list of filters"""
filters = DEFAULT_FILTERS
if filters_file:
try:
with open(filters_file, 'r') as outfile:
lines = outfile.readlines()
filters += [parse_filter(l) for l in lines if parse_filter(l)]
except IOError as exception:
logging.warning("Failed to open filter file %s: %s", filters_file, exception)
return filters
def main():
parser = argparse.ArgumentParser("Filter files.")
parser.add_argument('-f', '--filters', type=str,
help='File with glob filters of files')
parser.add_argument('files', nargs='*', type=str,
help='List of files to be filtered')
args = parser.parse_args()
filters = parse_filters(args.filters)
for fname in args.files:
def matches(pattern, fname=fname):
return fnmatch.fnmatch(fname, pattern)
if not any(map(matches, filters)):
print(fname)
if __name__ == '__main__':
sys.exit(main())