2018-11-13 00:36:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-04-16 09:29:20 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
# Copyright (C) 2017-2020 The Project X-Ray Authors.
|
|
|
|
|
#
|
|
|
|
|
# Use of this source code is governed by a ISC-style
|
|
|
|
|
# license that can be found in the LICENSE file or at
|
|
|
|
|
# https://opensource.org/licenses/ISC
|
|
|
|
|
#
|
|
|
|
|
# SPDX-License-Identifier: ISC
|
2018-11-13 00:36:55 +01:00
|
|
|
|
2018-12-10 06:25:45 +01:00
|
|
|
import sys, re
|
2022-03-15 10:47:58 +01:00
|
|
|
from prjxray.util import OpenSafeFile, db_root_arg, parse_db_line
|
2018-11-13 00:36:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(fnin, fnout=None, strict=False, verbose=False):
|
2022-03-15 10:47:58 +01:00
|
|
|
with OpenSafeFile(fnin) as f:
|
|
|
|
|
lines = f.read().split('\n')
|
2018-11-13 23:38:26 +01:00
|
|
|
tags = dict()
|
2018-12-11 20:55:51 +01:00
|
|
|
bitss = dict()
|
2018-11-13 00:36:55 +01:00
|
|
|
for line in lines:
|
|
|
|
|
line = line.strip()
|
|
|
|
|
if line == '':
|
|
|
|
|
continue
|
2018-11-14 04:47:53 +01:00
|
|
|
# TODO: figure out what to do with masks
|
|
|
|
|
if line.startswith("bit "):
|
|
|
|
|
continue
|
2022-03-15 10:47:58 +01:00
|
|
|
tag, bits, mode, _ = parse_db_line(line)
|
2018-11-13 00:36:55 +01:00
|
|
|
if strict:
|
2018-11-14 20:33:30 +01:00
|
|
|
if mode != "always":
|
|
|
|
|
assert not mode, "strict: got ill defined line: %s" % (line, )
|
2018-11-13 23:38:26 +01:00
|
|
|
if tag in tags:
|
2019-07-08 20:20:53 +02:00
|
|
|
print("Original line: %s" % tags[tag], file=sys.stderr)
|
|
|
|
|
print("New line: %s" % line, file=sys.stderr)
|
2018-11-13 23:38:26 +01:00
|
|
|
assert 0, "strict: got duplicate tag %s" % (tag, )
|
2018-12-11 20:55:51 +01:00
|
|
|
assert bits not in bitss, "strict: got duplicate bits %s: %s %s" % (
|
|
|
|
|
bits, tag, bitss[bits])
|
2018-11-13 23:38:26 +01:00
|
|
|
tags[tag] = line
|
2018-11-14 20:33:30 +01:00
|
|
|
if bits != None:
|
2018-12-11 20:55:51 +01:00
|
|
|
bitss[bits] = tag
|
2018-11-13 00:36:55 +01:00
|
|
|
|
|
|
|
|
if fnout:
|
2022-03-15 10:47:58 +01:00
|
|
|
with OpenSafeFile(fnout, "w") as fout:
|
2018-11-13 00:36:55 +01:00
|
|
|
for line in sorted(lines):
|
|
|
|
|
line = line.strip()
|
|
|
|
|
if line == '':
|
|
|
|
|
continue
|
|
|
|
|
fout.write(line + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2018-12-10 20:21:56 +01:00
|
|
|
description="Parse a db file, checking for consistency")
|
2018-11-13 00:36:55 +01:00
|
|
|
|
2022-03-15 10:47:58 +01:00
|
|
|
db_root_arg(parser)
|
2018-11-13 00:36:55 +01:00
|
|
|
parser.add_argument('--verbose', action='store_true', help='')
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--strict',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='Complain on unresolved entries (ex: <0 candidates>, <const0>)')
|
|
|
|
|
parser.add_argument('fin', help='')
|
|
|
|
|
parser.add_argument('fout', nargs='?', help='')
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
run(args.fin, args.fout, strict=args.strict, verbose=args.verbose)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|