X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ice_validator%2Ftests%2Futils%2Fnested_files.py;h=c551646f836d7b1203e080e5d0aa4fffff60ead0;hb=refs%2Fchanges%2F83%2F66483%2F4;hp=02f733d148bec6a72087d2cabcfd61f99ffd1752;hpb=8de26dd1cc9ed33c3ab85a5014ac949f174db932;p=vvp%2Fvalidation-scripts.git diff --git a/ice_validator/tests/utils/nested_files.py b/ice_validator/tests/utils/nested_files.py index 02f733d..c551646 100644 --- a/ice_validator/tests/utils/nested_files.py +++ b/ice_validator/tests/utils/nested_files.py @@ -43,17 +43,17 @@ from os import path import re -import yaml +from tests import cached_yaml as yaml -VERSION = "1.0.2" +VERSION = '1.0.2' def get_list_of_nested_files(yml, dirpath): - """ + ''' return a list of all nested files - """ + ''' - if not hasattr(yml, "items"): + if not hasattr(yml, 'items'): return [] nested_files = [] @@ -63,34 +63,41 @@ def get_list_of_nested_files(yml, dirpath): t = v["type"] if t.endswith(".yml") or t.endswith(".yaml"): filepath = path.join(dirpath, t) - with open(filepath) as fh: - t_yml = yaml.load(fh) - nested_files.append(filepath) - nested_files.extend(get_list_of_nested_files(t_yml, dirpath)) + if path.exists(filepath): + with open(filepath) as fh: + t_yml = yaml.load(fh) + nested_files.append(filepath) + nested_files.extend(get_list_of_nested_files(t_yml, dirpath)) elif t == "OS::Heat::ResourceGroup": - rdt = v.get("properties", {}).get("resource_def", {}).get("type", None) + rdt = (v.get("properties", {}) + .get("resource_def", {}) + .get("type", None)) if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")): filepath = path.join(dirpath, rdt) - with open(filepath) as fh: - rdt_yml = yaml.load(fh) - nested_files.append(filepath) - nested_files.extend(get_list_of_nested_files(rdt_yml, dirpath)) + if path.exists(filepath): + with open(filepath) as fh: + rdt_yml = yaml.load(fh) + nested_files.append(filepath) + nested_files.extend( + get_list_of_nested_files(rdt_yml, dirpath)) if isinstance(v, dict): - nested_files.extend(get_list_of_nested_files(v, dirpath)) + nested_files.extend( + get_list_of_nested_files(v, dirpath)) elif isinstance(v, list): for d in v: - nested_files.extend(get_list_of_nested_files(d, dirpath)) + nested_files.extend( + get_list_of_nested_files(d, dirpath)) return nested_files def check_for_invalid_nesting(yml, yaml_file, dirpath): - """ + ''' return a list of all nested files - """ - if not hasattr(yml, "items"): + ''' + if not hasattr(yml, 'items'): return [] invalid_nesting = [] - p = re.compile("^[A-z]*::[A-z]*::[A-z]*$") + p = re.compile('^[A-z]*::[A-z]*::[A-z]*$') for v in yml.values(): if isinstance(v, dict) and "type" in v: @@ -102,9 +109,7 @@ def check_for_invalid_nesting(yml, yaml_file, dirpath): if not isinstance(rd, dict) or "type" not in rd: invalid_nesting.append(yaml_file) continue - elif not p.match(rd["type"]) and not ( - rd["type"].endswith(".yml") or rd["type"].endswith(".yaml") - ): + elif not p.match(rd["type"]): filepath = path.join(dirpath, rd["type"]) else: continue @@ -115,11 +120,21 @@ def check_for_invalid_nesting(yml, yaml_file, dirpath): yml = yaml.load(fh) except yaml.YAMLError as e: invalid_nesting.append(filepath) - print(e) # pylint: disable=superfluous-parens - invalid_nesting.extend(check_for_invalid_nesting(yml, filepath, dirpath)) + print(e) # pylint: disable=superfluous-parens + invalid_nesting.extend(check_for_invalid_nesting( + yml, + filepath, + dirpath)) if isinstance(v, dict): - invalid_nesting.extend(check_for_invalid_nesting(v, yaml_file, dirpath)) + invalid_nesting.extend(check_for_invalid_nesting( + v, + yaml_file, + dirpath)) elif isinstance(v, list): for d in v: - invalid_nesting.extend(check_for_invalid_nesting(d, yaml_file, dirpath)) + invalid_nesting.extend(check_for_invalid_nesting( + d, + yaml_file, + dirpath)) return invalid_nesting +