Compare commits
3 Commits
753e79d63f
...
1cd8113839
Author | SHA1 | Date | |
---|---|---|---|
1cd8113839 | |||
a9aedfe190 | |||
f35df0e059 |
@ -7,6 +7,15 @@ import re
|
||||
import shutil
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
def rm(p):
|
||||
if os.path.exists(p):
|
||||
os.remove(p)
|
||||
|
||||
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):
|
||||
@ -46,21 +55,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)
|
||||
rm(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)
|
||||
rm(filepath("status"))
|
||||
elif action == "lcs":
|
||||
if not silent:
|
||||
print("{0} {1}...".format(lbl, key))
|
||||
@ -68,15 +79,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)
|
||||
rm(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)
|
||||
rm(filepath("status"))
|
||||
return output
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -90,14 +103,15 @@ if __name__ == "__main__":
|
||||
print(f"Unable to parse {yaml_file_path}.", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
r = list(cfg.keys())
|
||||
# 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)
|
||||
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
|
||||
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 = 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)
|
||||
Path(config_file).touch()
|
||||
if len(sys.argv) == 3:
|
||||
if not sys.argv[2] in cfg.keys():
|
||||
print("{0} is not one of your repositories.".format(sys.argv[2]))
|
||||
@ -108,19 +122,18 @@ 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):
|
||||
print("From cache...")
|
||||
with open(cache_file, "rt") as fin:
|
||||
dat = json.load(fin)
|
||||
else:
|
||||
print("Updating cache...")
|
||||
for k in r:
|
||||
if os.path.exists(cfg[k]["path"]):
|
||||
cmd = "git -C \"{0}\" status --porcelain".format(cfg[k]["path"])
|
||||
path = os.path.expanduser( cfg[k]["path"] )
|
||||
if os.path.exists( path ):
|
||||
cmd = "git -C \"{0}\" status --porcelain".format( path )
|
||||
output = execute_command(cmd).split("\n")
|
||||
dat[k] = 0 if len(output[0]) == 0 else len(output)
|
||||
with open(os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.status"), "wt") as fout:
|
||||
with open(cache_file, "wt") as fout:
|
||||
json.dump(dat, fout, indent = 2)
|
||||
for k in dat:
|
||||
status = "-" if dat[k] == 0 else dat[k]
|
||||
@ -143,9 +156,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()
|
||||
@ -154,9 +167,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"):
|
||||
@ -170,7 +183,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:
|
||||
@ -181,9 +194,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"]))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user