Compare commits

...

5 commits

Author SHA1 Message Date
7d394abcd3 sync: Added missing files/folders detection 2024-01-05 17:48:02 +01:00
ecad407e19 sync: Sync now asks user selection by default
Removed --select flag in favor of --all flag
2024-01-05 17:27:55 +01:00
794e204891 sync: Add diff detection 2024-01-05 17:25:57 +01:00
517f637c16 bin: Lint jaaj script 2024-01-05 17:25:27 +01:00
599b189fc5 sync: Made colon count reactive 2024-01-05 15:51:09 +01:00
2 changed files with 333 additions and 283 deletions

555
bin/jaaj

File diff suppressed because it is too large Load diff

61
sync
View file

@ -1,13 +1,24 @@
#!venv/bin/python
import os
import filecmp
import argparse
from enum import Enum
class SyncStatus:
SAME = 1
DIFF = 2
SRC_MISSING = 3
DEST_MISSING = 4
# https://stackoverflow.com/a/287944
class bcolors:
BLUE = "\033[94m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
PURPLE = "\033[95m"
ENDC = "\033[0m"
BOLD = "\033[1m"
@ -20,9 +31,9 @@ parser = argparse.ArgumentParser(
)
parser.add_argument("action", choices=["save", "restore"])
parser.add_argument(
"-s",
"--select",
help="Select the files and folders to sync",
"-a",
"--all",
help="Sync all files",
action="store_true",
)
@ -72,17 +83,53 @@ def restore(fd: tuple[str, str]):
os.system(f"rsync -r {fd[0]} {fd[1]}")
def get_sync_status(fd: tuple[str, str]) -> SyncStatus:
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()])
if 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
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
print(
f"{bcolors.PURPLE}{i} {bcolors.GREEN}{f[int(reverse)]} {bcolors.ENDC}-> {bcolors.GREEN}{f[int(not reverse)]}{bcolors.ENDC}"
f"{bcolors.PURPLE}{i} {f1_c}{f[int(reverse)]} {bcolors.ENDC}-> {f2_c}{f[int(not reverse)]}{bcolors.ENDC}"
)
colon = ":" * len_len
user_action = input(
f"{bcolors.BOLD}{bcolors.BLUE}::{bcolors.ENDC} {bcolors.BOLD}Files to sync (eg: 1 2 3, 1-3):\n{bcolors.BLUE}::{bcolors.ENDC} "
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} "
)
# Parse input
@ -102,7 +149,7 @@ if __name__ == "__main__":
args = parser.parse_args()
msg = ""
r = list_files(args.action == "save") if args.select else range(len(synced_files))
r = range(len(synced_files)) if args.all else list_files(args.action == "save")
for ri, i in enumerate(r):
if args.action == "save":
save(synced_files[i])