nut/chipnut/commands/setup.py
2026-04-04 12:02:15 +02:00

48 lines
1.5 KiB
Python

from ..paths import get_templates_path
import shutil
import os
def find_template_conflicts(dest_dir: str) -> list[str]:
templates_path = get_templates_path() / "project"
if not os.path.exists(templates_path):
raise RuntimeError(f"Project template path does not exist: {templates_path}")
conflicts = []
for root, _, files in os.walk(templates_path):
rel = os.path.relpath(root, templates_path)
target_root = os.path.join(dest_dir, rel) if rel != "." else dest_dir
for file in files:
target_file = os.path.join(target_root, file)
if os.path.exists(target_file):
conflicts.append(target_file)
return conflicts
def copy_template_files(dest_dir: str, force: bool = False, checked: bool = False) -> None:
templates_path = get_templates_path() / "project"
if not os.path.exists(templates_path):
raise RuntimeError(f"Project template path does not exist: {templates_path}")
os.makedirs(dest_dir, exist_ok=True)
if not checked:
conflicts = find_template_conflicts(dest_dir)
if conflicts and not force:
raise FileExistsError(
"Refusing to overwrite existing files:\n" +
"\n".join(conflicts)
)
shutil.copytree(
templates_path,
dest_dir,
dirs_exist_ok=True
)
def is_chipnut_workspace(path: str = ".") -> bool:
return os.path.isfile(os.path.join(path, "Nutfile"))