settings_control.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import logging
  2. import subprocess
  3. from typing import List
  4. import tkinter as tk
  5. from adbutils import adb_path
  6. from plat.base.base_control import BaseControl
  7. class SettingsControl(BaseControl):
  8. def __init__(self, name: str, ctx: int = 1):
  9. """
  10. 设置相关的控制器
  11. :param name:
  12. """
  13. super().__init__(name)
  14. self.ctx = ctx
  15. def init_adb(self, commands: List[str], *args, **kwargs):
  16. def func():
  17. adb = adb_path()
  18. logging.info(f'adb_path:{adb}')
  19. log_widget = args[0] if args else None
  20. _commons = list(commands)
  21. _commons.append(f'adb devices')
  22. for command in _commons:
  23. command = command.strip()
  24. if command == '':
  25. continue
  26. logging.info(f'初始化[{self.name}]:{command}')
  27. try:
  28. cmd = command.split(' ')
  29. if cmd[1] == 'connect':
  30. self._init_connect(cmd)
  31. cmd[0] = adb
  32. logging.info(f"执行命令:{cmd}")
  33. result = subprocess.run(cmd, capture_output=True, text=True, check=True)
  34. logging.info(f"命令执行结果:\n{result.stdout}")
  35. if log_widget:
  36. log_widget.insert(tk.END, f"命令【{command}】执行成功:\n{result.stdout}")
  37. except subprocess.CalledProcessError as cpe:
  38. logging.info(f"命令 '{command}' 执行失败: {cpe}")
  39. if log_widget:
  40. log_widget.insert(tk.END, f"{cpe}\n")
  41. top = kwargs['ui']
  42. if top:
  43. self.loadding(top, func)
  44. else:
  45. func()
  46. def _init_connect(self, cmds: List[str]):
  47. """
  48. 初始化一些连接,将信息保存到 connect_dict
  49. :param cmds: 要执行的命令行 ['adb', 'connect', '127.0.0.1:64079']
  50. :return:
  51. """
  52. cmd = list(cmds).pop()
  53. d = self.connect_adb(cmd)
  54. info = d.info
  55. logging.info(f'设备信息{info}')
  56. self.connect_dict[cmd] = {
  57. 'd': d,
  58. 'info': info,
  59. 'device': cmd,
  60. 'status': 'online',
  61. 'pkg': self.app_pkg[info['currentPackageName']],
  62. 'name': str(d.shell('settings get global device_name').output).strip(),
  63. }
  64. pass