Fix bug 2073 to delete properly unzipped packages during osm repo-index 94/12194/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 10 Jun 2022 11:24:19 +0000 (13:24 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 10 Jun 2022 11:24:32 +0000 (13:24 +0200)
Change-Id: I906a9cf66265f857e00f565caf0152806cd878f0
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/sol005/osmrepo.py

index 02318de..d731e30 100644 (file)
@@ -188,6 +188,7 @@ class OSMRepo(Repo):
         artifacts = []
         directories = []
         for f in listdir(origin):
+            self._logger.debug(f"Element: {join(origin,f)}")
             if isfile(join(origin, f)) and f.endswith(".tar.gz"):
                 artifacts.append(f)
             elif (
@@ -200,13 +201,15 @@ class OSMRepo(Repo):
                 )  # TODO: Document that nested directories are not supported
             else:
                 self._logger.debug(f"Ignoring {f}")
-        for artifact in artifacts:
-            self.register_artifact_in_repository(
-                join(origin, artifact), destination, source="artifact"
+        self._logger.debug(f"Artifacts: {artifacts}")
+        for package in artifacts:
+            self.register_package_in_repository(
+                join(origin, package), origin, destination, kind="artifact"
             )
-        for artifact in directories:
-            self.register_artifact_in_repository(
-                join(origin, artifact), destination, source="directory"
+        self._logger.debug(f"Directories: {directories}")
+        for package in directories:
+            self.register_package_in_repository(
+                join(origin, package), origin, destination, kind="directory"
             )
         self._logger.info("\nFinal Results: ")
         self._logger.info(
@@ -351,21 +354,24 @@ class OSMRepo(Repo):
         descriptor_file = glob.glob("{}/*.y*ml".format(folder))[0]
         return folder, descriptor_file
 
-    def validate_artifact(self, path, source):
+    def validate_artifact(self, path, origin, kind):
         """
         Validation of artifact.
         :param path: file path
-        :param source: flag to select the correct file type (directory or artifact)
+        :param origin: folder where the package is located
+        :param kind: flag to select the correct file type (directory or artifact)
         :return: status details, status, fields, package_type
         """
-        self._logger.debug(f"Validating {path} {source}")
+        self._logger.debug(f"Validating {path} {kind}")
         package_type = ""
         folder = ""
         try:
-            if source == "directory":
+            if kind == "directory":
                 descriptor_file = glob.glob("{}/*.y*ml".format(path))[0]
             else:
                 folder, descriptor_file = self.zip_extraction(path)
+                folder = join(origin, folder)
+                self._logger.debug(f"Kind is an artifact (tar.gz). Folder: {folder}. Descriptor_file: {descriptor_file}")
 
             self._logger.debug("Opening descriptor file: {}".format(descriptor_file))
 
@@ -409,25 +415,26 @@ class OSMRepo(Repo):
             if folder:
                 rmtree(folder, ignore_errors=True)
 
-    def register_artifact_in_repository(self, path, destination, source):
+    def register_package_in_repository(self, path, origin, destination, kind):
         """
         Registration of one artifact in a repository
-        param path:
-        param destination: path for index creation
-        param source:
+        :param path: absolute path of the VNF/NS package
+        :param origin: folder where the package is located
+        :param destination: path for index creation
+        :param kind: artifact (tar.gz) or directory
         """
         self._logger.debug("")
         pt = PackageTool()
         compressed = False
         try:
             fields = {}
-            _, valid, fields, package_type = self.validate_artifact(path, source)
+            _, valid, fields, package_type = self.validate_artifact(path, origin, kind)
             if not valid:
                 raise Exception(
                     "{} {} Not well configured.".format(package_type.upper(), str(path))
                 )
             else:
-                if source == "directory":
+                if kind == "directory":
                     path = pt.build(path)
                     self._logger.debug(f"Directory path {path}")
                     compressed = True
@@ -436,12 +443,12 @@ class OSMRepo(Repo):
 
         except Exception as e:
             self._logger.exception(
-                "Error registering artifact in Repository: {}".format(e)
+                "Error registering package in Repository: {}".format(e)
             )
             raise ClientException(e)
 
         finally:
-            if source == "directory" and compressed:
+            if kind == "directory" and compressed:
                 remove(path)
 
     def indexation(self, destination, path, package_type, fields):