def run_with_error_support(program_name, database_name, command):
    """
    Runs a command and attempts to find appropriate error in database

    Example: run_with_error_support(["make", "Makefiles"])
    """

    dbhandler = DatabaseHandler(database_name)
    _, error = cmd_mgr.CommandManager.run(None, command[0], *command[1:])

    if error.rstrip() == "":
        return []

    parse_filter = pyfilter.Filter()
    error = parse_filter.parse(error)

    try:
        possible_errors = {}
        for line in error.split("\n"):
            if line == "":
                break
            for error_tuple in dbhandler.find(line):
                if error_tuple[1] not in possible_errors:
                    possible_errors[error_tuple[1]] = 1
                else:
                    possible_errors[error_tuple[1]] += 1

        # reverse tuple pairs to sort by hits
        if len(possible_errors) > 0:
            errors = [(value, key) for key, value in possible_errors.items()]
            errors.sort()
            errors.reverse()
            return errors, error
        else:
            return [], error

    except:
        print("{}: ".format(program_name), *sys.exc_info()[:-1])
        print("{}: Has the database been initialized?".format(program_name))
        return []
def run_mutation_on_file(filename, database_name):
    """
    filename is the xml file for generation make files.
    This function mutate on the file so that one node
    is missing per iteration. After mutating so every
    node have once been remove the original file will
    be restored.

    In every iteration the usuall procedure for creating
    makefiles is executed. If any errors occures during
    the "make" command the function[run_mutation_on_file]
    will save the error message and the node with all its
    parent nodes saved into tables in the (sqlite3)database.
    """
    try:
        dbhandler = DatabaseHandler(database_name)
    except Exception as e:
        print(e)
        sys.exit(1)

    try:
        mutator = Mutator(filename)
    except:
        print("ERROR: not vaild filename/path for xml compilation file")
        sys.exit(1)

    prettyFilter = pyfilter.Filter()

    for node_list in mutator.begin_mutation():
        try:
            print("NODELIST: " , node_list)
            #CommandManager.run catch error_messages
            cmdManager = CommandManager()
            output, error_message = cmdManager.run("g++", filename)
            print("error_message: " , error_message)

            """
            Insert procedure for generating and building makefiles
            here. Use cmdManager.run([command]...,)
            example: cmdManager.run("mbede-generator", "-l")
            example: cmdManager.run("mv", filename, "new_filename.txt")

            When runing "make" command use the vars output and error_message
            to make it possible to save content to database.
            """

            """
            #example of build procedure code
            cmdManager.run("mbede-generator", "-l")
            cmdManager.run("make", "makefiles")
            output, error_message = cmdaManager.run("make")#, target)
            """

            #If error occures saved the errmsg in db
            if error_message != "":                
                error_message_pretty = prettyFilter.parse(error_message) 
                #reverse order of nodes. Parent... node
                node_list = node_list[::-1]
                print("run_mutation: DB INSERT")
                print("error_msg: " + error_message_pretty)
                print("error_type: " + " ".join(node_list))
                dbhandler.insert(error_message_pretty, node_list)

        except KeyboardInterrupt:
            mutator.restore_xml_file()
            print("KeyboardInterrupt caught, skipping to next xml mutation")
            continue
        except:
            #if something goes wrong continue to next mutation
            print("EXCEPTION: ", sys.exc_info())
            continue