supplychain/scripts/load_path.py
ymq 3a93804655 perf(load_path): 批量直写 DB,避免 159 次 subprocess 启动
原 subprocess.run(set_role_perm.py) 每个路径起一个 python 进程,159 路径太慢。
改为一次连接批量注册 permission + rolepermission。
2026-08-26 16:42:51 +08:00

209 lines
11 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""RBAC path registration for supplychain module (pipeline format).
从应用根执行py3/bin/python pkgs/supplychain/scripts/load_path.py
load_path.sh 自动扫描 pkgs/*/scripts/load_path.py 并调用本脚本)
批量直写 DB避免逐个 subprocess 启动 set_role_perm.py159 路径)。
"""
import sys
import os
import asyncio
# 应用根:本脚本在 pkgs/supplychain/scripts/ 下,上溯三级
APP_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../.."))
sys.path.insert(0, os.path.join(APP_ROOT, "py3", "lib", "python3.10", "site-packages"))
sys.path.insert(0, APP_ROOT)
from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig
from appPublic.uniqueID import getID
PATHS_ANY = [
"/supplychain/menu.ui",
]
PATHS_LOGINED = [
"/supplychain",
"/supplychain/accounting.ui",
"/supplychain/add_provider_recharge.dspy",
"/supplychain/add_provider_settlement.dspy",
"/supplychain/api/calculate_accounting.dspy",
"/supplychain/api/contract_attach.ui",
"/supplychain/api/contract_attach_delete.dspy",
"/supplychain/api/contract_attach_list.dspy",
"/supplychain/api/contract_upload.dspy",
"/supplychain/api/distribution_agreement_items_create.dspy",
"/supplychain/api/distribution_agreement_items_delete.dspy",
"/supplychain/api/distribution_agreement_items_update.dspy",
"/supplychain/api/distribution_agreements_create.dspy",
"/supplychain/api/distribution_agreements_delete.dspy",
"/supplychain/api/distribution_agreements_update.dspy",
"/supplychain/api/get_search_agreement_id.dspy",
"/supplychain/api/get_search_contract_id.dspy",
"/supplychain/api/get_search_orgid.dspy",
"/supplychain/api/get_search_prodtypeid.dspy",
"/supplychain/api/get_search_productid.dspy",
"/supplychain/api/get_search_sub_distributor_id.dspy",
"/supplychain/api/get_search_sub_reseller_id.dspy",
"/supplychain/api/get_search_supplier_id.dspy",
"/supplychain/api/get_search_supply_relation_id.dspy",
"/supplychain/api/import_supplier_product.dspy",
"/supplychain/api/platform_supply_products_create.dspy",
"/supplychain/api/platform_supply_products_delete.dspy",
"/supplychain/api/platform_supply_products_update.dspy",
"/supplychain/api/platform_supply_relations_create.dspy",
"/supplychain/api/platform_supply_relations_delete.dspy",
"/supplychain/api/platform_supply_relations_update.dspy",
"/supplychain/api/product_supplier_mapping_create.dspy",
"/supplychain/api/product_supplier_mapping_delete.dspy",
"/supplychain/api/product_supplier_mapping_update.dspy",
"/supplychain/api/query_dist_discount.dspy",
"/supplychain/api/query_platform_products.dspy",
"/supplychain/api/query_platform_suppliers.dspy",
"/supplychain/api/query_supply_discount.dspy",
"/supplychain/api/sales_ledger_create.dspy",
"/supplychain/api/sales_ledger_delete.dspy",
"/supplychain/api/sales_ledger_update.dspy",
"/supplychain/api/sub_distributors_create.dspy",
"/supplychain/api/sub_distributors_delete.dspy",
"/supplychain/api/sub_distributors_update.dspy",
"/supplychain/api/sub_resellers_create.dspy",
"/supplychain/api/sub_resellers_delete.dspy",
"/supplychain/api/sub_resellers_update.dspy",
"/supplychain/api/supplier_resource_price_create.dspy",
"/supplychain/api/supplier_resource_price_delete.dspy",
"/supplychain/api/supplier_resource_price_update.dspy",
"/supplychain/api/suppliers_create.dspy",
"/supplychain/api/suppliers_delete.dspy",
"/supplychain/api/suppliers_update.dspy",
"/supplychain/api/supply_contract_items_create.dspy",
"/supplychain/api/supply_contract_items_delete.dspy",
"/supplychain/api/supply_contract_items_update.dspy",
"/supplychain/api/supply_contracts_create.dspy",
"/supplychain/api/supply_contracts_delete.dspy",
"/supplychain/api/supply_contracts_update.dspy",
"/supplychain/api/supplychain_accounting_create.dspy",
"/supplychain/api/supplychain_accounting_delete.dspy",
"/supplychain/api/supplychain_accounting_update.dspy",
"/supplychain/distribution_agreement_items_list",
"/supplychain/distribution_agreement_items_list/add_distribution_agreement_items.dspy",
"/supplychain/distribution_agreement_items_list/delete_distribution_agreement_items.dspy",
"/supplychain/distribution_agreement_items_list/get_distribution_agreement_items.dspy",
"/supplychain/distribution_agreement_items_list/index.ui",
"/supplychain/distribution_agreement_items_list/update_distribution_agreement_items.dspy",
"/supplychain/distribution_agreements.ui",
"/supplychain/distribution_agreements_list",
"/supplychain/distribution_agreements_list/add_distribution_agreements.dspy",
"/supplychain/distribution_agreements_list/delete_distribution_agreements.dspy",
"/supplychain/distribution_agreements_list/get_distribution_agreements.dspy",
"/supplychain/distribution_agreements_list/index.ui",
"/supplychain/distribution_agreements_list/update_distribution_agreements.dspy",
"/supplychain/get_provider_detail_reconcile.dspy",
"/supplychain/get_provider_recharge.dspy",
"/supplychain/get_provider_reconcile.dspy",
"/supplychain/get_provider_settlement.dspy",
"/supplychain/get_reseller_detail_reconcile.dspy",
"/supplychain/get_reseller_reconcile.dspy",
"/supplychain/get_reseller_settlement.dspy",
"/supplychain/index.ui",
"/supplychain/platform_supply_products_list",
"/supplychain/platform_supply_products_list/add_platform_supply_products.dspy",
"/supplychain/platform_supply_products_list/delete_platform_supply_products.dspy",
"/supplychain/platform_supply_products_list/get_platform_supply_products.dspy",
"/supplychain/platform_supply_products_list/index.ui",
"/supplychain/platform_supply_products_list/update_platform_supply_products.dspy",
"/supplychain/platform_supply_relations_list",
"/supplychain/platform_supply_relations_list/add_platform_supply_relations.dspy",
"/supplychain/platform_supply_relations_list/delete_platform_supply_relations.dspy",
"/supplychain/platform_supply_relations_list/get_platform_supply_relations.dspy",
"/supplychain/platform_supply_relations_list/index.ui",
"/supplychain/platform_supply_relations_list/update_platform_supply_relations.dspy",
"/supplychain/product_supplier_mapping_list",
"/supplychain/product_supplier_mapping_list/add_product_supplier_mapping.dspy",
"/supplychain/product_supplier_mapping_list/delete_product_supplier_mapping.dspy",
"/supplychain/product_supplier_mapping_list/get_product_supplier_mapping.dspy",
"/supplychain/product_supplier_mapping_list/index.ui",
"/supplychain/product_supplier_mapping_list/update_product_supplier_mapping.dspy",
"/supplychain/provider_detail_reconcile.ui",
"/supplychain/provider_recharge.ui",
"/supplychain/provider_reconcile.ui",
"/supplychain/provider_settlement.ui",
"/supplychain/reseller_detail_reconcile.ui",
"/supplychain/reseller_reconcile.ui",
"/supplychain/reseller_settlement.ui",
"/supplychain/sales_ledger_list",
"/supplychain/sales_ledger_list/add_sales_ledger.dspy",
"/supplychain/sales_ledger_list/delete_sales_ledger.dspy",
"/supplychain/sales_ledger_list/get_sales_ledger.dspy",
"/supplychain/sales_ledger_list/index.ui",
"/supplychain/sales_ledger_list/update_sales_ledger.dspy",
"/supplychain/sub_distributors.ui",
"/supplychain/sub_distributors_list",
"/supplychain/sub_distributors_list/add_sub_distributors.dspy",
"/supplychain/sub_distributors_list/delete_sub_distributors.dspy",
"/supplychain/sub_distributors_list/get_sub_distributors.dspy",
"/supplychain/sub_distributors_list/index.ui",
"/supplychain/sub_distributors_list/update_sub_distributors.dspy",
"/supplychain/sub_resellers_list",
"/supplychain/sub_resellers_list/add_sub_resellers.dspy",
"/supplychain/sub_resellers_list/delete_sub_resellers.dspy",
"/supplychain/sub_resellers_list/get_sub_resellers.dspy",
"/supplychain/sub_resellers_list/index.ui",
"/supplychain/sub_resellers_list/update_sub_resellers.dspy",
"/supplychain/supplier_resource_price_list",
"/supplychain/supplier_resource_price_list/index.ui",
"/supplychain/suppliers.ui",
"/supplychain/suppliers_list",
"/supplychain/suppliers_list/add_suppliers.dspy",
"/supplychain/suppliers_list/delete_suppliers.dspy",
"/supplychain/suppliers_list/get_suppliers.dspy",
"/supplychain/suppliers_list/index.ui",
"/supplychain/suppliers_list/update_suppliers.dspy",
"/supplychain/supply_contract_items_list",
"/supplychain/supply_contract_items_list/add_supply_contract_items.dspy",
"/supplychain/supply_contract_items_list/delete_supply_contract_items.dspy",
"/supplychain/supply_contract_items_list/get_supply_contract_items.dspy",
"/supplychain/supply_contract_items_list/index.ui",
"/supplychain/supply_contract_items_list/update_supply_contract_items.dspy",
"/supplychain/supply_contracts.ui",
"/supplychain/supply_contracts_list",
"/supplychain/supply_contracts_list/add_supply_contracts.dspy",
"/supplychain/supply_contracts_list/delete_supply_contracts.dspy",
"/supplychain/supply_contracts_list/get_supply_contracts.dspy",
"/supplychain/supply_contracts_list/index.ui",
"/supplychain/supply_contracts_list/update_supply_contracts.dspy",
"/supplychain/supplychain_accounting_list",
"/supplychain/supplychain_accounting_list/add_supplychain_accounting.dspy",
"/supplychain/supplychain_accounting_list/delete_supplychain_accounting.dspy",
"/supplychain/supplychain_accounting_list/get_supplychain_accounting.dspy",
"/supplychain/supplychain_accounting_list/index.ui",
"/supplychain/supplychain_accounting_list/update_supplychain_accounting.dspy",
"/supplychain/update_provider_recharge.dspy",
"/supplychain/update_provider_settlement.dspy",
"/supplychain/update_reseller_settlement.dspy",
]
async def main():
config = getConfig(APP_ROOT, NS={"workdir": APP_ROOT})
DBPools(config.databases)
cnt = 0
async with DBPools().sqlorContext("pipeline") as sor:
for role, paths in (("any", PATHS_ANY), ("logined", PATHS_LOGINED)):
for path in paths:
recs = await sor.R("permission", {"path": path})
if recs:
permid = recs[0].id
else:
permid = getID()
await sor.C("permission", {"id": permid, "path": path})
rp = await sor.R("rolepermission", {"roleid": role, "permid": permid})
if not rp:
await sor.C("rolepermission", {"id": getID(), "roleid": role, "permid": permid})
cnt += 1
print(f"Done. any={len(PATHS_ANY)} logined={len(PATHS_LOGINED)} total={cnt}")
if __name__ == "__main__":
asyncio.run(main())