interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

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 <id> every field of one record (the delete-token hash is omitted)
6 allen-contacts delete <id> remove one record (atomic rewrite; audit.log gets a count)
7
8Store: /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
11at another directory and skips the re-exec.
12Text from visitors is printed with every non-printable character escaped.
13"""
14import os
15import pwd
16import re
17import sys
18
19SELF = os.path.realpath(__file__)
20STORE_USER = "allen-contacts"
21
22if "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
27for _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
31import allen_contacts_ingest as aci # noqa: E402
32
33SHOW_ORDER = ("id", "received", "via", "kind", "name", "org", "contact", "github", "origin",
34 "purpose", "homepage", "framework", "model", "human")
35ID_RE = re.compile(r"c_[0-9a-f]{16}")
36
37
38def 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
44def usage():
45 sys.stderr.write("usage: allen-contacts list | show <id> | delete <id>\n")
46 return 64
47
48
49def 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
90if __name__ == "__main__":
91 sys.exit(main(sys.argv[1:]))