Skip to content

Commit a928fc0

Browse files
committed
UX changes
ux change UX changes
1 parent 62a4575 commit a928fc0

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import re
66
import shutil
77
import site
8+
import sys
89
from contextlib import contextmanager
910
from functools import lru_cache
1011
from pathlib import Path
1112
from tempfile import TemporaryDirectory
1213

1314
import tomlkit
1415

15-
from codeflash.cli_cmds.console import logger
16+
from codeflash.cli_cmds.console import logger, paneled_text
1617
from codeflash.code_utils.config_parser import find_pyproject_toml
1718

1819
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
@@ -213,3 +214,9 @@ def cleanup_paths(paths: list[Path]) -> None:
213214
def restore_conftest(path_to_content_map: dict[Path, str]) -> None:
214215
for path, file_content in path_to_content_map.items():
215216
path.write_text(file_content, encoding="utf8")
217+
218+
219+
def exit_with_message(message: str, *, error_on_exit: bool = False) -> None:
220+
paneled_text(message, panel_args={"style": "red"})
221+
222+
sys.exit(1 if error_on_exit else 0)

codeflash/code_utils/env_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

33
import os
4-
import sys
54
import tempfile
65
from functools import lru_cache
76
from pathlib import Path
87
from typing import Optional
98

109
from codeflash.cli_cmds.console import logger
10+
from codeflash.code_utils.code_utils import exit_with_message
1111
from codeflash.code_utils.formatter import format_code
1212
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config
1313

@@ -24,11 +24,11 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
2424
try:
2525
format_code(formatter_cmds, tmp_file, print_status=False)
2626
except Exception:
27-
print(
28-
"⚠️ Codeflash requires a code formatter to be installed in your environment, but none was found. Please install a supported formatter, verify the formatter-cmds in your codeflash pyproject.toml config and try again."
27+
exit_with_message(
28+
"⚠️ Codeflash requires a code formatter to be installed in your environment, but none was found. Please install a supported formatter, verify the formatter-cmds in your codeflash pyproject.toml config and try again.",
29+
error_on_exit=True,
2930
)
30-
if exit_on_failure:
31-
sys.exit(1)
31+
3232
return return_code
3333

3434

codeflash/discovery/functions_to_optimize.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from codeflash.api.cfapi import get_blocklisted_functions, is_function_being_optimized_again
1818
from codeflash.cli_cmds.console import DEBUG_MODE, console, logger
1919
from codeflash.code_utils.code_utils import (
20+
exit_with_message,
2021
is_class_defined_in_file,
2122
module_name_from_file_path,
2223
path_belongs_to_site_packages,
@@ -179,8 +180,9 @@ def get_functions_to_optimize(
179180
if only_get_this_function is not None:
180181
split_function = only_get_this_function.split(".")
181182
if len(split_function) > 2:
182-
msg = "Function name should be in the format 'function_name' or 'class_name.function_name'"
183-
raise ValueError(msg)
183+
exit_with_message(
184+
"Function name should be in the format 'function_name' or 'class_name.function_name'"
185+
)
184186
if len(split_function) == 2:
185187
class_name, only_function_name = split_function
186188
else:
@@ -193,8 +195,9 @@ def get_functions_to_optimize(
193195
):
194196
found_function = fn
195197
if found_function is None:
196-
msg = f"Function {only_function_name} not found in file {file} or the function does not have a 'return' statement or is a property"
197-
raise ValueError(msg)
198+
exit_with_message(
199+
f"Function {only_function_name} not found in file {file}\nor the function does not have a 'return' statement or is a property"
200+
)
198201
functions[file] = [found_function]
199202
else:
200203
logger.info("Finding all functions modified in the current git diff ...")

codeflash/optimization/optimizer.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def create_function_optimizer(
7272
def run(self) -> None:
7373
from codeflash.code_utils.checkpoint import CodeflashRunCheckpoint
7474
from codeflash.code_utils.code_replacer import normalize_code, normalize_node
75-
from codeflash.code_utils.code_utils import cleanup_paths
7675
from codeflash.code_utils.static_analysis import (
7776
analyze_imported_modules,
7877
get_first_top_level_function_or_method_ast,
@@ -283,14 +282,22 @@ def run(self) -> None:
283282
if function_optimizer:
284283
function_optimizer.cleanup_generated_files()
285284

286-
if self.test_cfg.concolic_test_root_dir:
287-
cleanup_paths([self.test_cfg.concolic_test_root_dir])
285+
self.cleanup_temporary_paths()
286+
287+
def cleanup_temporary_paths(self) -> None:
288+
from codeflash.code_utils.code_utils import cleanup_paths
289+
290+
cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir])
288291

289292

290293
def run_with_args(args: Namespace) -> None:
294+
optimizer = None
291295
try:
292296
optimizer = Optimizer(args)
293297
optimizer.run()
294298
except KeyboardInterrupt:
295-
logger.warning("Keyboard interrupt received. Exiting, please wait…")
299+
logger.warning("Keyboard interrupt received. Cleaning up and exiting, please wait…")
300+
if optimizer:
301+
optimizer.cleanup_temporary_paths()
302+
296303
raise SystemExit from None

0 commit comments

Comments
 (0)