Debian Codegraph @

scripts/filter_whence.py

raw symbols SCIP
1 #! /usr/bin/python3
2 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2024 Linaro Ltd.
4 #
5 # Filter WHENCE entries to only those referenced by a config file
6
7 import os, re, sys
8
9 def load_config(cfg):
10 with open(cfg, encoding="utf-8") as config:
11 pattern_inst = re.compile("^Install: ([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)\n$")
12 for line in config:
13 match = pattern_inst.match(line)
14 if match:
15 yield os.path.join(match.group(1), match.group(3))
16
17 def filter_whence(whence, cfg):
18 with open(whence, encoding="utf-8") as file:
19 pattern_dir = re.compile("^Dir: (.*)\n$")
20 pattern_break = re.compile("^----")
21 matched = True # output file header until the first section
22
23 for line in file:
24 match = pattern_dir.match(line)
25 if match:
26 if match.group(1) in cfg:
27 matched = True
28 yield line
29 continue
30
31 match = pattern_break.match(line)
32 if match:
33 if matched:
34 matched = False
35 yield line
36 yield "\n"
37 continue
38
39 if matched:
40 yield line
41
42
43 def main(args):
44 if len(args) != 4:
45 print("Usage: %s config.txt WHENCE OUT" % args[0])
46
47 cfg = dict.fromkeys(load_config(args[1]))
48
49 with open(args[3], mode="wt", encoding="utf-8") as out:
50 for line in filter_whence(args[2], cfg):
51 out.write(line)
52
53 if __name__ == "__main__":
54 sys.exit(main(sys.argv))