Example #1
0
def test(dir, target_file, command_file):

    mutated_file_paths = list_py_files(dir)

    made = False

    while not made:
        try:
            os.mkdir("MrTopoTest")
            made = True
        except OSError:
            log("MrTopoTest exists, removing")
            shutil.rmtree("MrTopoTest")

    f = open(f"MrTopoTest/original_topology.py", "x")
    f.close()
    shutil.copyfile(target_file, "MrTopoTest/original_topology.py")

    test_out = []

    for path in mutated_file_paths:
        out = log(
            f"############################################# Executing test with: {path} #############################################"
        ) + "\n"
        shutil.copyfile(path, target_file)
        out += call(f"sh {command_file}")
        test_out.append(out)

    shutil.copyfile("MrTopoTest/original_topology.py", target_file)
    return test_out
Example #2
0
def main_routine(args, number_of_mutations):

    log('MrTopo.v.' + __version__ + '>')

    _file, file_type = args

    if file_type == FileType.CONFIG:
        configuration = config.Config()  # initialize default config

        # translate
        if _file:
            configuration = c_read(_file)

        # interpret
        interpret(configuration)

    elif file_type == FileType.PYTHON:

        pyfile = None

        # translate
        if _file:
            pyfile = p_read(_file)

        network = None
        if pyfile:
            # interpret
            network = interpret(pyfile)

        mutant_networks = mutate(network, number_of_mutations)

        for mutant in mutant_networks:
            m_write(mutant, _file)

        desc_write(mutant_networks)
Example #3
0
def analyze_routine(mutation_file, results_file, nums):
    log('MrTopo.v.' + __version__ + '-analyzer>')

    results = analyze(mutation_file, results_file)

    make_folder("MrTopoAnalyzed")

    list_write(results, f"MrTopoAnalyzed/mrtopo_analysis_{nums}.csv")
Example #4
0
def analyze(mutation_file, results_file):
    log(f'Analyzing results from {results_file}, with mutated topology {mutation_file}'
        )

    mutations = convert_mutated_to_list(mutation_file)
    results = convert_results_to_list(results_file)

    return tie(mutations, results)
Example #5
0
def mutate(network, number_of_mutations=30):
    log("Mutator - mutating network " + str(number_of_mutations) + " times")
    mutant_networks = []  # type MutantNetwork
    for i in range(number_of_mutations):
        operation = random.choice(list(Operations))
        mn = do(operation, network.deep_copy(),
                i)  # mutate deep copy of network
        if mn:
            mutant_networks.append(mn)
    return mutant_networks
Example #6
0
def c_read(file: str):
    log('Translator - Reading: ' + file)
    _j = find_file(file)
    _d = None

    if len(_j) == 1:
        _d = read_json(_j[0])
    else:
        pass
        # TODO ERROR
    if _d:
        configuration = config.Config.build(_d)
    else:
        configuration = config.Config()

    return configuration  # TODO Validation
Example #7
0
def p_read(file: str):  # TODO ADD TESTS
    log('Translator - Reading: ' + file)
    _p = find_file(file)
    _d = {"switch_lines": [], "link_lines": [], "host_lines": []}
    o_rf = open_read(file)
    line_no = 0
    for l in o_rf:
        if l.__contains__("self.addSwitch"):
            _d["switch_lines"].append(to_line_obj(l, line_no))
        if l.__contains__("self.addHost"):
            _d["host_lines"].append(to_line_obj(l, line_no))
        if l.__contains__("self.addLink"):
            _d["link_lines"].append(to_line_obj(l, line_no))
        line_no += 1

    o_rf.close()
    return _d
Example #8
0
def validate_routine(args, name=None, long=False):
    log('MrTopo.v.' + __version__ + '-validator>')

    dest, file_type = args

    files = []

    if file_type == FileType.DIRECTORY:
        dir = os.listdir(dest)
        for _file in dir:
            n = len(_file)
            if _file[n-2] + _file[n-1] == "py":
                files.append(f'{dest}/{_file}')
    else:
        files.append(dest)

    descriptor = []

    for _file in files:
        descriptor.append(validate(_file, name, long))

    list_write(descriptor, "validator.txt")
Example #9
0
def get_input(message: str, validator=vald_def):
    u_in = input(message)

    log('Validating ~' + u_in)

    if validator(u_in):
        log('\n~' + u_in + ' valid')
        return u_in

    log('Invalid input ~' + u_in + ' try again...')
    return None
Example #10
0
def test_routine(dir, target_file, command_file):
    log('MrTopo.v.' + __version__ + '-tester>')

    results = test(dir, target_file, command_file)

    list_write(results, "MrTopoTest/test.txt")
Example #11
0
def handle_error(e_type=ErrorIdentities.UNDEFINED, message=""):
    o_msg = "MrTopo Error - " + str(e_type.name) + "\n" + str(message)
    log(item=o_msg, level=e_type.value)
Example #12
0
def call(command):
    try:
        return log(
            subprocess.check_output(command, shell=True).decode('utf-8'))
    except subprocess.CalledProcessError:
        return log(f'Command: {command} - failed')
Example #13
0
def interpret(cfg: config.Config):
    log('Interpreter - interpreting config')
    return c_construct_networks(cfg.topologies)
Example #14
0
def interpret(py: dict):
    log('Interpreter - interpreting py file')
    return p_construct_networks(py)