def check_recommended_fields(self, keys=None, script_name=""): """ The function checks whether all fields in INI file are fullfiled If solution_type is mentioned than HTML page can be used. HTML solution type can contain standard HTML tags field are needed by YAML file """ fields = ['content_title', 'check_script', 'solution', 'applies_to'] optional = ['solution_type'] unused = [x for x in fields if not keys.get(x)] if unused: title = 'Following tags are missing in INI file %s\n' % script_name if 'applies_to' not in unused: print_error_msg(title=title, msg=' '.join(unused)) raise MissingTagsIniFileError if 'solution_type' in keys: if keys.get('solution_type') == "html" or keys.get( 'solution_type') == "text": pass else: print_error_msg( title="Wrong solution_type. Allowed are 'html' or 'text' %s" % script_name) os.sys.exit(0)
def update_values_list(self, section, search_exp, replace_exp): """ The function replaces tags taken from INI files. Tags are mentioned in xml_tags.py """ forbidden_empty = ["{scap_name}", "{main_dir}"] if search_exp == "{content_description}": replace_exp = replace_exp.rstrip() elif search_exp == "{check_description}": replace_exp = '\n' + replace_exp + '\n' elif search_exp == "{config_file}": new_text = "" for lines in replace_exp.split(','): new_text = new_text+"<xhtml:li>"+lines.strip()+"</xhtml:li>" replace_exp = new_text.rstrip() elif search_exp == "{solution}": new_text = get_file_content(os.path.join(self.dirname, replace_exp), "rb", True) # we does not need interpreter for fix script # in XML therefore skip first line replace_exp = ''.join(new_text[1:]) elif search_exp == "{solution_text}": new_text = "_" + '_'.join(get_full_xml_tag(self.dirname))\ + "_SOLUTION_MSG_" + replace_exp.upper() replace_exp = new_text if replace_exp == '' and search_exp in forbidden_empty: print_error_msg(title="Disapproved empty replacement for tag '%s'" % search_exp) raise EmptyTagIniFileError for cnt, line in enumerate(section): if search_exp in line: section[cnt] = line.replace(search_exp, replace_exp)
def update_values_list(self, section, search_exp, replace_exp): """ The function replaces tags taken from INI files. Tags are mentioned in xml_tags.py """ forbidden_empty = ["{scap_name}", "{main_dir}"] if search_exp == "{content_description}": replace_exp = replace_exp.rstrip() elif search_exp == "{check_description}": replace_exp = '\n' + replace_exp + '\n' elif search_exp == "{config_file}": new_text = "" for lines in replace_exp.split(','): new_text = new_text + "<xhtml:li>" + lines.strip( ) + "</xhtml:li>" replace_exp = new_text.rstrip() elif search_exp == "{solution}": new_text = get_file_content( os.path.join(self.dirname, replace_exp), "rb", True) # we does not need interpreter for fix script # in XML therefore skip first line replace_exp = ''.join(new_text[1:]) elif search_exp == "{solution_text}": new_text = "_" + '_'.join(get_full_xml_tag(self.dirname))\ + "_SOLUTION_MSG_" + replace_exp.upper() replace_exp = new_text if replace_exp == '' and search_exp in forbidden_empty: print_error_msg( title="Disapproved empty replacement for tag '%s'" % search_exp) raise EmptyTagIniFileError for cnt, line in enumerate(section): if search_exp in line: section[cnt] = line.replace(search_exp, replace_exp)
def check_executable(dir_name, script_name=""): """ The function checks whether script is executable. If not then ERROR message arise """ if not os.access(get_full_path(dir_name, script_name), os.X_OK): print_error_msg(title="The file %s is not executable" % os.path.join(dir_name, script_name))
def update_check_script(dir_name, updates, script_name=None, author=""): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = get_script_type(dir_name, script_name) if author == "": author = "<empty_line>" generated_section, functions = generate_common_stuff(settings.license.format(author), updates, script_type) full_path_script = get_full_path(dir_name, script_name) lines = get_file_content(full_path_script, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: print_error_msg("#END GENERATED SECTION is missing in check_script {0}".format(full_path_script)) raise MissingHeaderCheckScriptError for func in functions: lines = [x for x in lines if func not in x.strip()] output_text = "" for line in lines: if '#END GENERATED SECTION' in line: new_line = '\n'.join(generated_section) new_line = new_line.replace('<empty_line>', '').replace('<new_line>', '') output_text += new_line+'\n' if 'check_applies' in updates: component = updates['check_applies'] else: component = "distribution" if script_type == "sh": output_text += 'COMPONENT="'+component+'"\n' else: output_text += 'set_component("'+component+'")\n' output_text += line write_to_file(full_path_script, "wb", output_text)
def _test_init_file(self): test_dict = copy.deepcopy(self.ini_files) allowed_tags = ['check_script', 'content_description', 'content_title', 'applies_to', 'author', 'binary_req', 'solution', 'bugzilla', 'config_file', 'group_title', 'mode', 'requires', 'solution_type'] for ini, content in six.iteritems(test_dict): content_dict = content[0] for tag in allowed_tags: if tag in content_dict: del content_dict[tag] if content_dict: tags = ','. join(six.iterkeys(content_dict)) print_error_msg(title="The tag '%s' is not allowed in INI file %s." % (tags, ini), msg="\nAllowed tags for contents are %s" % ','.join(allowed_tags), level=' WARNING ')
def _test_init_file(self): test_dict = copy.deepcopy(self.ini_files) allowed_tags = [ 'check_script', 'content_description', 'content_title', 'applies_to', 'author', 'binary_req', 'solution', 'bugzilla', 'config_file', 'group_title', 'mode', 'requires', 'solution_type' ] for ini, content in six.iteritems(test_dict): content_dict = content[0] for tag in allowed_tags: if tag in content_dict: del content_dict[tag] if content_dict: tags = ','.join(six.iterkeys(content_dict)) print_error_msg( title="The tag '%s' is not allowed in INI file %s." % (tags, ini), msg="\nAllowed tags for contents are %s" % ','.join(allowed_tags), level=' WARNING ')
def check_recommended_fields(self, keys=None, script_name=""): """ The function checks whether all fields in INI file are fullfiled If solution_type is mentioned than HTML page can be used. HTML solution type can contain standard HTML tags field are needed by YAML file """ fields = ['content_title', 'check_script', 'solution', 'applies_to'] optional = ['solution_type'] unused = [x for x in fields if not keys.get(x)] if unused: title = 'Following tags are missing in INI file %s\n' % script_name if 'applies_to' not in unused: print_error_msg(title=title, msg=' '.join(unused)) raise MissingTagsIniFileError if 'solution_type' in keys: if keys.get('solution_type') == "html" or keys.get('solution_type') == "text": pass else: print_error_msg( title="Wrong solution_type. Allowed are 'html' or 'text' %s" % script_name ) os.sys.exit(0)
def update_check_script(dir_name, updates, script_name=None, author=""): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = get_script_type(dir_name, script_name) if author == "": author = "<empty_line>" generated_section, functions = generate_common_stuff( settings.license.format(author), updates, script_type) full_path_script = get_full_path(dir_name, script_name) lines = get_file_content(full_path_script, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: print_error_msg( "#END GENERATED SECTION is missing in check_script {0}".format( full_path_script)) raise MissingHeaderCheckScriptError for func in functions: lines = [x for x in lines if func not in x.strip()] output_text = "" for line in lines: if '#END GENERATED SECTION' in line: new_line = '\n'.join(generated_section) new_line = new_line.replace('<empty_line>', '').replace('<new_line>', '') output_text += new_line + '\n' if 'check_applies' in updates: component = updates['check_applies'] else: component = "distribution" if script_type == "sh": output_text += 'COMPONENT="' + component + '"\n' else: output_text += 'set_component("' + component + '")\n' output_text += line write_to_file(full_path_script, "wb", output_text)
def create_xml_from_ini(self, main): """ The function creates group.xml file from INI file. All tag are replaced by function update_value_list Function also checks whether check script full fills all criteria """ self.select_rules.append(xml_tags.SELECT_TAG) update_fnc = { 'config_file': self.fnc_config_file, 'check_script': self.fnc_check_script, 'check_description': self.fnc_check_description, 'solution': self.fnc_solution_text, 'applies_to': self.dummy_fnc, 'binary_req': self.dummy_fnc, 'content_title': self.update_text, 'content_description': self.update_text, } for key in self.keys: self.check_recommended_fields(key, script_name=main) # Add solution text into value if 'solution' in key: xml_tags.DIC_VALUES['solution_file'] = key['solution'] else: xml_tags.DIC_VALUES['solution_file'] = 'solution.txt' # Add flag where will be shown content if in admin part or in user part if 'result_part' in key: xml_tags.DIC_VALUES['result_part'] = key['result_part'] else: xml_tags.DIC_VALUES['result_part'] = 'admin' self.update_values_list(self.rule, "{rule_tag}", ''.join(xml_tags.RULE_SECTION)) value_tag, check_export_tag = self.add_value_tag() self.update_values_list(self.rule, "{check_export}", ''.join(check_export_tag)) self.update_values_list(self.rule, "{group_value}", ''.join(value_tag)) try: for k, function in six.iteritems(update_fnc): try: function(key, k) except IOError as e: e_title = "Wrong value for tag '%s' in INI file '%s'\n" % (k, main) e_msg = "'%s': %s" % (key[k], e.strerror) print_error_msg(title=e_title, msg=e_msg) raise MissingTagsIniFileError except MissingTagsIniFileError: title = "Following tag '%s' is missing in INI File %s\n" % (k, main) print_error_msg(title=title) raise MissingTagsIniFileError self.update_values_list(self.rule, '{group_title}', html_escape_string(key['content_title'])) try: if 'mode' not in key: self.fnc_update_mode(key['check_script'], 'migrate, upgrade') else: self.fnc_update_mode(key['check_script'], key['mode']) except KeyError: pass
def create_xml_from_ini(self, main): """ The function creates group.xml file from INI file. All tag are replaced by function update_value_list Function also checks whether check script full fills all criteria """ self.select_rules.append(xml_tags.SELECT_TAG) update_fnc = { 'config_file': self.fnc_config_file, 'check_script': self.fnc_check_script, 'check_description': self.fnc_check_description, 'solution': self.fnc_solution_text, 'applies_to': self.dummy_fnc, 'binary_req': self.dummy_fnc, 'content_title': self.update_text, 'content_description': self.update_text, } for key in self.keys: self.check_recommended_fields(key, script_name=main) # Add solution text into value if 'solution' in key: xml_tags.DIC_VALUES['solution_file'] = key['solution'] else: xml_tags.DIC_VALUES['solution_file'] = 'solution.txt' # Add flag where will be shown content if in admin part or in user part if 'result_part' in key: xml_tags.DIC_VALUES['result_part'] = key['result_part'] else: xml_tags.DIC_VALUES['result_part'] = 'admin' self.update_values_list(self.rule, "{rule_tag}", ''.join(xml_tags.RULE_SECTION)) value_tag, check_export_tag = self.add_value_tag() self.update_values_list(self.rule, "{check_export}", ''.join(check_export_tag)) self.update_values_list(self.rule, "{group_value}", ''.join(value_tag)) try: for k, function in six.iteritems(update_fnc): try: function(key, k) except IOError as e: e_title = "Wrong value for tag '%s' in INI file '%s'\n" % ( k, main) e_msg = "'%s': %s" % (key[k], e.strerror) print_error_msg(title=e_title, msg=e_msg) raise MissingTagsIniFileError except MissingTagsIniFileError: title = "Following tag '%s' is missing in INI File %s\n" % ( k, main) print_error_msg(title=title) raise MissingTagsIniFileError self.update_values_list(self.rule, '{group_title}', html_escape_string(key['content_title'])) try: if 'mode' not in key: self.fnc_update_mode(key['check_script'], 'migrate, upgrade') else: self.fnc_update_mode(key['check_script'], key['mode']) except KeyError: pass