Skip to content

Commit b7258a9

Browse files
authored
Merge branch 'main' into trace-and-optimize
2 parents ce68cad + 9760a51 commit b7258a9

File tree

8 files changed

+59
-44
lines changed

8 files changed

+59
-44
lines changed

codeflash/code_utils/shell_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
SHELL_RC_EXPORT_PATTERN = re.compile(r"^set CODEFLASH_API_KEY=(cf-.*)$", re.MULTILINE)
1616
SHELL_RC_EXPORT_PREFIX = "set CODEFLASH_API_KEY="
1717
else:
18-
SHELL_RC_EXPORT_PATTERN = re.compile(r'^export CODEFLASH_API_KEY="?(cf-[^\s"]+)"?$', re.MULTILINE)
18+
SHELL_RC_EXPORT_PATTERN = re.compile(r'^(?!#)export CODEFLASH_API_KEY=[\'"]?(cf-[^\s"]+)[\'"]$', re.MULTILINE)
1919
SHELL_RC_EXPORT_PREFIX = "export CODEFLASH_API_KEY="
2020

2121

codeflash/optimization/function_optimizer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,11 +1457,9 @@ def submit_test_generation_tasks(
14571457
]
14581458

14591459
def cleanup_generated_files(self) -> None:
1460-
paths_to_cleanup = [self.test_cfg.concolic_test_root_dir]
1460+
paths_to_cleanup = []
14611461
for test_file in self.test_files:
14621462
paths_to_cleanup.append(test_file.instrumented_behavior_file_path)
14631463
paths_to_cleanup.append(test_file.benchmarking_file_path)
14641464

14651465
cleanup_paths(paths_to_cleanup)
1466-
if hasattr(get_run_tmp_file, "tmpdir"):
1467-
get_run_tmp_file.tmpdir.cleanup()

codeflash/optimization/optimizer.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient
1313
from codeflash.cli_cmds.console import console, logger, progress_bar
1414
from codeflash.code_utils import env_utils
15-
from codeflash.code_utils.code_utils import cleanup_paths
15+
from codeflash.code_utils.code_utils import cleanup_paths, get_run_tmp_file
1616
from codeflash.code_utils.env_utils import get_pr_number
1717
from codeflash.either import is_successful
1818
from codeflash.models.models import ValidCode
@@ -289,31 +289,36 @@ def run(self) -> None:
289289
f"{function_to_optimize.qualified_name}"
290290
)
291291
console.rule()
292+
function_optimizer = None
293+
try:
294+
function_optimizer = self.create_function_optimizer(
295+
function_to_optimize,
296+
function_to_tests=function_to_tests,
297+
function_to_optimize_source_code=validated_original_code[original_module_path].source_code,
298+
function_benchmark_timings=function_benchmark_timings,
299+
total_benchmark_timings=total_benchmark_timings,
300+
original_module_ast=original_module_ast,
301+
original_module_path=original_module_path,
302+
)
292303

293-
function_optimizer = self.create_function_optimizer(
294-
function_to_optimize,
295-
function_to_tests=function_to_tests,
296-
function_to_optimize_source_code=validated_original_code[original_module_path].source_code,
297-
function_benchmark_timings=function_benchmark_timings,
298-
total_benchmark_timings=total_benchmark_timings,
299-
original_module_ast=original_module_ast,
300-
original_module_path=original_module_path,
301-
)
302-
303-
self.current_function_optimizer = (
304-
function_optimizer # needed to clean up from the outside of this function
305-
)
306-
best_optimization = function_optimizer.optimize_function()
307-
if self.functions_checkpoint:
308-
self.functions_checkpoint.add_function_to_checkpoint(
309-
function_to_optimize.qualified_name_with_modules_from_root(self.args.project_root)
304+
self.current_function_optimizer = (
305+
function_optimizer # needed to clean up from the outside of this function
310306
)
311-
if is_successful(best_optimization):
312-
optimizations_found += 1
313-
else:
314-
logger.warning(best_optimization.failure())
315-
console.rule()
316-
continue
307+
best_optimization = function_optimizer.optimize_function()
308+
if self.functions_checkpoint:
309+
self.functions_checkpoint.add_function_to_checkpoint(
310+
function_to_optimize.qualified_name_with_modules_from_root(self.args.project_root)
311+
)
312+
if is_successful(best_optimization):
313+
optimizations_found += 1
314+
else:
315+
logger.warning(best_optimization.failure())
316+
console.rule()
317+
continue
318+
finally:
319+
if function_optimizer is not None:
320+
function_optimizer.cleanup_generated_files()
321+
317322
ph("cli-optimize-run-finished", {"optimizations_found": optimizations_found})
318323
if self.functions_checkpoint:
319324
self.functions_checkpoint.cleanup()
@@ -351,6 +356,9 @@ def cleanup_temporary_paths(self) -> None:
351356
if self.current_function_optimizer:
352357
self.current_function_optimizer.cleanup_generated_files()
353358

359+
if hasattr(get_run_tmp_file, "tmpdir"):
360+
get_run_tmp_file.tmpdir.cleanup()
361+
del get_run_tmp_file.tmpdir
354362
cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir])
355363

