1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Added replication, Added clean stops, logs and releases (0.2.0)

Step 11: the servers replicate by name (push_to and accept_from), their
messages and attachments, damaged copies asked for again.

Step 12: the server stops cleanly on SIGTERM/SIGINT (Ctrl+C, Ctrl+Break
and the console closing on Windows), committing what was queued; logs go
to the console, BAKASHROT_LOG sets the level. release.py builds a
release per platform (the programs, the DLL, a Python wheel with the
library, THIRD-PARTY.txt) and smoke-tests it; CI runs it on v* tags.
Built for size (opt-level "s"): 25-31% smaller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 20:38:32 +01:00
parent e044fcf359
commit 68966a8031
30 changed files with 2456 additions and 172 deletions
+395
View File
@@ -0,0 +1,395 @@
"""Builds a BakaShrot release for this machine's platform (Windows x64 or
Linux x86-64), checks it works, and archives it:
python release.py [--skip-test]
Into target/dist/:
bakashrot-<version>-<platform>/ the release:
bakashrot-server, bakashrot, bakashrot-cert the programs
bakashrot.dll / libbakashrot.so the client library (C)
include/bakashrot.h its C header
purebasic/*.pbi its PureBasic prototypes
python/bakashrot.py the Python client, over it
python/bakashrot-<version>-*.whl the same with the library in it
server.example.toml, client.example.toml the configs, documented
docs/ Source/docs/
README.txt what's where, how to start
LICENSE.txt, THIRD-PARTY.txt BakaShrot's (CC0), its dependencies'
bakashrot-<version>-<platform>.zip (Windows) or .tar.gz (Linux)
No keys or certificates in it: bakashrot-cert makes them.
The check (unless --skip-test), in the release folder's _test/ (exes run
from temp folders get blocked by antivirus here), deleted after unless
it failed: a CA and certificates by bakashrot-cert, the
server started on a free port with a throwaway database, then the tool
pushing (and an attachment of 3 MB), reading back, listing, querying;
Python through the library, then the wheel installed by pip and used with
no path given; then the server stopped as a service manager would
(SIGTERM, or Ctrl+Break on Windows), which must end cleanly.
The version is the workspace's (Cargo.toml), which bakashrot.py's
__version__ must match. Needs Python 3.11+ (tomllib) and cargo (rustup).
"""
import json
import os
import platform
import re
import shutil
import signal
import socket
import subprocess
import sys
import time
import tomllib
from datetime import datetime, timezone
HERE = os.path.dirname(os.path.abspath(__file__))
WINDOWS = sys.platform == "win32"
EXE = ".exe" if WINDOWS else ""
LIBRARY = "bakashrot.dll" if WINDOWS else "libbakashrot.so"
PROGRAMS = ["bakashrot-server", "bakashrot", "bakashrot-cert"]
CRATES = ["bakashrot-server", "bakashrot-tools", "bakashrot-capi"]
CLIENT_EXAMPLE = """\
# What the bakashrot tool and Python's Client.from_file() read, to connect
# to a server. Paths are relative to this file's folder. See
# docs/client.md.
server = "10.0.0.2:7000" # host:port, [an IPv6 address]:port
ca = "ca.crt" # the CA's certificate (bakashrot-cert ca)
certificate = "kitchen.crt" # this node's (bakashrot-cert issue --name kitchen ...)
key = "kitchen.key"
# crl = "ca.crl" # optional: certificates the CA revoked
"""
def fail(why):
sys.exit(f"release.py: {why}")
def run(command, cwd=HERE, **kwargs):
"""Runs a command, its standard output; fails with its output if it does."""
done = subprocess.run(command, cwd=cwd, capture_output=True, encoding="utf-8", errors="replace", **kwargs)
if done.returncode != 0:
fail(f"{' '.join(map(str, command))} failed ({done.returncode}):\n{done.stdout}{done.stderr}")
return done.stdout
def version():
with open(os.path.join(HERE, "Cargo.toml"), "rb") as file:
version = tomllib.load(file)["workspace"]["package"]["version"]
with open(os.path.join(HERE, "bindings", "python", "bakashrot.py"), encoding="utf-8") as file:
python = re.search(r'^__version__ = "([^"]+)"', file.read(), re.MULTILINE)
if not python or python.group(1) != version:
fail(f"bakashrot.py's __version__ isn't the workspace's {version}")
return version
def platform_name():
machine = platform.machine().lower()
if WINDOWS and machine in ("amd64", "x86_64"):
return "windows-x64"
if sys.platform.startswith("linux") and machine == "x86_64":
return "linux-x64"
fail(f"no release for {sys.platform} {machine}")
def commit():
try:
sha = subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=HERE,
capture_output=True, text=True).stdout.strip()
dirty = subprocess.run(["git", "status", "--porcelain", "."], cwd=HERE,
capture_output=True, text=True).stdout.strip()
except OSError:
return "unknown"
return (sha or "unknown") + (" (with uncommitted changes)" if dirty else "")
def third_party():
"""Every crate built into the programs and the library (normal
dependencies, this platform's), with its license files' text."""
host = re.search(r"^host: (\S+)", run(["rustc", "-vV"]), re.MULTILINE).group(1)
metadata = json.loads(run(["cargo", "metadata", "--format-version", "1", "--filter-platform", host]))
packages = {package["id"]: package for package in metadata["packages"]}
nodes = {node["id"]: node for node in metadata["resolve"]["nodes"]}
roots = [id for id, package in packages.items() if package["name"] in CRATES]
seen, stack = set(), list(roots)
while stack:
id = stack.pop()
if id in seen:
continue
seen.add(id)
for dep in nodes[id]["deps"]:
if any(kind["kind"] is None for kind in dep["dep_kinds"]):
stack.append(dep["pkg"])
workspace = set(metadata["workspace_members"])
used = sorted((packages[id] for id in seen - workspace), key=lambda p: (p["name"], p["version"]))
lines = [
"BakaShrot's programs and client library are built with these crates",
"(their code is in them). Each one's license, then their license files,",
"each text once with the crates it comes from.",
"SQLite (in libsqlite3-sys) is in the public domain.",
"",
]
lines += [f"{p['name']} {p['version']}: {p['license'] or 'see its files'}" for p in used]
texts = {} # a license file's text: the crates' files it is
for package in used:
folder = os.path.dirname(package["manifest_path"])
names = sorted(name for name in os.listdir(folder)
if re.match(r"(LICEN[CS]E|COPYING|COPYRIGHT|NOTICE)", name, re.IGNORECASE)
and os.path.isfile(os.path.join(folder, name)))
for name in names:
with open(os.path.join(folder, name), encoding="utf-8", errors="replace") as file:
text = file.read().strip()
texts.setdefault(text, []).append(f"{package['name']} {package['version']} ({name})")
for text, sources in texts.items():
lines += ["", "=" * 76, *sources, "=" * 76, "", text]
return "\n".join(lines) + "\n"
def readme(version, name):
built = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
rustc = run(["rustc", "--version"]).strip()
return f"""\
BakaShrot {version} ({name})
Built {built}, commit {commit()}, {rustc}.
Licenses: BakaShrot is CC0 (LICENSE.txt); the crates built into it are
listed in THIRD-PARTY.txt with their licenses.
A message store with replication: servers take messages per topic and node
from clients with certificates of your own CA, store them in SQLite, and
replicate them between servers. docs/ has the details (config.md,
certificates.md, server.md, client.md, protocol.md).
Certificates (bakashrot-cert{EXE}; docs/certificates.md):
bakashrot-cert ca --name "Home CA" ca.crt, ca.key (keep ca.key safe)
bakashrot-cert issue --name server-1 --server a server's
bakashrot-cert issue --name kitchen --role sensor a client's: its node, its roles
bakashrot-cert revoke kitchen.crt ca.crl
The server (bakashrot-server{EXE} server.toml; docs/config.md, server.md):
server.example.toml has every setting. It logs to the console
(BAKASHROT_LOG=debug for more) and stops cleanly on SIGTERM or SIGINT
(Linux), Ctrl+C, Ctrl+Break or its console closed (Windows): what was
being written is committed first. No console commands: run it from a
service manager (systemd, NSSM...) or a terminal.
The tool (bakashrot{EXE}, with a client.toml: client.example.toml;
docs/client.md), --help on each command:
bakashrot push logs '{{"t":21}}' bakashrot push logs @file.json -a photo.jpg
bakashrot latest logs kitchen bakashrot messages logs kitchen [-f]
bakashrot attachment logs kitchen 43 copy.jpg
bakashrot list bakashrot query "SELECT * FROM messages"
The client library ({LIBRARY}): the BakaShrot_* exports, include/bakashrot.h
for C, purebasic/ for PureBasic, python/bakashrot.py for Python (no
dependencies; it looks for the library next to itself or in BAKASHROT_DLL).
Or: pip install python/bakashrot-*.whl, the library inside.
Compatibility: protocol 7. Servers and clients must come from the same
release: another version's connection is refused. Nothing from the old
(PureBasic) BakaShrot is read: not its configs, keys or databases.
"""
def build():
print("Building")
command = ["cargo", "build", "--release", "--locked"]
for crate in CRATES:
command += ["-p", crate]
run(command)
return os.path.join(HERE, "target", "release")
def package(built, version, name, out):
print(f"Packaging {out}")
for program in PROGRAMS:
shutil.copy2(os.path.join(built, program + EXE), out)
shutil.copy2(os.path.join(built, LIBRARY), out)
bindings = os.path.join(HERE, "bindings")
os.makedirs(os.path.join(out, "include"))
shutil.copy2(os.path.join(bindings, "c", "bakashrot.h"), os.path.join(out, "include"))
shutil.copytree(os.path.join(bindings, "purebasic"), os.path.join(out, "purebasic"))
os.makedirs(os.path.join(out, "python"))
shutil.copy2(os.path.join(bindings, "python", "bakashrot.py"), os.path.join(out, "python"))
shutil.copytree(os.path.join(HERE, "docs"), os.path.join(out, "docs"))
shutil.copy2(os.path.join(HERE, "crates", "bakashrot-config", "example.toml"),
os.path.join(out, "server.example.toml"))
write(os.path.join(out, "client.example.toml"), CLIENT_EXAMPLE)
shutil.copy2(os.path.join(HERE, "..", "LICENSE"), os.path.join(out, "LICENSE.txt"))
write(os.path.join(out, "THIRD-PARTY.txt"), third_party())
write(os.path.join(out, "README.txt"), readme(version, name))
wheel = run([sys.executable, "-B", os.path.join(bindings, "python", "make_wheel.py"),
os.path.join(out, LIBRARY), os.path.join(out, "python"),
os.path.join(out, "LICENSE.txt"), os.path.join(out, "THIRD-PARTY.txt")]).strip()
print(f"Wheel: {os.path.basename(wheel)}")
return wheel
def write(path, text):
"""A text file, with the platform's line endings."""
with open(path, "w", encoding="utf-8") as file:
file.write(text)
def free_port():
with socket.socket() as listener:
listener.bind(("127.0.0.1", 0))
return listener.getsockname()[1]
def smoke_test(out, wheel):
print("Testing the release")
test = os.path.join(out, "_test")
os.makedirs(test)
tool = lambda program, *args: run([os.path.join(out, program + EXE), *args], cwd=test)
def expect(what, got, pattern):
if not re.search(pattern, got, re.MULTILINE):
fail(f"{what}: unexpected output:\n{got}")
tool("bakashrot-cert", "ca", "--name", "Release test CA")
tool("bakashrot-cert", "issue", "--name", "server-1", "--server")
tool("bakashrot-cert", "issue", "--name", "tester", "--role", "tester")
port = free_port()
write(os.path.join(test, "server.toml"), f"""\
[server]
listen = "127.0.0.1:{port}"
ca = "ca.crt"
certificate = "server-1.crt"
key = "server-1.key"
database = "test.sqlite"
topics = ["releasetest"]
[roles.tester]
push_topics = ["releasetest"]
read_topics = ["*"]
query = true
""")
write(os.path.join(test, "client.toml"), f"""\
server = "127.0.0.1:{port}"
ca = "ca.crt"
certificate = "tester.crt"
key = "tester.key"
""")
write(os.path.join(test, "message.json"), '{"release":"test","value":42}')
attachment = os.urandom(3 * 1024 * 1024)
with open(os.path.join(test, "attachment.bin"), "wb") as file:
file.write(attachment)
log = open(os.path.join(test, "server.log"), "w")
# Its own process group on Windows, to be sent Ctrl+Break alone.
flags = subprocess.CREATE_NEW_PROCESS_GROUP if WINDOWS else 0
server = subprocess.Popen([os.path.join(out, "bakashrot-server" + EXE), "server.toml"], cwd=test,
stdout=log, stderr=subprocess.STDOUT, creationflags=flags)
try:
deadline = time.monotonic() + 15
while True:
if server.poll() is not None:
fail(f"the server exited ({server.returncode}):\n{read_log(test)}")
try:
socket.create_connection(("127.0.0.1", port), timeout=0.2).close()
break
except OSError:
if time.monotonic() > deadline:
fail(f"the server isn't listening on {port}:\n{read_log(test)}")
time.sleep(0.1)
expect("push", tool("bakashrot", "push", "releasetest", "@message.json"), r"message #1\b")
expect("push -a", tool("bakashrot", "push", "releasetest", '{"with":"file"}', "-a", "attachment.bin"),
r"message #2, with an attachment of 3145728 bytes")
latest = tool("bakashrot", "latest", "releasetest", "tester")
expect("latest", latest, r"releasetest:tester#2")
expect("latest", latest, r"Attachment: 3145728 bytes")
expect("attachment", tool("bakashrot", "attachment", "releasetest", "tester", "2", "copy.bin"),
r"Saved 3145728 bytes")
with open(os.path.join(test, "copy.bin"), "rb") as file:
if file.read() != attachment:
fail("the attachment came back different")
messages = tool("bakashrot", "messages", "releasetest", "tester")
expect("messages", messages, r'^1\t\{"release":"test","value":42\}$')
expect("messages", messages, r'^2\t\{"with":"file"\}$')
expect("list", tool("bakashrot", "list", "nodes", "--json"), r'"names":\["tester"\]')
expect("query", tool("bakashrot", "query",
"SELECT message_data ->> '$.value' AS v FROM messages WHERE topic = 'releasetest' AND message_id = 1"),
r'"rows":\[\["42"\]\]')
print(" The tools: pushed, an attachment too, read back, listed, queried: OK")
script = ("import sys; sys.path.insert(0, sys.argv[1]); import bakashrot\n"
"with bakashrot.Client.from_file('client.toml', sys.argv[2]) as c:\n"
" print(c.latest('releasetest', 'tester').id, c.get_attachment('releasetest', 'tester', 2)"
" == open('attachment.bin', 'rb').read(), c.push({'py': 1}, 'releasetest'))\n")
got = run([sys.executable, "-B", "-c", script, os.path.join(out, "python"), os.path.join(out, LIBRARY)],
cwd=test).strip()
if got != "2 True 3":
fail(f"Python through the library: unexpected output: {got}")
print(" Python, through the library: OK")
installed = os.path.join(test, "wheel")
run([sys.executable, "-m", "pip", "install", "--no-index", "--no-deps", "--disable-pip-version-check",
"--quiet", "--target", installed, wheel])
script = ("import os, sys; sys.path.insert(0, sys.argv[1]); import bakashrot\n"
"with bakashrot.Client.from_file('client.toml') as c:\n"
" inside = os.path.dirname(c._dll.path) == os.path.dirname(bakashrot.__file__)\n"
" print(inside, c.latest('releasetest', 'tester').json()['py'], c.push({'wheel': 1}, 'releasetest'))\n")
environment = {key: value for key, value in os.environ.items() if key != "BAKASHROT_DLL"}
got = run([sys.executable, "-B", "-c", script, installed], cwd=test, env=environment).strip()
if got != "True 1 4":
fail(f"the wheel: unexpected output: {got}")
print(" The wheel, installed by pip: OK")
server.send_signal(signal.CTRL_BREAK_EVENT if WINDOWS else signal.SIGTERM)
try:
code = server.wait(timeout=15)
except subprocess.TimeoutExpired:
fail(f"the server didn't stop when asked:\n{read_log(test)}")
if code != 0 or "stopped" not in read_log(test):
fail(f"the server didn't stop cleanly ({code}):\n{read_log(test)}")
print(" Stopped cleanly: OK")
finally:
if server.poll() is None:
server.kill()
server.wait()
log.close()
return test
def read_log(test):
with open(os.path.join(test, "server.log"), encoding="utf-8", errors="replace") as file:
return file.read()
def archive(out):
"""The release folder in a .zip (Windows) or .tar.gz (Linux, keeping
the programs executable)."""
return shutil.make_archive(out, "zip" if WINDOWS else "gztar", *os.path.split(out))
def main():
args = set(sys.argv[1:])
if args - {"--skip-test"}:
sys.exit(__doc__)
# rustup's folder isn't on every shell's PATH.
os.environ["PATH"] = os.path.join(os.path.expanduser("~"), ".cargo", "bin") + os.pathsep + os.environ["PATH"]
if not shutil.which("cargo"):
fail("cargo not found (rustup)")
release, name = version(), platform_name()
dist = os.path.join(HERE, "target", "dist")
out = os.path.join(dist, f"bakashrot-{release}-{name}")
for old in (out, out + ".zip", out + ".tar.gz"):
if os.path.isdir(old):
shutil.rmtree(old)
elif os.path.exists(old):
os.remove(old)
os.makedirs(out)
print(f"BakaShrot {release} for {name}, into {out}")
wheel = package(build(), release, name, out)
if "--skip-test" not in args:
shutil.rmtree(smoke_test(out, wheel))
print(f"Archive: {archive(out)}")
if __name__ == "__main__":
main()