Bug 2189 fixing osm package-build command
[osm/osmclient.git] / osmclient / common / package_tool.py
index fe058db..2531f7a 100644 (file)
@@ -16,7 +16,6 @@
 #    under the License.
 
 import glob
-import hashlib
 import logging
 import os
 import shutil
@@ -29,6 +28,7 @@ from osm_im.validation import ValidationException
 from osm_im import im_translation
 from osmclient.common import package_handling as package_handling
 from osmclient.common.exceptions import ClientException
+from osmclient.common import utils
 from .sol004_package import SOL004Package
 from .sol007_package import SOL007Package
 import yaml
@@ -81,7 +81,7 @@ class PackageTool(object):
         self._logger.debug("")
         # print("location: {}".format(osmclient.__path__))
         file_loader = PackageLoader("osmclient")
-        env = Environment(loader=file_loader)
+        env = Environment(loader=file_loader, autoescape=True)
         if package_type == "ns":
             template = env.get_template("nsd.yaml.j2" if not old else "nsd_old.yaml.j2")
             content = {
@@ -396,13 +396,7 @@ class PackageTool(object):
             for file_item in files:
                 if "checksums.txt" in file_item:
                     continue
-                # from https://www.quickprogrammingtips.com/python/how-to-calculate-md5-hash-of-a-file-in-python.html
-                md5_hash = hashlib.md5()
-                with open(file_item, "rb") as f:
-                    # Read and update hash in chunks of 4K
-                    for byte_block in iter(lambda: f.read(4096), b""):
-                        md5_hash.update(byte_block)
-                    checksum.write("{}\t{}\n".format(md5_hash.hexdigest(), file_item))
+                checksum.write("{}\t{}\n".format(utils.md5(file_item), file_item))
 
     def create_folders(self, folders, package_type):
         """
@@ -525,13 +519,28 @@ class PackageTool(object):
             ]
         else:
             descriptors_paths = [f for f in glob.glob(package_folder + "/*.yaml")]
-        for file in descriptors_paths:
-            if file.endswith("nfd.yaml"):
-                descriptor_file = True
-                charms_set = self.charms_search(file, "vnf")
-            if file.endswith("nsd.yaml"):
-                descriptor_file = True
-                charms_set = self.charms_search(file, "ns")
+        if len(descriptors_paths) == 1:
+            # The base folder usually has a single yaml file with the descriptor
+            descriptor_file = True
+            pkg_type = utils.get_key_val_from_descriptor(descriptors_paths[0])
+            if pkg_type is None:
+                raise ClientException("Cannot determine package type")
+            if pkg_type["type"] == "nsd":
+                charms_set = self.charms_search(descriptors_paths[0], "ns")
+            else:
+                charms_set = self.charms_search(descriptors_paths[0], "vnf")
+        elif len(descriptors_paths) > 1:
+            for file in descriptors_paths:
+                if file.endswith("nfd.yaml"):
+                    descriptor_file = True
+                    charms_set = self.charms_search(file, "vnf")
+                if file.endswith("nsd.yaml"):
+                    descriptor_file = True
+                    charms_set = self.charms_search(file, "ns")
+        else:
+            raise ClientException(
+                "Package folder does not contain valid descriptor files '*.yaml'"
+            )
         print("List of charms in the descriptor: {}".format(charms_set))
         if not descriptor_file:
             raise ClientException(