Repository Public
1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/recorders/_parity_support.py4# Description: Helpers shared by the "parity" recorder scenarios (databases, search, graph2, inbox, research, aiconv,5# agents, misc): multipart bodies, clean-up with a fresh session, and a recorder that hides volatile ids.6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9#10# Environment (besides the one of spn_recorder.py): optional SPN_LIVE_FRESH_CMD — a shell command printing a NEW dev11# session token on its last line (a purge needs a passkey verification younger than 5 minutes), e.g.12# SPN_LIVE_FRESH_CMD="ssh M4M64c 'cd ~/dev/note-next && bash scripts/next-dev-server.sh token'".13import os14import subprocess15import sys16import uuid1718sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))19from spn_recorder import Recorder, new_id # noqa: E402,F401202122def multipart(fields, file_field=None, filename=None, content=b"", mime="text/plain"):23 """Builds a multipart/form-data body. Returns (bytes, content type)."""24 boundary = "spnrec-" + uuid.uuid4().hex25 parts = []26 for name, value in fields.items():27 parts.append(("--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n" % (boundary, name, value)).encode("utf-8"))28 if file_field:29 head = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n" % (boundary, file_field, filename, mime)30 parts.append(head.encode("utf-8") + content + b"\r\n")31 parts.append(("--%s--\r\n" % boundary).encode("utf-8"))32 return b"".join(parts), "multipart/form-data; boundary=" + boundary333435class ParityRecorder(Recorder):36 """The recorder of spn_recorder.py plus: multipart calls, and a clean-up that purges what the scenario created."""3738 def __init__(self, group):39 super().__init__(group)40 self.pages = []4142 def page(self, title, **extra):43 """Creates a page (titles are prefixed `Fixture · `) and remembers it for the clean-up."""44 body = {"id": new_id(), "title": "Fixture · " + title}45 body.update(extra)46 reply = self.call("POST", "/api/pages", body)47 if reply.status not in (200, 201):48 sys.exit("cannot create a page: %s %s" % (reply.status, reply.body[:200]))49 self.pages.append(body["id"])50 return reply.json5152 def track(self, page_id):53 self.pages.append(page_id)54 return page_id5556 def form(self, method, path, fields, file_field=None, filename=None, content=b"", mime="text/plain", save=None):57 raw, content_type = multipart(fields, file_field, filename, content, mime)58 return self.call(method, path, raw=raw, content_type=content_type, save=save)5960 def _save(self, name, method, path, body, reply):61 # A route that does not exist answers with the HTML 404 page of Next.js: keep a one-line marker, not the page.62 if "text/html" in reply.headers.get("content-type", ""):63 reply.body = ("<!-- HTML page of %d bytes (Next.js, HTTP %d): no JSON answer, the route does not exist on this server -->\n"64 % (len(reply.body), reply.status)).encode("utf-8")65 reply.headers = {k: v for k, v in reply.headers.items() if k != "etag"}66 super()._save(name, method, path, body, reply)6768 def keep(self, name):69 """Keeps the fixture `name` of the previous recording (used when the state it needs cannot be reproduced now)."""70 import json71 try:72 with open(os.path.join(self.dir, "_index.json"), encoding="utf-8") as handle:73 old = [e for e in json.load(handle) if e["name"] == name]74 except (OSError, ValueError):75 old = []76 if old and os.path.exists(os.path.join(self.dir, old[0]["file"])):77 self.index.append(old[0])78 return True79 print("%s: fixture %s is missing and cannot be recorded in this state" % (self.group, name))80 return False8182 def fresh(self):83 """Switches to a new (fresh) session token when SPN_LIVE_FRESH_CMD is set."""84 return self._fresh()8586 def _fresh(self):87 command = os.environ.get("SPN_LIVE_FRESH_CMD")88 if not command:89 return False90 out = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=60).stdout.strip().splitlines()91 if not out or not out[-1].strip():92 return False93 self.token = out[-1].strip()94 return True9596 def cleanup(self):97 """Trashes then purges every remembered page (newest first). A purge needs a fresh session: a new token is98 minted with SPN_LIVE_FRESH_CMD when the server answers `reauth_required`; without it the pages stay in the trash."""99 left = 0100 refreshed = False101 for page_id in reversed(self.pages):102 self.call("DELETE", "/api/pages/" + page_id)103 reply = self.call("DELETE", "/api/pages/" + page_id + "?purge=1")104 if reply.status == 403 and not refreshed:105 refreshed = True106 if self._fresh():107 reply = self.call("DELETE", "/api/pages/" + page_id + "?purge=1")108 if reply.status not in (200, 404):109 left += 1110 self.pages = []111 if left:112 print("%s: %d page(s) left in the trash (no fresh session: set SPN_LIVE_FRESH_CMD)" % (self.group, left))113114 def finish(self):115 self.cleanup()116 super().finish()117