#!/usr/bin/env python3 """Build a bound source snapshot in fresh per-target directories; never install it.""" import argparse import hashlib import json from pathlib import Path import platform import re import shutil import subprocess import sys def digest(path): return hashlib.sha256(path.read_bytes()).hexdigest() def source_digest(source): h=hashlib.sha256() for part in ('server','extern'): for path in sorted((source/part).rglob('*')): if not path.is_file() or any(p in ('.obj','OBJDIR','obj','.git') for p in path.parts):continue if path.suffix in ('.o','.a','.so','.d'):continue h.update(str(path.relative_to(source)).encode()+b'\0'+path.read_bytes()+b'\0') return h.hexdigest() def validate_dependencies(source, manifest, revision): if manifest.get('source_sha256')!=revision:raise ValueError('dependency manifest source mismatch') archives=manifest.get('archives',{}) actual={str(p.relative_to(source)):digest(p) for p in source.rglob('*.a')} if not actual or archives!=actual:raise ValueError('all dependency archives must match the reviewed source-bound manifest') for name, expected in manifest.get('system_archives',{}).items(): dependency=Path(name) if not dependency.is_absolute() or not dependency.is_file() or digest(dependency)!=expected: raise ValueError('system dependency archive mismatch: '+name) def prepare(source, output, target, dependency_manifest, warnings=False): production=Path('/usr/metin2/server') if output==production or production in output.parents or source==output or source in output.parents: raise ValueError('output must be outside source and production trees') revision=source_digest(source) validate_dependencies(source,dependency_manifest,revision) output.mkdir(parents=True,exist_ok=True) state=output/'source-binding.json' binding={'source':str(source),'sha256':revision,'dependencies':dependency_manifest} if state.exists(): if json.loads(state.read_text())!=binding:raise ValueError('output belongs to another source/dependency set') elif any(output.iterdir()):raise ValueError('unbound output must be empty') else:state.write_text(json.dumps(binding,indent=2)+'\n') run=output/(target+('-warnings' if warnings else '-release')) run.mkdir() # Never silently reuse objects, even after a failed build. snapshot=run/'source' snapshot.mkdir() for name in ('server','extern'): shutil.copytree(source/name,snapshot/name,ignore=shutil.ignore_patterns('*.o','.obj','OBJDIR','*.d')) for target_dir in (snapshot/'server/game/src',snapshot/'server/db/src'): makefile=target_dir/'Makefile' text=makefile.read_bytes().decode('latin1') # Depend paths must follow this target's OBJDIR, not the original checkout. text=re.sub(r'(?