From bf6fea96d47bc64c2e0d538fce3d4d177cfbc0b1 Mon Sep 17 00:00:00 2001 From: Everett Hildenbrandt Date: Mon, 28 May 2018 13:09:17 -0600 Subject: test.py: add generic infrastructure, usage messages, JSON reading, format command --- test.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test.py b/test.py index 703dcf327..04c100542 100755 --- a/test.py +++ b/test.py @@ -25,3 +25,49 @@ # - GeneralStateTest filler # eg. stVMTests/vmArithmeticTest/add0Filler.json +import sys +import os +import json + +def _report(*msg): + print("== " + sys.argv[0] + ":", *msg, file=sys.stderr) + +def _die(*msg, exit_code=1): + _report(*msg) + _report("exiting...") + sys.exit(exit_code) + +def readJSONFile(fname): + if not os.path.isfile(fname): + _die("Not a file:", fname) + with open(fname, "r") as f: + fcontents = f.read() + return json.loads(fcontents) + +def writeJSONFile(fname, fcontents): + if not os.path.exists(os.path.dirname(fname)): + os.makedirs(os.path.dirname(fname)) + with open(fname, "w") as f: + f.write(json.dumps(fcontents, indent=4, sort_keys=True)) + +def _usage(): + usage_lines = [ "" + , " usage: " + sys.argv[0] + " format " + , " where:" + , " format: command to format/sort the JSON file." + , " : JSON test file/filler to read and write with sorted keys/standard formatting." + ] + _die("\n".join(usage_lines)) + +def main(): + if len(sys.argv) < 2: + _usage() + test_command = sys.argv[1] + if test_command == "format": + file_name = sys.argv[2] + writeJSONFile(file_name, readJSONFile(file_name)) + else: + _usage() + +if __name__ == "__main__": + main() -- cgit v1.2.3