Lazy commit on <function hostname at 0x71c961d10180>.

This commit is contained in:
James Downie 2025-07-14 20:52:22 +10:00
parent 68e4a97d9b
commit 7b2ec93611

View File

@ -92,10 +92,13 @@ def parse_yaml(file_path):
print(f"Error parsing YAML file: {e}")
return None
def execute_command(command, dump_error = True):
def execute_command(command, cwd = None, dump_error = True):
command = f"/bin/bash -c '{command}'"
try:
result = subprocess.run(command, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if cwd != None:
result = subprocess.run(command, cwd=cwd, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
result = subprocess.run(command, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
return result.stdout.strip()
else:
@ -108,6 +111,12 @@ def execute_command(command, dump_error = True):
print(command)
return None
def isFossil(cfg):
ret = False
if "vcs" in cfg.keys():
ret = ( cfg["vcs"] == "fossil" )
return ret
def perform_action(action, key, item, silent = False):
output = None
lbl = "{0}ing".format(action).title()
@ -117,10 +126,17 @@ def perform_action(action, key, item, silent = False):
if "push" in item.keys():
push = item["push"]
if push or action in list([ "pull", "fetch" ]):
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)
if isFossil(item):
cwd = Path(os.path.expanduser(item["path"]))
cmd = f"fossil {action}"
if not silent:
print("{0} {1}...".format(lbl, key))
output = execute_command(cmd, cwd=cwd)
else:
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:
@ -137,14 +153,21 @@ def perform_action(action, key, item, silent = False):
perform_action("push", key, item, silent=True)
rm(filepath("status"))
elif action == "lc":
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(os.path.expanduser(item["path"]))
output = execute_command(cmd)
cmd = "git -C \"{0}\" commit -m \"Lazy commit on {1}.\"".format(os.path.expanduser(item["path"]), hostname)
output = execute_command(cmd)
if isFossil(item):
cwd = Path(os.path.expanduser(item["path"]))
cmd = f"fossil add ."
output = execute_command(cmd, cwd=cwd)
cmd = f"fossil commit -m \"Lazy commit on {hostname}\""
output = execute_command(cmd, cwd=cwd)
else:
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(os.path.expanduser(item["path"]))
output = execute_command(cmd)
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
@ -183,31 +206,39 @@ if __name__ == "__main__":
for k in r:
path = os.path.expanduser( cfg[k]["path"] )
if os.path.exists( path ):
cmd = "git -C \"{0}\" status --porcelain=2 --branch".format( path )
props = dict()
props["key"] = k
lines = execute_command(cmd).split("\n")
n = 0
for line in lines:
m = re.match(r"# branch\.(\S+)\s+(.+)$", line)
if m:
prop = m.group(1)
rest = m.group(2)
if prop == "ab":
m2 = re.match(r"[+](\d+)\s[-](\d+)", rest)
p2 = rest
if m2:
p2 = dict()
p2["push"] = int(m2.group(1))
p2["pull"] = int(m2.group(2))
if isFossil(cfg[k]):
cwd = os.path.expanduser(cfg[k]["path"])
cmd = "fossil json status"
output = json.loads(execute_command(cmd))
n = len(output["payload"]["files"])
cmd = "fossil extras"
n = n + len(execute_command(cmd).split("\n"))
else:
cmd = "git -C \"{0}\" status --porcelain=2 --branch".format( path )
props = dict()
props["key"] = k
lines = execute_command(cmd).split("\n")
for line in lines:
m = re.match(r"# branch\.(\S+)\s+(.+)$", line)
if m:
prop = m.group(1)
rest = m.group(2)
if prop == "ab":
m2 = re.match(r"[+](\d+)\s[-](\d+)", rest)
p2 = rest
if m2:
p2 = dict()
p2["push"] = int(m2.group(1))
p2["pull"] = int(m2.group(2))
props[prop] = p2
props[prop] = p2
props[prop] = p2
elif prop == "oid":
props["commit"] = rest
elif prop == "oid":
props["commit"] = rest
else:
props[prop] = rest
else:
props[prop] = rest
else:
n = n + 1
n = n + 1
props["n"] = n
dat[k] = props
with open(cache_file, "wt") as fout:
@ -245,8 +276,15 @@ if __name__ == "__main__":
hosttest = hn in cfg[k]["hosts"]
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)
if isFossil(cfg[k]):
cwd = Path(os.path.expanduser(cfg[k]["path"])).parent
cmd = "fossil clone --save-http-password \"{0}\"".format(cfg[k]["url"])
output = execute_command(cmd, cwd=cwd)
with open(os.path.expanduser(f"{cfg[k]['path']}.password"), "wt") as fout:
fout.write(output)
else:
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()
p = re.compile("^.*Fetch URL: (.*)$")