- apk_source 表(schema v22):直链/包名/间隔/上次检查;随备份走 - 后台巡检每 5 分钟;先探 302 文件名,没变不重复下载 - 下载后解析包名/版本,比现有最新版新才入库 - 安装设备接口 ?package=… 并行读 dumpsys package,弹窗显示当前版本/未安装/已是最新 - 新增 test_apk_source 用例;API/DEVELOPMENT/CHANGES 文档同步
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""应用管理·自动下载:文件名解析 + 设备已安装版本解析(假 adb,不连设备)。"""
|
||
import io
|
||
import os
|
||
import sys
|
||
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||
os.chdir(sys.path[0])
|
||
|
||
from core.apk_manager import _apk_filename # noqa: E402
|
||
from web import apks_api # noqa: E402
|
||
|
||
fails = []
|
||
|
||
|
||
def check(label, got, want):
|
||
ok = got == want
|
||
if not ok:
|
||
fails.append(label)
|
||
print((' ✅ ' if ok else ' ❌ ') + label + ('' if ok else '\n 得到 %r\n 期望 %r' % (got, want)))
|
||
|
||
|
||
print('=== 1. APK 文件名解析(重定向 URL / Content-Disposition)===')
|
||
check('URL 带查询串',
|
||
_apk_filename('https://cdn.example.com/path/aweme_v1.apk?v=123'),
|
||
'aweme_v1.apk')
|
||
check('优先 Content-Disposition',
|
||
_apk_filename('https://cdn.example.com/download?id=1',
|
||
{'Content-Disposition': 'attachment;filename="aweme_v2.apk"'}),
|
||
'aweme_v2.apk')
|
||
check('没有文件名返回空', _apk_filename('https://cdn.example.com/'), '')
|
||
|
||
print('=== 2. 设备已安装版本(dumpsys package)===')
|
||
_orig = apks_api._adb
|
||
|
||
|
||
def _fake_installed(*args, **kw):
|
||
return ("Packages:\n Package [com.ss.android.ugc.aweme] (abc):\n"
|
||
" versionCode=400801 minSdk=29\n"
|
||
" versionName=40.8.1\n")
|
||
|
||
|
||
def _fake_missing(*args, **kw):
|
||
return "Unable to find package: com.ss.android.ugc.aweme"
|
||
|
||
|
||
try:
|
||
apks_api._adb = _fake_installed
|
||
r = apks_api._installed_version('192.168.20.65:5555', 'com.ss.android.ugc.aweme')
|
||
check('已安装', r['installed'], True)
|
||
check('版本名', r['installed_version'], '40.8.1')
|
||
check('版本码', r['installed_version_code'], 400801)
|
||
|
||
apks_api._adb = _fake_missing
|
||
r = apks_api._installed_version('192.168.20.65:5555', 'com.ss.android.ugc.aweme')
|
||
check('未安装', r['installed'], False)
|
||
check('未安装版本为空', (r['installed_version'], r['installed_version_code']), ('', 0))
|
||
finally:
|
||
apks_api._adb = _orig
|
||
|
||
print()
|
||
if fails:
|
||
print('❌ 失败 %d 项:%s' % (len(fails), ' / '.join(fails)))
|
||
sys.exit(1)
|
||
print('✅ test_apk_source 全部通过(纯解析,没连设备)')
|