def has_prefix_cce(yaml_file, product_yaml=None): rule = yaml.open_and_macro_expand(yaml_file, product_yaml) if 'identifiers' in rule and rule['identifiers'] is not None: for i_type, i_value in rule['identifiers'].items(): if i_type[0:3] == 'cce': has_prefix = i_value[0:3].upper() == 'CCE' remainder_valid = checks.is_cce_format_valid("CCE-" + i_value[3:]) remainder_valid |= checks.is_cce_format_valid("CCE-" + i_value[4:]) return has_prefix and remainder_valid return False
def rewrite_value_remove_prefix(line): # Rewrites a key's value to remove a "CCE" prefix. key_end = line.index(':') key = line[0:key_end] value = line[key_end + 1:].strip() new_value = value if checks.is_cce_format_valid("CCE-" + value[3:]): new_value = value[3:] elif checks.is_cce_format_valid("CCE-" + value[4:]): new_value = value[4:] return key + ": " + new_value
def rewrite_value_remove_prefix(line): # Rewrites a key's value to remove a "CCE" prefix. key_end = line.index(':') key = line[0:key_end] value = line[key_end+1:].strip() new_value = value if checks.is_cce_format_valid("CCE-" + value[3:]): new_value = value[3:] elif checks.is_cce_format_valid("CCE-" + value[4:]): new_value = value[4:] return key + ": " + new_value
def fix_prefix_cce(file_contents, yaml_contents): section = 'identifiers' prefixed_identifiers = [] if yaml_contents[section] is not None: for i_type, i_value in yaml_contents[section].items(): if i_type[0:3] == 'cce': has_prefix = i_value[0:3].upper() == 'CCE' remainder_valid = checks.is_cce_format_valid("CCE-" + str(i_value[3:])) remainder_valid |= checks.is_cce_format_valid("CCE-" + str(i_value[4:])) if has_prefix and remainder_valid: prefixed_identifiers.append(i_type) return rewrite_section_value(file_contents, yaml_contents, section, prefixed_identifiers, rewrite_value_remove_prefix)