-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylog.py
110 lines (83 loc) · 3.22 KB
/
pylog.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
#!/usr/bin/python
# -*- coding: cp1252 -*-
# get lines of text from serial port, save them to a file
#this line uses print as a function like inpython 3
from __future__ import print_function
import serial, io
import time
import datetime
import os
import sys
import argv
config=argv.Config()
config.cmdInputs()
##config.writeConfig()
config.readConfig()
addr = int(config.port) # serial port to read data from
baud = int(config.baudrate) # baud rate for serial port
location =os.path.normpath(config.location)
fname = int(config.name) # log file to save data in, serial number
fmode = config.mode # log file mode = append
print(addr, baud, location, fname, fmode)
flag=0
fstart="DM385-GP rev 1.0"
fend="TEST PASS"
testFail="TEST FAIL" # have to add a case when test fails clear flag and
# print warning
stopBoot="factory jumper detected"
promt="Z3-DM385"
com1="run update-env\n"
com1Flag=0
com1Success="Writing to Nand... done"
com1SuccessFlag=0
com2="run update-ubifs-all\n"
com2Flag=0
updateSuccess="*** UPDATE SUCCESS ****"
updateFlag=0
##
## TO DO: add mac address check for session, as whole to avoid double check for
##module
with serial.Serial(addr,baud) as pt:
spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
encoding='ascii', errors='ignore', newline='\r',line_buffering=True)
while (1):
x = spb.readline() # read one line of text from serial port
if x.find(fend)!=-1:
flag=1
print('\n\nFlag Set\n\n', file=sys.stderr)
pt.write('\n')
elif x.find(stopBoot)!=-1 and updateFlag==0:
print('\n\nSTOP BOOT\n\n', file=sys.stderr)
pt.write('S')
pt.write('\n')
if x.find(promt)!=-1 and com1Flag==0:
print('\n\nPROMT com1\n\n', file=sys.stderr)
pt.write(com1)
com1Flag=1
if x.find(com1Success)!=-1 and com1Flag==1 and com2Flag==0:
print('\n\ncom1SuccessFlag\n\n', file=sys.stderr)
com1SuccessFlag=1
pt.write('\n')
if x.find(promt)!=-1 and com1Flag==1 and com1SuccessFlag==1 and updateFlag==0:
print('\n\nPROMT com2\n\n', file=sys.stderr)
pt.write(com2)
com2Flag=1
if x.find(updateSuccess)!=-1 and com2Flag==1:
updateFlag=1
elif x.find(fstart)!=-1 and flag==1:
fname=fname-1
flag=0
com1Flag=0
com2Flag=0
updateFlag=0
com1SuccessFlag=0
outf.close() #close the previous file
with open((location+str(fname).zfill(3)+".log"),fmode) as outf:
print('=', file=sys.stderr)
if x.find(fstart)!=-1:
ts = time.time()
outf.write("\n\n\n["+datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')+"]\n\n\n") # timestamp at the beginning of the file
print (x,end='') # echo line of text on-screen
## outf.write("["+str(datetime.datetime.utcnow())+"]"+x) # write line of text to file]
outf.write(x)
outf.flush() # make sure it actually gets written out