MCPcopy Index your code
hub / github.com/github/spec-kit / atomic_write_json

Function atomic_write_json

src/specify_cli/_utils.py:157–191  ·  view source on GitHub ↗

Atomically write JSON while preserving existing mode bits when possible.

(target_file: Path, payload: dict[str, Any])

Source from the content-addressed store, hash-verified

155 console.print(f"[{color}]{message}[/] {rel_path}")
156
157 def atomic_write_json(target_file: Path, payload: dict[str, Any]) -> None:
158 """Atomically write JSON while preserving existing mode bits when possible."""
159 temp_path: Path | None = None
160 try:
161 with tempfile.NamedTemporaryFile(
162 mode='w',
163 encoding='utf-8',
164 dir=target_file.parent,
165 prefix=f"{target_file.name}.",
166 suffix=".tmp",
167 delete=False,
168 ) as f:
169 temp_path = Path(f.name)
170 json.dump(payload, f, indent=4)
171 f.write('\n')
172
173 if target_file.exists():
174 try:
175 existing_stat = target_file.stat()
176 os.chmod(temp_path, stat.S_IMODE(existing_stat.st_mode))
177 if hasattr(os, "chown"):
178 try:
179 os.chown(temp_path, existing_stat.st_uid, existing_stat.st_gid)
180 except PermissionError:
181 # Best-effort owner/group preservation without requiring elevated privileges.
182 pass
183 except OSError:
184 # Best-effort metadata preservation; data safety is prioritized.
185 pass
186
187 os.replace(temp_path, target_file)
188 except Exception:
189 if temp_path and temp_path.exists():
190 temp_path.unlink()
191 raise
192
193 try:
194 with open(sub_item, 'r', encoding='utf-8') as f:

Callers 1

handle_vscode_settingsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected