Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
48 lines (40 loc) · 1.59 KB
/
Copy pathutils.py
File metadata and controls
48 lines (40 loc) · 1.59 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
from anyio import Path
from noneprompt import ConfirmPrompt
async def safe_write_file(
file_path: Path, content: str | bytes, force: bool = False
) -> int | None:
directory = file_path.parent
if not await directory.exists():
await directory.mkdir(exist_ok=True, parents=True)
elif not await directory.is_dir():
raise RuntimeError(f"File {directory} already exists and is not a directory")
if (
await file_path.exists()
and not force
and not await ConfirmPrompt(
f"File {file_path} already exists, overwrite?",
default_choice=False,
).prompt_async()
):
return
return (
await file_path.write_text(content, encoding="utf-8")
if isinstance(content, str)
else await file_path.write_bytes(content)
)
async def safe_copy_dir(source: Path, destination: Path, force: bool = False):
if not await source.exists():
raise RuntimeError(f"Directory {source} does not exist")
if not await source.is_dir():
raise RuntimeError(f"Path {source} is not a directory")
if not await destination.exists():
await destination.mkdir(exist_ok=True, parents=True)
elif not await destination.is_dir():
raise RuntimeError(f"File {destination} already exists and is not a directory")
async for file in source.iterdir():
if await file.is_dir():
await safe_copy_dir(file, destination / file.name, force=force)
else:
await safe_write_file(
destination / file.name, await file.read_bytes(), force=force
)