-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathutils.py
More file actions
57 lines (47 loc) · 1.58 KB
/
Copy pathutils.py
File metadata and controls
57 lines (47 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Utilities shared by code generation backends."""
import os
import subprocess
from pathlib import Path
from typing import Mapping, Optional, Sequence
def load_project_env(project_root: Path) -> None:
try:
from dotenv import load_dotenv
env_path = project_root / ".env"
if env_path.exists():
load_dotenv(env_path)
except Exception:
pass
def truthy_env(name: str, default: str = "false") -> bool:
return os.getenv(name, default).lower() in {"1", "true", "yes", "on"}
def extract_code(model_output: str) -> str:
if "```python" in model_output:
start_idx = model_output.find("```python") + len("```python")
end_idx = model_output.find("```", start_idx)
if end_idx != -1:
return model_output[start_idx:end_idx].strip()
return model_output[start_idx:].strip()
if "```" in model_output:
start_idx = model_output.find("```") + len("```")
end_idx = model_output.find("```", start_idx)
if end_idx != -1:
return model_output[start_idx:end_idx].strip()
return model_output[start_idx:].strip()
return model_output.strip()
def run_command(
cmd: Sequence[str],
*,
cwd: Path,
env: Optional[Mapping[str, str]] = None,
prompt: Optional[str] = None,
timeout: int = 1800,
) -> subprocess.CompletedProcess:
return subprocess.run(
list(cmd),
cwd=cwd,
env=dict(env) if env is not None else None,
input=prompt,
capture_output=True,
text=True,
timeout=timeout,
check=False,
)