forked from armink/UtestRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_runner.py
151 lines (127 loc) · 4.64 KB
/
serial_runner.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
141
142
143
144
145
146
147
148
149
150
151
#!/user/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020, Armink, <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
#
# Change Logs:
# Date Author Notes
# 2020-01-26 armink the first version
# 2023-07-03 flybreak support serial port
#
import argparse
import os
import sys
import threading
import logging
import time
import asyncio
from serial_asyncio import open_serial_connection
from utest import Utest
LOG_LVL = logging.DEBUG
LOG_TAG = 'serial_runner'
logger = logging.getLogger(LOG_TAG)
class SerialRunner:
def __init__(self, port, baudrate, delay):
logging.basicConfig(level=LOG_LVL, format='%(asctime)s %(name)s %(levelname)s: %(message)s', datefmt=None)
self.port = port
self.baudrate = baudrate
self.delay = delay
self.env_version = ''
self.sub_proc = None
self.reader = None
self.writer = None
self.log_recorder = None
self.logs = []
self.logs_lock = threading.Lock()
self.is_executing = False
def run(self):
event = threading.Event()
# RT-Thread console log recorder with serial process
def log_recorder_entry():
async def run():
reader, writer = await open_serial_connection(url=self.port, baudrate=self.baudrate)
self.reader = reader
self.writer = writer
with open('rtt_console.log', 'w') as log_file:
while True:
line = await self.reader.readline()
if line:
line = str(line, encoding="utf8").replace('\n', '').replace('\r', '')
log_file.write(line + '\n')
log_file.flush()
self.logs_lock.acquire()
self.logs.append(line)
self.logs_lock.release()
if self.is_executing and line.find('[ utest ] finished') != -1:
event.set()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(run())
self.log_recorder = threading.Thread(target=log_recorder_entry)
self.log_recorder.daemon = True
self.log_recorder.start()
time.sleep(0.1)
if self.writer == None:
logger.error("Utest init failed!")
return False
def exec_utest_cmd(name, timeout):
self.logs_lock.acquire()
self.logs.clear()
self.logs_lock.release()
self.is_executing = True
# send command to serial
cmb_str = name + '\n'
if self.writer:
self.writer.write(bytes(cmb_str, encoding='utf8'))
else:
self.is_executing = False
return False, []
# wait command execute finish
event.clear()
signaled = event.wait(timeout=timeout)
self.is_executing = False
self.logs_lock.acquire()
logs = self.logs
self.logs_lock.release()
if logs == []:
logger.error("Command exec failed!")
return signaled, logs
time.sleep(self.delay)
utest = Utest(exec_utest_cmd)
if utest.init_result == False:
logger.error("Utest init failed!")
self.destroy()
return False
success = utest.test_all()
self.destroy()
return success
def destroy(self):
logger.info("serial runner destroy")
def main():
parser = argparse.ArgumentParser(description='serial runner for RT-Thread utest', prog=os.path.basename(sys.argv[0]))
parser.add_argument('--port',
metavar='port',
help='port for serial',
default="com16",
type=str,
required=False)
parser.add_argument('--baudrate',
metavar='bps',
help='baudrate for serial',
default=115200,
type=int,
required=False)
parser.add_argument('--delay',
metavar='seconds',
help='delay some seconds for serial running finish',
default=5,
type=int,
required=False)
args = parser.parse_args()
runner = SerialRunner(args.port, args.baudrate, args.delay)
if not runner.run():
sys.exit(-1)
if __name__ == '__main__':
main()