Blueprint validation

#!/usr/bin/python
import os
import sys
import yaml
import json
from termcolor import colored
bp_dir = sys.argv[1]
cwd = os.getcwd()
bppath = os.path.join(cwd, bp_dir)
inpath = os.path.join(bppath, 'input')
y_file = os.path.join(bppath, 'blueprint.yaml')
def get_diff(list1, list2):
   difference = set(list1).symmetric_difference(set(list2))
   list_difference = list(difference)
   if len(list_difference) == 0:
       print("input.json parameters are matching with blueprint.yaml")
   else:
       print('Found miss match between YAML and JSON, please check below parameters:')
       print(colored(list_difference, 'red'))
def yaml_json_compare(yaml_file, json_file):
with open(yaml_file, 'r') as stream:
   try:
       dict = yaml.load(stream, Loader=yaml.FullLoader)
       for key, value in dict.items():
           if key == 'inputs':
               dict2 = value
               aList = list(dict2)
               #print(aList)
               with open(json_file) as f:
                   try:
                       data = json.load(f)
                       bList = list(data)
                       print(json_file + " is valid")
                       get_diff(aList, bList)
                   except ValueError as exc:
                       print(json_file + " is invalid")
   except yaml.YAMLError as exc:
       print(exc)
       print("blueprint.yaml is invalid")
def walk(dirname):
   for name in os.listdir(dirname):
       path = os.path.join(dirname, name)
       if os.path.isfile(path):
           yaml_json_compare(y_file, path)
       else:
           walk(path)
if __name__ == '__main__':
   walk(inpath)
here is the output:
(venv) lindas-MacBook-Air:hello linda$ python3 write_excel.py DEMO
/Users/linda/PycharmProjects/hello/DEMO/input/SIT/inputs.json is valid
Found miss match between YAML and JSON, please check below parameters:
['vm_size']
/Users/linda/PycharmProjects/hello/DEMO/input/DEV/inputs.json is valid
Found miss match between YAML and JSON, please check below parameters:
['cost1', 'cost']
/Users/linda/PycharmProjects/hello/DEMO/input/UAT/inputs.json is valid
input.json parameters are matching with blueprint.yaml

Leave a Reply

Your email address will not be published. Required fields are marked *