Created
February 19, 2016 10:21
-
-
Save flou/c9a7231192f93a16019f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import json | |
import os | |
def find_nodes(node, key, debug=False): | |
if isinstance(node, unicode) or isinstance(node, list): | |
return | |
for k, v in node.items(): | |
if k == key: | |
yield v | |
elif isinstance(v, list): | |
for n in v: | |
for id_val in find_nodes(n, key): | |
yield id_val | |
elif isinstance(v, dict): | |
for id_val in find_nodes(v, key): | |
yield id_val | |
def ref_exists(cform, key, file): | |
if not [x for x in find_nodes(cform, key)]: | |
print('TemplateError: Ref("%s") is undefined in %s' % (key, file)) | |
def sanity_check(directory): | |
cform_files = [f for f in glob.glob(directory + '/*.cform') if os.path.isfile(f)] | |
for cform_file in cform_files: | |
print('Checking %s' % cform_file) | |
with open(cform_file, 'r') as file: | |
data = json.load(file) | |
resources = data.copy() | |
if 'Outputs' in data.keys(): | |
del resources['Outputs'] | |
refs = [x for x in find_nodes(data, 'Ref')] | |
for ref in refs: | |
ref_exists(resources, ref, cform_file) | |
sanity_check('.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment