world_sync/scripts/load_path.py

68 lines
2.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""world_sync 模块 RBAC 路径注册脚本(load_path.py) 显式路径, 禁止通配符"""
import os
import sys
import subprocess
MOD = 'world_sync'
PATHS_LOGINED = [
f'/{MOD}/index.ui',
f'/{MOD}/world_sync_list.ui',
f'/{MOD}/world_sync_task_list.ui',
f'/{MOD}/world_sync_log_list.ui',
f'/{MOD}/api/world_sync_create.dspy',
f'/{MOD}/api/world_sync_update.dspy',
f'/{MOD}/api/world_sync_delete.dspy',
f'/{MOD}/api/world_sync_list.dspy',
f'/{MOD}/api/world_sync_task_create.dspy',
f'/{MOD}/api/world_sync_task_execute.dspy',
f'/{MOD}/api/world_sync_task_list.dspy',
f'/{MOD}/api/world_sync_log_list.dspy',
f'/{MOD}/api/world_sync_dict.dspy',
f'/{MOD}/api/world_sync_write_guard.dspy',
]
PATHS_ANY = []
def find_sage_root():
here = os.path.dirname(os.path.abspath(__file__))
for cand in (os.path.join(here, '..', '..', '..'),
os.path.expanduser('~/repos/sage'),
os.path.expanduser('~/sage')):
cand = os.path.abspath(cand)
if os.path.isdir(os.path.join(cand, 'wwwroot')) and os.path.isdir(os.path.join(cand, 'py3', 'bin')):
return cand
return None
def register(path, role):
sage = find_sage_root()
if not sage:
print(f'[world_sync] 未找到 Sage 根目录, 跳过 RBAC 注册(本地模块交付不阻塞)')
return
script = os.path.join(sage, 'load_path.py')
if os.path.exists(script):
print(f'[world_sync] 请在 {script} 中登记: {path} {role}')
return
set_role = os.path.join(sage, 'set_role_perm.py')
if os.path.exists(set_role):
try:
subprocess.run([sys.executable, set_role, path, role], check=False, cwd=sage)
print(f'[world_sync] 已注册: {path} {role}')
except Exception as e:
print(f'[world_sync] 注册失败 {path}: {e}')
def main():
for p in PATHS_LOGINED:
register(p, 'logined')
for p in PATHS_ANY:
register(p, 'any')
if __name__ == '__main__':
main()