MC/network/server-control/install-kit-equipment.py
2026-11-27 23:43:05 +00:00

79 lines
3.2 KiB
Python

"""Install only the reviewed kit bridge modules; refuse unrelated source changes."""
import ast
import hashlib
import os
from pathlib import Path
import py_compile
import sys
BASELINES = {
'player_kits.py': 'fa76420423941189ba08b94382274e40762121e30b008ec0d08e35aa1a00fda3',
'kit_commerce.py': '29395a483fdb3f6d94b36fa57587e3a1391d6e4ed8a44159cbb488dd47d960e6',
}
FILES = ['player_kits.py', 'kit_commerce.py', 'kit_equipment.py', 'kit-equipment-contract.json']
def kit_body_limit(source):
tree = ast.parse(source)
candidates = [node for node in tree.body if isinstance(node, ast.FunctionDef)
and any(isinstance(value, ast.Constant) and value.value == '/kits/player/definition' for value in ast.walk(node))]
if len(candidates) != 1:
raise RuntimeError('Native office kit handler changed')
node = candidates[0]
lines = source.splitlines(keepends=True)
section = ''.join(lines[node.lineno - 1:node.end_lineno])
old, new = 'if not 0 < length <= 8192:', 'if not 0 < length <= 32768:'
if section.count(new) == 1 and old not in section:
return source
if section.count(old) != 1:
raise RuntimeError('Native office body limit changed')
lines[node.lineno - 1:node.end_lineno] = [section.replace(old, new)]
return ''.join(lines)
def install(apply=False):
source = Path(__file__).resolve().parent
target = Path('/opt/dunerealms/server-control')
plans = {}
for name in FILES:
content = (source / name).read_bytes()
destination = target / name
before = destination.read_bytes() if destination.exists() else None
if before == content:
continue
if before is not None and hashlib.sha256(before).hexdigest() != BASELINES.get(name):
raise RuntimeError('Concurrent native change detected in ' + name)
plans[destination] = content
agent = target / 'agent.py'
before = agent.read_text()
after = kit_body_limit(before)
if after != before:
plans[agent] = after.encode()
for name in ['player_kits.py', 'kit_commerce.py', 'kit_equipment.py']:
py_compile.compile(str(source / name), doraise=True)
if not apply:
print('Plan: install', len(plans), 'exact kit module/body-limit changes; no files changed')
return
originals = {path: path.read_bytes() if path.exists() else None for path in plans}
changed = []
try:
for path, content in plans.items():
temporary = path.with_name(path.name + '.kit-v3-new')
temporary.write_bytes(content)
temporary.chmod(path.stat().st_mode & 0o777 if path.exists() else 0o644)
if path.suffix == '.py':
py_compile.compile(str(temporary), doraise=True)
os.replace(temporary, path)
changed.append(path)
except Exception:
for path in reversed(changed):
if originals[path] is None:
path.unlink()
else:
path.write_bytes(originals[path])
raise
print('Installed reviewed kit metadata modules; restart the private control service to activate')
if __name__ == '__main__':
install('--apply' in sys.argv)