Compare commits
3 Commits
753e79d63f
...
1cd8113839
Author | SHA1 | Date | |
---|---|---|---|
1cd8113839 | |||
a9aedfe190 | |||
f35df0e059 |
@ -7,6 +7,15 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import json
|
import json
|
||||||
import time
|
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):
|
def is_file_recent(filepath, minutes=10):
|
||||||
if not os.path.isfile(filepath):
|
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):
|
def perform_action(action, key, item, silent = False):
|
||||||
output = None
|
output = None
|
||||||
lbl = "{0}ing".format(action).title()
|
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" ]):
|
if action in list([ "pull", "push", "fetch" ]):
|
||||||
push = True
|
push = True
|
||||||
if "push" in item.keys():
|
if "push" in item.keys():
|
||||||
push = item["push"]
|
push = item["push"]
|
||||||
if push or action in list([ "pull", "fetch" ]):
|
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:
|
if not silent:
|
||||||
print("{0} {1}...".format(lbl, key))
|
print("{0} {1}...".format(lbl, key))
|
||||||
output = execute_command(cmd)
|
output = execute_command(cmd)
|
||||||
|
rm(filepath("status"))
|
||||||
elif action == "sync":
|
elif action == "sync":
|
||||||
if not silent:
|
if not silent:
|
||||||
print("{0} {1}...".format(lbl, key))
|
print("{0} {1}...".format(lbl, key))
|
||||||
perform_action("pull", key, item, silent=True)
|
perform_action("pull", key, item, silent=True)
|
||||||
perform_action("push", key, item, silent=True)
|
perform_action("push", key, item, silent=True)
|
||||||
|
rm(filepath("status"))
|
||||||
elif action == "lcs":
|
elif action == "lcs":
|
||||||
if not silent:
|
if not silent:
|
||||||
print("{0} {1}...".format(lbl, key))
|
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("lc", key, item, silent=True)
|
||||||
perform_action("pull", key, item, silent=True)
|
perform_action("pull", key, item, silent=True)
|
||||||
perform_action("push", key, item, silent=True)
|
perform_action("push", key, item, silent=True)
|
||||||
|
rm(filepath("status"))
|
||||||
elif action == "lc":
|
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")
|
output = execute_command(cmd).split("\n")
|
||||||
if len(output[0]) > 0:
|
if len(output[0]) > 0:
|
||||||
print("Lazy committing {0}...".format(key))
|
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)
|
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)
|
output = execute_command(cmd)
|
||||||
|
rm(filepath("status"))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -90,14 +103,15 @@ if __name__ == "__main__":
|
|||||||
print(f"Unable to parse {yaml_file_path}.", file=sys.stderr)
|
print(f"Unable to parse {yaml_file_path}.", file=sys.stderr)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
r = list(cfg.keys())
|
r = list(cfg.keys())
|
||||||
# Let's quickly sweep through and expand any tildes ("~") in the path references...
|
config_file = filepath("config")
|
||||||
for k in r:
|
if not is_file_recent(config_file, minutes = 60 * 60):
|
||||||
path = cfg[k]["path"]
|
# Let's quickly sweep through and expand any tildes ("~") in the path references...
|
||||||
path = os.path.expanduser(path)
|
for k in r:
|
||||||
cmd = f"git config --global --add safe.directory {path}"
|
path = os.path.expanduser(cfg[k]["path"])
|
||||||
# I am not outputting any errors here because of a dumb bug in WSL.
|
cmd = f"git config --global --add safe.directory {path}"
|
||||||
output = execute_command(cmd, False)
|
# I am not outputting any errors here because of a dumb bug in WSL.
|
||||||
cfg[k]["path"] = path
|
output = execute_command(cmd, False)
|
||||||
|
Path(config_file).touch()
|
||||||
if len(sys.argv) == 3:
|
if len(sys.argv) == 3:
|
||||||
if not sys.argv[2] in cfg.keys():
|
if not sys.argv[2] in cfg.keys():
|
||||||
print("{0} is not one of your repositories.".format(sys.argv[2]))
|
print("{0} is not one of your repositories.".format(sys.argv[2]))
|
||||||
@ -108,19 +122,18 @@ if __name__ == "__main__":
|
|||||||
print(k)
|
print(k)
|
||||||
elif sys.argv[1] == "status":
|
elif sys.argv[1] == "status":
|
||||||
dat = dict()
|
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):
|
if is_file_recent(cache_file):
|
||||||
print("From cache...")
|
|
||||||
with open(cache_file, "rt") as fin:
|
with open(cache_file, "rt") as fin:
|
||||||
dat = json.load(fin)
|
dat = json.load(fin)
|
||||||
else:
|
else:
|
||||||
print("Updating cache...")
|
|
||||||
for k in r:
|
for k in r:
|
||||||
if os.path.exists(cfg[k]["path"]):
|
path = os.path.expanduser( cfg[k]["path"] )
|
||||||
cmd = "git -C \"{0}\" status --porcelain".format(cfg[k]["path"])
|
if os.path.exists( path ):
|
||||||
|
cmd = "git -C \"{0}\" status --porcelain".format( path )
|
||||||
output = execute_command(cmd).split("\n")
|
output = execute_command(cmd).split("\n")
|
||||||
dat[k] = 0 if len(output[0]) == 0 else len(output)
|
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)
|
json.dump(dat, fout, indent = 2)
|
||||||
for k in dat:
|
for k in dat:
|
||||||
status = "-" if dat[k] == 0 else dat[k]
|
status = "-" if dat[k] == 0 else dat[k]
|
||||||
@ -143,9 +156,9 @@ if __name__ == "__main__":
|
|||||||
hosttest = True
|
hosttest = True
|
||||||
if "hosts" in cfg[k].keys():
|
if "hosts" in cfg[k].keys():
|
||||||
hosttest = hn in cfg[k]["hosts"]
|
hosttest = hn in cfg[k]["hosts"]
|
||||||
if hosttest and not os.path.exists(cfg[k]["path"]):
|
if hosttest and not os.path.exists(os.path.expanduser(cfg[k]["path"])):
|
||||||
print("Cloning {0} into {1}...".format(k, 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"], cfg[k]["path"])
|
cmd = "git clone \"{0}\" \"{1}\"".format(cfg[k]["url"], os.path.expanduser(cfg[k]["path"]))
|
||||||
output = execute_command(cmd)
|
output = execute_command(cmd)
|
||||||
elif sys.argv[1] == "align":
|
elif sys.argv[1] == "align":
|
||||||
hn = hostname()
|
hn = hostname()
|
||||||
@ -154,9 +167,9 @@ if __name__ == "__main__":
|
|||||||
hosttest = True
|
hosttest = True
|
||||||
if "hosts" in cfg[k].keys():
|
if "hosts" in cfg[k].keys():
|
||||||
hosttest = hn in cfg[k]["hosts"]
|
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))
|
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)
|
output = execute_command(cmd)
|
||||||
url = None
|
url = None
|
||||||
for line in output.split("\n"):
|
for line in output.split("\n"):
|
||||||
@ -170,7 +183,7 @@ if __name__ == "__main__":
|
|||||||
print(" 🟢 {0}".format(url))
|
print(" 🟢 {0}".format(url))
|
||||||
else:
|
else:
|
||||||
print(" 🔴 {0}".format(url))
|
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)
|
output = execute_command(cmd)
|
||||||
print(" 🟢 {0}".format(cfg[k]["url"]))
|
print(" 🟢 {0}".format(cfg[k]["url"]))
|
||||||
# else:
|
# else:
|
||||||
@ -181,9 +194,9 @@ if __name__ == "__main__":
|
|||||||
hosttest = True
|
hosttest = True
|
||||||
if "hosts" in cfg[k].keys():
|
if "hosts" in cfg[k].keys():
|
||||||
hosttest = hn in cfg[k]["hosts"]
|
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("lc", k, cfg[k])
|
||||||
perform_action("sync", k, cfg[k])
|
perform_action("sync", k, cfg[k])
|
||||||
print("Pruning {0}".format(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