| 1 | """ |
| 2 | Perform validation of a patch, on a given task instance. |
| 3 | """ |
| 4 | |
| 5 | import ast |
| 6 | import itertools |
| 7 | import shlex |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | import tempfile |
| 11 | from os import PathLike |
| 12 | from pathlib import Path |
| 13 | from subprocess import PIPE, STDOUT |
| 14 | |
| 15 | from unidiff import PatchSet |
| 16 | |
| 17 | from app.analysis.sbfl import method_ranges_in_file |
| 18 | from app.data_structures import MethodId |
| 19 | |
| 20 | |
| 21 | def perfect_angelic_debug( |
| 22 | task_id: str, diff_file: str, project_path: str |
| 23 | ) -> tuple[ |
| 24 | set[tuple[str, MethodId]], set[tuple[str, MethodId]], set[tuple[str, MethodId]] |
| 25 | ]: |
| 26 | """Do perfect angelic debugging and return a list of incorrect fix locations. |
| 27 | |
| 28 | Args: |
| 29 | task_id: the task id, used to find developer patch |
| 30 | diff_file: path of diff file |
| 31 | |
| 32 | Returns: |
| 33 | A list of (filename, MethodId) that should not have been changed by diff_file |
| 34 | """ |
| 35 | return compare_fix_locations( |
| 36 | diff_file, get_developer_patch_file(task_id), project_path |
| 37 | ) |
| 38 | |
| 39 | |
| 40 | def compare_fix_locations( |
| 41 | diff_file: str, dev_diff_file: str, project_path: str |
| 42 | ) -> tuple[ |
| 43 | set[tuple[str, MethodId]], set[tuple[str, MethodId]], set[tuple[str, MethodId]] |
| 44 | ]: |
| 45 | """Compare the changed methods in two diff files |
| 46 | |
| 47 | Args: |
| 48 | diff_file: path to diff file |
| 49 | dev_diff_file: path to a "correct" diff file |
| 50 | |
| 51 | Returns: |
| 52 | list of (filename, MethodId) that are changed in diff_file but not in dev_diff_file |
| 53 | """ |
| 54 | methods_map = get_changed_methods(diff_file, project_path) |
| 55 | dev_methods_map = get_changed_methods(dev_diff_file, project_path) |
| 56 | |
| 57 | methods_set = set( |
| 58 | itertools.chain.from_iterable( |
| 59 | [(k, method_id) for method_id in v] for k, v in methods_map.items() |
| 60 | ) |
| 61 | ) |
| 62 | dev_methods_set = set( |
| 63 | itertools.chain.from_iterable( |
| 64 | [(k, method_id) for method_id in v] for k, v in dev_methods_map.items() |
| 65 | ) |
| 66 | ) |
| 67 | |
| 68 | return ( |
| 69 | methods_set - dev_methods_set, |
| 70 | methods_set & dev_methods_set, |
| 71 | dev_methods_set - methods_set, |
| 72 | ) |
| 73 | |
| 74 | |
| 75 | def get_developer_patch_file(task_id: str) -> str: |
| 76 | processed_data_lite = Path(__file__).parent.parent.with_name("processed_data_lite") |
| 77 | dev_patch_file = Path( |
| 78 | processed_data_lite, "test", task_id, "developer_patch.diff" |
| 79 | ).resolve() |
| 80 | if not dev_patch_file.is_file(): |
| 81 | raise RuntimeError(f"Failed to find developer patch at {dev_patch_file!s}") |
| 82 | return str(dev_patch_file) |
| 83 | |
| 84 | |
| 85 | def get_method_id(file: str, line: int) -> MethodId | None: |
| 86 | ranges = method_ranges_in_file(file) |
| 87 | for method_id, (lower, upper) in ranges.items(): |
| 88 | if lower <= line <= upper: |
| 89 | return method_id |
| 90 | return None |
| 91 | |
| 92 | |
| 93 | def get_changed_methods( |
| 94 | diff_file: str, project_path: str = "" |
| 95 | ) -> dict[str, set[MethodId]]: |
| 96 | with open(diff_file) as f: |
| 97 | patch_content = f.read() |
| 98 | |
| 99 | changed_files = [] |
| 100 | |
| 101 | patch = PatchSet(patch_content) |
| 102 | for file in patch: |
| 103 | file_name = file.source_file.removeprefix("a/").removeprefix("b/") |
| 104 | changed_files.append(file_name) |
| 105 | |
| 106 | orig_definitions: dict[tuple[str, MethodId], str] = {} |
| 107 | for file in changed_files: |
| 108 | def_map = collect_method_definitions(Path(project_path, file)) |
| 109 | |
| 110 | for method_id, definition in def_map.items(): |
| 111 | orig_definitions[(file, method_id)] = definition |
| 112 | |
| 113 | temp_dir = tempfile.mkdtemp(dir="/tmp", prefix="apply_patch_") |
| 114 | for file in changed_files: |
| 115 | copy_path = Path(temp_dir, file) |
| 116 | copy_path.parent.mkdir(parents=True, exist_ok=True) |
| 117 | shutil.copy2(Path(project_path, file), copy_path) |
| 118 | patch_cmd = f"patch -p1 -f -i {diff_file}" |
| 119 | cp = subprocess.run( |
| 120 | shlex.split(patch_cmd), cwd=temp_dir, stdout=PIPE, stderr=STDOUT, text=True |
| 121 | ) |
| 122 | if cp.returncode != 0: |
| 123 | raise RuntimeError( |
| 124 | f"Patch command in directory {temp_dir} exit with {cp.returncode}: {patch_cmd}" |
| 125 | ) |
| 126 | |
| 127 | new_definitions: dict[tuple[str, MethodId], str] = {} |
| 128 | for file in changed_files: |
| 129 | def_map = collect_method_definitions(Path(temp_dir, file)) |
| 130 | |
| 131 | for method_id, definition in def_map.items(): |
| 132 | new_definitions[(file, method_id)] = definition |
| 133 | |
| 134 | shutil.rmtree(temp_dir) |
| 135 | |
| 136 | result = {} |
| 137 | for key, definition in orig_definitions.items(): |
| 138 | if new_definitions.get(key, "") != definition: |
| 139 | file, method_id = key |
| 140 | result[file] = result.get(file, set()) | {method_id} |
| 141 | |
| 142 | return result |
| 143 | |
| 144 | |
| 145 | def collect_method_definitions(file: str | PathLike) -> dict[MethodId, str]: |
| 146 | if not str(file).endswith(".py"): |
| 147 | return {} |
| 148 | |
| 149 | collector = MethodDefCollector() |
| 150 | |
| 151 | source = Path(file).read_text() |
| 152 | tree = ast.parse(source, file) |
| 153 | |
| 154 | collector.visit(tree) |
| 155 | return collector.def_map |
| 156 | |
| 157 | |
| 158 | class MethodDefCollector(ast.NodeVisitor): |
| 159 | def __init__(self): |
| 160 | self.def_map: dict[MethodId, str] = {} |
| 161 | self.class_name = "" |
| 162 | |
| 163 | def calc_method_id(self, method_name: str) -> MethodId: |
| 164 | return MethodId(self.class_name, method_name) |
| 165 | |
| 166 | def visit_ClassDef(self, node: ast.ClassDef) -> None: |
| 167 | self.class_name = node.name |
| 168 | super().generic_visit(node) |
| 169 | self.class_name = "" |
| 170 | |
| 171 | def visit_FunctionDef(self, node: ast.FunctionDef) -> None: |
| 172 | method_id = self.calc_method_id(node.name) |
| 173 | self.def_map[method_id] = ast.unparse(node) |
| 174 | |
| 175 | def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None: |
| 176 | method_id = self.calc_method_id(node.name) |
| 177 | self.def_map[method_id] = ast.unparse(node) |