356364

codeflash/telemetry/posthog_cf.py

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

88
from codeflash.api.cfapi import get_user_id
99
from codeflash.cli_cmds.console import logger
10-
from codeflash.version import __version__, __version_tuple__
10+
from codeflash.version import __version__
1111

1212
_posthog = None
1313

@@ -36,7 +36,7 @@ def ph(event: str, properties: dict[str, Any] | None = None) -> None:
3636
return
3737

3838
properties = properties or {}
39-
properties.update({"cli_version": __version__, "cli_version_tuple": __version_tuple__})
39+
properties.update({"cli_version": __version__})
4040

4141
user_id = get_user_id()
4242

codeflash/update_license_version.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from datetime import datetime
33
from pathlib import Path
44

5-
from version import __version_tuple__
5+
from .version import __version__
66

77

88
def main() -> None:
99
# Use the version tuple from version.py
10-
version = __version_tuple__
10+
version = __version__
1111

1212
# Use the major and minor version components from the version tuple
13-
major_minor_version = ".".join(map(str, version[:2]))
13+
major_minor_version = ".".join(map(str, version.split(".")[:2]))
1414

1515
# Define the pattern to search for and the replacement string for the version
1616
version_pattern = re.compile(r"(Licensed Work:\s+Codeflash Client version\s+)(0\.\d+)(\.x)")

codeflash/version.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# These version placeholders will be replaced by uv-dynamic-versioning during build.
2-
__version__ = "0.14.6"
3-
__version_tuple__ = (0, 14, 6)
2+
__version__ = "0.14.7"

pyproject.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,17 @@ enable = true
268268
style = "pep440"
269269
vcs = "git"
270270

271-
[tool.uv-dynamic-versioning.substitution]
272-
files = ["codeflash/version.py"]
273-
274-
[tool.uv-dynamic-versioning.files."codeflash/version.py"]
275-
persistent-substitution = true
276-
initial-content = """
277-
# These version placeholders will be replaced by uv-dynamic-versioning during build.
278-
__version__ = "0.0.0"
279-
__version_tuple__ = (0, 0, 0)
271+
[tool.hatch.build.hooks.version]
272+
path = "codeflash/version.py"
273+
template = """# These version placeholders will be replaced by uv-dynamic-versioning during build.
274+
__version__ = "{version}"
280275
"""
281276

277+
278+
#[tool.hatch.build.hooks.custom]
279+
#path = "codeflash/update_license_version.py"
280+
281+
282282
[tool.codeflash]
283283
module-root = "codeflash"
284284
tests-root = "tests"

tests/test_shell_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def test_valid_api_key(self):
5454
) as mock_file:
5555
self.assertEqual(read_api_key_from_shell_config(), self.api_key)
5656
mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")
57+
with patch(
58+
"builtins.open", mock_open(read_data=f'export CODEFLASH_API_KEY=\'{self.api_key}\'\n')
59+
) as mock_file:
60+
self.assertEqual(read_api_key_from_shell_config(), self.api_key)
61+
mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")
62+
with patch(
63+
"builtins.open", mock_open(read_data=f'#export CODEFLASH_API_KEY=\'{self.api_key}\'\n')
64+
) as mock_file:
65+
self.assertEqual(read_api_key_from_shell_config(), None)
66+
mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")
5767

5868
@patch("codeflash.code_utils.shell_utils.get_shell_rc_path")
5969
def test_no_api_key(self, mock_get_shell_rc_path):

0 commit comments

Comments
 (0)