-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
216 lines (183 loc) · 6.26 KB
/
config.py
File metadata and controls
216 lines (183 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import copy
import subprocess
from typing import Optional, Union
from deploy.Windows.logger import logger
from deploy.Windows.utils import *
class ExecutionError(Exception):
pass
class ConfigModel:
# Git
Repository: str = "https://github.com/LmeSzinc/AzurLaneAutoScript"
Branch: str = "master"
GitExecutable: str = "./toolkit/Git/mingw64/bin/git.exe"
GitProxy: Optional[str] = None
SSLVerify: bool = False
AutoUpdate: bool = True
KeepLocalChanges: bool = False
# Python
PythonExecutable: str = "./toolkit/python.exe"
PypiMirror: Optional[str] = None
InstallDependencies: bool = True
RequirementsFile: str = "requirements.txt"
# Adb
AdbExecutable: str = "./toolkit/Lib/site-packages/adbutils/binaries/adb.exe"
ReplaceAdb: bool = True
AutoConnect: bool = True
InstallUiautomator2: bool = True
# Ocr
UseOcrServer: bool = False
StartOcrServer: bool = False
OcrServerPort: int = 22268
OcrClientAddress: str = "127.0.0.1:22268"
# Update
EnableReload: bool = True
CheckUpdateInterval: int = 5
AutoRestartTime: str = "03:50"
# Misc
DiscordRichPresence: bool = False
# Remote Access
EnableRemoteAccess: bool = False
SSHUser: Optional[str] = None
SSHServer: Optional[str] = None
SSHExecutable: Optional[str] = None
# Webui
WebuiHost: str = "0.0.0.0"
WebuiPort: int = 22367
Language: str = "en-US"
Theme: str = "default"
DpiScaling: bool = True
Password: Optional[str] = None
CDN: Union[str, bool] = False
Run: Optional[str] = None
class DeployConfig(ConfigModel):
def __init__(self, file=DEPLOY_CONFIG):
"""
Args:
file (str): User deploy config.
"""
self.file = file
self.config = {}
self.config_template = {}
self.read()
if self.Repository == 'https://gitee.com/LmeSzinc/AzurLaneAutoScript':
self.Repository = 'https://e.coding.net/llop18870/alas/AzurLaneAutoScript.git'
if self.Repository == 'https://gitee.com/lmeszinc/azur-lane-auto-script-mirror':
self.Repository = 'https://e.coding.net/llop18870/alas/AzurLaneAutoScript.git'
self.write()
self.show_config()
def show_config(self):
logger.hr("Show deploy config", 1)
for k, v in self.config.items():
if k in ("Password", "SSHUser"):
continue
if self.config_template[k] == v:
continue
logger.info(f"{k}: {v}")
logger.info(f"Rest of the configs are the same as default")
def read(self):
self.config = poor_yaml_read(DEPLOY_TEMPLATE)
self.config_template = copy.deepcopy(self.config)
self.config.update(poor_yaml_read(self.file))
for key, value in self.config.items():
if hasattr(self, key):
super().__setattr__(key, value)
def write(self):
poor_yaml_write(self.config, self.file)
def filepath(self, path):
"""
Args:
path (str):
Returns:
str: Absolute filepath.
"""
return (
os.path.abspath(os.path.join(self.root_filepath, path))
.replace(r"\\", "/")
.replace("\\", "/")
.replace('"', '"')
)
@cached_property
def root_filepath(self):
return (
os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))
.replace(r"\\", "/")
.replace("\\", "/")
.replace('"', '"')
)
@cached_property
def adb(self) -> str:
exe = self.filepath(self.AdbExecutable)
if os.path.exists(exe):
return exe
logger.warning(f'AdbExecutable: {exe} does not exists, use `adb` instead')
return 'adb'
@cached_property
def git(self) -> str:
exe = self.filepath(self.GitExecutable)
if os.path.exists(exe):
return exe
logger.warning(f'GitExecutable: {exe} does not exists, use `git` instead')
return 'git'
@cached_property
def python(self) -> str:
return self.filepath(self.PythonExecutable)
@cached_property
def requirements_file(self) -> str:
if self.RequirementsFile == 'requirements.txt':
return 'requirements.txt'
else:
return self.filepath(self.RequirementsFile)
def execute(self, command, allow_failure=False, output=True):
"""
Args:
command (str):
allow_failure (bool):
output(bool):
Returns:
bool: If success.
Terminate installation if failed to execute and not allow_failure.
"""
command = command.replace(r"\\", "/").replace("\\", "/").replace('"', '"')
if not output:
command = command + ' >nul 2>nul'
logger.info(command)
error_code = os.system(command)
if error_code:
if allow_failure:
logger.info(f"[ allowed failure ], error_code: {error_code}")
return False
else:
logger.info(f"[ failure ], error_code: {error_code}")
self.show_error(command)
raise ExecutionError
else:
logger.info(f"[ success ]")
return True
def subprocess_execute(self, cmd, timeout=10):
"""
Args:
cmd (list[str]):
timeout:
Returns:
str:
"""
logger.info(' '.join(cmd))
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
try:
stdout, stderr = process.communicate(timeout=timeout)
process.kill()
except subprocess.TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
logger.info(f'TimeoutExpired, stdout={stdout}, stderr={stderr}')
return stdout.decode()
def show_error(self, command=None):
logger.hr("Update failed", 0)
self.show_config()
logger.info("")
logger.info(f"Last command: {command}")
logger.info(
"Please check your deploy settings in config/deploy.yaml "
"and re-open Alas.exe"
)
logger.info("Take the screenshot of entire window if you need help")