From ef5817946279cbfaacbd137e8ce061964914fe2d Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 27 Feb 2016 22:31:19 +0000 Subject: [PATCH] Refactor ``mk_pat_db()`` so that it is usable externally via a new function ``mk_pat_db_internal()`` which is called by a new ``mk_pat_db.py`` script. This will allow other build systems to generate the ``database.h`` file. --- scripts/mk_pat_db.py | 34 ++++++++++++++++++++++++++++++++++ scripts/mk_util.py | 21 ++++++++++++--------- 2 files changed, 46 insertions(+), 9 deletions(-) create mode 100755 scripts/mk_pat_db.py diff --git a/scripts/mk_pat_db.py b/scripts/mk_pat_db.py new file mode 100755 index 000000000..3f2e7c507 --- /dev/null +++ b/scripts/mk_pat_db.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +""" +Reads a pattern database and generates the corresponding +header file. +""" +import mk_util +import argparse +import logging +import os +import sys + +def main(args): + logging.basicConfig(level=logging.INFO) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("db_file", help="pattern database file") + parser.add_argument("output_file", help="output header file path") + pargs = parser.parse_args(args) + + if not os.path.exists(pargs.db_file): + logging.error('"{}" does not exist'.format(pargs.db_file)) + return 1 + + if (os.path.exists(pargs.output_file) and + not os.path.isfile(pargs.output_file)): + logging.error('Refusing to overwrite "{}"'.format( + pargs.output_file)) + return 1 + + mk_util.mk_pat_db_internal(pargs.db_file, pargs.output_file) + return 0 + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) + diff --git a/scripts/mk_util.py b/scripts/mk_util.py index dbc83353f..77a8df0e2 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -2602,16 +2602,19 @@ def exec_pyg_scripts(): # database.smt ==> database.h def mk_pat_db(): c = get_component(PATTERN_COMPONENT) - fin = open(os.path.join(c.src_dir, 'database.smt2'), 'r') - fout = open(os.path.join(c.src_dir, 'database.h'), 'w') - fout.write('static char const g_pattern_database[] =\n') - for line in fin: - fout.write('"%s\\n"\n' % line.strip('\n')) - fout.write(';\n') - fin.close() - fout.close() + fin = os.path.join(c.src_dir, 'database.smt2') + fout = os.path.join(c.src_dir, 'database.h') + mk_pat_db_internal(fin, fout) + +def mk_pat_db_internal(inputFilePath, outputFilePath): + with open(inputFilePath, 'r') as fin: + with open(outputFilePath, 'w') as fout: + fout.write('static char const g_pattern_database[] =\n') + for line in fin: + fout.write('"%s\\n"\n' % line.strip('\n')) + fout.write(';\n') if VERBOSE: - print("Generated '%s'" % os.path.join(c.src_dir, 'database.h')) + print("Generated '%s'" % outputFilePath) # Update version numbers def update_version():