-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_db.py
42 lines (37 loc) · 1.12 KB
/
compile_db.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
import argparse
import hyperscan
import json
import pickle
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", default="hs.db")
args = parser.parse_args()
sys.stderr.write("Collecting patterns...\n")
regexes = set()
for line in sys.stdin:
line = json.loads(line)
db = hyperscan.Database()
try:
db.compile(
expressions=(line.encode("utf8"),),
ids=(0,),
flags=(hyperscan.HS_FLAG_SINGLEMATCH | hyperscan.HS_FLAG_UTF8,),
)
regexes.add(line)
except hyperscan.error:
pass
# Build input for the final Hyperscan database
db = hyperscan.Database()
patterns = []
ids = []
flags = []
for i, regex in enumerate(regexes):
print(json.dumps(regex))
patterns.append(regex.encode("utf8"))
ids.append(i)
flags.append(hyperscan.HS_FLAG_SINGLEMATCH | hyperscan.HS_FLAG_UTF8)
# Compile the final database and save to file
sys.stderr.write("Compiling %d patterns...\n" % len(patterns))
db.compile(expressions=patterns, ids=ids, flags=flags)
with open(args.output, "wb") as f:
pickle.dump([len(patterns), hyperscan.dumpb(db)], f)