#!/usr/bin/env python3 # -*- coding: utf-8 -*- """runtime 模块 RBAC 路径注册脚本(scripts/load_path.py)。 模块目录结构硬性要求(review-develop 第一章):每个业务模块必须携带本脚本。 本脚本将 /runtime/play.html、/runtime/api/*.dspy、index.ui 等全部新路径注册到 宿主 RBAC(角色分层:any=静态资源免登录;logined=登录即可), 并同步提示写入中央 load_path.py(双入口,防 per-module 脚本静默失败)。 禁止通配符(% / *):每条路径显式列出。 """ import os import subprocess import sys MOD = 'runtime' # ---- 角色分层路径清单(新增页面/API 必须同步维护此处) ---- # any:免登录静态资源(JS/CSS;页面内由 ahserver 自动服务) PATHS_ANY = [ '/runtime/runtime.js', ] # logined:登录即可访问的页面与 API PATHS_LOGINED = [ '/runtime', '/runtime/index.ui', '/runtime/play.html', '/runtime/api/load_world.dspy', '/runtime/api/runtime_status.dspy', '/runtime/api/runtime_event.dspy', '/runtime/api/runtime_control.dspy', ] CENTRAL_HINT = ( '请同时在宿主中央 load_path.py 中登记(双入口兜底):\n' + '\n'.join(' "' + p + ' logined"' if p not in PATHS_ANY else ' "' + p + ' any"' for p in PATHS_LOGINED + PATHS_ANY) ) def find_sage_root(): """自动查找宿主应用根(含 wwwroot 目录即可)。""" here = os.path.dirname(os.path.abspath(__file__)) candidates = [ os.path.expanduser('~/repos/sage'), os.path.expanduser('~/sage'), '/d/scense/scense_app', ] for up in ('../..', '../../..', '../../../..'): candidates.append(os.path.normpath(os.path.join(here, up))) for c in candidates: if os.path.isdir(os.path.join(c, 'wwwroot')): return c return None def set_perm(sage_root, path, role): """调用宿主 set_role_perm.py 注册单条路径(失败不中断,返回 False)。""" script = os.path.join(sage_root, 'set_role_perm.py') py = os.path.join(sage_root, 'py3', 'bin', 'python') if not os.path.exists(script): print('[load_path] WARN set_role_perm.py not found at ' + script) return False cmd = [py, script, path, role] try: r = subprocess.run(cmd, capture_output=True, text=True, timeout=120) print('[load_path] ' + path + ' ' + role + ' -> rc=' + str(r.returncode)) if r.stdout: print(r.stdout[-500:]) if r.stderr: print(r.stderr[-500:]) return r.returncode == 0 except Exception as exc: print('[load_path] FAIL ' + path + ': ' + str(exc)) return False def main(): sage_root = find_sage_root() if not sage_root: print('[load_path] ERROR: sage/host root not found; register manually.') print(CENTRAL_HINT) sys.exit(2) print('[load_path] host root: ' + sage_root) ok = True for p in PATHS_ANY: ok = set_perm(sage_root, p, 'any') and ok for p in PATHS_LOGINED: ok = set_perm(sage_root, p, 'logined') and ok print(CENTRAL_HINT) sys.exit(0 if ok else 1) if __name__ == '__main__': main()