#!/usr/bin/env nix-shell #! nix-shell -i python3 --pure #! nix-shell -p python3 import os import json import subprocess import re from datetime import datetime import time tmp_path = '/tmp/waybar-scripts/updates' json_cache = tmp_path + "/data.json" git_repo = 'git@github.com:mjallen18/nix-config.git' def cache_json(data): with open(json_cache, mode="w") as cache: json.dump(data, cache) def create_folders(): if not os.path.exists(tmp_path): os.makedirs(tmp_path) os.chdir(tmp_path) def clone_repo(): reset_repo() if os.path.exists(tmp_path) and len(os.listdir(tmp_path)) == 0: subprocess.run(["git", "clone", git_repo, tmp_path]) def check_for_flake_updates(): return subprocess.run(["sudo", "nix", "flake", "update", "-I", tmp_path, tmp_path], capture_output=True, text=True) def reset_repo(): if os.path.exists(tmp_path) and not len(os.listdir(tmp_path)) == 0: subprocess.run(["git", "reset", "--hard"]) subprocess.run(["git", "pull"]) def parse_output(output): updates_dict = {} updates = re.findall(r"(• Updated input '(.+)':((\n|\r|\n)(.+\((\d\d\d\d-\d\d-\d\d\)))){2})", output) for update in updates: input = re.findall(r"'(.+)':", update[0])[0].strip() dates = re.findall(r"(\d{4}-\d{2}-\d{2})", update[0]) updates_dict[input] = dates return updates_dict def check(): data = {} # print("create folders") create_folders() # print("clone repo") clone_repo() # print("checking for updates") updates = check_for_flake_updates() updates_dict = parse_output(updates.stderr) if len(updates_dict.keys()) > 0: data["text"] = ("󰏕") else: data["text"] = (u"\u00A0") if len(updates_dict.keys()) <= 0: data["tooltip"] = ("flake inputs up to date") else: data["tooltip"] = ("flake input updates:\n\n") for input in updates_dict.keys(): data["tooltip"] += "{}\n".format(input) data["tooltip"] += "Old Ref: {0} → {1}\n\n".format(updates_dict[input][0].strip(), updates_dict[input][1].strip()) cache_json(data) print(json.dumps(data)) def read_cache(): with open(json_cache, mode="r") as cache: print(json.dumps(json.load(cache))) def main(): now = int(datetime.now().strftime("%H")) if os.path.exists(json_cache): mtime = int(time.localtime(os.stat(json_cache).st_ctime).tm_hour) else: mtime = now + 1 if (now % 2 == 0 and mtime < now) or not os.path.exists(json_cache): check() else: read_cache() main()