adb.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # This Python file uses the following encoding: utf-8
  2. # copy from alas https://github.com/LmeSzinc/AzurLaneAutoScript
  3. import logging
  4. from deploy.config import DeployConfig
  5. from deploy.emulator import EmulatorConnect
  6. from deploy.logger import logger
  7. from deploy.utils import *
  8. def show_fix_tip(module):
  9. logger.info(f"""
  10. To fix this:
  11. 1. Open console.bat
  12. 2. Execute the following commands:
  13. pip uninstall -y {module}
  14. pip install --no-cache-dir {module}
  15. 3. Re-open Alas.exe
  16. """)
  17. class AdbManager(DeployConfig):
  18. @cached_property
  19. def adb(self):
  20. return self.filepath('AdbExecutable')
  21. def adb_install(self):
  22. logger.hr('Start ADB service', 0)
  23. emulator = EmulatorConnect(adb=self.adb)
  24. if self.ReplaceAdb:
  25. logger.hr('Replace ADB', 1)
  26. emulator.adb_replace()
  27. elif self.AutoConnect:
  28. logger.hr('ADB Connect', 1)
  29. emulator.brute_force_connect()
  30. if self.InstallUiautomator2:
  31. logger.hr('Uiautomator2 Init', 1)
  32. try:
  33. import adbutils
  34. from uiautomator2 import init
  35. except ModuleNotFoundError as e:
  36. message = str(e)
  37. for module in ['apkutils2', 'progress']:
  38. # ModuleNotFoundError: No module named 'apkutils2'
  39. # ModuleNotFoundError: No module named 'progress.bar'
  40. if module in message:
  41. show_fix_tip(module)
  42. exit(1)
  43. raise
  44. # Remove global proxies, or uiautomator2 will go through it
  45. for k in list(os.environ.keys()):
  46. if k.lower().endswith('_proxy'):
  47. del os.environ[k]
  48. for device in adbutils.adb.iter_device():
  49. initer = init.Initer(device, loglevel=logging.DEBUG)
  50. # MuMu X has no ro.product.cpu.abi, pick abi from ro.product.cpu.abilist
  51. if initer.abi not in ['x86_64', 'x86', 'arm64-v8a', 'armeabi-v7a', 'armeabi']:
  52. initer.abi = initer.abis[0]
  53. initer.set_atx_agent_addr('127.0.0.1:7912')
  54. for _ in range(2):
  55. try:
  56. initer.install()
  57. break
  58. except AssertionError:
  59. logger.info(f'AssertionError when installing uiautomator2 on device {device.serial}')
  60. logger.info('If you are using BlueStacks or LD player or WSA, '
  61. 'please enable ADB in the settings of your emulator')
  62. exit(1)
  63. except ConnectionError:
  64. if _ == 1:
  65. raise
  66. init.GITHUB_BASEURL = 'http://tool.appetizer.io/openatx'
  67. initer._device.shell(["rm", "/data/local/tmp/minicap"])
  68. initer._device.shell(["rm", "/data/local/tmp/minicap.so"])