#!/usr/bin/env python3 """Read-only export of public template tables; run on the FreeBSD game host. Usage: python3 tools/wiki/export_public_tables.py /tmp/wiki-export mysql uses the operator's existing option file/socket credentials; no password is printed/stored. """ import argparse import hashlib import json import os import re import shutil import subprocess from pathlib import Path def export(server, output, database, mysql): if not re.fullmatch(r'[A-Za-z0-9_]+', database): raise ValueError('invalid database identifier') if output.exists(): raise ValueError('choose a fresh output directory') conf=server/'share/conf' output.mkdir(parents=True,mode=0o700) try: for name in ('item_proto.txt','item_names.txt','mob_proto.txt','mob_names.txt'): shutil.copyfile(conf/name,output/name) query=('SELECT id,cost,prob,vnum0,count0,vnum1,count1,vnum2,count2,' 'vnum3,count3,vnum4,count4 FROM refine_proto ORDER BY id') with (output/'refine.tsv').open('wb') as stream: subprocess.run([mysql,'-N','-B',database,'-e',query],stdout=stream,check=True) report={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in output.iterdir()} (output/'source-manifest.json').write_text(json.dumps(report,indent=2)+'\n') except Exception: shutil.rmtree(output) raise return report if __name__=='__main__': parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('output',type=Path) parser.add_argument('--server',type=Path,default=Path('/usr/metin2/server')) parser.add_argument('--database',default='player') parser.add_argument('--mysql',default='mysql') args=parser.parse_args() print(json.dumps(export(args.server,args.output,args.database,args.mysql),indent=2))