diff --git a/jdownie/repo/repo b/jdownie/repo/repo index 4630454..51a2f08 100755 --- a/jdownie/repo/repo +++ b/jdownie/repo/repo @@ -9,6 +9,10 @@ import json import time from pathlib import Path +def filepath(f): + ret = os.path.join(os.environ.get("XDG_CACHE_HOME"), f"repo.{f}") + return ret + def is_file_recent(filepath, minutes=10): if not os.path.isfile(filepath): return False @@ -47,21 +51,23 @@ def execute_command(command, dump_error = True): def perform_action(action, key, item, silent = False): output = None lbl = "{0}ing".format(action).title() - if os.path.exists(item["path"]): + if os.path.exists(os.path.expanduser(item["path"])): if action in list([ "pull", "push", "fetch" ]): push = True if "push" in item.keys(): push = item["push"] if push or action in list([ "pull", "fetch" ]): - cmd = "git -C \"{0}\" {1}".format(item["path"], action) + cmd = "git -C \"{0}\" {1}".format(os.path.expanduser(item["path"]), action) if not silent: print("{0} {1}...".format(lbl, key)) output = execute_command(cmd) + os.remove(filepath("status")) elif action == "sync": if not silent: print("{0} {1}...".format(lbl, key)) perform_action("pull", key, item, silent=True) perform_action("push", key, item, silent=True) + os.remove(filepath("status")) elif action == "lcs": if not silent: print("{0} {1}...".format(lbl, key)) @@ -69,15 +75,17 @@ def perform_action(action, key, item, silent = False): perform_action("lc", key, item, silent=True) perform_action("pull", key, item, silent=True) perform_action("push", key, item, silent=True) + os.remove(filepath("status")) elif action == "lc": - cmd = "git -C \"{0}\" status --porcelain".format(item["path"]) + cmd = "git -C \"{0}\" status --porcelain".format(os.path.expanduser(item["path"])) output = execute_command(cmd).split("\n") if len(output[0]) > 0: print("Lazy committing {0}...".format(key)) - cmd = "git -C \"{0}\" add .".format(item["path"]) + cmd = "git -C \"{0}\" add .".format(os.path.expanduser(item["path"])) output = execute_command(cmd) - cmd = "git -C \"{0}\" commit -m \"Lazy commit on {1}.\"".format(item["path"], hostname) + cmd = "git -C \"{0}\" commit -m \"Lazy commit on {1}.\"".format(os.path.expanduser(item["path"]), hostname) output = execute_command(cmd) + os.remove(filepath("status")) return output if __name__ == "__main__": @@ -91,16 +99,14 @@ if __name__ == "__main__": print(f"Unable to parse {yaml_file_path}.", file=sys.stderr) sys.exit(2) r = list(cfg.keys()) - config_file = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.config") + config_file = filepath("config") if not is_file_recent(config_file, minutes = 60 * 60): # Let's quickly sweep through and expand any tildes ("~") in the path references... for k in r: - path = cfg[k]["path"] - path = os.path.expanduser(path) + path = os.path.expanduser(cfg[k]["path"]) cmd = f"git config --global --add safe.directory {path}" # I am not outputting any errors here because of a dumb bug in WSL. output = execute_command(cmd, False) - cfg[k]["path"] = path Path(config_file).touch() if len(sys.argv) == 3: if not sys.argv[2] in cfg.keys(): @@ -112,7 +118,7 @@ if __name__ == "__main__": print(k) elif sys.argv[1] == "status": dat = dict() - cache_file = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.status") + cache_file = filepath("status") if is_file_recent(cache_file): with open(cache_file, "rt") as fin: dat = json.load(fin) @@ -146,9 +152,9 @@ if __name__ == "__main__": hosttest = True if "hosts" in cfg[k].keys(): hosttest = hn in cfg[k]["hosts"] - if hosttest and not os.path.exists(cfg[k]["path"]): - print("Cloning {0} into {1}...".format(k, cfg[k]["path"])) - cmd = "git clone \"{0}\" \"{1}\"".format(cfg[k]["url"], cfg[k]["path"]) + if hosttest and not os.path.exists(os.path.expanduser(cfg[k]["path"])): + print("Cloning {0} into {1}...".format(k, os.path.expanduser(cfg[k]["path"]))) + cmd = "git clone \"{0}\" \"{1}\"".format(cfg[k]["url"], os.path.expanduser(cfg[k]["path"])) output = execute_command(cmd) elif sys.argv[1] == "align": hn = hostname() @@ -157,9 +163,9 @@ if __name__ == "__main__": hosttest = True if "hosts" in cfg[k].keys(): hosttest = hn in cfg[k]["hosts"] - if hosttest and os.path.exists(cfg[k]["path"]): + if hosttest and os.path.exists(os.path.expanduser(cfg[k]["path"])): print("Aligning {0}...".format(k)) - cmd = "git -C \"{0}\" remote show -n origin".format(cfg[k]["path"]) + cmd = "git -C \"{0}\" remote show -n origin".format(os.path.expanduser(cfg[k]["path"])) output = execute_command(cmd) url = None for line in output.split("\n"): @@ -173,7 +179,7 @@ if __name__ == "__main__": print(" 🟢 {0}".format(url)) else: print(" 🔴 {0}".format(url)) - cmd = "git -C \"{0}\" remote set-url origin \"{1}\"".format(cfg[k]["path"], cfg[k]["url"]) + cmd = "git -C \"{0}\" remote set-url origin \"{1}\"".format(os.path.expanduser(cfg[k]["path"]), cfg[k]["url"]) output = execute_command(cmd) print(" 🟢 {0}".format(cfg[k]["url"])) # else: @@ -184,9 +190,9 @@ if __name__ == "__main__": hosttest = True if "hosts" in cfg[k].keys(): hosttest = hn in cfg[k]["hosts"] - if not hosttest and os.path.exists(cfg[k]["path"]): + if not hosttest and os.path.exists(os.path.expanduser(cfg[k]["path"])): perform_action("lc", k, cfg[k]) perform_action("sync", k, cfg[k]) print("Pruning {0}".format(k)) - shutil.rmtree(cfg[k]["path"]) + shutil.rmtree(os.path.expanduser(cfg[k]["path"]))