Fix bug 2074 to delete properly unzipped packages during osm repo-index 43/12443/1 v10.0 v10.1.3 v10.1.4 v10.1.5
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 10 Jun 2022 11:24:19 +0000 (13:24 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 10 Aug 2022 14:34:48 +0000 (16:34 +0200)
Change-Id: I906a9cf66265f857e00f565caf0152806cd878f0
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/sol005/osmrepo.py

index 3568e8c..b14d051 100644 (file)
@@ -188,19 +188,22 @@ class OSMRepo(Repo):
         artifacts = []
         directories = []
         for f in listdir(origin):
-            if isfile(join(origin, f)) and f.endswith('.tar.gz'):
+            self._logger.debug(f"Element: {join(origin,f)}")
+            if isfile(join(origin, f)) and f.endswith(".tar.gz"):
                 artifacts.append(f)
             elif isdir(join(origin, f)) and f != destination.split('/')[-1] and not f.startswith('.'):
                 directories.append(f)  # 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(
@@ -337,21 +340,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))
 
@@ -395,25 +401,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
@@ -422,12 +429,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):