Skip to content

Commit 6addecc

Browse files
committed
consolidate logic around path cleanup
1 parent 905d34e commit 6addecc

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ast
44
import os
5+
import shutil
56
import site
67
from functools import lru_cache
78
from pathlib import Path
@@ -118,4 +119,7 @@ def has_any_async_functions(code: str) -> bool:
118119

119120
def cleanup_paths(paths: list[Path]) -> None:
120121
for path in paths:
121-
path.unlink(missing_ok=True)
122+
if path.is_dir():
123+
shutil.rmtree(path, ignore_errors=True)
124+
else:
125+
path.unlink(missing_ok=True)

codeflash/optimization/function_optimizer.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,6 @@ def optimize_function(self) -> Result[BestOptimization, str]:
345345
original_helper_code,
346346
self.function_to_optimize.file_path,
347347
)
348-
for generated_test_path in generated_test_paths:
349-
generated_test_path.unlink(missing_ok=True)
350-
for generated_perf_test_path in generated_perf_test_paths:
351-
generated_perf_test_path.unlink(missing_ok=True)
352-
for test_paths in instrumented_unittests_created_for_function:
353-
test_paths.unlink(missing_ok=True)
354-
for fn in function_to_concolic_tests:
355-
for test in function_to_concolic_tests[fn]:
356-
if not test.tests_in_file.test_file.parent.exists():
357-
logger.warning(
358-
f"Concolic test directory {test.tests_in_file.test_file.parent} does not exist so could not be deleted."
359-
)
360-
shutil.rmtree(test.tests_in_file.test_file.parent, ignore_errors=True)
361-
break # need to delete only one test directory
362348

363349
if not best_optimization:
364350
return Failure(f"No best optimizations found for function {self.function_to_optimize.qualified_name}")
@@ -1242,3 +1228,25 @@ def generate_and_instrument_tests(
12421228
zip(generated_test_paths, generated_perf_test_paths)
12431229
)
12441230
]
1231+
1232+
def cleanup_generated_files(self) -> None:
1233+
paths_to_cleanup = (
1234+
[
1235+
test_file.instrumented_behavior_file_path
1236+
for test_type in [
1237+
TestType.GENERATED_REGRESSION,
1238+
TestType.EXISTING_UNIT_TEST,
1239+
TestType.CONCOLIC_COVERAGE_TEST,
1240+
]
1241+
for test_file in self.test_files.get_by_type(test_type).test_files
1242+
]
1243+
+ [
1244+
test_file.benchmarking_file_path
1245+
for test_type in [TestType.GENERATED_REGRESSION, TestType.EXISTING_UNIT_TEST]
1246+
for test_file in self.test_files.get_by_type(test_type).test_files
1247+
]
1248+
+ [self.test_cfg.concolic_test_root_dir]
1249+
)
1250+
cleanup_paths(paths_to_cleanup)
1251+
if hasattr(get_run_tmp_file, "tmpdir"):
1252+
get_run_tmp_file.tmpdir.cleanup()

codeflash/optimization/optimizer.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ast
44
import os
5-
import shutil
65
import tempfile
76
import time
87
from collections import defaultdict
@@ -19,7 +18,7 @@
1918
from codeflash.code_utils import env_utils
2019
from codeflash.code_utils.checkpoint import CodeflashRunCheckpoint, ask_should_use_checkpoint_get_functions
2120
from codeflash.code_utils.code_replacer import normalize_code, normalize_node
22-
from codeflash.code_utils.code_utils import get_run_tmp_file
21+
from codeflash.code_utils.code_utils import cleanup_paths, get_run_tmp_file
2322
from codeflash.code_utils.static_analysis import analyze_imported_modules, get_first_top_level_function_or_method_ast
2423
from codeflash.discovery.discover_unit_tests import discover_unit_tests
2524
from codeflash.discovery.functions_to_optimize import get_functions_to_optimize
@@ -266,22 +265,7 @@ def run(self) -> None:
266265
logger.info("✨ All functions have been optimized! ✨")
267266
finally:
268267
if function_optimizer:
269-
for test_file in function_optimizer.test_files.get_by_type(TestType.GENERATED_REGRESSION).test_files:
270-
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
271-
test_file.benchmarking_file_path.unlink(missing_ok=True)
272-
for test_file in function_optimizer.test_files.get_by_type(TestType.EXISTING_UNIT_TEST).test_files:
273-
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
274-
test_file.benchmarking_file_path.unlink(missing_ok=True)
275-
for test_file in function_optimizer.test_files.get_by_type(TestType.CONCOLIC_COVERAGE_TEST).test_files:
276-
test_file.instrumented_behavior_file_path.unlink(missing_ok=True)
277-
if function_optimizer.test_cfg.concolic_test_root_dir:
278-
shutil.rmtree(function_optimizer.test_cfg.concolic_test_root_dir, ignore_errors=True)
279-
if self.args.benchmark:
280-
if self.replay_tests_dir.exists():
281-
shutil.rmtree(self.replay_tests_dir, ignore_errors=True)
282-
trace_file.unlink(missing_ok=True)
283-
if hasattr(get_run_tmp_file, "tmpdir"):
284-
get_run_tmp_file.tmpdir.cleanup()
268+
function_optimizer.cleanup_generated_files()
285269

286270

287271
def run_with_args(args: Namespace) -> None:

0 commit comments

Comments
 (0)