예제 #1
0
    def tv_code(self, code, timeout=2):
        code = regularize(code)
        try:
            code = add_iil_holes(code)
        except:
            print("Failed to add iil holes.")
            print(code)
            return {}

        tr_dict = {}
        for tc_id in self.get_tc_id_list():
            Holes.init_global_vars()
            real_output, exp_output = self.run_tc(code, tc_id, timeout)

            # Debug
            #if real_output != exp_output:
            #    print(real_output)
            #    print(exp_output)

            tr_dict[tc_id] = (real_output == exp_output)

            if "{" in real_output and "{" in exp_output:
                a, b = None, None

                try:
                    a = eval(real_output.strip())
                except:
                    pass

                try:
                    b = eval(exp_output.strip())
                except:
                    pass
                #print(a)
                #print(b)
                tr_dict[tc_id] = tr_dict[tc_id] or (a == b)

        return tr_dict
예제 #2
0
def ofl_refactor_ques(ques_dir_path, timeout, max_depth, sampling_rate, exp_idx, is_resume=False, verbose=False):

    print("Current Setting:", ques_dir_path, sampling_rate, exp_idx)

    refactor_dir = ques_dir_path + "/code/refactor"
    if not os.path.isdir(refactor_dir):
        os.mkdir(refactor_dir)

    pickle_path = refactor_dir + "/refactor_sample_" + str(sampling_rate) + \
                  "_" + str(exp_idx) + ".pickle"

    if is_resume:
        if os.path.isfile(pickle_path):
            return

    corr_dir_path = ques_dir_path + "/code/correct"
    ref_dir_path = ques_dir_path + "/code/reference"
    wrong_dir_path = ques_dir_path + "/code/wrong"

    buggy_code_list = []
    for buggy_file in os.listdir(wrong_dir_path):
        buggy_file_path = wrong_dir_path + "/" + buggy_file
        with open(buggy_file_path, "r") as f:
            buggy_code = f.read()
            buggy_code = regularize(buggy_code) # change code style (tab to space)
            buggy_code_list.append(buggy_code)

    corr_path_list = []
    if sampling_rate == 100:
        corr_path_list = shf_corr_path_list(ques_dir_path)
    elif sampling_rate == 0:
        corr_path_list = []
    else:
        # sample correct programs
        import random
        corr_path_list = shf_corr_path_list(ques_dir_path)
        l = len(corr_path_list)
        corr_path_list = corr_path_list[:int(sampling_rate / 100 * l)]

    corr_code_map = {}
    for file_name in os.listdir(ref_dir_path):
        ref_code_path = ref_dir_path + "/" + file_name
        with open(ref_code_path, "r") as f:
            ref_code = regularize(f.read())
            corr_code_map[file_name] = ref_code

    # test correct programs
    t = Tester(ques_dir_path)

    pseudo_corr_dir_path = ques_dir_path + "/code/pseudo_correct"
    if not os.path.isdir(pseudo_corr_dir_path):
        os.makedirs(pseudo_corr_dir_path)

    for corr_code_path in corr_path_list:
        with open(corr_code_path, "r") as f:
            file_name = corr_code_path.split("/")[-1]
            corr_code = regularize(f.read())
            tr = t.tv_code(corr_code)
            if t.is_pass(tr):
                corr_code_map[file_name] = corr_code
            else:
                print("The so-called correct code is not correct.")
                print(tr)
                print(corr_code_path)
                shutil.move(corr_code_path, pseudo_corr_dir_path)

    print(
    	"Filter Pseudo Corr. Code:",
    	len(corr_path_list) + len(os.listdir(ref_dir_path)),
    	"->",
    	len(list(corr_code_map.values())))

    assert(len(list(corr_code_map.values())) > 0)

    # offline refactoring
    rpt = Reporter(buggy_code_list) # printing logs
    rft = Refactoring(corr_code_map, timeout, max_depth, rpt)
    cluster_list_map = rft.ofl_bfs(csv_report=verbose) # offline breadth-first-search

    if verbose:
        # to csv
        csv_path = ques_dir_path + "/ofl_rfty_" + \
               str(sampling_rate) + "_" + \
               str(exp_idx) + ".csv"
        rft.to_csv(csv_path)

    # extract expression templates, for constant repl in block repair
    corr_code_list = list(corr_code_map.values())
    temp_list, const_list = get_temp_cons_lists(corr_code_list)

    # store refacotered correct programs to pickle file
    with open(pickle_path, 'wb') as f:
        #(cluster_list_map, expression_templates, constant_list)
        pickle.dump((cluster_list_map, temp_list, const_list, corr_code_list), f, protocol=pickle.HIGHEST_PROTOCOL)
예제 #3
0
 def visit_FunctionDef(self, node):
     self.func_map[node.name] = regularize(astunparse.unparse(node))
예제 #4
0
def gen_rep_code(ss_list, holed_func_code):
    assert(len(ss_list) > 0)
    fb_list, fb_score_list = gen_feedback(ss_list)

    if len(fb_score_list) == 0:
        return ""

    choice = fb_score_list.index(max(fb_score_list))
    fb = fb_list[choice]

    rep_code = ""
    for line in holed_func_code.split("\n"):
        if "Holes.iil_hole(" in line or \
                "Holes.empty_hole(" in line or \
                "from basic_framework.holes import *" in line:
            continue
        else:
            hole_sig_str = "Holes.condition_hole("
            ind = line.find(hole_sig_str)
            if ind != -1:
                front_str = line[:ind]
                ln = float(line[ind + len(hole_sig_str):].split(",")[0])
                rep_code += front_str + fb[ln] + ":\n"
                continue

            hole_sig_str = "Holes.assign_hole("
            ind = line.find(hole_sig_str)
            if ind != -1:
                front_str = line[:ind]
                ln = float(line[ind + len(hole_sig_str):].split(",")[0])
                rep_code += front_str + fb[ln] + "\n"
                continue

            hole_sig_str = "Holes.simple_assign_hole("
            ind = line.find(hole_sig_str)
            if ind != -1:
                front_str = line[:ind]
                ln = float(line[ind + len(hole_sig_str):].split(",")[0])
                rep_code += front_str + fb[ln] + "\n"
                continue

            hole_sig_str = "Holes.init_hole("
            ind = line.find(hole_sig_str)
            if ind != -1:
                front_str = line[:ind]
                ln = float(line[ind + len(hole_sig_str):].split(",")[0])
                rep_code += front_str + fb[ln] + "\n"
                continue

            rep_code += line + "\n"

    final_rep_code = ""
    line_list = rep_code.split("\n")[:-1]
    for line in line_list:
        while True:
            frt_str = "var_dict[\""
            end_str = "\"]"
            l = line.find(frt_str)
            if l == -1:
                break
            else:
                r = l + len(frt_str) + line[l + len(frt_str):].find(end_str)
                line = line[:l] + line[l + len(frt_str):r].strip() + line[r + len(end_str):]
        final_rep_code += line + "\n"
    final_rep_code = regularize(final_rep_code)
    return final_rep_code