config.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # This Python file uses the following encoding: utf-8
  2. # copy from alas https://github.com/LmeSzinc/AzurLaneAutoScript
  3. import copy
  4. from typing import Optional, Union
  5. from deploy.logger import logger
  6. from deploy.utils import *
  7. class ExecutionError(Exception):
  8. pass
  9. class ConfigModel:
  10. # Git
  11. Repository: str = "https://gitcode.com/OnmyojiAutoScript/OnmyojiAutoScript.git"
  12. Branch: str = "master"
  13. GitExecutable: str = "./toolkit/Git/mingw64/bin/git.exe"
  14. GitProxy: Optional[str] = None
  15. SSLVerify: bool = False
  16. AutoUpdate: bool = True
  17. KeepLocalChanges: bool = False
  18. # Python
  19. PythonExecutable: str = "./toolkit/python.exe"
  20. PypiMirror: Optional[str] = None
  21. InstallDependencies: bool = True
  22. RequirementsFile: str = "requirements.txt"
  23. # Adb
  24. AdbExecutable: str = "./toolkit/Lib/site-packages/adbutils/binaries/adb.exe"
  25. ReplaceAdb: bool = True
  26. AutoConnect: bool = True
  27. InstallUiautomator2: bool = True
  28. # Ocr
  29. UseOcrServer: bool = False
  30. StartOcrServer: bool = False
  31. OcrServerPort: int = 22268
  32. OcrClientAddress: str = "127.0.0.1:22268"
  33. # Update
  34. EnableReload: bool = True
  35. CheckUpdateInterval: int = 5
  36. AutoRestartTime: str = "03:50"
  37. # Misc
  38. DiscordRichPresence: bool = False
  39. # Remote Access
  40. EnableRemoteAccess: bool = False
  41. SSHUser: Optional[str] = None
  42. SSHServer: Optional[str] = None
  43. SSHExecutable: Optional[str] = None
  44. # Webui
  45. WebuiHost: str = "0.0.0.0"
  46. WebuiPort: int = 22267
  47. Language: str = "en-US"
  48. Theme: str = "default"
  49. DpiScaling: bool = True
  50. Password: Optional[str] = None
  51. CDN: Union[str, bool] = False
  52. Run: Optional[str] = None
  53. class DeployConfig(ConfigModel):
  54. def __init__(self, file=DEPLOY_CONFIG):
  55. """
  56. Args:
  57. file (str): User deploy config.
  58. """
  59. self.file = file
  60. self.config = {}
  61. self.read()
  62. self.write()
  63. self.show_config()
  64. def show_config(self):
  65. logger.hr("Show deploy config", 1)
  66. for k, v in self.config.items():
  67. if k in ("Password", "SSHUser"):
  68. continue
  69. if self.config_template[k] == v:
  70. continue
  71. logger.info(f"{k}: {v}")
  72. logger.info(f"Rest of the configs are the same as default")
  73. def read(self):
  74. self.config_template = poor_yaml_read(DEPLOY_TEMPLATE)
  75. self.config = copy.deepcopy(self.config_template)
  76. self.config.update(poor_yaml_read(self.file))
  77. # https://e.coding.net/onmyojiautoscript/oas/OnmyojiAutoScript.git
  78. # 2025.09.01 腾讯coding跑路了
  79. if self.config["Repository"].startswith("https://e.coding.net/"):
  80. self.config["Repository"] = "https://gitcode.com/OnmyojiAutoScript/OnmyojiAutoScript.git"
  81. for key, value in self.config.items():
  82. if hasattr(self, key):
  83. super().__setattr__(key, value)
  84. def write(self):
  85. poor_yaml_write(self.config, self.file)
  86. def filepath(self, key):
  87. """
  88. Args:
  89. key (str):
  90. Returns:
  91. str: Absolute filepath.
  92. """
  93. return (
  94. os.path.abspath(os.path.join(self.root_filepath, self.config[key]))
  95. .replace(r"\\", "/")
  96. .replace("\\", "/")
  97. .replace('"', '"')
  98. )
  99. @cached_property
  100. def root_filepath(self):
  101. return (
  102. os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
  103. .replace(r"\\", "/")
  104. .replace("\\", "/")
  105. .replace('"', '"')
  106. )
  107. def execute(self, command, allow_failure=False, output=True):
  108. """
  109. Args:
  110. command (str):
  111. allow_failure (bool):
  112. output(bool):
  113. Returns:
  114. bool: If success.
  115. Terminate installation if failed to execute and not allow_failure.
  116. """
  117. command = command.replace(r"\\", "/").replace("\\", "/").replace('"', '"')
  118. if not output:
  119. command = command + ' >nul 2>nul'
  120. logger.info(command)
  121. error_code = os.system(command)
  122. if error_code:
  123. if allow_failure:
  124. logger.info(f"[ allowed failure ], error_code: {error_code}")
  125. return False
  126. else:
  127. logger.info(f"[ failure ], error_code: {error_code}")
  128. self.show_error(command)
  129. raise ExecutionError
  130. else:
  131. logger.info(f"[ success ]")
  132. return True
  133. def show_error(self, command=None):
  134. logger.hr("Update failed", 0)
  135. self.show_config()
  136. logger.info("")
  137. logger.info(f"Last command: {command}")
  138. logger.info(
  139. "Please check your deploy settings in config/deploy.yaml "
  140. "and re-open Alas.exe"
  141. )
  142. logger.info("Take the screenshot of entire window if you need help")