blob: 2b001d763570ff333edc98f5dd16771570672b78 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001import os
2import re
3import sys
4
5def check_encoding(encoding, scan_dir, regex_pattern):
6 fname = None
7 try:
8 assert encoding in ['ascii', 'utf-8'], "unexpected encoding"
9 cmp = re.compile(regex_pattern)
10 for root, dirs, files in os.walk(scan_dir):
11 fname = root
12 cmp_list = [f for f in files if cmp.search(f) is not None]
13 for f in cmp_list:
14 fname = os.path.join(root, f)
15 with open(fname, mode='rb') as test_file:
16 btext = test_file.read()
17 # check encoding
18 btext.decode(encoding=encoding, errors="strict")
19 if encoding == "utf-8" and btext.startswith(b'\xEF\xBB\xBF'):
20 raise ValueError("unexpected BOM in file")
21 # check LF line endings
22 LF = btext.count(b'\n')
23 CR = btext.count(b'\r')
24 if CR!=0:
25 raise ValueError("invalid line endings: LF({})/CR({})".format(LF, CR))
26 except Exception as err:
27 print("ERROR with [{}]: {}".format(fname, err))
28 return -1
29 else:
30 return 0
31
32if __name__ == "__main__":
33 # python check-sources.sh.py 'ascii' '.' '.*\.(cpp|h)$'
34 res = check_encoding(sys.argv[1], sys.argv[2], sys.argv[3])
35 sys.exit(0 if res == 0 else -1)