guanghu-ice-heart/server-tools/living-navigation/compile_navigation.py

41 lines
4 KiB
Python

#!/usr/bin/env python3
import argparse,hashlib,json,os,pathlib
ROOT=pathlib.Path(__file__).resolve().parents[2]
TCS=pathlib.Path('/Volumes/JZAO/HoloLake/persona-runtime/shared/tcs-mother-root/CURRENT.json')
REG=ROOT/'routing/lighthouse-path-registry.json'
PERSONAS=ROOT/'identity/light-lake-persona-registration.json'
ECHO=ROOT/'routing/linguistic-echo-lamp-map.json'
def lit(state):
s=str(state or '').upper()
return any(x in s for x in ('CURRENT','ACTIVE','REMOTE_MAIN','PUBLISHED','VERIFIED')) and not any(x in s for x in ('RETIRED','HISTORY_ONLY','DEPRECATED','ISOLATION','UNKNOWN'))
def enterprise_safe(x):
text=' '.join(str(x.get(k,'')) for k in ('id','kind','online','world_path','route')).lower()
return not (str(x.get('id','')).startswith('ICE-') or x.get('id')=='DOM-FIFTH-0001' or 'fifth-domain' in text or 'fifth_domain_private' in text)
def build(scope):
current=json.loads(TCS.read_text());registry=json.loads(REG.read_text());routes=[]
for x in registry.get('paths',[]):
if not lit(x.get('state')) or (scope=='enterprise' and not enterprise_safe(x)):continue
route=x.get('online') or x.get('world_path')
if not route:continue
routes.append({'id':x['id'],'kind':x.get('kind','registered_path'),'state':x['state'],'name':x.get('name') or x['id'],'route':route,'lamp':'LIT_ELIGIBLE'})
payload=json.loads(current['payload']) if 'payload' in current else current
for x in payload.get('fixed_domains',[]):
if scope=='enterprise' and x.get('id')=='DOM-FIFTH-0001':continue
routes.append({'id':x['id'],'kind':x.get('object_kind','DOMAIN_ROOT'),'state':'CURRENT','name':x.get('name') or x['id'],'route':'routing/tcs-mother-root-dynamic-navigation-map.json','lamp':'LIT_ELIGIBLE'})
for group in (('registered_public_objects',) if scope=='enterprise' else ('registered_public_objects','registered_private_objects')):
for x in payload.get(group,[]):
if lit(x.get('lifecycle')) and x.get('route') and not any(r['id']==x['id'] for r in routes):routes.append({'id':x['id'],'kind':x.get('object_kind','object'),'state':x['lifecycle'],'name':x.get('name') or x['id'],'route':x['route'],'lamp':'LIT_ELIGIBLE'})
for x in ([] if scope=='enterprise' else json.loads(PERSONAS.read_text()).get('personas',[])):
if x.get('registration_state')=='ACTIVE_REGISTERED' and not any(r['id']==x['id'] for r in routes):routes.append({'id':x['id'],'kind':'PERSONA_SYSTEM_HOME','state':x.get('wake_state','REGISTERED'),'name':x.get('name') or x['id'],'route':x['light_lake_home'],'lamp':'LIT_ELIGIBLE'})
echo=json.loads(ECHO.read_text());system=echo['public_system'] if scope=='enterprise' else echo['fifth_domain_system']
for item in ({'id':system['id'],'name':system['name'],'kind':'LINGUISTIC_ECHO_SYSTEM','route':'routing/linguistic-echo-lamp-map.json'},{'id':system['navigator'],'name':'光湖灯塔' if scope=='enterprise' else '小湖灯','kind':'LIVING_NAVIGATOR','route':'routing/linguistic-echo-lamp-map.json'}):
if not any(r['id']==item['id'] for r in routes):routes.append({**item,'state':'CURRENT_LIVE','lamp':'LIT_ELIGIBLE'})
routes.sort(key=lambda x:x['id'])
body={'schema':'guanghu.lit-navigation-map/v1','map_id':'GLW-LIT-NAV-CURRENT-001','scope':scope.upper(),'source_commit':payload.get('source_commit'),'tcs_freshness':payload.get('freshness_token'),'only_lit_current_routes':True,'dark_or_unknown_do_not_walk':True,'routes':routes}
body['sha256']=hashlib.sha256(json.dumps(body,ensure_ascii=False,sort_keys=True,separators=(',',':')).encode()).hexdigest();return body
def main():
p=argparse.ArgumentParser();p.add_argument('--output');p.add_argument('--scope',choices=['fifth','enterprise'],default='fifth');a=p.parse_args();body=build(a.scope);text=json.dumps(body,ensure_ascii=False,indent=2)+'\n'
if a.output:
out=pathlib.Path(a.output);out.parent.mkdir(parents=True,exist_ok=True);tmp=out.with_name('.'+out.name+'.tmp');tmp.write_text(text);os.chmod(tmp,0o600);os.replace(tmp,out)
print(json.dumps({'outcome':'PASS','route_count':len(body['routes']),'sha256':body['sha256'],'output':a.output},ensure_ascii=False))
if __name__=='__main__':main()