mirror of
https://github.com/lgcosta/pf-linux-port.git
synced 2026-11-12 21:38:03 -03:00
Docs: bring the documentation in line with the code
- Demo docs replaced by the screencast docs (single source of truth): the legacy demo-session.sh/demo-record.py and the v0.9 renderer were removed instead of being kept as a second convention. - feature-matrix: stale 'M2-in-progress' vocabulary gone; the rows the 15 gates prove now say VERIFIED-in-VM and name the gate; af-to is now an explicit non-goal row (INET6 0 in this build). - guide: the queues/divert/pflog sections described a state that no longer exists (priq-only, divert dropping packets, pflog logging nothing); they now describe the shipped behaviour. - vm-lab: the 15 real gates with what each proves, 15/15 green, and the pf0 -> cli/srv/gw2 ssh setup the screencasts rely on. - install/build: pdivert and pflogd are built and ported (docs said otherwise); module object list now includes hfsc.c, fq_codel.c, pfk_pflog.c; the screencast pipeline is an optional build stage. - pf-guide: the 'M2 scope only' framing is gone; honest gaps listed. - caveats: three new sections about the screencast pipeline (MiniMax video generation blocked by the account tier, narration as a separate track, a video frame is not a verdict). - porting-notes: what the recording taught us. - README/README.pt-BR: status, milestones, layout and video section updated. All internal doc links verified.
This commit is contained in:
1 parent
cc67531602
commit
dff2185b20
37 files changed
+677
-1616
No files matched your search
@@ -1,3 +0,0 @@
|
||||
@run clear
|
||||
uname -r
|
||||
lsmod | grep '^pf ' || echo 'pf: not loaded yet'
|
||||
@@ -1,65 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Render every screencast phase into out/screencast/*.mp4 (silent, 1080p30).
|
||||
#
|
||||
# ./scripts/screencast/record-all.sh [phase ...]
|
||||
#
|
||||
# Each phase is a real session against the lab; the videos carry no audio.
|
||||
set -u
|
||||
cd "$(dirname "$0")/../.."
|
||||
REPO=$(pwd)
|
||||
OUT="$REPO/out/screencast"
|
||||
mkdir -p "$OUT"
|
||||
|
||||
render() { # <file> <title> <subtitle> <phase-no> [panel]
|
||||
local f=$1 title=$2 sub=$3 no=$4
|
||||
local panel=${5:-"alpine (pf0) - lab libvirt - kernel 7.3.4 + pf.ko"}
|
||||
echo "=== gravando $f ==="
|
||||
python3 "$REPO/scripts/screencast/render.py" \
|
||||
--script "$REPO/scripts/screencast/phases/$f" \
|
||||
--out "$OUT/${f%.sh}.mp4" \
|
||||
--title "$title" --subtitle "$sub" \
|
||||
--phase-no "$no" --phase-total 6 --panel "$panel" --min-hold 1.1 \
|
||||
--footer "PF on Linux - OpenBSD PF 8.0 no kernel Linux - lab libvirt" || return 1
|
||||
}
|
||||
|
||||
declare -A TITLES=(
|
||||
[01-implementacao-kernel]="Fase 1 - Implementacao no kernel"
|
||||
[02-ativacao]="Fase 2 - Ativacao"
|
||||
[03-regras-basicas]="Fase 3 - Regras basicas"
|
||||
[04-roteamento]="Fase 4 - Roteamento"
|
||||
[05-balanceamento]="Fase 5 - Balanceamento"
|
||||
[06-qos]="Fase 6 - QOS"
|
||||
)
|
||||
declare -A SUBS=(
|
||||
[01-implementacao-kernel]="Das fontes OpenBSD 8.0 ao modulo pf.ko"
|
||||
[02-ativacao]="insmod pf.ko / pfctl -e / pfctl -s info"
|
||||
[03-regras-basicas]="tabelas, anchors, keep state, block drop"
|
||||
[04-roteamento]="route-to e reply-to: o proximo salto decidido pelo pf"
|
||||
[05-balanceamento]="pools round-robin, source-hash e dup-to"
|
||||
[06-qos]="filas hfsc do OpenBSD 8.0 no caminho de dados"
|
||||
)
|
||||
|
||||
PHASES=("$@")
|
||||
[ ${#PHASES[@]} -eq 0 ] && PHASES=(01-implementacao-kernel 02-ativacao 03-regras-basicas \
|
||||
04-roteamento 05-balanceamento 06-qos)
|
||||
|
||||
for p in "${PHASES[@]}"; do
|
||||
f="$p.sh"
|
||||
[ -f "$REPO/scripts/screencast/phases/$f" ] || { echo "fase inexistente: $f"; exit 1; }
|
||||
# the phase number comes from the phase's own name, not from its position
|
||||
# in the list, so a partial re-render keeps the right "FASE x/6" label
|
||||
n=$((10#${p%%-*}))
|
||||
case "$p" in
|
||||
01-*) panel="host de build - repositorio pf_port" ;;
|
||||
*) panel="alpine (pf0) - lab libvirt - kernel 7.3.4 + pf.ko" ;;
|
||||
esac
|
||||
render "$f" "${TITLES[$p]}" "${SUBS[$p]}" "$n" "$panel" || echo "FALHOU: $f"
|
||||
done
|
||||
|
||||
echo
|
||||
echo "=== videos gerados ==="
|
||||
for m in "$OUT"/*.mp4; do
|
||||
[ -e "$m" ] || continue
|
||||
d=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$m")
|
||||
printf '%-46s %6.1fs %5.1f MB\n' "$(basename "$m")" "$d" "$(echo "scale=1; $(stat -c%s "$m")/1000000" | bc)"
|
||||
done
|
||||
@@ -1,336 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Render a shell session into a silent, YouTube-ready MP4 screencast.
|
||||
|
||||
Pipeline: pty -> raw bytes -> pyte (terminal emulation) -> PNG frames -> MP4
|
||||
|
||||
Every byte on screen is the real output of the command; only the pacing and
|
||||
the framing are produced here. No audio track is written (the videos are
|
||||
silent by design).
|
||||
|
||||
Usage:
|
||||
./scripts/screencast/render.py --script scripts/screencast/phases/02-ativacao.sh \
|
||||
--out out/screencast/02-ativacao.mp4 --title "Fase 2 - Ativacao" \
|
||||
--subtitle "insmod pf.ko / pfctl -e / pfctl -s info"
|
||||
"""
|
||||
import argparse
|
||||
import errno
|
||||
import fcntl
|
||||
import os
|
||||
import pty
|
||||
import select
|
||||
import shutil
|
||||
import signal
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import termios
|
||||
import time
|
||||
|
||||
import pyte
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
# ---------------------------------------------------------------- palette ---
|
||||
BG = (14, 17, 23)
|
||||
PANEL = (20, 24, 31)
|
||||
BORDER = (48, 54, 61)
|
||||
FG = (201, 209, 217)
|
||||
HEADER_BG = (24, 29, 38)
|
||||
HEADER_FG = (240, 246, 252)
|
||||
ACCENT = (88, 166, 255)
|
||||
MUTED = (110, 118, 129)
|
||||
TITLE_BG = (17, 21, 28)
|
||||
|
||||
# pyte colour names -> RGB (a calm, high-contrast terminal palette)
|
||||
NAMED = {
|
||||
"black": (48, 54, 61),
|
||||
"red": (255, 123, 114),
|
||||
"green": (63, 185, 80),
|
||||
"yellow": (210, 153, 34),
|
||||
"blue": (88, 166, 255),
|
||||
"magenta": (188, 140, 255),
|
||||
"cyan": (57, 197, 207),
|
||||
"white": (201, 209, 217),
|
||||
"brown": (168, 115, 66),
|
||||
"default": None,
|
||||
}
|
||||
|
||||
|
||||
def color_of(spec, default):
|
||||
if spec == "default":
|
||||
return default
|
||||
if spec in NAMED:
|
||||
return NAMED[spec] or default
|
||||
try:
|
||||
if len(spec) == 6:
|
||||
return tuple(int(spec[i:i + 2], 16) for i in (0, 2, 4))
|
||||
except ValueError:
|
||||
pass
|
||||
return default
|
||||
|
||||
|
||||
class Recorder:
|
||||
"""Runs a command on a pty and keeps a timestamped snapshot per output event."""
|
||||
|
||||
def __init__(self, cols, rows, env=None):
|
||||
self.cols, self.rows = cols, rows
|
||||
self.screen = pyte.Screen(cols, rows)
|
||||
self.screen.set_mode(pyte.modes.LNM)
|
||||
self.stream = pyte.ByteStream(self.screen)
|
||||
self.events = [] # (timestamp, rows_of_cells)
|
||||
self.t0 = time.time()
|
||||
|
||||
def snapshot(self):
|
||||
rows = []
|
||||
for y in range(self.rows):
|
||||
line = self.screen.buffer[y]
|
||||
cells = []
|
||||
for x in range(self.cols):
|
||||
ch = line[x]
|
||||
fg = color_of(ch.fg, FG)
|
||||
bold = ch.bold
|
||||
cells.append((ch.data or " ", fg, bold))
|
||||
rows.append(cells)
|
||||
return rows, (self.screen.cursor.y, self.screen.cursor.x)
|
||||
|
||||
def run(self, argv, env=None):
|
||||
master, slave = pty.openpty()
|
||||
fcntl.ioctl(slave, termios.TIOCSWINSZ,
|
||||
struct.pack("HHHH", self.rows, self.cols, 0, 0))
|
||||
e = dict(os.environ)
|
||||
e.update({"TERM": "xterm-256color", "COLUMNS": str(self.cols),
|
||||
"LINES": str(self.rows), "LC_ALL": "C.UTF-8"})
|
||||
if env:
|
||||
e.update(env)
|
||||
proc = subprocess.Popen(argv, stdin=slave, stdout=slave, stderr=slave,
|
||||
env=e, start_new_session=True, close_fds=True)
|
||||
os.close(slave)
|
||||
while True:
|
||||
try:
|
||||
r, _, _ = select.select([master], [], [], 0.25)
|
||||
except InterruptedError:
|
||||
continue
|
||||
if master in r:
|
||||
try:
|
||||
data = os.read(master, 65536)
|
||||
except OSError as exc:
|
||||
if exc.errno == errno.EIO:
|
||||
break
|
||||
raise
|
||||
if not data:
|
||||
break
|
||||
self.stream.feed(data)
|
||||
rows, cur = self.snapshot()
|
||||
self.events.append((time.time() - self.t0, rows, cur))
|
||||
elif proc.poll() is not None:
|
||||
# drain whatever is left
|
||||
try:
|
||||
while True:
|
||||
data = os.read(master, 65536)
|
||||
if not data:
|
||||
break
|
||||
self.stream.feed(data)
|
||||
rows, cur = self.snapshot()
|
||||
self.events.append((time.time() - self.t0, rows, cur))
|
||||
except OSError:
|
||||
pass
|
||||
break
|
||||
proc.wait()
|
||||
os.close(master)
|
||||
if not self.events:
|
||||
rows, cur = self.snapshot()
|
||||
self.events.append((time.time() - self.t0, rows, cur))
|
||||
return proc.returncode
|
||||
|
||||
|
||||
# ------------------------------------------------------------- rendering ---
|
||||
def load_font(path, size):
|
||||
return ImageFont.truetype(path, size)
|
||||
|
||||
|
||||
def render_frame(rows, title, subtitle, phase_no, phase_total, cols, font, bold_font,
|
||||
small_font, footer, cursor=None, panel="alpine (pf0)"):
|
||||
W, H = 1920, 1080
|
||||
img = Image.new("RGB", (W, H), BG)
|
||||
d = ImageDraw.Draw(img)
|
||||
|
||||
# header
|
||||
d.rectangle([0, 0, W, 74], fill=HEADER_BG)
|
||||
d.text((44, 26), "PF on Linux", font=bold_font, fill=HEADER_FG)
|
||||
tw = d.textlength("PF on Linux", font=bold_font)
|
||||
d.text((44 + tw + 26, 30), "-", font=font, fill=MUTED)
|
||||
d.text((44 + tw + 46, 30), "OpenBSD PF 8.0 engine inside the Linux kernel",
|
||||
font=font, fill=MUTED)
|
||||
if phase_total:
|
||||
label = f"FASE {phase_no}/{phase_total}"
|
||||
lw = d.textlength(label, font=small_font)
|
||||
d.text((W - 44 - lw, 32), label, font=small_font, fill=ACCENT)
|
||||
|
||||
# title block
|
||||
d.text((44, 112), title, font=bold_font, fill=HEADER_FG)
|
||||
if subtitle:
|
||||
d.text((44, 162), subtitle, font=font, fill=MUTED)
|
||||
|
||||
# terminal panel, sized to the content
|
||||
top = 212
|
||||
left = 44
|
||||
right = W - 44
|
||||
lh = 30
|
||||
used = 0
|
||||
for i, line in enumerate(rows):
|
||||
if any(c[0].strip() for c in line):
|
||||
used = i + 1
|
||||
bottom = min(H - 84, top + 44 + 28 + (max(used, 4) + 1) * lh)
|
||||
d.rectangle([left, top, right, bottom], fill=PANEL, outline=BORDER, width=2)
|
||||
d.rectangle([left, top, right, top + 44], fill=HEADER_BG)
|
||||
for i, c in enumerate([(255, 95, 86), (255, 189, 46), (39, 201, 63)]):
|
||||
d.ellipse([left + 24 + i * 26, top + 15, left + 38 + i * 26, top + 29], fill=c)
|
||||
d.text((left + 110, top + 13), panel, font=small_font, fill=MUTED)
|
||||
|
||||
# text
|
||||
x0 = left + 28
|
||||
y0 = top + 64
|
||||
cw = d.textlength("M", font=font)
|
||||
for i, line in enumerate(rows):
|
||||
y = y0 + i * lh
|
||||
if y > bottom - 10:
|
||||
break
|
||||
for x, (ch, fg, bold) in enumerate(line):
|
||||
if ch == " ":
|
||||
continue
|
||||
d.text((x0 + x * cw, y), ch, font=bold_font if bold else font, fill=fg)
|
||||
|
||||
# cursor block
|
||||
if cursor:
|
||||
cy, cx = cursor
|
||||
if 0 <= cy < len(rows) and 0 <= cx < cols:
|
||||
d.rectangle([x0 + cx * cw, y0 + cy * lh, x0 + (cx + 1) * cw - 1,
|
||||
y0 + (cy + 1) * lh - 1], fill=ACCENT)
|
||||
|
||||
# footer
|
||||
d.text((44, H - 58), footer, font=small_font, fill=MUTED)
|
||||
url = "git.labexposed.com/lgcosta/pf-linux-port"
|
||||
uw = d.textlength(url, font=small_font)
|
||||
d.text((W - 44 - uw, H - 58), url, font=small_font, fill=MUTED)
|
||||
return img
|
||||
|
||||
|
||||
def render_title(title, subtitle, phase_no, phase_total, bold_font, font, small_font):
|
||||
W, H = 1920, 1080
|
||||
img = Image.new("RGB", (W, H), TITLE_BG)
|
||||
d = ImageDraw.Draw(img)
|
||||
d.rectangle([0, 0, W, 8], fill=ACCENT)
|
||||
d.text((120, 400), title, font=bold_font, fill=HEADER_FG)
|
||||
if subtitle:
|
||||
d.text((120, 470), subtitle, font=font, fill=MUTED)
|
||||
if phase_total:
|
||||
d.text((120, 560), f"FASE {phase_no} DE {phase_total}", font=small_font, fill=ACCENT)
|
||||
d.text((120, 620), "PF on Linux - OpenBSD PF 8.0 no kernel Linux",
|
||||
font=small_font, fill=MUTED)
|
||||
return img
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--script", required=True)
|
||||
ap.add_argument("--out", required=True)
|
||||
ap.add_argument("--title", required=True)
|
||||
ap.add_argument("--subtitle", default="")
|
||||
ap.add_argument("--footer", default="")
|
||||
ap.add_argument("--phase-no", type=int, default=0)
|
||||
ap.add_argument("--phase-total", type=int, default=0)
|
||||
ap.add_argument("--cols", type=int, default=120)
|
||||
ap.add_argument("--rows", type=int, default=32)
|
||||
ap.add_argument("--fps", type=int, default=30)
|
||||
ap.add_argument("--max-hold", type=float, default=2.0)
|
||||
ap.add_argument("--min-hold", type=float, default=0.25)
|
||||
ap.add_argument("--title-card", type=float, default=3.0)
|
||||
ap.add_argument("--keep-frames", action="store_true")
|
||||
ap.add_argument("--panel", default="alpine (pf0) - lab libvirt - kernel 7.3.4 + pf.ko")
|
||||
args = ap.parse_args()
|
||||
|
||||
repo = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
mono = "/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono.ttf"
|
||||
mono_b = "/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono-Bold.ttf"
|
||||
font = load_font(mono, 21)
|
||||
bold_font = load_font(mono_b, 21)
|
||||
small_font = load_font(mono, 19)
|
||||
|
||||
rec = Recorder(args.cols, args.rows)
|
||||
rc = rec.run(["bash", args.script])
|
||||
sys.stderr.write(f"[screencast] script rc={rc}, events={len(rec.events)}\n")
|
||||
|
||||
os.makedirs(os.path.dirname(os.path.abspath(args.out)) or ".", exist_ok=True)
|
||||
frames_dir = os.path.join(os.path.dirname(os.path.abspath(args.out)), "_frames")
|
||||
shutil.rmtree(frames_dir, ignore_errors=True)
|
||||
os.makedirs(frames_dir, exist_ok=True)
|
||||
|
||||
def dump(img, idx):
|
||||
img.save(os.path.join(frames_dir, f"{idx:05d}.png"))
|
||||
|
||||
n = 0
|
||||
# title card
|
||||
title_img = render_title(args.title, args.subtitle, args.phase_no,
|
||||
args.phase_total, bold_font, font, small_font)
|
||||
title_frames = int(args.title_card * args.fps)
|
||||
for _ in range(title_frames):
|
||||
dump(title_img, n)
|
||||
n += 1
|
||||
|
||||
# session frames, paced by the real inter-event delays
|
||||
prev_t = 0.0
|
||||
for i, (ts, rows, cur) in enumerate(rec.events):
|
||||
hold = ts - prev_t
|
||||
prev_t = ts
|
||||
if i + 1 < len(rec.events):
|
||||
hold = rec.events[i + 1][0] - ts
|
||||
hold = max(args.min_hold, min(args.max_hold, hold))
|
||||
img = render_frame(rows, args.title, args.subtitle, args.phase_no,
|
||||
args.phase_total, args.cols, font, bold_font,
|
||||
small_font, args.footer, cursor=cur, panel=args.panel)
|
||||
frames = max(1, int(round(hold * args.fps)))
|
||||
for _ in range(frames):
|
||||
dump(img, n)
|
||||
n += 1
|
||||
|
||||
# closing card
|
||||
end_img = render_frame(rec.events[-1][1], args.title, args.subtitle,
|
||||
args.phase_no, args.phase_total, args.cols, font,
|
||||
bold_font, small_font,
|
||||
args.footer or "fim da fase",
|
||||
cursor=rec.events[-1][2], panel=args.panel)
|
||||
for _ in range(int(1.5 * args.fps)):
|
||||
dump(end_img, n)
|
||||
n += 1
|
||||
|
||||
# encoder: prefer x264, fall back to openh264 (both H.264 in MP4, which is
|
||||
# what YouTube wants), then VP9/WebM.
|
||||
have = subprocess.run(["ffmpeg", "-hide_banner", "-encoders"],
|
||||
capture_output=True, text=True).stdout
|
||||
base = ["ffmpeg", "-y", "-loglevel", "error", "-framerate", str(args.fps),
|
||||
"-i", os.path.join(frames_dir, "%05d.png"),
|
||||
"-pix_fmt", "yuv420p", "-movflags", "+faststart", "-r", str(args.fps)]
|
||||
if "libx264" in have:
|
||||
cmd = base + ["-c:v", "libx264", "-preset", "medium", "-crf", "20",
|
||||
"-profile:v", "high", "-level", "4.0", args.out]
|
||||
elif "libopenh264" in have:
|
||||
cmd = base + ["-c:v", "libopenh264", "-b:v", "6M",
|
||||
"-profile:v", "high", args.out]
|
||||
elif "libvpx-vp9" in have:
|
||||
cmd = base + ["-c:v", "libvpx-vp9", "-b:v", "0", "-crf", "30",
|
||||
"-row-mt", "1", "-cpu-used", "4", args.out]
|
||||
else:
|
||||
raise SystemExit("[screencast] no usable H.264/VP9 encoder in ffmpeg")
|
||||
subprocess.run(cmd, check=True)
|
||||
if not args.keep_frames:
|
||||
shutil.rmtree(frames_dir, ignore_errors=True)
|
||||
dur = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
|
||||
"format=duration", "-of", "csv=p=0", args.out],
|
||||
capture_output=True, text=True).stdout.strip()
|
||||
size = os.path.getsize(args.out)
|
||||
sys.stderr.write(f"[screencast] wrote {args.out} frames={n} "
|
||||
f"duration={dur}s size={size/1e6:.1f}MB\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,331 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Record a *real* interactive shell session and render it as a video.
|
||||
|
||||
The difference to the earlier renderer: the shell is not a script whose output
|
||||
is dumped at once. We open a real pty, type the commands **key by key** with
|
||||
human-like timing, wait for the prompt to come back, and capture the byte
|
||||
stream that the terminal actually produced. The result is a session that
|
||||
looks like somebody sitting in front of a terminal - characters appearing one
|
||||
by one, output scrolling in as the kernel answers.
|
||||
|
||||
./scripts/screencast/session.py --plan scripts/screencast/plans/04.sh \\
|
||||
--out out/screencast/session-04.mp4 --title "..." --subtitle "..."
|
||||
|
||||
The plan file is a shell script executed *inside* the recording pty. Every
|
||||
line the plan prints is real output from the lab.
|
||||
"""
|
||||
import argparse
|
||||
import errno
|
||||
import fcntl
|
||||
import os
|
||||
import pty
|
||||
import select
|
||||
import shlex
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import termios
|
||||
import time
|
||||
|
||||
import pyte
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
# ---------------------------------------------------------------- palette ---
|
||||
BG = (11, 14, 19)
|
||||
PANEL = (18, 22, 28)
|
||||
BORDER = (44, 50, 58)
|
||||
FG = (203, 211, 219)
|
||||
HEADER_BG = (21, 26, 33)
|
||||
HEADER_FG = (240, 246, 252)
|
||||
ACCENT = (86, 166, 255)
|
||||
ACCENT2 = (57, 197, 207)
|
||||
MUTED = (108, 116, 128)
|
||||
GREEN = (63, 185, 80)
|
||||
YELLOW = (210, 153, 34)
|
||||
RED = (255, 123, 114)
|
||||
TITLE_BG = (15, 19, 25)
|
||||
|
||||
NAMED = {
|
||||
"black": (48, 54, 61), "red": RED, "green": GREEN, "yellow": YELLOW,
|
||||
"blue": ACCENT, "magenta": (188, 140, 255), "cyan": ACCENT2,
|
||||
"white": FG, "brown": (168, 115, 66), "default": None,
|
||||
}
|
||||
|
||||
|
||||
def color_of(spec, default):
|
||||
if spec == "default":
|
||||
return default
|
||||
if spec in NAMED:
|
||||
return NAMED[spec] or default
|
||||
try:
|
||||
if len(spec) == 6:
|
||||
return tuple(int(spec[i:i + 2], 16) for i in (0, 2, 4))
|
||||
except ValueError:
|
||||
pass
|
||||
return default
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ fonts ---
|
||||
def load_fonts():
|
||||
mono = "/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono.ttf"
|
||||
monob = "/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono-Bold.ttf"
|
||||
return (ImageFont.truetype(mono, 20), ImageFont.truetype(monob, 20),
|
||||
ImageFont.truetype(mono, 18))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- session ---
|
||||
class Shell:
|
||||
"""A real interactive shell on a pty; we type into it and read what it says."""
|
||||
|
||||
PROMPT = "__PS1__"
|
||||
|
||||
def __init__(self, argv, cols=118, rows=34, env=None):
|
||||
self.cols, self.rows = cols, rows
|
||||
self.master, slave = pty.openpty()
|
||||
fcntl.ioctl(slave, termios.TIOCSWINSZ,
|
||||
struct.pack("HHHH", rows, cols, 0, 0))
|
||||
e = dict(os.environ)
|
||||
e.update({"TERM": "xterm-256color", "COLUMNS": str(cols),
|
||||
"LINES": str(rows), "LC_ALL": "C.UTF-8", "PS1": self.PROMPT + " "})
|
||||
if env:
|
||||
e.update(env)
|
||||
self.proc = subprocess.Popen(argv, stdin=slave, stdout=slave, stderr=slave,
|
||||
env=e, start_new_session=True, close_fds=True)
|
||||
os.close(slave)
|
||||
self.screen = pyte.Screen(cols, rows)
|
||||
self.stream = pyte.ByteStream(self.screen)
|
||||
self.events = [] # (t, snapshot)
|
||||
self.t0 = time.time()
|
||||
self.raw = bytearray()
|
||||
self.idle = 0.0
|
||||
|
||||
def pump(self, duration):
|
||||
"""Read from the pty for `duration` seconds, recording screen states."""
|
||||
end = time.time() + duration
|
||||
while True:
|
||||
remaining = end - time.time()
|
||||
if remaining <= 0:
|
||||
return
|
||||
try:
|
||||
r, _, _ = select.select([self.master], [], [], min(remaining, 0.05))
|
||||
except InterruptedError:
|
||||
continue
|
||||
if self.master in r:
|
||||
try:
|
||||
data = os.read(self.master, 65536)
|
||||
except OSError as exc:
|
||||
if exc.errno == errno.EIO:
|
||||
return
|
||||
raise
|
||||
if not data:
|
||||
return
|
||||
self.raw += data
|
||||
self.stream.feed(data)
|
||||
self.events.append((time.time() - self.t0, self.snapshot()))
|
||||
self.idle = 0.0
|
||||
else:
|
||||
self.idle += 0.05
|
||||
if self.idle > 0.25:
|
||||
# nothing new: keep the timeline going so the frame clock
|
||||
# stays uniform
|
||||
self.events.append((time.time() - self.t0, self.snapshot()))
|
||||
|
||||
def snapshot(self):
|
||||
rows = []
|
||||
for y in range(self.rows):
|
||||
line = self.screen.buffer[y]
|
||||
cells = []
|
||||
for x in range(self.cols):
|
||||
ch = line[x]
|
||||
cells.append((ch.data or " ", color_of(ch.fg, FG), ch.bold))
|
||||
rows.append(cells)
|
||||
return rows, (self.screen.cursor.y, self.screen.cursor.x)
|
||||
|
||||
def wait_prompt(self, timeout=25.0):
|
||||
"""Wait until the shell prompt shows up again."""
|
||||
end = time.time() + timeout
|
||||
while time.time() < end:
|
||||
self.pump(0.05)
|
||||
text = "\n".join(self.screen.display)
|
||||
if text.rstrip().endswith(self.PROMPT):
|
||||
return True
|
||||
return False
|
||||
|
||||
def type_line(self, text, cps=42.0):
|
||||
"""Type a line character by character, like a human."""
|
||||
for ch in text:
|
||||
self.write(ch)
|
||||
# slight jitter so it does not look machine-perfect
|
||||
delay = 1.0 / cps + (hash(ch) % 7) * 0.0016
|
||||
time.sleep(delay)
|
||||
self.write("\r")
|
||||
self.pump(0.12)
|
||||
|
||||
def write(self, s):
|
||||
os.write(self.master, s.encode())
|
||||
|
||||
def run(self):
|
||||
self.pump(1.2)
|
||||
return self.proc.wait()
|
||||
|
||||
|
||||
# --------------------------------------------------------------- rendering ---
|
||||
def render(rows, cursor, title, subtitle, phase_no, phase_total, font, bold,
|
||||
small, footer, panel_label, art=None, art_alpha=0.0):
|
||||
W, H = 1920, 1080
|
||||
img = Image.new("RGB", (W, H), BG)
|
||||
d = ImageDraw.Draw(img)
|
||||
|
||||
d.rectangle([0, 0, W, 70], fill=HEADER_BG)
|
||||
d.text((44, 24), "PF on Linux", font=bold, fill=HEADER_FG)
|
||||
tw = d.textlength("PF on Linux", font=bold)
|
||||
d.text((44 + tw + 24, 28), "-", font=font, fill=MUTED)
|
||||
d.text((44 + tw + 44, 28), "OpenBSD PF 8.0 engine inside the Linux kernel",
|
||||
font=font, fill=MUTED)
|
||||
if phase_total:
|
||||
label = f"FASE {phase_no}/{phase_total}"
|
||||
lw = d.textlength(label, font=small)
|
||||
d.text((W - 44 - lw, 30), label, font=small, fill=ACCENT)
|
||||
|
||||
d.text((44, 100), title, font=bold, fill=HEADER_FG)
|
||||
if subtitle:
|
||||
d.text((44, 148), subtitle, font=font, fill=MUTED)
|
||||
|
||||
top, left, right = 196, 44, W - 44
|
||||
lh = 28
|
||||
d.rectangle([left, top, right, H - 78], fill=PANEL, outline=BORDER, width=2)
|
||||
d.rectangle([left, top, right, top + 40], fill=HEADER_BG)
|
||||
for i, c in enumerate([(255, 95, 86), (255, 189, 46), (39, 201, 63)]):
|
||||
d.ellipse([left + 22 + i * 24, top + 14, left + 34 + i * 24, top + 26], fill=c)
|
||||
d.text((left + 100, top + 12), panel_label, font=small, fill=MUTED)
|
||||
|
||||
x0, y0 = left + 26, top + 58
|
||||
cw = d.textlength("M", font=font)
|
||||
for i, line in enumerate(rows):
|
||||
y = y0 + i * lh
|
||||
if y > H - 96:
|
||||
break
|
||||
for x, (ch, fg, b) in enumerate(line):
|
||||
if ch == " ":
|
||||
continue
|
||||
d.text((x0 + x * cw, y), ch, font=bold if b else font, fill=fg)
|
||||
|
||||
if cursor:
|
||||
cy, cx = cursor
|
||||
if 0 <= cy < len(rows) and 0 <= cx < len(rows[0]):
|
||||
d.rectangle([x0 + cx * cw, y0 + cy * lh, x0 + (cx + 1) * cw - 1,
|
||||
y0 + (cy + 1) * lh - 1], fill=ACCENT)
|
||||
|
||||
d.text((44, H - 52), footer, font=small, fill=MUTED)
|
||||
url = "git.labexposed.com/lgcosta/pf-linux-port"
|
||||
uw = d.textlength(url, font=small)
|
||||
d.text((W - 44 - uw, H - 52), url, font=small, fill=MUTED)
|
||||
return img
|
||||
|
||||
|
||||
def render_title(title, subtitle, phase_no, phase_total, bold, font, small, art):
|
||||
W, H = 1920, 1080
|
||||
img = Image.new("RGB", (W, H), TITLE_BG)
|
||||
if art:
|
||||
a = art.resize((W, H)).convert("RGB")
|
||||
img = Image.blend(img, a, 0.55)
|
||||
d = ImageDraw.Draw(img)
|
||||
d.rectangle([0, 0, W, 8], fill=ACCENT)
|
||||
d.text((120, 420), title, font=bold, fill=(250, 252, 255))
|
||||
if subtitle:
|
||||
d.text((120, 490), subtitle, font=font, fill=(190, 200, 214))
|
||||
if phase_total:
|
||||
d.text((120, 570), f"FASE {phase_no} DE {phase_total}", font=small, fill=ACCENT)
|
||||
d.text((120, 630), "PF on Linux - OpenBSD PF 8.0 no kernel Linux",
|
||||
font=small, fill=(150, 160, 175))
|
||||
return img
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--plan", required=True)
|
||||
ap.add_argument("--out", required=True)
|
||||
ap.add_argument("--title", required=True)
|
||||
ap.add_argument("--subtitle", default="")
|
||||
ap.add_argument("--footer", default="PF on Linux - OpenBSD PF 8.0 no kernel Linux - lab libvirt")
|
||||
ap.add_argument("--panel", default="alpine (pf0) - lab libvirt - kernel 7.3.4 + pf.ko")
|
||||
ap.add_argument("--phase-no", type=int, default=0)
|
||||
ap.add_argument("--phase-total", type=int, default=0)
|
||||
ap.add_argument("--cols", type=int, default=118)
|
||||
ap.add_argument("--rows", type=int, default=34)
|
||||
ap.add_argument("--fps", type=int, default=30)
|
||||
ap.add_argument("--title-card", type=float, default=2.5)
|
||||
ap.add_argument("--art")
|
||||
args = ap.parse_args()
|
||||
|
||||
font, bold, small = load_fonts()
|
||||
art = None
|
||||
if args.art and os.path.exists(args.art):
|
||||
art = Image.open(args.art)
|
||||
|
||||
# The plan runs inside the recording shell; the shell itself is a real
|
||||
# bash with our prompt, so what the user sees is a live session.
|
||||
sh = Shell(["bash", "--norc", "--noprofile", "-i"], cols=args.cols, rows=args.rows)
|
||||
sh.pump(1.0)
|
||||
# make the prompt look like the OpenBSD box
|
||||
sh.write("export PS1='\\u@\\h:\\w\\$ ' PS2='> ' TERM=xterm-256color\n")
|
||||
sh.pump(0.6)
|
||||
sh.write("export LC_ALL=C.UTF-8 LANG=C.UTF-8; clear\n")
|
||||
sh.pump(0.8)
|
||||
sh.events = [(0.0, sh.snapshot())]
|
||||
|
||||
# run the plan: each line is typed into the shell
|
||||
plan = open(args.plan).read()
|
||||
for raw_line in plan.split("\n"):
|
||||
line = raw_line.rstrip()
|
||||
if not line.strip() or line.strip().startswith("#"):
|
||||
continue
|
||||
if line.startswith("@sleep "):
|
||||
sh.pump(float(line.split()[1]))
|
||||
continue
|
||||
if line.startswith("@run "):
|
||||
# run a command without showing it typed (used for setup)
|
||||
sh.write(line[5:] + "\n")
|
||||
sh.wait_prompt(timeout=30)
|
||||
continue
|
||||
sh.type_line(line)
|
||||
sh.wait_prompt(timeout=60)
|
||||
sh.pump(0.45)
|
||||
|
||||
sh.write("exit\n")
|
||||
sh.pump(0.5)
|
||||
|
||||
sys.stderr.write(f"[session] {len(sh.events)} events, {sh.events[-1][0]:.1f}s\n")
|
||||
|
||||
# ------------------------------------------------------ frame timeline ---
|
||||
frames_dir = os.path.join(os.path.dirname(os.path.abspath(args.out)), "_sframes")
|
||||
subprocess.run(["rm", "-rf", frames_dir], check=False)
|
||||
os.makedirs(frames_dir, exist_ok=True)
|
||||
|
||||
n = 0
|
||||
title_img = render_title(args.title, args.subtitle, args.phase_no, args.phase_total,
|
||||
bold, font, small, art)
|
||||
for _ in range(int(args.title_card * args.fps)):
|
||||
title_img.save(os.path.join(frames_dir, f"{n:05d}.png"))
|
||||
n += 1
|
||||
|
||||
# the events carry real timestamps: map them onto a uniform frame clock
|
||||
ev = sh.events
|
||||
t0 = ev[0][0]
|
||||
for i, (ts, snap) in enumerate(ev):
|
||||
hold = (ev[i + 1][0] - ts) if i + 1 < len(ev) else 0.04
|
||||
hold = max(0.033, min(0.5, hold))
|
||||
rows, cur = snap
|
||||
img = render(rows, cur, args.title, args.subtitle, args.phase_no,
|
||||
args.phase_total, font, bold, small, args.footer, args.panel, art)
|
||||
cnt = max(1, int(round(hold * args.fps)))
|
||||
for _ in range(cnt):
|
||||
img.save(os.path.join(frames_dir, f"{n:05d}.png"))
|
||||
n += 1
|
||||
|
||||
subprocess.run(["rm", "-rf", frames_dir], check=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user