Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/scripts/allen/contacts/allen-contacts # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / scripts / allen / contacts / allen-contacts` 91 lines ยท 3370 bytes Raw Blame History 1 #!/usr/bin/python3 -I 2 """allen-contacts: read or delete records in Drake's private Allen contact store. 3 4 allen-contacts list one line per record: id, received, kind, name, org 5 allen-contacts show every field of one record (the delete-token hash is omitted) 6 allen-contacts delete remove one record (atomic rewrite; audit.log gets a count) 7 8 Store: /var/lib/allen-contacts/contacts.jsonl, owned by the system user allen-contacts 9 (0700). Run by anyone else, the command re-executes itself through 10 `sudo -u allen-contacts` (clean environment). ALLEN_CONTACTS_STORE (tests only) points it 11 at another directory and skips the re-exec. 12 Text from visitors is printed with every non-printable character escaped. 13 """ 14 import os 15 import pwd 16 import re 17 import sys 18 19 SELF = os.path.realpath(__file__) 20 STORE_USER = "allen-contacts" 21 22 if "ALLEN_CONTACTS_STORE" not in os.environ and os.getuid() != pwd.getpwnam(STORE_USER).pw_uid: 23 os.chdir("/") 24 os.execvp("sudo", ["sudo", "-u", STORE_USER, "--", "/usr/bin/python3", "-I", "-B", SELF] 25 + sys.argv[1:]) 26 27 for _d in (os.path.dirname(SELF), "/opt/allen-contacts"): 28 if os.path.isfile(os.path.join(_d, "allen_contacts_ingest.py")): 29 sys.path.insert(0, _d) 30 break 31 import allen_contacts_ingest as aci # noqa: E402 32 33 SHOW_ORDER = ("id", "received", "via", "kind", "name", "org", "contact", "github", "origin", 34 "purpose", "homepage", "framework", "model", "human") 35 ID_RE = re.compile(r"c_[0-9a-f]{16}") 36 37 38 def clean(v): 39 s = "" if v is None else str(v) 40 return "".join(c if c.isprintable() else "\\u%04x" % ord(c) if ord(c) > 0xff 41 else {"\n": "\\n", "\t": "\\t"}.get(c, "\\x%02x" % ord(c)) for c in s) 42 43 44 def usage(): 45 sys.stderr.write("usage: allen-contacts list | show | delete \n") 46 return 64 47 48 49 def main(argv): 50 if not argv: 51 return usage() 52 cmd, rest = argv[0], argv[1:] 53 store = aci.Store() 54 if cmd == "list" and not rest: 55 entries = store.lines() # readers need no lock: rewrites are atomic renames 56 bad = 0 57 for _, r in entries: 58 if r is None: 59 bad += 1 60 continue 61 print("\t".join(clean(r.get(k, "")) for k in ("id", "received", "kind", "name", "org"))) 62 if bad: 63 sys.stderr.write("allen-contacts: %d unreadable line(s) kept in the store\n" % bad) 64 return 0 65 if cmd in ("show", "delete") and len(rest) == 1: 66 cid = rest[0] 67 if not ID_RE.fullmatch(cid): 68 sys.stderr.write("allen-contacts: id must look like c_<16 hex>\n") 69 return 64 70 if cmd == "show": 71 _, r = store.find(cid) 72 if r is None: 73 sys.stderr.write("allen-contacts: no record %s\n" % cid) 74 return 1 75 for k in SHOW_ORDER: 76 print("%s: %s" % (k, clean(r.get(k, "")))) 77 return 0 78 with store: 79 removed, remaining = store.remove("id", cid) 80 if removed: 81 store.audit("cli-delete", removed) 82 if removed: 83 print("deleted %s (%d record(s) left)" % (cid, remaining)) 84 return 0 85 sys.stderr.write("allen-contacts: no record %s\n" % cid) 86 return 1 87 return usage() 88 89 90 if __name__ == "__main__": 91 sys.exit(main(sys.argv[1:]))