pointfichiers/sync

171 lines
5.3 KiB
Text
Raw Normal View History

2024-01-05 19:05:32 +01:00
#!/usr/bin/env python3
2023-09-28 10:52:20 +02:00
import os
2024-01-05 17:25:57 +01:00
import filecmp
2023-09-28 10:52:20 +02:00
import argparse
from enum import Enum
class SyncStatus:
SAME = 1
DIFF = 2
SRC_MISSING = 3
DEST_MISSING = 4
2024-01-05 17:53:22 +01:00
BOTH_MISSING = 5
2023-09-28 10:52:20 +02:00
2024-01-05 15:38:00 +01:00
# https://stackoverflow.com/a/287944
class bcolors:
RED = "\033[91m"
2024-01-05 15:38:00 +01:00
GREEN = "\033[92m"
2024-01-05 17:25:57 +01:00
YELLOW = "\033[93m"
BLUE = "\033[94m"
2024-01-05 15:38:00 +01:00
PURPLE = "\033[95m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
2023-09-28 10:52:20 +02:00
parser = argparse.ArgumentParser(
2023-11-13 11:02:35 +01:00
prog="Dotfiles sync",
description="Saves and restores my dotfiles",
2023-09-28 10:52:20 +02:00
)
2023-11-13 11:02:35 +01:00
parser.add_argument("action", choices=["save", "restore"])
2024-01-05 15:38:00 +01:00
parser.add_argument(
"-a",
"--all",
help="Sync all files",
2024-01-05 15:38:00 +01:00
action="store_true",
)
2023-09-28 10:52:20 +02:00
synced_files = [
2023-11-13 11:02:35 +01:00
("editor/helix/", "~/.config/helix/"),
("de/i3/", "~/.config/i3/"),
("de/hypr/", "~/.config/hypr/"),
2023-12-12 09:51:42 +01:00
("shell/bash/.bash_profile", "~/.bash_profile"),
2023-11-13 11:02:35 +01:00
("shell/bash/.bashrc", "~/.bashrc"),
("shell/bash/.bash_aliases", "~/.bash_aliases"),
("shell/bash/.bash_exec", "~/.bash_exec"),
("shell/nu/.nu_aliases", "~/.nu_aliases"),
("term/rio/", "~/.config/rio/"),
("term/alacritty/", "~/.config/alacritty/"),
("bar/waybar/", "~/.config/waybar/"),
("bar/i3status-rust/", "~/.config/i3status-rust/"),
("bar/eww/", "~/.config/eww/"),
2023-11-13 11:02:35 +01:00
("home/xinitrc", "~/.xinitrc"),
("misc/picom/", "~/.config/picom/"),
("misc/runst/", "~/.config/runst/"),
("misc/mako/", "~/.config/mako/"),
("misc/swayosd/", "~/.config/swayosd/"),
2024-04-04 20:04:54 +02:00
("misc/anyrun/", "~/.config/anyrun/"),
2023-11-13 11:02:35 +01:00
("misc/x11-toggle-gpu/", "~/.local/share/x11-toggle-gpu/"),
("bin/swaylock-hyprland", "~/.local/bin/swaylock-hyprland"),
("bin/Hyprland", "~/.local/bin/Hyprland"),
("bin/jaaj", "~/.local/bin/jaaj"),
("bin/xtoggle-touchpad", "~/.local/bin/xtoggle-touchpad"),
("bin/wtoggle-touchpad", "~/.local/bin/wtoggle-touchpad"),
("bin/togglescreen", "~/.local/bin/togglescreen"),
("bin/mc-key-fix", "~/.local/bin/mc-key-fix"),
("bin/x11-toggle-primary-gpu", "~/.local/bin/x11-toggle-primary-gpu"),
2023-11-21 13:34:32 +01:00
("bin/uwu-launcher", "~/.local/bin/uwu-launcher"),
2024-01-26 09:58:21 +01:00
("bin/eww-bard", "~/.local/bin/eww-bard"),
2024-04-03 15:27:56 +02:00
("bin/wallpaperctl", "~/.local/bin/wallpaperctl"),
# Submodules
("Ahurac-dotfiles/bin/ssh-fwd", "~/.local/bin/ssh-fwd"),
2023-09-28 10:52:20 +02:00
]
2023-11-13 11:02:35 +01:00
def save(fd: tuple[str, str]):
folder = "/".join(fd[0].split("/")[0:-1])
if not os.path.exists(folder):
os.mkdir(folder)
os.system(f"rsync -r {fd[1]} {fd[0]}")
2023-11-13 11:02:35 +01:00
def restore(fd: tuple[str, str]):
os.system(f"rsync -r {fd[0]} {fd[1]}")
2023-09-28 10:52:20 +02:00
2023-11-13 11:02:35 +01:00
def get_sync_status(fd: tuple[str, str]) -> SyncStatus:
2024-01-05 17:25:57 +01:00
f1 = fd[0]
f2 = os.path.expanduser(fd[1])
def has_differences(dcmp):
differences = dcmp.left_only + dcmp.right_only + dcmp.diff_files
if differences:
return True
return any([has_differences(subdcmp) for subdcmp in dcmp.subdirs.values()])
2024-01-05 17:53:22 +01:00
if not (os.path.exists(f1) or os.path.exists(f2)):
return SyncStatus.BOTH_MISSING
elif not os.path.exists(f1):
return SyncStatus.SRC_MISSING
elif not os.path.exists(f2):
return SyncStatus.DEST_MISSING
elif os.path.isfile(f1) and filecmp.cmp(f1, f2):
return SyncStatus.SAME
elif os.path.isdir(f1) and not has_differences(filecmp.dircmp(f1, f2)):
return SyncStatus.SAME
else:
return SyncStatus.DIFF
2024-01-05 17:25:57 +01:00
2024-01-05 15:38:00 +01:00
def list_files(reverse: bool = False) -> list[int]:
len_len = len(str(len(synced_files)))
for i, f in enumerate(synced_files):
i = str(i)
i = " " * (len_len - len(i)) + i
status = get_sync_status(f)
f1_c, f2_c = bcolors.GREEN, bcolors.GREEN
if status == SyncStatus.DIFF:
f1_c, f2_c = bcolors.YELLOW, bcolors.YELLOW
elif status == SyncStatus.SRC_MISSING:
f1_c = bcolors.RED if not reverse else f1_c
f2_c = bcolors.RED if reverse else f2_c
elif status == SyncStatus.DEST_MISSING:
f1_c = bcolors.RED if reverse else f1_c
f2_c = bcolors.RED if not reverse else f2_c
2024-01-05 17:53:22 +01:00
elif status == SyncStatus.BOTH_MISSING:
f1_c, f2_c = bcolors.RED, bcolors.RED
2024-01-05 17:25:57 +01:00
2024-01-05 15:38:00 +01:00
print(
f"{bcolors.PURPLE}{i} {f1_c}{f[int(reverse)]} {bcolors.ENDC}-> {f2_c}{f[int(not reverse)]}{bcolors.ENDC}"
2024-01-05 15:38:00 +01:00
)
2024-01-05 15:51:09 +01:00
colon = ":" * len_len
2024-01-05 15:38:00 +01:00
user_action = input(
2024-01-05 15:51:09 +01:00
f"{bcolors.BOLD}{bcolors.BLUE}{colon}{bcolors.ENDC} {bcolors.BOLD}Files to sync (eg: 1 2 3, 1-3):\n{bcolors.BLUE}{colon}{bcolors.ENDC} "
2024-01-05 17:58:42 +01:00
).strip()
2024-01-05 15:38:00 +01:00
# Parse input
out = []
2024-01-05 17:58:42 +01:00
if user_action == "":
pass
elif user_action.count("-") == 0:
2024-01-05 15:38:00 +01:00
out = list(map(lambda x: int(x), user_action.split()))
elif user_action.count("-") == 1:
out = list(range(*map(lambda x: int(x), user_action.split("-"))))
out += [out[-1] + 1]
else:
raise Exception("Invalid user input")
return out
2023-11-13 11:02:35 +01:00
if __name__ == "__main__":
2023-09-28 10:52:20 +02:00
args = parser.parse_args()
2024-01-05 15:38:00 +01:00
msg = ""
r = range(len(synced_files)) if args.all else list_files(args.action == "save")
2024-01-05 15:38:00 +01:00
for ri, i in enumerate(r):
if args.action == "save":
save(synced_files[i])
elif args.action == "restore":
restore(synced_files[i])
print(" " * len(msg), end="\r")
index = "0" * (len(str(len(r))) - len(str(ri + 1))) + str(ri + 1)
msg = f"[{index}/{len(r)}] Synced {synced_files[i]}"
print(msg, end="\r")