patch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import re
  3. from deploy.logger import logger
  4. def patch_trust_env(file):
  5. """
  6. People use proxies, but they never realize that proxy software leaves a
  7. global proxy pointing to itself even when the software is not running.
  8. In most situations we set `session.trust_env = False` in requests, but this
  9. does not effect the `pip` command.
  10. To handle untrusted user environment for good. We patch the code file in
  11. requests directly. Of course, the patch only effect the python env inside
  12. Alas.
  13. Returns:
  14. bool: If patched.
  15. """
  16. if os.path.exists(file):
  17. with open(file, 'r', encoding='utf-8') as f:
  18. content = f.read()
  19. if re.search('self.trust_env = True', content):
  20. content = re.sub('self.trust_env = True', 'self.trust_env = False', content)
  21. with open(file, 'w', encoding='utf-8') as f:
  22. f.write(content)
  23. logger.info(f'{file} trust_env patched')
  24. elif re.search('self.trust_env = False', content):
  25. logger.info(f'{file} trust_env already patched')
  26. else:
  27. logger.info(f'{file} trust_env not found')
  28. else:
  29. logger.info(f'{file} trust_env no need to patch')
  30. def check_running_directory():
  31. """
  32. An fool-proof mechanism.
  33. Show error if user is running Easy Install in compressing software,
  34. since Alas can't install in temp directories.
  35. """
  36. file = __file__.replace(r"\\", "/").replace("\\", "/")
  37. # C:/Users/<user>/AppData/Local/Temp/360zip$temp/360$3/AzurLaneAutoScript
  38. if 'Temp/360zip' in file:
  39. logger.critical('请先解压Oas的压缩包,再安装Oas')
  40. exit(1)
  41. # C:/Users/<user>/AppData/Local/Temp/Rar$EXa9248.23428/AzurLaneAutoScript
  42. if 'Temp/Rar' in file or 'Local/Temp' in file:
  43. logger.critical('Please unzip ALAS installer first')
  44. exit(1)
  45. def patch_uiautomator2():
  46. """
  47. uiautomator2 download assets from https://tool.appetizer.io first then fallback to https://github.com/openatx.
  48. https://tool.appetizer.io is added to bypass the wall in China but https://tool.appetizer.io is slow outside of CN
  49. plus some CN users cannot access it for unknown reason.
  50. So we patch `uiautomator2/init.py` to a local assets cache `uiautomator2cache/cache`.
  51. appdir = os.path.join(os.path.expanduser('~'), '.uiautomator2')
  52. to:
  53. appdir = os.path.abspath(os.path.join(__file__, '../../uiautomator2cache'))
  54. And we also remove minicap installations since emulators doesn't need it.
  55. for url in self.minicap_urls:
  56. self.push_url(url)
  57. to:
  58. for url in []:
  59. self.push_url(url)
  60. """
  61. cache_dir = './toolkit/Lib/site-packages/uiautomator2cache/cache'
  62. init_file = './toolkit/Lib/site-packages/uiautomator2/init.py'
  63. appdir = "os.path.abspath(os.path.join(__file__, '../../uiautomator2cache'))"
  64. if not os.path.exists(init_file):
  65. logger.info('uiautomator2 is not installed skip patching')
  66. return
  67. modified = False
  68. with open(init_file, 'r', encoding='utf-8') as f:
  69. content = f.read()
  70. # Patch minicap_urls
  71. res = re.search(r'self.minicap_urls', content)
  72. if res:
  73. content = re.sub(r'self.minicap_urls', '[]', content)
  74. modified = True
  75. logger.info(f'{init_file} minicap_urls patched')
  76. else:
  77. logger.info(f'{init_file} minicap_urls no need to patch')
  78. # Patch appdir
  79. if os.path.exists(cache_dir):
  80. res = re.search(r'appdir ?=(.*)\n', content)
  81. if res:
  82. prev = res.group(1).strip()
  83. if prev == appdir:
  84. logger.info(f'{init_file} appdir already patched')
  85. else:
  86. content = re.sub(r'appdir ?=.*\n', f'appdir = {appdir}\n', content)
  87. modified = True
  88. logger.info(f'{init_file} appdir patched')
  89. else:
  90. logger.info(f'{init_file} appdir not found')
  91. else:
  92. logger.info('uiautomator2cache is not installed skip patching')
  93. # Save file
  94. if modified:
  95. with open(init_file, 'w', encoding='utf-8') as f:
  96. f.write(content)
  97. logger.info(f'{init_file} content saved')
  98. def pre_checks():
  99. check_running_directory()
  100. # patch_trust_env
  101. patch_trust_env('./toolkit/Lib/site-packages/requests/sessions.py')
  102. patch_trust_env('./toolkit/Lib/site-packages/pip/_vendor/requests/sessions.py')
  103. patch_uiautomator2()
  104. if __name__ == '__main__':
  105. pre_checks()