Skip to content

Commit

Permalink
Fixed Pylint Errors
Browse files Browse the repository at this point in the history
Signed-off-by: Kanav Phull <[email protected]>
  • Loading branch information
kanavphull committed Mar 21, 2024
1 parent d96f15c commit d25dc4a
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions scripts/image_tags_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -138,28 +136,42 @@ 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
if image_path.strip().split("/")[-1] != "ironbank":
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:
Expand All @@ -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:
Expand All @@ -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 """
Expand Down

0 comments on commit d25dc4a

Please sign in to comment.