def has_int_reference(yaml_file, product_yaml=None): rule = yaml.open_and_macro_expand(yaml_file, product_yaml) if 'references' in rule and rule['references'] is not None: for _, value in rule['references'].items(): if type(value) != str: return True return False
def has_invalid_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': if not checks.is_cce_value_valid("CCE-" + str(i_value)): return True return False
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 has_empty_references(yaml_file, product_yaml=None): rule = yaml.open_and_macro_expand(yaml_file, product_yaml) if 'references' in rule and rule['references'] is None: return True if 'references' in rule and rule['references'] is not None: for _, value in rule['references'].items(): if str(value).strip() == "": return True return False
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 = cce.is_cce_format_valid("CCE-" + i_value[3:]) remainder_valid |= cce.is_cce_format_valid("CCE-" + i_value[4:]) return has_prefix and remainder_valid return False
def has_unordered_sections(yaml_file, product_yaml=None): rule = yaml.open_and_macro_expand(yaml_file, product_yaml) if 'references' in rule or 'identifiers' in rule: rule_lines = read_file_list(yaml_file) new_lines = ssg.rule_yaml.sort_section_keys(yaml_file, rule_lines, TO_SORT) # Compare string representations to avoid issues with references being # different. return "\n".join(rule_lines) != "\n".join(new_lines) return False
def has_no_cce(yaml_file, product_yaml=None): rule = yaml.open_and_macro_expand(yaml_file, product_yaml) product = product_yaml["product"] if "prodtype" in rule and product not in rule["prodtype"]: return False if 'identifiers' in rule and rule['identifiers'] is None: return True if 'identifiers' in rule and rule['identifiers'] is not None: for ident in rule['identifiers']: if ident == "cce@" + product: return False return True
def _fixed_file_contents(path, file_contents, product_yaml, func): if file_contents[-1] == '': file_contents = file_contents[:-1] subst_dict = product_yaml yaml_contents = yaml.open_and_macro_expand(path, subst_dict) try: new_file_contents = func(file_contents, yaml_contents) except Exception as exc: msg = "Refusing to fix file: {path}: {error}".format(path=path, error=str(exc)) raise RuntimeError(msg) return new_file_contents
def fix_file(path, product_yaml, func): file_contents = open(path, 'r').read().split("\n") if file_contents[-1] == '': file_contents = file_contents[:-1] yaml_contents = yaml.open_and_macro_expand(path, product_yaml) print("====BEGIN BEFORE====") print_file(file_contents) print("====END BEFORE====") file_contents = func(file_contents, yaml_contents) print("====BEGIN AFTER====") print_file(file_contents) print("====END AFTER====") response = input_func("Confirm writing output to %s: (y/n): " % path) if response.strip() == 'y': f = open(path, 'w') for line in file_contents: f.write(line) f.write("\n") f.flush() f.close()
def fix_file(path, product_yaml, func): file_contents = open(path, 'r').read().split("\n") if file_contents[-1] == '': file_contents = file_contents[:-1] yaml_contents = yaml.open_and_macro_expand(path, product_yaml) print("====BEGIN BEFORE====") print_file(file_contents) print("====END BEFORE====") file_contents = func(file_contents, yaml_contents) print("====BEGIN AFTER====") print_file(file_contents) print("====END AFTER====") response = input_func("Confirm writing output to %s: (y/n): " % path) if response.strip() == 'y': f = open(path, 'w') for line in file_contents: f.write(line) f.write("\n") f.flush() f.close()
def has_unsorted_prodtype(yaml_file, product_yaml=None): rule = yaml.open_and_macro_expand(yaml_file, product_yaml) if 'prodtype' in rule: prodtypes = rule['prodtype'].split(',') return prodtypes != sorted(prodtypes) return False