feat: 工具Tab新增应用版本管理(按包名并发查询所有设备安装版本)+ 文档同步更新(README/API/ARCHITECTURE/DEPLOY:工具Tab各工具、条件判断if_el+OCR、STF设备管理、.env密钥配置)
This commit is contained in:
+74
-9
@@ -23,7 +23,7 @@ import shlex
|
||||
import re
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FuturesTimeout
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError as FuturesTimeout
|
||||
|
||||
from flask import (Flask, jsonify, request, redirect, url_for, render_template,
|
||||
Response, make_response, session)
|
||||
@@ -880,13 +880,10 @@ def _blocked_adb_cmd(cmd):
|
||||
return any(p in low for p in _ADB_BLOCKED_PATTERNS)
|
||||
|
||||
|
||||
@app.route("/api/adb/devices")
|
||||
@admin_required
|
||||
def api_adb_devices():
|
||||
"""维护终端设备列表(仅管理员):本地 adb 已连接 + STF 在线设备池。
|
||||
def _merged_device_list():
|
||||
"""本地 adb(含 USB)+ STF 在线设备池的合并列表([{serial, state}])。
|
||||
|
||||
供终端设备选择器使用——选中后自动附加 `-s <serial>`,
|
||||
离线/未连接的设备也可选,配合"重连设备"按钮恢复。
|
||||
供维护终端/剪贴板注入/应用版本管理等设备选择场景共用。
|
||||
"""
|
||||
states = {}
|
||||
out = _adb("devices")
|
||||
@@ -901,8 +898,18 @@ def api_adb_devices():
|
||||
states[serial] = "stf"
|
||||
except Exception:
|
||||
pass
|
||||
devices = [{"serial": s, "state": st} for s, st in sorted(states.items())]
|
||||
return jsonify({"ok": True, "devices": devices})
|
||||
return [{"serial": s, "state": st} for s, st in sorted(states.items())]
|
||||
|
||||
|
||||
@app.route("/api/adb/devices")
|
||||
@admin_required
|
||||
def api_adb_devices():
|
||||
"""维护终端设备列表(仅管理员):本地 adb 已连接 + STF 在线设备池。
|
||||
|
||||
供终端设备选择器使用——选中后自动附加 `-s <serial>`,
|
||||
离线/未连接的设备也可选,配合"重连设备"按钮恢复。
|
||||
"""
|
||||
return jsonify({"ok": True, "devices": _merged_device_list()})
|
||||
|
||||
|
||||
@app.route("/api/adb/cmd", methods=["POST"])
|
||||
@@ -1032,6 +1039,64 @@ def api_stf_devmgmt_remove():
|
||||
return jsonify({"ok": False, "error": str(e)}), 502
|
||||
|
||||
|
||||
def _app_ver_on_device(serial, pkg):
|
||||
"""单设备包版本查询:pm path 检查安装 → dumpsys 取 versionName/versionCode。
|
||||
|
||||
IP:port 设备先 adb connect(已连接自动跳过;绝不 disconnect,红线)。
|
||||
查询是只读 shell 命令,无需 adb server 锁(锁只保护 connect/kill-server 类操作)。
|
||||
"""
|
||||
if ":" in serial:
|
||||
try:
|
||||
adb_connect(serial)
|
||||
except Exception as e:
|
||||
return {"installed": False, "error": f"adb 连接失败: {e}"}
|
||||
try:
|
||||
r = subprocess.run([ADB_PATH, "-s", serial, "shell", "pm", "path", pkg],
|
||||
capture_output=True, timeout=15)
|
||||
if b"package:" not in (r.stdout or b""):
|
||||
return {"installed": False, "version_name": "", "version_code": ""}
|
||||
r2 = subprocess.run([ADB_PATH, "-s", serial, "shell", "dumpsys", "package", pkg],
|
||||
capture_output=True, timeout=20)
|
||||
out2 = (r2.stdout or b"").decode("utf-8", errors="replace")
|
||||
vm = re.search(r"versionName=(\S+)", out2)
|
||||
vc = re.search(r"versionCode=(\d+)", out2)
|
||||
return {"installed": True,
|
||||
"version_name": vm.group(1) if vm else "",
|
||||
"version_code": vc.group(1) if vc else ""}
|
||||
except subprocess.TimeoutExpired:
|
||||
return {"installed": False, "error": "查询超时"}
|
||||
except Exception as e:
|
||||
return {"installed": False, "error": str(e)}
|
||||
|
||||
|
||||
@app.route("/api/tools/appver", methods=["POST"])
|
||||
@admin_required
|
||||
def api_tools_appver():
|
||||
"""工具-应用版本管理:查询所有设备上指定包名的安装情况与版本号。
|
||||
|
||||
请求: {"pkg": "com.ss.android.ugc.aweme"}
|
||||
设备来源:本地 adb(含 USB)+ STF 在线池,并发查询(最多 10 台同时)。
|
||||
"""
|
||||
pkg = (request.json or {}).get("pkg", "").strip()
|
||||
if not re.match(r"^[A-Za-z0-9_.]+$", pkg or ""):
|
||||
return jsonify({"ok": False, "error": "包名格式不正确(仅字母/数字/._)"}), 400
|
||||
devices = _merged_device_list()
|
||||
if not devices:
|
||||
return jsonify({"ok": False, "error": "无可用设备"}), 404
|
||||
results = {}
|
||||
with ThreadPoolExecutor(max_workers=min(10, len(devices))) as pool:
|
||||
futures = {pool.submit(_app_ver_on_device, d["serial"], pkg): d for d in devices}
|
||||
for fut in as_completed(futures, timeout=90):
|
||||
d = futures[fut]
|
||||
try:
|
||||
results[d["serial"]] = fut.result()
|
||||
except Exception as e:
|
||||
results[d["serial"]] = {"installed": False, "error": str(e)}
|
||||
fail = sum(1 for v in results.values() if v.get("error"))
|
||||
return jsonify({"ok": True, "results": results, "fail": fail,
|
||||
"total": len(results)})
|
||||
|
||||
|
||||
@app.route("/api/stf/restart", methods=["POST"])
|
||||
@admin_required
|
||||
def api_stf_restart():
|
||||
|
||||
Reference in New Issue
Block a user