Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/specify_cli/_init_options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Helpers for interpreting persisted init options."""

import json
import os
import tempfile
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Union
Expand All @@ -23,10 +25,22 @@ def save_init_options(project_path: Path, options: dict[str, Any]) -> None:
"""Persist the CLI options used during ``specify init``."""
dest = project_path / INIT_OPTIONS_FILE
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_text(
json.dumps(options, indent=2, sort_keys=True, ensure_ascii=False) + "\n",
encoding="utf-8",
fd, tmp = tempfile.mkstemp(
dir=str(dest.parent),
prefix=f".{dest.name}.",
suffix=".tmp",
)
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(options, f, indent=2, sort_keys=True, ensure_ascii=False)
f.write("\n")
os.replace(tmp, dest)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise


def load_init_options(project_path: Path) -> dict[str, Any]:
Expand Down