X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Flcm_utils.py;h=956e44f70de2b34e5a9700e7bb1261e9c42a48c6;hb=1dda84cafdeeb4a6affb443e65102a0c8149ec5c;hp=7dac350bb383b6b640bca4c4d47ee277efc39731;hpb=1411a004e64e080851cfd072d88edbf7cccd78d0;p=osm%2FLCM.git diff --git a/osm_lcm/lcm_utils.py b/osm_lcm/lcm_utils.py index 7dac350..956e44f 100644 --- a/osm_lcm/lcm_utils.py +++ b/osm_lcm/lcm_utils.py @@ -19,6 +19,7 @@ import asyncio import checksumdir from collections import OrderedDict +import hashlib import os import shutil import traceback @@ -200,6 +201,54 @@ class LcmBase: # except DbException as e: # self.logger.error("Updating {} _id={} with '{}'. Error: {}".format(item, _id, _desc, e)) + @staticmethod + def calculate_charm_hash(zipped_file): + """Calculate the hash of charm files which ends with .charm + + Args: + zipped_file (str): Existing charm package full path + + Returns: + hex digest (str): The hash of the charm file + """ + filehash = hashlib.md5() + with open(zipped_file, mode="rb") as file: + contents = file.read() + filehash.update(contents) + return filehash.hexdigest() + + @staticmethod + def compare_charm_hash(current_charm, target_charm): + """Compare the existing charm and the target charm if the charms + are given as zip files ends with .charm + + Args: + current_charm (str): Existing charm package full path + target_charm (str): Target charm package full path + + Returns: + True/False (bool): if charm has changed it returns True + """ + return LcmBase.calculate_charm_hash( + current_charm + ) != LcmBase.calculate_charm_hash(target_charm) + + @staticmethod + def compare_charmdir_hash(current_charm_dir, target_charm_dir): + """Compare the existing charm and the target charm if the charms + are given as directories + + Args: + current_charm_dir (str): Existing charm package directory path + target_charm_dir (str): Target charm package directory path + + Returns: + True/False (bool): if charm has changed it returns True + """ + return checksumdir.dirhash(current_charm_dir) != checksumdir.dirhash( + target_charm_dir + ) + def check_charm_hash_changed( self, current_charm_path: str, target_charm_path: str ) -> bool: @@ -214,25 +263,30 @@ class LcmBase: True/False (bool): if charm has changed it returns True """ - # Check if the charm artifacts are available - if os.path.exists(self.fs.path + current_charm_path) and os.path.exists( - self.fs.path + target_charm_path - ): - # Compare the hash of charm folders - if checksumdir.dirhash( - self.fs.path + current_charm_path - ) != checksumdir.dirhash(self.fs.path + target_charm_path): + try: + # Check if the charm artifacts are available + current_charm = self.fs.path + current_charm_path + target_charm = self.fs.path + target_charm_path - return True + if os.path.exists(current_charm) and os.path.exists(target_charm): - return False + # Compare the hash of .charm files + if current_charm.endswith(".charm"): + return LcmBase.compare_charm_hash(current_charm, target_charm) - else: - raise LcmException( - "Charm artifact {} does not exist in the VNF Package".format( - self.fs.path + target_charm_path + # Compare the hash of charm folders + return LcmBase.compare_charmdir_hash(current_charm, target_charm) + + else: + raise LcmException( + "Charm artifact {} does not exist in the VNF Package".format( + self.fs.path + target_charm_path + ) ) - ) + except (IOError, OSError, TypeError) as error: + self.logger.debug(traceback.format_exc()) + self.logger.error(f"{error} occured while checking the charm hashes") + raise LcmException(error) @staticmethod def get_charm_name(charm_metadata_file: str) -> str: