70 lines
3.2 KiB
Python
70 lines
3.2 KiB
Python
"""Read mounted definitions; write only a caller-selected staging directory.
|
|
|
|
No mounted file, existing cache, game setting, or live process is changed.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
|
|
STAGE = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location('native_lane_permissions', STAGE/'telemetry/native_lane_permissions.py')
|
|
permissions = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(permissions)
|
|
|
|
|
|
def extract(project_root, output):
|
|
output = Path(output).resolve()
|
|
if not output.is_relative_to(STAGE):
|
|
raise ValueError('This staged tool may write only below its staging folder')
|
|
sys.path.insert(0, str(Path(project_root).resolve()))
|
|
from ui.map_background import _Archive, loaded_sources
|
|
mounts, executable = loaded_sources()
|
|
archives = []
|
|
try:
|
|
archives = [_Archive(m['path']) for m in mounts]
|
|
names = set()
|
|
for archive in archives:
|
|
names.update('def/world/'+n for n in archive.files('def/world')
|
|
if permissions.DEFINITION_RE.fullmatch(n))
|
|
captured = {}
|
|
def resolve(name):
|
|
for archive in reversed(archives):
|
|
if archive.has(name):
|
|
payload = archive.read(name)
|
|
if payload is None:
|
|
raise ValueError('Unsupported mounted asset: '+name)
|
|
proof = dict(archive=str(archive.path),sha256=hashlib.sha256(payload).hexdigest())
|
|
captured[name] = (payload,proof)
|
|
return payload,proof
|
|
return None
|
|
catalog = permissions.build_catalog(resolve,names)
|
|
end, _ = loaded_sources()
|
|
if [m['path'] for m in mounts] != [m['path'] for m in end] or any(not a.unchanged() for a in archives):
|
|
raise ValueError('Mounted sources changed during permission extraction')
|
|
catalog['provenance'] = dict(loaded_archive_order=[m['path'] for m in mounts],
|
|
archives=[a.fingerprint for a in archives],
|
|
game_executable=str(executable))
|
|
output.mkdir(parents=True,exist_ok=True)
|
|
(output/'lane-permission-catalog.json').write_text(json.dumps(catalog,indent=2),encoding='utf-8')
|
|
# Exact installed source fixtures make tests independent of the game.
|
|
for name,(payload,proof) in captured.items():
|
|
path=output/'assets'/name
|
|
path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(payload)
|
|
(output/'manifest.json').write_text(json.dumps({k:v[1] for k,v in captured.items()},indent=2),encoding='utf-8')
|
|
return dict(units=len(catalog['units']),assets=len(catalog['assets']),errors=catalog['errors'],
|
|
mounted_archives=len(archives),output=str(output))
|
|
finally:
|
|
for archive in archives: archive.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
p=argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument('--project-root',type=Path,required=True)
|
|
p.add_argument('--output',type=Path,default=STAGE/'fixtures')
|
|
args=p.parse_args()
|
|
print(json.dumps(extract(args.project_root,args.output),indent=2))
|