1313import re
1414import mmap
1515import sys
16- from os import path
16+ import pathlib
17+ from collections import defaultdict
18+ from os import path , listdir
1719from contextlib import contextmanager
1820
1921TYPES = {
@@ -41,14 +43,19 @@ def mmap_open(name, mode="r"):
4143
4244def check_header (header_dir , files , verbose = False ):
4345 retval = 0
44- header_re = {}
45- header_len = {}
46-
47- for headertype in TYPES :
48- with open (path .join (header_dir , headertype ), "rb" ) as fhandle :
49- header_content = fhandle .read ()
50- header_re [headertype ] = re .compile (re .escape (header_content ))
51- header_len [headertype ] = len (header_content )
46+ header_re = defaultdict (list )
47+ header_len = defaultdict (list )
48+
49+ for headerfile in listdir (header_dir ):
50+ headertype = pathlib .Path (headerfile ).stem
51+ if headertype in TYPES :
52+ with open (path .join (header_dir , headerfile ), "rb" ) as fhandle :
53+ header_content = fhandle .read ()
54+ header_re [headertype ].append (re .compile (re .escape (header_content )))
55+ header_len [headertype ].append (len (header_content ))
56+ else :
57+ print ("no matching headerfile to file extensions" )
58+ sys .exit (1 )
5259
5360 ext_map = {e : t for t , exts in TYPES .items () for e in exts }
5461
@@ -62,9 +69,10 @@ def check_header(header_dir, files, verbose=False):
6269
6370 with mmap_open (fpath ) as fmapped :
6471 header_type = ext_map [fext ]
65- match = header_re [header_type ].search (
66- fmapped , 0 , ALLOWED_LINES * MAX_LINE_LENGTH + header_len [header_type ]
67- )
72+ for h_re , h_len in zip (header_re [header_type ], header_len [header_type ]):
73+ match = h_re .search (fmapped , 0 , ALLOWED_LINES * MAX_LINE_LENGTH + h_len )
74+ if match :
75+ break
6876
6977 if not match :
7078 print ("✗ {} ... required header not found" .format (fpath ))
0 commit comments