Skip to content

Commit ac0894a

Browse files
committed
running all commands in list to ensure all run
1 parent dfc5177 commit ac0894a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import shlex
33
import subprocess
4+
import tempfile
45
from functools import lru_cache
56
from typing import Optional
67

@@ -9,16 +10,21 @@
910

1011

1112
def check_formatter_installed(formatter_cmds: list[str]) -> bool:
12-
clean_cmd = formatter_cmds[0].replace("uvx ","").replace("uv ","").replace("tool ","").replace("run ","")
13-
clean_cmd_parts = shlex.split(clean_cmd," ")
14-
formatter = "black" if "black" in clean_cmd_parts else "ruff" if "ruff" in clean_cmd_parts else clean_cmd_parts[0]
15-
if not formatter:
16-
try:
17-
subprocess.run([formatter], check=False)
18-
except (FileNotFoundError, NotADirectoryError):
19-
logger.error(f"⚠️ Formatter not found: {formatter}, please ensure it is installed")
20-
return False
21-
return True
13+
return_code = True
14+
if formatter_cmds[0] == "disabled":
15+
return return_code
16+
tmp_code = """print("hello world")"""
17+
tmp_file = tempfile.NamedTemporaryFile(suffix=".py").write_text(tmp_code, encoding="utf8")
18+
file_token = "$file" # noqa: S105
19+
for command in set(formatter_cmds):
20+
formatter_cmd_list = shlex.split(command, posix=os.name != "nt")
21+
formatter_cmd_list = [tmp_file.as_posix() if chunk == file_token else chunk for chunk in formatter_cmd_list]
22+
result = subprocess.run(formatter_cmd_list, capture_output=True, check=False)
23+
if result.returncode:
24+
return_code = False
25+
break
26+
tmp_file.unlink(missing_ok=True)
27+
return return_code
2228

2329

2430
@lru_cache(maxsize=1)

0 commit comments

Comments
 (0)