-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNRBot.py
140 lines (114 loc) · 3.96 KB
/
NRBot.py
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
import json
import os
import shutil
import shlex
import sys
import webbrowser
import subprocess
import logging
import time
from pathlib import Path
from datetime import datetime
logger = logging.getLogger('NRBot')
def init_logger():
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter(
fmt='[%(asctime)s][%(levelname)s]<%(name)s> %(message)s',
datefmt='%I:%M:%S'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
def logtime():
return datetime.now().strftime(r'%Y%m%d%H%M%S')
def run(command, name):
logger.info("Starting job [%s]" % name)
logger.debug("Command: %s" % command)
process = subprocess.Popen(
shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
while process.poll() is None:
output = process.stdout.readline().rstrip()
if not output:
break
if not output.startswith('[current_line]') and not '[DEBUG]' in output:
print(output)
def get_settings():
for s in ['settings.jsonc', 'settings.default.jsonc']:
if os.path.isfile(s):
return read_settings(s)
logger.error(
'Settings are missing. Copy settings.default.jsonc to settings.jsonc and adjust.')
return None
def read_settings(filename):
logger.info("Loading %s." % filename)
with open(filename, 'r') as txt:
builder = ""
# Naively remove comments to avoid depending on other lib
for linestr in txt.readlines():
line = linestr.strip()
if not line.startswith('//'):
builder += line
settings = json.loads(builder)
return settings
def get_log_dir(rootdir, settings):
reportdir = os.path.join(rootdir, settings['LogReportDirectory'])
logdir = os.path.join(reportdir, logtime())
if settings['CleanUpLogBeforeRun'] and os.path.isdir(reportdir):
shutil.rmtree(reportdir)
os.makedirs(logdir, exist_ok=True)
return logdir
def make_screenshot_dir(rootdir, settings):
ssdir = os.path.join(rootdir, settings['ScreenshotPath'])
if not os.path.isdir(ssdir):
os.makedirs(ssdir, exist_ok=True)
return
def get_script():
if (len(sys.argv) < 2):
logger.error(
'Please provide a script name! e.g. resetfarming, darkdaily, dungeon.')
return None
script = str(sys.argv[1]).lower()
if script not in ["resetfarming", "darkdaily", "dungeon", "arena"]:
logger.error(
'Script %s is not recognized. e.g. resetfarming, darkdaily, dungeon.' % script)
return None
return script
def main():
init_logger()
script = get_script()
if script is None:
return
settings = get_settings()
if settings is None:
return
run('adb devices', 'ADB')
rootdir = Path(__file__).parent.absolute()
airtest = os.path.join(
settings['AirtestIDEDirectory'], 'AirtestIDE.exe')
device = settings['DeviceAdbUrl']
params = '&&'.join([
'cap_method=JAVACAP',
'ori_method=MINICAPORI',
'touch_method=MINITOUCH',
])
main = os.path.join(rootdir, '%s.air' % script)
logdir = get_log_dir(rootdir, settings)
make_screenshot_dir(rootdir, settings)
run_command = '"%s" runner "%s" ' % (airtest, main)
run_command += ' --device "%s?%s" ' % (device, params)
run_command += ' --log "%s" ' % (logdir)
run(run_command, 'Runner')
if settings['ShowLogReportAfterRun']:
loghtml = os.path.join(logdir, 'log.html')
report_command = '"%s" reporter "%s" ' % (airtest, main)
report_command += ' --log_root "%s" ' % logdir
report_command += ' --outfile "%s" ' % loghtml
run(report_command, 'Reporter')
if os.path.isfile(loghtml):
webbrowser.open(loghtml, new=2)
else:
logger.warning(
"Cannot find log HTML, runner or reporter has errors.")
return
if __name__ == '__main__':
main()