File size: 2,713 Bytes
784ee2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #!/usr/bin/env python2
# This scripts appends the test metadata to the master json
# -------------------------------------------------------------------------
import argparse # argument parsing
import json # json parsing
import sys
import re
import os # filesystem manipulation
import csv
import datetime
import uuid
from collections import OrderedDict
# 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="+"
)
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)
else:
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("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)
# 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)
|