def write_xml(self): """Function writes XML document to file""" self.target_tree.set('xmlns:xhtml', 'http://www.w3.org/1999/xhtml/') # we really must set encoding here! and suppress it in write_to_file data = ElementTree.tostring(self.target_tree, "utf-8") FileHelper.write_to_file(self.path, 'wb', data, False) self.target_tree = ElementTree.parse(self.path).getroot()
def update_check_script(self, updates, author=None): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = FileHelper.get_script_type(self.check_script_path) if author is None: author = "<empty_line>" generated_section, functions = ModuleHelper.generate_common_stuff( settings.license % author, updates, script_type) lines = FileHelper.get_file_content(self.check_script_path, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: raise MissingHeaderCheckScriptError(self.check_script_path) 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' output_text += line FileHelper.write_to_file(self.check_script_path, "wb", output_text)
def setUp(self): self.root_dir_name = "tests/FOOBAR6_7" + settings.results_postfix self.dirname = os.path.join(self.root_dir_name, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.loaded_ini = {} self.test_ini = {'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf' } self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, settings.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, settings.solution_txt) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU)
def setUp(self): self.dir_name = "tests/FOOBAR6_7/incorrect_ini" os.makedirs(self.dir_name) self.filename = os.path.join(self.dir_name, 'test.ini') self.rule = [] self.test_solution = "test_solution.sh" self.check_script = "check_script.sh" self.loaded_ini[self.filename] = [] self.test_ini = { 'content_title': 'Testing content title', 'content_description': 'Some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test' } solution_text = """ A solution text for test suite" """ check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ FileHelper.write_to_file( os.path.join(self.dir_name, self.test_solution), "wb", solution_text) FileHelper.write_to_file( os.path.join(self.dir_name, self.check_script), "wb", check_sh)
def _copy_xccdf_file(self, update_text=None, update_return_values=None): temp_dir = tempfile.mkdtemp() xccdf_file = os.path.join(os.getcwd(), 'tests', 'generated_results', 'inplace_combined_risk_test.xml') temp_file = os.path.join(temp_dir, 'all-xccdf.xml') shutil.copyfile(xccdf_file, temp_file) content = FileHelper.get_file_content(temp_file, 'rb', decode_flag=False) if update_text: if update_text[0] is not None: new_text = b'preupg.risk.%s: Test %s Inplace risk' % ( update_text[0], update_text[0]) if update_text is not None: content = content.replace(b'INPLACE_TAG1', new_text) else: content = content.replace(b'INPLACE_TAG1', "") if update_text[1] is not None: new_text = b'preupg.risk.%s: Test %s Inplace risk' % ( update_text[1], update_text[1]) if update_text is not None: content = content.replace(b'INPLACE_TAG2', new_text) else: content = content.replace(b'INPLACE_TAG2', "") content = content.replace(b'RESULT_VALUE1', update_return_values[0]) content = content.replace(b'RESULT_VALUE2', update_return_values[1]) FileHelper.write_to_file(temp_file, 'wb', content) return temp_file
def comment_kickstart_issues(self): """Comment out those line in kickstart that are to be reviewed by user and uncommented by themself if necessary. """ list_issues = [ ' --', 'group', 'user ', 'repo', 'url', 'rootpw', 'part', 'volgroup', 'logvol', 'raid' ] kickstart_data = [] try: kickstart_data = FileHelper.get_file_content(os.path.join( settings.KS_DIR, self.kickstart_name), 'rb', method=True, decode_flag=False) except IOError: log_message( "The %s file is missing. The partitioning layout might not be complete." % self.kickstart_name, level=logging.WARNING) return None for index, row in enumerate(kickstart_data): tag = [com for com in list_issues if row.startswith(com)] if tag: kickstart_data[index] = "#" + row FileHelper.write_to_file(self.kickstart_name, 'wb', kickstart_data)
def setUp(self): self.root_dir_name = "tests/FOOBAR6_7" + settings.results_postfix self.dirname = os.path.join(self.root_dir_name, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.loaded_ini = {} self.test_ini = { 'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf' } self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, settings.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, settings.solution_txt) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU)
def update_check_script(self, updates, author=None): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = FileHelper.get_script_type(self.full_path_name) if author is None: author = "<empty_line>" generated_section, functions = ModuleHelper.generate_common_stuff(settings.license % author, updates, script_type) lines = FileHelper.get_file_content(self.full_path_name, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: MessageHelper.print_error_msg("#END GENERATED SECTION is missing in check_script %s" % self.full_path_name) 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 FileHelper.write_to_file(self.full_path_name, "wb", output_text)
def setUp(self): self.root_dir_name = "tests/FOOBAR6_7" self.dir_name = os.path.join(self.root_dir_name, "incorrect_ini") os.makedirs(self.dir_name) self.filename = os.path.join(self.dir_name, 'test.ini') self.rule = [] self.test_ini = {'content_title': 'Testing content title', 'content_description': 'Some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'applies_to': 'test' } solution_text = """ A solution text for test suite" """ check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ FileHelper.write_to_file(os.path.join( self.dir_name, settings.solution_txt), "wb", solution_text) FileHelper.write_to_file(os.path.join( self.dir_name, settings.check_script), "wb", check_sh)
def clean_html(report_path): """ Function cleans a report """ file_content = FileHelper.get_file_content(report_path, 'rb') s_testres = ( '[\t ]*<div id="intro">[\t ]*\n[\t ]*<h2>Introduction</h2>[\t ]*\n', False) e_testres = ( '[\t ]*</table>[\t ]*\n[\t ]*</div>[\t ]*\n[\t ]*</div>[\t ]*\n', False) s_score = ('[\t ]*<div>[\t ]*\n[\t ]*<h3>Score</h3>\s*', True) e_score = ('[\t ]*</div>[\t ]*\n[\t ]*<div id="results-overview">[\t ]*\n', False) # remove test results nl = remove_lines(file_content, s_testres, e_testres) # remove score table #nl = remove_lines(nl, s_score, e_score) # sed XCCDF test results nl = re.sub('XCCDF test result', 'Preupgrade Assistant', nl) # add preupg nvr nl = re.sub('[\t ]*<h2>Introduction</h2>[\t ]*\n', add_preupg_scanner_info(), nl) FileHelper.write_to_file(report_path, 'wb', nl)
def test_secret_check_script(self): """Check occurrence of secret file for check script""" self.test_ini['check_script'] = '.minicheck' text = """#!/usr/bin/sh\necho 'ahojky'\n""" FileHelper.write_to_file(os.path.join(self.dir_name, self.check_script), "wb", text) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
def test_secret_check_script(self): """Check occurrence of secret file for check script""" self.test_ini['check_script'] = '.minicheck' text = """#!/usr/bin/sh\necho 'ahojky'\n""" FileHelper.write_to_file( os.path.join(self.dir_name, self.check_script), "wb", text) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
def test_hashes(self): text_to_hash=""" This is preupgrade assistant test has string" """ self.dir_name = "tests/hashes" os.mkdir(self.dir_name) FileHelper.write_to_file(os.path.join(self.dir_name, "post_script"), 'wb', text_to_hash) PostupgradeHelper.hash_postupgrade_file(False, self.dir_name) return_value = PostupgradeHelper.hash_postupgrade_file(False, self.dir_name, check=True) self.assertTrue(return_value)
def _remove_obsolete_data(self): if os.path.exists(KickstartGenerator.get_kickstart_path( self.dir_name)): lines = FileHelper.get_file_content( KickstartGenerator.get_kickstart_path(self.dir_name), "r", method=True) lines = [x for x in lines if not x.startswith('key')] FileHelper.write_to_file( KickstartGenerator.get_kickstart_path(self.dir_name), "w", lines)
def test_incorrect_tag(self): """ Check occurrence of incorrect tag Tests issue #30 """ text_ini = '[preupgrade]\n' text_ini += '\n'.join([key + " = " + self.test_ini[key] for key in self.test_ini]) text_ini += '\n[]\neliskk\n' FileHelper.write_to_file(self.filename, "wb", text_ini) oscap = OscapGroupXml(self.root_dir_name, self.dir_name) self.assertRaises(configparser.ParsingError, oscap.find_all_ini)
def write_xml(self): """The function is used for storing a group.xml file""" self.find_all_ini() self.write_list_rules() xml_utils = XmlUtils(self.dirname, self.loaded) self.rule = xml_utils.prepare_sections() file_name = os.path.join(self.dirname, "group.xml") try: FileHelper.write_to_file(file_name, "wb", ["%s" % item for item in self.rule]) except IOError as ior: print ('Problem with write data to the file ', file_name, ior.message)
def write_profile_xml(self, target_tree): """The function stores all-xccdf.xml file into content directory""" file_name = os.path.join(self.dirname, "all-xccdf.xml") print ('File which can be used by Preupgrade-Assistant is:\n', ''.join(file_name)) try: # encoding must be set! otherwise ElementTree return non-ascii characters # as html entities instead, which are unsusable for us data = ElementTree.tostring(target_tree, "utf-8") FileHelper.write_to_file(file_name, "wb", data, False) except IOError as ioe: print ('Problem with writing to file ', file_name, ioe.message)
def write_profile_xml(self, target_tree): """The function stores all-xccdf.xml file into content directory""" file_name = os.path.join(self.dirname, "all-xccdf.xml") print('File which can be used by Preupgrade-Assistant is:\n', ''.join(file_name)) try: # encoding must be set! otherwise ElementTree return non-ascii characters # as html entities instead, which are unsusable for us data = ElementTree.tostring(target_tree, "utf-8") FileHelper.write_to_file(file_name, "wb", data, False) except IOError as ioe: print('Problem with writing to file ', file_name, ioe.message)
def test_incorrect_tag(self): """ Check occurrence of incorrect tag Tests issue #30 """ text_ini = '[preupgrade]\n' text_ini += '\n'.join( [key + " = " + self.test_ini[key] for key in self.test_ini]) text_ini += '\n[]\neliskk\n' FileHelper.write_to_file(self.filename, "wb", text_ini) oscap = OscapGroupXml(self.dir_name) self.assertRaises(SystemExit, oscap.find_all_ini)
def _create_check_script(self): if self.check_script: if self.script_type == "sh": content = settings.temp_bash_script else: content = settings.temp_python_script FileHelper.write_to_file( os.path.join(self.get_content_path(), self.get_check_script()), 'wb', content) os.chmod( os.path.join(self.get_content_path(), self.get_check_script()), 0755)
def update_report(self, report_path): """Update XML or HTML report with relevant solution texts.""" if not self.solution_texts: self.load_solution_texts() orig_file = os.path.join(self.assessment_result_path, report_path) report_content = FileHelper.get_file_content(orig_file, "rb") for solution_placeholer, solution_text in self.solution_texts.items(): report_content = report_content.replace(solution_placeholer, solution_text) FileHelper.write_to_file(orig_file, "wb", report_content)
def _create_check_script(self): if self.check_script: if self.script_type == "sh": content = settings.temp_bash_script else: content = settings.temp_python_script FileHelper.write_to_file(os.path.join(self.get_content_path(), self.get_check_script()), 'wb', content) os.chmod(os.path.join(self.get_content_path(), self.get_check_script()), 0755)
def write_xccdf_version(file_name, direction=False): """ Function updates XCCDF version because of separate HTML generation and our own XSL stylesheet """ namespace_1 = 'http://checklists.nist.gov/xccdf/1.1' namespace_2 = 'http://checklists.nist.gov/xccdf/1.2' content = FileHelper.get_file_content(file_name, "rb") if direction: content = re.sub(namespace_2, namespace_1, content) else: content = re.sub(namespace_1, namespace_2, content) FileHelper.write_to_file(file_name, 'wb', content)
def write_xml(self): """The function is used for storing a group.xml file""" self.find_all_ini() self.write_list_rules() xml_utils = XmlUtils(self.module_set_dir, self.dirname, self.loaded) self.rule = xml_utils.prepare_sections() file_name = os.path.join(self.dirname, "group.xml") try: FileHelper.write_to_file(file_name, "wb", ["%s" % item for item in self.rule]) except IOError as ior: raise IOError('Problem with writing to file %s.\nDetails: %s' % (file_name, ior.message))
def _copy_xccdf_file(self, update_text=None, update_return_value=None): temp_dir = tempfile.mkdtemp() xccdf_file = os.path.join(os.getcwd(), "tests", "generated_results", "inplace_risk_test.xml") temp_file = os.path.join(temp_dir, "all-xccdf.xml") shutil.copyfile(xccdf_file, temp_file) content = FileHelper.get_file_content(temp_file, "rb", decode_flag=False) new_text = b"preupg.risk.%s: Test %s Inplace risk" % (update_text, update_text) if update_text is not None: content = content.replace(b"INPLACE_TAG", new_text) else: content = content.replace(b"INPLACE_TAG", "") content = content.replace(b"RESULT_VALUE", update_return_value) FileHelper.write_to_file(temp_file, "wb", content) return temp_file
def solution_file(message): """ Function appends a message to solution file. solution file will be created in module directory :param message: Message - string of list of strings :return: """ if os.path.exists(SOLUTION_FILE): mod = "a+b" else: mod = "wb" FileHelper.write_to_file(SOLUTION_FILE, mod, message)
def update_files(self, file_name, content): """Function updates file_name <migrate or update> according to INI file.""" """ :param file_name: specified in INI file like mode: upgrade, migrate :param content: name of the content like xccdf_rule_... :return: Nothing """ path_name = os.path.join(settings.UPGRADE_PATH, file_name) lines = [] if os.path.exists(path_name): lines = FileHelper.get_file_content(path_name, 'rb', method=True) test_content = [x.strip() for x in lines if content in x.strip()] if not test_content: lines.append(content + '\n') FileHelper.write_to_file(path_name, 'wb', lines)
def write_list_rules(self): end_point = self.dirname.find(SystemIdentification.get_valid_scenario(self.dirname)) rule_name = '_'.join(self.dirname[end_point:].split('/')[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) else: lines = [] for values in six.itervalues(self.loaded): check_script = [v for k, v in six.iteritems(values[0]) if k == 'check_script'] if check_script: check_script = os.path.splitext(''.join(check_script))[0] lines.append(settings.xccdf_tag + rule_name + '_' + check_script + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def solution_file(message): """ Function appends a message to solution file. solution file will be created in module directory :param message: Message - string of list of strings :return: """ solution_filename = os.path.join(VALUE_CURRENT_DIRECTORY, SOLUTION_FILE) if os.path.exists(solution_filename): mod = "a+b" else: mod = "wb" FileHelper.write_to_file(solution_filename, mod, message)
def write_list_rules(self): module_path = self.dirname.replace(self.module_set_dir, '') rule_name = '_'.join(module_path.split(os.sep)[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) # add rule only for modules (dir which contains module.ini) if os.path.isfile(os.path.join(self.dirname, settings.module_ini)): lines.append(settings.xccdf_tag + rule_name + '_check' + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def update_files(self, file_name, content): """Function updates file_name <migrate or update> according to INI file. :param file_name: specified in INI file like mode: upgrade, migrate :param content: name of the content like xccdf_rule_... :return: Nothing """ path_name = os.path.join(settings.UPGRADE_PATH, file_name) lines = [] if os.path.exists(path_name): lines = FileHelper.get_file_content(path_name, 'rb', method=True) test_content = [x.strip() for x in lines if content in x.strip()] if not test_content: lines.append(content + '\n') FileHelper.write_to_file(path_name, 'wb', lines)
def update_platform(full_path): file_lines = FileHelper.get_file_content(full_path, 'rb', method=True) platform = '' platform_id = '' if not SystemIdentification.get_system(): platform = settings.CPE_RHEL else: platform = settings.CPE_FEDORA platform_id = SystemIdentification.get_assessment_version(full_path) for index, line in enumerate(file_lines): if 'PLATFORM_NAME' in line: line = line.replace('PLATFORM_NAME', platform) if 'PLATFORM_ID' in line: line = line.replace('PLATFORM_ID', platform_id[0]) file_lines[index] = line FileHelper.write_to_file(full_path, 'wb', file_lines)
def comment_kickstart_issues(self): list_issues = [' --', 'group', 'user ', 'repo', 'url', 'rootpw'] kickstart_data = [] try: kickstart_data = FileHelper.get_file_content(os.path.join(settings.KS_DIR, self.kickstart_name), 'rb', method=True, decode_flag=False) except IOError: log_message("The %s file is missing. The partitioning layout might not be complete." % self.kickstart_name, level=logging.WARNING) return None for index, row in enumerate(kickstart_data): tag = [com for com in list_issues if row.startswith(com)] if tag: kickstart_data[index] = "#" + row FileHelper.write_to_file(self.kickstart_name, 'wb', kickstart_data)
def update_html(self, result_name, solution_files, extension="html"): """ Function updates a XML or HTML file with relevant solution texts """ orig_file = os.path.join(self.dirname, result_name + "." + extension) lines = FileHelper.get_file_content(orig_file, "rb", method=True) for dir_name, files in six.iteritems(solution_files): section = dir_name.replace( os.path.join(self.dirname, self.scenario), "").replace("/", "_") solution_text = section + "_SOLUTION_MSG" file_name = self._return_correct_text_file(section, files) if not file_name or file_name is None: continue else: logger_report.debug("Solution text '%s' name '%s'", solution_text, file_name) text = FileHelper.get_file_content(os.path.join( dir_name, file_name), "rb", method=True) for cnt, line in enumerate(lines): # If in preupg.risk is a [link] then update them # to /root/pre{migrate,upgrade}/... if 'preupg.risk.' in line.strip(): logger_report.debug(line.strip()) lines[cnt] = tag_formating([line], extension)[0] continue # Find correct block if solution_text not in line.strip(): continue # Get updated text if it is HTML or TEXT lines[cnt] = self.get_updated_text(solution_text, text, line, extension) if extension == 'xml': for cnt, line in enumerate(lines): if 'SOLUTION_MSG' in line.strip(): lines[cnt] = re.sub(r'>.*SOLUTION_MSG.*<', '><', line.strip()) FileHelper.write_to_file(orig_file, "wb", lines)
def remove_html_information(self): report_path = os.path.join(self.dirname, self.result_base + ".html") file_content = FileHelper.get_file_content(report_path, 'rb', method=True) detail_start = '<br /><br /><strong class="bold">Details:</strong><br />' detail_end = '[\t ]*<div class="xccdf-fixtext">' new_content = [] found_section = False for line in file_content: if detail_start in line: found_section = True continue if detail_end in line: found_section = False if not found_section: new_content.append(line) FileHelper.write_to_file(report_path, 'wb', new_content)
def generate_xml(self, generate_from_ini=True): if SystemIdentification.get_valid_scenario(self.dir_name) is None: print ('Use valid scenario like RHEL6_7 or CENTOS6_RHEL6') return ReturnValues.SCENARIO dir_util.copy_tree(self.result_dir, self.dir_name) target_tree = ComposeXML.run_compose(self.dir_name, generate_from_ini=generate_from_ini) report_filename = os.path.join(self.dir_name, settings.content_file) try: FileHelper.write_to_file(report_filename, "wb", ElementTree.tostring(target_tree, "utf-8"), False) if generate_from_ini: print ('Generate report file for preupgrade-assistant is:', ''.join(report_filename)) except IOError: print ("Problem with writing file ", report_filename) raise return 0
def setUp(self): self.dirname = os.path.join("tests", "FOOBAR6_7" + settings.results_postfix, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.test_solution = "test_solution.txt" self.check_script = "check_script.sh" self.loaded_ini = {} test_ini = { 'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test', 'requires': 'bash', 'binary_req': 'sed' } self.loaded_ini[self.filename] = [] self.loaded_ini[self.filename].append(test_ini) self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, self.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, self.test_solution) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.xml_utils = XmlUtils(self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections()
def update_html(self, result_name, solution_files, extension="html"): """ Function updates a XML or HTML file with relevant solution texts """ orig_file = os.path.join(self.dirname, result_name + "." + extension) lines = FileHelper.get_file_content(orig_file, "rb", method=True) for dir_name, files in six.iteritems(solution_files): section = dir_name.replace(os.path.join(self.dirname, self.scenario), "").replace("/", "_") solution_text = section + "_SOLUTION_MSG" file_name = self._return_correct_text_file(section, files) if not file_name or file_name is None: continue else: logger_report.debug("Solution text '%s' name '%s'", solution_text, file_name) text = FileHelper.get_file_content(os.path.join(dir_name, file_name), "rb", method=True) for cnt, line in enumerate(lines): # If in preupg.risk is a [link] then update them # to /root/pre{migrate,upgrade}/... if 'preupg.risk.' in line.strip(): logger_report.debug(line.strip()) lines[cnt] = tag_formating([line], extension)[0] continue # Find correct block if solution_text not in line.strip(): continue # Get updated text if it is HTML or TEXT lines[cnt] = self.get_updated_text(solution_text, text, line, extension) if extension == 'xml': for cnt, line in enumerate(lines): if 'SOLUTION_MSG' in line.strip(): lines[cnt] = re.sub(r'>.*SOLUTION_MSG.*<', '><', line.strip()) FileHelper.write_to_file(orig_file, "wb", lines)
def add_pkg_to_kickstart(pkg_name): """ Function adds a package to special_pkg_list """ empty = False if isinstance(pkg_name, list): # list of packages if len(pkg_name) == 0: empty = True else: # string - pkg_name delimited by whitespace if len(pkg_name.strip()) == 0: empty = True else: # make list from string pkg_name = pkg_name.strip().split() if empty is True: log_debug("Missing parameters! Any package will be added.") return 1 for pkg in pkg_name: FileHelper.write_to_file(SPECIAL_PKG_LIST, "a+b", pkg.strip() + '\n') return 0
def setUp(self): self.dirname = os.path.join("tests", "FOOBAR6_7" + settings.results_postfix, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.test_solution = "test_solution.txt" self.check_script = "check_script.sh" self.loaded_ini = {} test_ini = {'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test', 'requires': 'bash', 'binary_req': 'sed'} self.loaded_ini[self.filename] = [] self.loaded_ini[self.filename].append(test_ini) self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, self.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, self.test_solution) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.xml_utils = XmlUtils(self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections()
def generate_xml(self, generate_from_ini=True): if SystemIdentification.get_valid_scenario(self.dir_name) is None: print('Use valid scenario like RHEL6_7 or CENTOS6_RHEL6') return ReturnValues.SCENARIO dir_util.copy_tree(self.result_dir, self.dir_name) target_tree = ComposeXML.run_compose( self.dir_name, generate_from_ini=generate_from_ini) report_filename = os.path.join(self.dir_name, settings.content_file) try: FileHelper.write_to_file( report_filename, "wb", ElementTree.tostring(target_tree, "utf-8"), False) if generate_from_ini: print('Generate report file for preupgrade-assistant is:', ''.join(report_filename)) except IOError: print("Problem with writing file ", report_filename) raise return 0
def clean_html(report_path): """ Function cleans a report """ file_content = FileHelper.get_file_content(report_path, 'rb') s_testres = ('[\t ]*<div id="intro">[\t ]*\n[\t ]*<h2>Introduction</h2>[\t ]*\n', False) e_testres = ('[\t ]*</table>[\t ]*\n[\t ]*</div>[\t ]*\n[\t ]*</div>[\t ]*\n', False) s_score = ('[\t ]*<div>[\t ]*\n[\t ]*<h3>Score</h3>\s*', True) e_score = ('[\t ]*</div>[\t ]*\n[\t ]*<div id="results-overview">[\t ]*\n', False) # remove test results nl = remove_lines(file_content, s_testres, e_testres) # remove score table #nl = remove_lines(nl, s_score, e_score) # sed XCCDF test results nl = re.sub('XCCDF test result', 'Preupgrade Assistant', nl) # add preupg nvr nl = re.sub('[\t ]*<h2>Introduction</h2>[\t ]*\n', add_preupg_scanner_info(), nl) FileHelper.write_to_file(report_path, 'wb', nl)
def write_list_rules(self): end_point = self.dirname.find( SystemIdentification.get_valid_scenario(self.dirname)) rule_name = '_'.join(self.dirname[end_point:].split('/')[1:]) file_list_rules = os.path.join(settings.UPGRADE_PATH, settings.file_list_rules) lines = [] if os.path.exists(file_list_rules): lines = FileHelper.get_file_content(file_list_rules, "rb", method=True) else: lines = [] for values in six.itervalues(self.loaded): check_script = [ v for k, v in six.iteritems(values[0]) if k == 'check_script' ] if check_script: check_script = os.path.splitext(''.join(check_script))[0] lines.append(settings.xccdf_tag + rule_name + '_' + check_script + '\n') FileHelper.write_to_file(file_list_rules, "wb", lines)
def update_check_script(self, updates, author=None): """ The function updates check script with license file and with API functions like check_rpm_to and check_applies_to """ script_type = FileHelper.get_script_type(self.full_path_name) if author is None: author = "<empty_line>" generated_section, functions = ModuleHelper.generate_common_stuff( settings.license % author, updates, script_type) lines = FileHelper.get_file_content(self.full_path_name, "rb", method=True) if not [x for x in lines if re.search(r'#END GENERATED SECTION', x)]: MessageHelper.print_error_msg( "#END GENERATED SECTION is missing in check_script %s" % self.full_path_name) 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 FileHelper.write_to_file(self.full_path_name, "wb", output_text)
def generate_xml(self, generate_from_ini=True): """ Copy files to result directory and if specified generate all-xccdf.xml file @param {bool} generate_from_ini - True if xccdf-compose tool is used, decide if all-xccdf.xml file will(True) be created or not(False) @throws {IOError} - when creation of all-xccdf.xml file fails @return {int} - 0 success !0 error """ try: ModuleSetUtils.get_module_set_os_versions(self.src_path) except EnvironmentError as err: sys.stderr.write("{0}\n".format(str(err))) return ReturnValues.SCENARIO # e.g. /root/preupgrade/RHEL6_7 -> /root/preupgrade/RHEL6_7-results dir_util.copy_tree(self.src_path, self.dst_path) # create content for all-xccdf.xml file as ElementTree object target_tree = ComposeXML.run_compose( self.dst_path, generate_from_ini=generate_from_ini) # path where all-xccdf.xml is going to be generated report_filename = os.path.join(self.dst_path, settings.all_xccdf_xml_filename) if generate_from_ini: try: FileHelper.write_to_file( report_filename, "wb", ElementTree.tostring(target_tree, "utf-8"), False ) logger_debug.debug('Generated: %s' % report_filename) except IOError: raise IOError("Error: Problem with writing file %s" % report_filename) return 0
def _create_solution_file(self): if self.solution_file: FileHelper.write_to_file(os.path.join(self.get_content_path(), self.get_solution_file()), 'wb', '')
def save_kickstart(self): kickstart_data = self.ks.handler.__str__() FileHelper.write_to_file(self.kickstart_name, 'wb', kickstart_data)