Coverage for osmclient/common/package_handling.py: 29%
28 statements
« prev ^ index » next coverage.py v7.3.1, created at 2024-06-22 09:01 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2024-06-22 09:01 +0000
1# Copyright ETSI Contributors and Others.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
16import osmclient.common.utils as utils
17import os
19SOL004_TOSCA = "SOL004_TOSCA"
20SOL004 = "SOL004"
21SOL007_TOSCA = "SOL007_TOSCA"
22SOL007 = "SOL007"
23OSM_OLD = "OSM_OLD"
26def get_package_type(package_folder):
27 """
28 Detects the package's structure and returns the type:
29 SOL004
30 SOL007
31 OSM_OLD
32 """
34 package_files = os.listdir(package_folder)
35 if "Definitions" in package_files and "TOSCA-Metadata" in package_files:
36 descriptors = [
37 definition
38 for definition in os.listdir(package_folder + "/Definitions")
39 if definition.endswith(".yaml") or definition.endswith(".yml")
40 ]
41 if len(descriptors) < 1:
42 raise Exception(
43 "No descriptor found on this package, OSM was expecting at least 1"
44 )
45 pkg_type = utils.get_key_val_from_pkg(descriptors[0])
46 if pkg_type == "nsd":
47 return SOL007_TOSCA
48 else:
49 return SOL004_TOSCA
50 else:
51 manifests = [afile for afile in package_files if afile.endswith(".mf")]
52 if len(manifests) < 1:
53 # No manifest found, probably old OSM package structure
54 return OSM_OLD
55 else:
56 descriptors = [
57 definition
58 for definition in package_files
59 if definition.endswith(".yaml") or definition.endswith(".yml")
60 ]
61 if len(descriptors) < 1:
62 raise Exception(
63 "No descriptor found on this package, OSM was expecting at least 1"
64 )
65 with open(os.path.join(package_folder, descriptors[0])) as descriptor:
66 pkg_type = utils.get_key_val_from_descriptor(descriptor)
67 if pkg_type["type"] == "nsd":
68 return SOL007
69 else:
70 return SOL004