dotfiles/common/sync.py

32 lines
1 KiB
Python
Raw Permalink Normal View History

2024-08-08 13:01:33 +02:00
from glob import glob
import shutil
import os
2024-08-08 12:28:10 +02:00
def sync(stuff: list[str]):
2024-08-08 13:01:33 +02:00
for pattern in stuff:
normalized_pattern = pattern.removeprefix("~")
normalized_pattern = normalized_pattern.removeprefix("/")
pattern_prefix = pattern[:2] if pattern.startswith("~") else pattern[0]
overwrite = False
for path in glob(normalized_pattern, recursive=True):
system_path = os.path.expanduser(pattern_prefix + path)
if os.path.isdir(path):
os.makedirs(system_path, exist_ok=True)
else:
2024-08-08 14:47:58 +02:00
print(f":: Copying {path} to {system_path}")
2024-08-08 13:01:33 +02:00
if os.path.exists(system_path) and not overwrite:
2024-08-08 14:47:58 +02:00
print(f":: Path {system_path} already exists. Do you want to overwrite it with {path}? y/n/a")
2024-08-08 13:01:33 +02:00
ask = input()
if ask not in ("y", "a"):
continue
overwrite = ask == "a"
shutil.copy2(path, system_path)
2024-08-07 22:46:13 +02:00
exit(1)