From 325037e8b979b4ad56bfc24566d3dbddf490a35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Tue, 30 Jul 2024 07:47:03 +0200 Subject: [PATCH] python: convert from python 2 to python 2 syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Øyvind Harboe --- flow/util/appendStatsToDb.py | 108 ++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/flow/util/appendStatsToDb.py b/flow/util/appendStatsToDb.py index 283d85ebc5..a4ef3da524 100755 --- a/flow/util/appendStatsToDb.py +++ b/flow/util/appendStatsToDb.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # This scripts appends the test metadata to the master json -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- import argparse # argument parsing import json # json parsing @@ -17,63 +17,79 @@ # Parse and validate arguments # ============================================================================== -parser = argparse.ArgumentParser( - description='Appends test metadata to master database') -parser.add_argument('--masterTestListPath', '-m', default="masterTestList.json", required=False, - help='Path to Master Metadata') -parser.add_argument('--testMetadataPaths', '-t', required=True, - help='Path to Json Metadata', nargs='+') +parser = argparse.ArgumentParser(description="Appends test metadata to master database") +parser.add_argument( + "--masterTestListPath", + "-m", + default="masterTestList.json", + required=False, + help="Path to Master Metadata", +) +parser.add_argument( + "--testMetadataPaths", "-t", required=True, help="Path to Json Metadata", nargs="+" +) args = parser.parse_args() # Open master file if os.path.isfile(args.masterTestListPath): - with open(args.masterTestListPath) as f: - masterJson = json.load(f, object_pairs_hook=OrderedDict) + with open(args.masterTestListPath) as f: + masterJson = json.load(f, object_pairs_hook=OrderedDict) else: - masterJson = {"fields": [], "testcases": []} + masterJson = {"fields": [], "testcases": []} for testMetadata in args.testMetadataPaths: - - if not os.path.isfile(testMetadata): - print "Error: testMetadataPath does not exist" - print "Path: " + testMetadata - sys.exit(1) - - # Open test metadata - try: - with open(testMetadata) as f: - designJson = json.load(f, object_pairs_hook=OrderedDict) - except ValueError as e: - print("Error occured opening or loading json file.") - print >> sys.stderr, "Exception: %s" % str(e) - sys.exit(1) - - if not designJson["uuid"] in [d["uuid"] for d in masterJson["testcases"]]: - masterJson["testcases"].append(designJson) - - # Update Headers if necessary - for key in list(designJson): - if not key in masterJson["fields"]: - masterJson["fields"].append(key) - print "Updating fields with", key - else: - print("Skipping " + designJson["platform"] + "/" + designJson["design"] + - " (" + designJson["uuid"] + ") already in masterDB") + if not os.path.isfile(testMetadata): + print("Error: testMetadataPath does not exist") + print("Path: " + testMetadata) + sys.exit(1) + + # Open test metadata + try: + with open(testMetadata) as f: + designJson = json.load(f, object_pairs_hook=OrderedDict) + except ValueError as e: + print("Error occured opening or loading json file.") + print("Exception: %s" % str(e), file=sys.stderr) + sys.exit(1) + + if not designJson["uuid"] in [d["uuid"] for d in masterJson["testcases"]]: + masterJson["testcases"].append(designJson) + + # Update Headers if necessary + for key in list(designJson): + if not key in masterJson["fields"]: + masterJson["fields"].append(key) + print("Updating fields with", key) + else: + print( + "Skipping " + + designJson["platform"] + + "/" + + designJson["design"] + + " (" + + designJson["uuid"] + + ") already in masterDB" + ) # Dump JSON with open(args.masterTestListPath, "w") as f: - json.dump(masterJson, f, indent=2) + json.dump(masterJson, f, indent=2) # Dump CSV -csvFilePath = os.path.splitext(args.masterTestListPath)[0] + '.csv' -with open(csvFilePath, 'w') as csvfile: - fieldnames = list(masterJson["fields"]) - writer = csv.DictWriter(csvfile, fieldnames=fieldnames, - restval="-", extrasaction="ignore", dialect='excel') - - writer.writeheader() - for testcase in masterJson["testcases"]: - writer.writerow(testcase) +csvFilePath = os.path.splitext(args.masterTestListPath)[0] + ".csv" +with open(csvFilePath, "w") as csvfile: + fieldnames = list(masterJson["fields"]) + writer = csv.DictWriter( + csvfile, + fieldnames=fieldnames, + restval="-", + extrasaction="ignore", + dialect="excel", + ) + + writer.writeheader() + for testcase in masterJson["testcases"]: + writer.writerow(testcase)