def is_existing_image(self): """Check if the integration has an image.""" is_image_in_yml = False is_image_in_package = False data_dictionary = get_yaml(self.file_path) if not data_dictionary: return False if data_dictionary.get('image'): is_image_in_yml = True if not re.match(PACKS_INTEGRATION_NON_SPLIT_YML_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) image_path = glob.glob(package_path + '/*.png') if image_path: is_image_in_package = True if is_image_in_package and is_image_in_yml: error_message, error_code = Errors.image_in_package_and_yml() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False return False if not (is_image_in_package or is_image_in_yml): error_message, error_code = Errors.no_image_given() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False return False return True
def is_valid_beta_description(self): """Check if beta disclaimer exists in detailed description""" data_dictionary = get_yaml(self.file_path) description_in_yml = data_dictionary.get('detaileddescription', '') if data_dictionary else '' if not re.match(PACKS_INTEGRATION_YML_REGEX, self.file_path, re.IGNORECASE): try: md_file_path = glob.glob(os.path.join(os.path.dirname(self.file_path), '*_description.md'))[0] except IndexError: error_message, error_code = Errors.description_missing_in_beta_integration() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False return False with open(md_file_path) as description_file: description = description_file.read() if BETA_INTEGRATION_DISCLAIMER not in description: error_message, error_code = Errors.no_beta_disclaimer_in_description() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False return False else: return True elif BETA_INTEGRATION_DISCLAIMER not in description_in_yml: error_message, error_code = Errors.no_beta_disclaimer_in_yml() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False return False return True
def oversize_image(self): """Check if the image if over sized, bigger than IMAGE_MAX_SIZE""" if re.match(IMAGE_REGEX, self.file_path, re.IGNORECASE): if os.path.getsize( self.file_path ) > self.IMAGE_MAX_SIZE: # disable-secrets-detection error_message, error_code = Errors.image_too_large() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False else: data_dictionary = get_yaml(self.file_path) if not data_dictionary: return image = data_dictionary.get('image', '') if ((len(image) - 22) / 4.0 ) * 3 > self.IMAGE_MAX_SIZE: # disable-secrets-detection error_message, error_code = Errors.image_too_large() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False
def is_duplicate_description(self): """Check if the integration has a non-duplicate description .""" is_description_in_yml = False is_description_in_package = False package_path = None md_file_path = None if not re.match(PACKS_INTEGRATION_YML_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) try: md_file_path = glob.glob(os.path.join(os.path.dirname(self.file_path), '*_description.md'))[0] except IndexError: print_warning("No detailed description file was found in the package {}." " Consider adding one.".format(package_path)) if md_file_path: is_description_in_package = True data_dictionary = get_yaml(self.file_path) if not data_dictionary: return is_description_in_package if data_dictionary.get('detaileddescription'): is_description_in_yml = True if is_description_in_package and is_description_in_yml: error_message, error_code = Errors.description_in_package_and_yml() if self.handle_error(error_message, error_code, file_path=package_path): self._is_valid = False return False return True
def is_existing_image(self): """Check if the integration has an image.""" is_image_in_yml = False is_image_in_package = False data_dictionary = get_yaml(self.file_path) if not data_dictionary: return False if data_dictionary.get('image'): is_image_in_yml = True if not re.match(INTEGRATION_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) image_path = glob.glob(package_path + '/*.png') if image_path: is_image_in_package = True if is_image_in_package and is_image_in_yml: print_error( "The file {} has image in both yml and package, remove the 'image' key from the yml file" .format(self.file_path)) self._is_valid = False return False if not (is_image_in_package or is_image_in_yml): print_error( "You have failed to add an image in the yml/package for {}". format(self.file_path)) self._is_valid = False return False return True
def is_duplicate_description(self): """Check if the integration has a non-duplicate description .""" is_description_in_yml = False is_description_in_package = False package_path = None md_file_path = None if not re.match(PACKS_INTEGRATION_YML_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) try: path_without_extension = os.path.splitext(self.file_path)[0] md_file_path = glob.glob(path_without_extension + '_description.md')[0] except IndexError: error_message, error_code = Errors.no_description_file_warning() self.handle_error(error_message, error_code, file_path=self.file_path, warning=True) if md_file_path: is_description_in_package = True data_dictionary = get_yaml(self.file_path) if not data_dictionary: return is_description_in_package if data_dictionary.get('detaileddescription'): is_description_in_yml = True if is_description_in_package and is_description_in_yml: error_message, error_code = Errors.description_in_package_and_yml() if self.handle_error(error_message, error_code, file_path=package_path): self._is_valid = False return False return True
def oversize_image(self): """Check if the image if over sized, bigger than IMAGE_MAX_SIZE""" if re.match(IMAGE_REGEX, self.file_path, re.IGNORECASE): if os.path.getsize( self.file_path ) > self.IMAGE_MAX_SIZE: # disable-secrets-detection print_error( "{} has too large logo, please update the logo to be under 10kB" .format(self.file_path)) self._is_valid = False else: data_dictionary = get_yaml(self.file_path) if not data_dictionary: return image = data_dictionary.get('image', '') if ((len(image) - 22) / 4.0 ) * 3 > self.IMAGE_MAX_SIZE: # disable-secrets-detection print_error( "{} has too large logo, please update the logo to be under 10kB" .format(self.file_path)) self._is_valid = False
def load_image(self): if re.match(IMAGE_REGEX, self.file_path, re.IGNORECASE): with open(self.file_path, "rb") as image: image_data = image.read() image = base64.b64encode(image_data) # type: ignore if isinstance(image, bytes): image = image.decode("utf-8") else: image = self.load_image_from_yml() return image
def __init__(self, file_path): self._is_valid = True if checked_type(file_path, INTEGRATION_REGXES) or re.match(IMAGE_REGEX, file_path, re.IGNORECASE): self.file_path = file_path else: if checked_type(file_path, YML_INTEGRATION_REGEXES): try: self.file_path = glob.glob(os.path.join(os.path.dirname(file_path), '*.png'))[0] except IndexError: self._is_valid = False print_error("You've created/modified a package but failed to provide an image as a .png file, " "please add an image in order to proceed.")
def is_duplicate_description(self): """Check if the integration has a non-duplicate description .""" is_description_in_yml = False is_description_in_package = False package_path = None md_file_path = None if not re.match(INTEGRATION_REGEX, self.file_path, re.IGNORECASE) \ and not re.match(BETA_INTEGRATION_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) try: md_file_path = glob.glob( os.path.join(os.path.dirname(self.file_path), '*_description.md'))[0] except IndexError: print_warning( "No detailed description file was found in the package {}." " Consider adding one.".format(package_path)) if md_file_path: is_description_in_package = True data_dictionary = get_yaml(self.file_path) if not data_dictionary: return is_description_in_package if data_dictionary.get('detaileddescription'): is_description_in_yml = True if is_description_in_package and is_description_in_yml: self._is_valid = False print_error( "A description was found both in the package and in the yml, " "please update the package {}.".format(package_path)) return False return True
def validate_size(self, allow_empty_image_file: bool, maximum_size: int = IMAGE_MAX_SIZE, should_validate_dimensions: bool = False, allowed_width: int = IMAGE_WIDTH, allowed_height: int = IMAGE_HEIGHT) -> None: """ Checks if image has a valid size. if 'allow_empty_image_file' is true, checks that the image file is not empty. Args: allow_empty_image_file (bool): Whether empty image file is an error. maximum_size (int): Maximum allowed size. should_validate_dimensions (bool): Should validate the image dimensions. allowed_height (int): the allowed height of the image allowed_width (int): the allowed weight of the image """ if re.match(IMAGE_REGEX, self.file_path, re.IGNORECASE): image_size = os.path.getsize(self.file_path) if image_size > maximum_size: # disable-secrets-detection error_message, error_code = Errors.image_too_large() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False if should_validate_dimensions: width, height = imagesize.get(self.file_path) if (width, height) != (allowed_width, allowed_height): error_message, error_code = Errors.invalid_image_dimensions(width, height) if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False else: data_dictionary = get_yaml(self.file_path) if not data_dictionary: return image = data_dictionary.get('image', '') image_size = int(((len(image) - 22) / 4) * 3) if image_size > self.IMAGE_MAX_SIZE: # disable-secrets-detection error_message, error_code = Errors.image_too_large() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False if not allow_empty_image_file and image_size == 0: error_message, error_code = Errors.image_is_empty(self.file_path) if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False
def is_valid_beta_description(self): """Check if beta disclaimer exists in detailed description""" data_dictionary = get_yaml(self.file_path) description_in_yml = data_dictionary.get('detaileddescription', '') if data_dictionary else '' if not re.match(BETA_INTEGRATION_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) try: md_file_path = glob.glob( os.path.join(os.path.dirname(self.file_path), '*_description.md'))[0] except IndexError: self._is_valid = False print_error( "No detailed description file was found in the package {}. Please add one," " and make sure it includes the beta disclaimer note." "It should contain the string in constant" "\"BETA_INTEGRATION_DISCLAIMER\"".format(package_path)) return False with open(md_file_path) as description_file: description = description_file.read() if BETA_INTEGRATION_DISCLAIMER not in description: self._is_valid = False print_error( "Detailed description in beta integration package {} " "dose not contain the beta disclaimer note. " "It should contain the string in constant" " \"BETA_INTEGRATION_DISCLAIMER\".".format(package_path)) return False else: return True elif BETA_INTEGRATION_DISCLAIMER not in description_in_yml: self._is_valid = False print_error("Detailed description field in beta integration {} " "dose not contain the beta disclaimer note." "It should contain the string in constant" " \"BETA_INTEGRATION_DISCLAIMER\".".format( self.file_path)) return False return True
def is_duplicate_description(self): """Check if the integration has a non-duplicate description .""" is_description_in_yml = False is_description_in_package = False package_path = None md_file_path = None if not re.match(PACKS_INTEGRATION_YML_REGEX, self.file_path, re.IGNORECASE): package_path = os.path.dirname(self.file_path) try: base_name_without_extension: str = os.path.basename(os.path.splitext(self.file_path)[0].replace( '_description', '')) dir_name: str = os.path.dirname(self.file_path) expected_description_name: str = os.path.join(dir_name, f'{base_name_without_extension}_description.md') md_file_path = glob.glob(expected_description_name)[0] except IndexError: is_unified_integration = self.data_dictionary.get('script', {}).get('script', '') not in {'-', ''} if not (self.data_dictionary.get('deprecated') or is_unified_integration): error_message, error_code = Errors.no_description_file_warning() self.handle_error(error_message, error_code, file_path=self.file_path, warning=True) if md_file_path: is_description_in_package = True if not self.data_dictionary: return is_description_in_package if self.data_dictionary.get('detaileddescription'): is_description_in_yml = True if is_description_in_package and is_description_in_yml: error_message, error_code = Errors.description_in_package_and_yml() if self.handle_error(error_message, error_code, file_path=package_path): self._is_valid = False return False return True
def __init__(self, file_path, ignored_errors=None, print_as_warnings=False): super().__init__(ignored_errors=ignored_errors, print_as_warnings=print_as_warnings) self._is_valid = True if checked_type(file_path, INTEGRATION_REGXES) or re.match( IMAGE_REGEX, file_path, re.IGNORECASE): self.file_path = file_path else: if checked_type(file_path, YML_INTEGRATION_REGEXES): try: self.file_path = glob.glob( os.path.join(os.path.dirname(file_path), '*.png'))[0] except IndexError: error_message, error_code = Errors.no_image_given() if self.handle_error(error_message, error_code, file_path=self.file_path): self._is_valid = False self.file_path = ''