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 = FileHelper.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:
            MessageHelper.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 = FileHelper.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:
            MessageHelper.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_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)
Exemple #4
0
    def check_recommended_fields(self, keys=None):
        """
        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']
        unused = [x for x in fields if not keys.get(x)]
        if unused:
            title = 'Following tags are missing in INI file %s\n' % self.script_name
            if 'applies_to' not in unused:
                MessageHelper.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:
                MessageHelper.print_error_msg(
                    title="Wrong solution_type. Allowed are 'html' or 'text' %s"
                    % self.script_name)
                os.sys.exit(0)
 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))
             MessageHelper.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))
             MessageHelper.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):
        """
        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']
        unused = [x for x in fields if not keys.get(x)]
        if unused:
            title = 'Following tags are missing in INI file %s\n' % self.script_name
            if 'applies_to' not in unused:
                MessageHelper.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:
                MessageHelper.print_error_msg(title="Wrong solution_type. Allowed are 'html' or 'text' %s" % self.script_name)
                os.sys.exit(0)
 def find_all_ini(self):
     """
     This function is used for finding all _fix files in the user defined
     directory
     """
     for dir_name in os.listdir(self.dirname):
         if dir_name.endswith(".ini"):
             self.lists.append(os.path.join(self.dirname, dir_name))
     for file_name in self.lists:
         if FileHelper.check_file(file_name, "r") is False:
             continue
         try:
             config = configparser.ConfigParser()
             filehander = codecs.open(file_name,
                                      'r',
                                      encoding=settings.defenc)
             config.readfp(filehander)
             fields = {}
             if config.has_section('premigrate'):
                 section = 'premigrate'
             else:
                 section = 'preupgrade'
             for option in config.options(section):
                 fields[option] = config.get(section, option)
             self.loaded[file_name] = [fields]
         except configparser.MissingSectionHeaderError:
             MessageHelper.print_error_msg(title="Missing section header")
         except configparser.NoSectionError:
             MessageHelper.print_error_msg(title="Missing section header")
         except configparser.ParsingError:
             MessageHelper.print_error_msg(title="Incorrect INI file\n",
                                           msg=file_name)
             os.sys.exit(1)
 def find_all_ini(self):
     """
     This function is used for finding all _fix files in the user defined
     directory
     """
     for dir_name in os.listdir(self.dirname):
         if dir_name.endswith(".ini"):
             self.lists.append(os.path.join(self.dirname, dir_name))
     for file_name in self.lists:
         if FileHelper.check_file(file_name, "r") is False:
             continue
         try:
             config = configparser.ConfigParser()
             filehander = codecs.open(file_name, 'r', encoding=settings.defenc)
             config.readfp(filehander)
             fields = {}
             if config.has_section('premigrate'):
                 section = 'premigrate'
             else:
                 section = 'preupgrade'
             for option in config.options(section):
                 fields[option] = config.get(section, option)
             self.loaded[file_name] = [fields]
         except configparser.MissingSectionHeaderError:
             MessageHelper.print_error_msg(title="Missing section header")
         except configparser.NoSectionError:
             MessageHelper.print_error_msg(title="Missing section header")
         except configparser.ParsingError:
             MessageHelper.print_error_msg(title="Incorrect INI file\n", msg=file_name)
             os.sys.exit(1)
Exemple #10
0
def show_message(message):
    """
    Prints message out on stdout message (kind of yes/no) and return answer.

    Return values are:
    Return True on accept (y/yes). Otherwise returns False
    """
    accept = ['y', 'yes']
    choice = MessageHelper.get_message(title=message, prompt='[Y/n]')
    if choice.lower() in accept:
        return True
    else:
        return False
def show_message(message):
    """
    Prints message out on stdout message (kind of yes/no) and return answer.

    Return values are:
    Return True on accept (y/yes). Otherwise returns False
    """
    accept = ['y', 'yes']
    choice = MessageHelper.get_message(title=message, prompt='[Y/n]')
    if choice.lower() in accept:
        return True
    else:
        return False
Exemple #12
0
 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 check_postmigrate_dir(self):
     if not FileHelper.get_list_executable_files_in_dir(os.path.join(settings.assessment_results_dir,
                                                                     settings.postmigrate_dir)):
         if not self.conf.assumeyes:
             accept = ['y', 'yes']
             log_message("The '%s' folder is empty - scripts to be executed "
                         "after the migration should be placed "
                         "here." % os.path.join(settings.result_dir,
                                                settings.postmigrate_dir))
             message = "Do you want to continue with kickstart " \
                       "generation without any postmigration scripts?"
             choice = MessageHelper.get_message(message=message, prompt="(Y/n)")
             if choice.lower() not in accept:
                 return None
     return True
Exemple #14
0
 def check_postmigrate_dir(self):
     if not FileHelper.get_list_executable_files_in_dir(
             os.path.join(settings.assessment_results_dir,
                          settings.postmigrate_dir)):
         if not self.conf.assumeyes:
             accept = ['y', 'yes']
             log_message(
                 "The '%s' folder is empty - scripts to be executed "
                 "after the migration should be placed "
                 "here." % os.path.join(settings.result_dir,
                                        settings.postmigrate_dir))
             message = "Do you want to continue with kickstart " \
                       "generation without any postmigration scripts?"
             choice = MessageHelper.get_message(message=message,
                                                prompt="(Y/n)")
             if choice.lower() not in accept:
                 return None
     return True
    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:
            if 'check_script' not in key:
                raise MissingTagsIniFileError
            if 'solution' not in key:
                raise MissingTagsIniFileError
            self.mh = ModuleHelper(os.path.dirname(main), key['check_script'],
                                   key['solution'])
            self.mh.check_recommended_fields(key)
            # 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)
                        MessageHelper.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)
                MessageHelper.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:
            if 'check_script' not in key:
                raise MissingTagsIniFileError
            if 'solution' not in key:
                raise MissingTagsIniFileError
            self.mh = ModuleHelper(os.path.dirname(main), key['check_script'], key['solution'])
            self.mh.check_recommended_fields(key)
            # 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)
                        MessageHelper.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)
                MessageHelper.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