Made some improvements to repo performance.

This commit is contained in:
James Downie 2025-06-29 20:11:36 +10:00
parent 753e79d63f
commit f35df0e059

View File

@ -7,6 +7,7 @@ import re
import shutil import shutil
import json import json
import time import time
from pathlib import Path
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):
@ -90,14 +91,17 @@ 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 = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.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 = cfg[k]["path"]
# I am not outputting any errors here because of a dumb bug in WSL. path = os.path.expanduser(path)
output = execute_command(cmd, False) cmd = f"git config --global --add safe.directory {path}"
cfg[k]["path"] = 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 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]))
@ -110,17 +114,16 @@ if __name__ == "__main__":
dat = dict() dat = dict()
cache_file = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.status") cache_file = os.path.join(os.environ.get("XDG_CACHE_HOME"), "repo.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]