From d25dc4a7625963a75b6f4851e9acab9d9cf27e2d Mon Sep 17 00:00:00 2001 From: Kanav Phull Date: Thu, 21 Mar 2024 02:35:59 -0700 Subject: [PATCH] Fixed Pylint Errors Signed-off-by: Kanav Phull --- scripts/image_tags_check.py | 47 +++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/scripts/image_tags_check.py b/scripts/image_tags_check.py index bc6dbfc3a0..eb60dd248a 100644 --- a/scripts/image_tags_check.py +++ b/scripts/image_tags_check.py @@ -32,12 +32,12 @@ class ImageTagsCheckHelper: _fetch_image_tags(image_name, repo, image_path): Fetches the tags for a specific image. run_tags_check(): Runs the tag check for all images in the image list file. """ - # pylint: disable=all def __init__(self) -> None: self.docherhub_username = os.environ.get("DOCKERHUB_USERNAME") self.dockerhub_password = os.environ.get("DOCKERHUB_PASSWORD") self.image_list_file = "image.lst" + self.image_list_file_path = os.path.join(self.script_path, "..", self.image_list_file) self.script_path = os.path.dirname(os.path.abspath(__file__)) self._dockerhub_auth_token = self._get_auth_token() @@ -64,8 +64,6 @@ def _get_auth_token(self): return token except requests.RequestException as e: raise requests.RequestException(f"Failed to make request to Docker Hub API: {e}") - except Exception as e: - raise Exception(f"Failed to authenticate with Docker Hub API: {e}") @staticmethod def parse_tags_from_api_response(response): @@ -110,14 +108,14 @@ def _fetch_image_tags(self, image_name, repo, image_path): if response.status_code == 200: source_image_tags = self.parse_tags_from_api_response(response) return source_image_tags - logging.error(f"Failed to fetch tags for {image_path}. Status code: {response.status_code}") + logging.error("Failed to fetch tags for %s. Status code: %d", image_path, response.status_code) return None except requests.RequestException as e: logging.error(f"Failed to fetch tags for {image_path}: {e}") return None @staticmethod - def _match_tags(source_image_tags, hardened_image_tags, image_path): + def match_tags(source_image_tags, hardened_image_tags, image_path): """ Checks if there are common tags between the source image tags and the hardened image tags. @@ -138,13 +136,32 @@ def _match_tags(source_image_tags, hardened_image_tags, image_path): # The value set to 2 can be changed according to the strictness required for tag matching. return common_count >= 2 + @staticmethod + def read_yml_from_path(yml_file_path): + """ + Reads a YAML file from the given file path and returns the parsed YAML data as a dictionary. + + Args: + yml_file_path (str): The path to the YAML file. + + Returns: + dict: A dictionary containing the parsed YAML data. + """ + try: + with open(yml_file_path, "r", encoding="utf8") as yml_stream: + yml_dict = yaml.safe_load(yml_stream) + return yml_dict + except yaml.YAMLError as exc: + logging.error("Cannot fetch image_dict from image.yml") + logging.error(exc) + return {} + def run_tags_check(self): """ Runs the tag check for all images in the image list file. """ failed_images = [] - image_list_file_path = os.path.join(self.script_path, "..", self.image_list_file) - with open(image_list_file_path, "r", encoding="utf8") as stream: + with open(self.image_list_file_path, "r", encoding="utf8") as stream: for image_path in stream.readlines(): image_path = image_path.strip() # all images except ironbank ones are supported @@ -152,14 +169,9 @@ def run_tags_check(self): logging.info(f"Starting Tag Check for Image: {image_path}") # reading image.yml for image name and repo - image_yml_path = os.path.join(self.script_path, "..", "community_images", image_path, "image.yml") - try: - with open(image_yml_path, "r", encoding="utf8") as yml_stream: - # this image_dict is later used to extract image name and repo for both source and hardened image - image_dict = yaml.safe_load(yml_stream) - except yaml.YAMLError as exc: - logging.error("Cannot fetch image_dict from image.yml") - logging.error(exc) + image_yml_path = os.path.join(self.script_path, "..", + "community_images", image_path, "image.yml") + image_dict = self.read_yml_from_path(image_yml_path) source_image_repo_link = image_dict.get("source_image_repo_link") if "/_/" in source_image_repo_link: @@ -182,7 +194,7 @@ def run_tags_check(self): # tag matching logic checks if we have atleast two tag matching for source and hardened image logging.info("Matching Tags ...") - if self._match_tags(source_image_tags, hardened_image_tags, image_path) is False: + if self.match_tags(source_image_tags, hardened_image_tags, image_path) is False: logging.warning(f"🚨 {image_path} image is not supported 🚨\n") failed_images.append(image_path) else: @@ -194,8 +206,7 @@ def run_tags_check(self): logging.info(image) logging.warning("Error: Some Images are not supported") sys.exit(1) - else: - logging.info("✅✅ Script Executed Successfully! All images are supported! ✅✅") + logging.info("✅✅ Script Executed Successfully! All images are supported! ✅✅") def main(): """ main function """