#!/usr/bin/env python3 """Install items-server.tsv (from make_items.py) into share/conf/item_proto.txt + item_names.txt. Rows with the same vnum are replaced, so a rerun is idempotent. The db reads the tables only at boot: restart db and every core afterwards. Usage: install_items.py """ import shutil,sys from pathlib import Path server,rows,backup=map(Path,sys.argv[1:4]) if backup.exists():raise SystemExit('use a fresh backup directory') items={int(l.split(b'\t',1)[0]):l for l in rows.read_bytes().splitlines() if l} backup.mkdir(parents=True) conf=server/'share/conf' for filename,row in (('item_proto.txt',lambda v:items[v]),('item_names.txt',lambda v:str(v).encode()+b'\t'+items[v].split(b'\t')[1])): path=conf/filename;raw=path.read_bytes();shutil.copy2(path,backup/filename) newline=b'\r\n' if b'\r\n' in raw else b'\n' kept=[l for l in raw.replace(b'\r\n',b'\n').split(b'\n') if l and not (l.split(b'\t',1)[0].isdigit() and int(l.split(b'\t',1)[0]) in items)] tmp=path.with_name(filename+'.tmp');tmp.write_bytes(newline.join(kept+[row(v) for v in sorted(items)])+newline);tmp.replace(path) print('AVA2 items installed:',len(items),'backup:',backup)