Download scripts/pending.py from zngw/openclaw: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/spaces/zngw/openclaw/resolve/main/scripts/pending.py
- Command line
-
hf download hf://spaces/zngw/openclaw/scripts/pending.py
-
curl -L -o pending.py https://huggingface.co/spaces/zngw/openclaw/resolve/main/scripts/pending.py
1.93 kB
| #!/usr/bin/env python3 | |
| import subprocess | |
| import json | |
| import sys | |
| import time | |
| def run_command(cmd): | |
| try: | |
| result = subprocess.run( | |
| cmd, | |
| shell=True, | |
| capture_output=True, | |
| text=True, | |
| check=True, | |
| timeout=10 # 增加超时防止卡死 | |
| ) | |
| return result.stdout.strip() | |
| except subprocess.CalledProcessError as e: | |
| # 如果是非零退出码,可能是没有 pending 请求,静默处理 | |
| return None | |
| except subprocess.TimeoutExpired: | |
| print("--- [Pending] 命令执行超时 ---") | |
| return None | |
| def main(): | |
| print("--- [Pending]: 检查设备待批准列表...") | |
| json_output = run_command("openclaw devices list --json") | |
| if not json_output: | |
| # 可能是还没初始化,或者命令本身不需要输出 | |
| sys.exit(0) | |
| try: | |
| data = json.loads(json_output) | |
| except json.JSONDecodeError: | |
| # 输出不是有效的 JSON,忽略 | |
| sys.exit(0) | |
| pending_list = data.get("pending", []) | |
| if not isinstance(pending_list, list): | |
| sys.exit(0) | |
| request_ids = [] | |
| # 逻辑优化:统一遍历逻辑,不再硬编码索引切片,除非数据结构非常特殊 | |
| # 根据原逻辑,似乎是从索引 3 开始或者是字典类型 | |
| for item in pending_list: | |
| if isinstance(item, dict) and "requestId" in item: | |
| request_ids.append(item["requestId"]) | |
| if not request_ids: | |
| sys.exit(0) | |
| print(f"--- [Pending] 发现 {len(request_ids)} 个待批准请求 ---") | |
| for req_id in request_ids: | |
| cmd = f"openclaw devices approve {req_id}" | |
| print(f"--- [Pending] 正在批准: {req_id} ---") | |
| subprocess.run(cmd, shell=True, capture_output=True, text=True) | |
| print("--- [Pending] 所有请求处理完毕 ---") | |
| if __name__ == "__main__": | |
| main() |