2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
28 # https://creativecommons.org/licenses/by/4.0/
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
36 # ============LICENSE_END============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
46 from tests import cached_yaml as yaml
51 def get_list_of_nested_files(yml, dirpath):
53 return a list of all nested files
56 if not hasattr(yml, 'items'):
61 for v in yml.values():
62 if isinstance(v, dict) and "type" in v:
64 if t.endswith(".yml") or t.endswith(".yaml"):
65 filepath = path.join(dirpath, t)
66 if path.exists(filepath):
67 with open(filepath) as fh:
69 nested_files.append(filepath)
70 nested_files.extend(get_list_of_nested_files(t_yml, dirpath))
71 elif t == "OS::Heat::ResourceGroup":
72 rdt = (v.get("properties", {})
73 .get("resource_def", {})
75 if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")):
76 filepath = path.join(dirpath, rdt)
77 if path.exists(filepath):
78 with open(filepath) as fh:
79 rdt_yml = yaml.load(fh)
80 nested_files.append(filepath)
82 get_list_of_nested_files(rdt_yml, dirpath))
83 if isinstance(v, dict):
85 get_list_of_nested_files(v, dirpath))
86 elif isinstance(v, list):
89 get_list_of_nested_files(d, dirpath))
93 def check_for_invalid_nesting(yml, yaml_file, dirpath):
95 return a list of all nested files
97 if not hasattr(yml, 'items'):
100 p = re.compile('^[A-z]*::[A-z]*::[A-z]*$')
102 for v in yml.values():
103 if isinstance(v, dict) and "type" in v:
105 if t.endswith(".yml") or t.endswith(".yaml"):
106 filepath = path.join(dirpath, t)
107 elif t == "OS::Heat::ResourceGroup":
108 rd = v["properties"]["resource_def"]
109 if not isinstance(rd, dict) or "type" not in rd:
110 invalid_nesting.append(yaml_file)
112 elif not p.match(rd["type"]):
113 filepath = path.join(dirpath, rd["type"])
119 with open(filepath) as fh:
121 except yaml.YAMLError as e:
122 invalid_nesting.append(filepath)
123 print(e) # pylint: disable=superfluous-parens
124 invalid_nesting.extend(check_for_invalid_nesting(
128 if isinstance(v, dict):
129 invalid_nesting.extend(check_for_invalid_nesting(
133 elif isinstance(v, list):
135 invalid_nesting.extend(check_for_invalid_nesting(
139 return invalid_nesting