| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import logging
- import subprocess
- from typing import List
- import tkinter as tk
- from adbutils import adb_path
- from plat.base.base_control import BaseControl
- class SettingsControl(BaseControl):
- def __init__(self, name: str, ctx: int = 1):
- """
- 设置相关的控制器
- :param name:
- """
- super().__init__(name)
- self.ctx = ctx
- def init_adb(self, commands: List[str], *args, **kwargs):
- def func():
- adb = adb_path()
- logging.info(f'adb_path:{adb}')
- log_widget = args[0] if args else None
- _commons = list(commands)
- _commons.append(f'adb devices')
- for command in _commons:
- command = command.strip()
- if command == '':
- continue
- logging.info(f'初始化[{self.name}]:{command}')
- try:
- cmd = command.split(' ')
- if cmd[1] == 'connect':
- self._init_connect(cmd)
- cmd[0] = adb
- logging.info(f"执行命令:{cmd}")
- result = subprocess.run(cmd, capture_output=True, text=True, check=True)
- logging.info(f"命令执行结果:\n{result.stdout}")
- if log_widget:
- log_widget.insert(tk.END, f"命令【{command}】执行成功:\n{result.stdout}")
- except subprocess.CalledProcessError as cpe:
- logging.info(f"命令 '{command}' 执行失败: {cpe}")
- if log_widget:
- log_widget.insert(tk.END, f"{cpe}\n")
- top = kwargs['ui']
- if top:
- self.loadding(top, func)
- else:
- func()
- def _init_connect(self, cmds: List[str]):
- """
- 初始化一些连接,将信息保存到 connect_dict
- :param cmds: 要执行的命令行 ['adb', 'connect', '127.0.0.1:64079']
- :return:
- """
- cmd = list(cmds).pop()
- d = self.connect_adb(cmd)
- info = d.info
- logging.info(f'设备信息{info}')
- self.connect_dict[cmd] = {
- 'd': d,
- 'info': info,
- 'device': cmd,
- 'status': 'online',
- 'pkg': self.app_pkg[info['currentPackageName']],
- 'name': str(d.shell('settings get global device_name').output).strip(),
- }
- pass
|