diff --git a/home/scripts/sync-all.py b/home/scripts/sync-all.py new file mode 100755 index 00000000..4171ea67 --- /dev/null +++ b/home/scripts/sync-all.py @@ -0,0 +1,68 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python3 -p git git-sync + +import os +import queue +import subprocess +from pathlib import Path +from typing import Dict + +HOME = Path("~").expanduser().resolve() + +SYNC_MAPPING: Dict[Path, str] = { + Path( + "~/.password-store" + ).expanduser(): "https://git.xenia.me.uk/pixelifytica/pass.git", + Path("~/.elfeed").expanduser(): "https://git.xenia.me.uk/pixelifytica/elfeed.git", + Path( + "~/Documents/Org" + ).expanduser(): "https://git.xenia.me.uk/pixelifytica/org.git", + Path( + "~/Documents/References" + ).expanduser(): "https://git.xenia.me.uk/pixelifytica/references.git", +} + +PULL_MAPPINGS: Dict[Path, str] = { + Path("/etc/nixos"): "https://git.xenia.me.uk/pixelifytica/nixos.git", + Path(os.getenv("XDG_CONFIG_HOME", "~/.config")).expanduser() + / "emacs": "https://git.xenia.me.uk/pixelifytica/emacs.git", +} + + +def git_set_url(directory: Path, url: str) -> None: + """Set url for directory""" + comp = subprocess.run(f"git remote set-url origin {url}".split(), cwd=directory) + if comp.returncode != 0: + raise UserWarning(f"{comp.returncode} from {' '.join(comp.args)}") + subprocess.run("git remote -v".split(), cwd=directory) + + +def git_sync(directory: Path, url: str) -> subprocess.CompletedProcess: + """Sync status of repository""" + print(f"--- sync: ${directory} ---") + if not directory.exists(): + return subprocess.run("exit 1".split()) # TODO + git_set_url(directory, url) + return subprocess.run("git-sync -ns".split(), cwd=directory) + + +def git_pull( + directory: Path, url: str, ff_only: bool = True +) -> subprocess.CompletedProcess: + """Pull updates for directory from url""" + print(f"--- pull: {directory} ---") + if not directory.exists(): + return subprocess.run("exit 1".split()) # TODO + git_set_url(directory, url) + return subprocess.run("git pull --ff-only".split(), cwd=directory) + + +def main(*args, **kwargs) -> None: + for directory, url in SYNC_MAPPING.items(): + print(git_sync(directory, url)) + for directory, url in PULL_MAPPINGS.items(): + print(git_pull(directory, url)) + + +if __name__ == "__main__": + main()