forked from seven6306/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoCheckScript.py
248 lines (243 loc) · 9.25 KB
/
AutoCheckScript.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from re import search
from time import sleep, strftime
from win32com.client import Dispatch
from os.path import join, isdir, basename
from threading import Thread, active_count
from os import system, environ, mkdir, listdir
from win32pdh import EnumObjects, EnumObjectItems
from calendar import monthcalendar, IllegalMonthError
from logging import basicConfig, DEBUG, getLogger, StreamHandler, Formatter
"""
NOTE:
line98 : iSMART tool command work confirm
line105 : erase count correct keywords
line239 : checkFail() fail item keywords
"""
Desktop = join('C:' + environ['HOMEPATH'], 'Desktop')
counter, kb = 0, Dispatch("WScript.Shell")
require_debug = input('Enable debug log mode? (default:N) [y/N]: ')
if require_debug in ['y', 'Y']:
basicConfig(filename='AutoCheckScript_' + strftime('%Y%m%d_%H-%M-%S') + '.log', level=DEBUG)
logger = getLogger(__name__)
fhandler = StreamHandler()
fhandler.setFormatter(Formatter())
logger.addHandler(fhandler)
status = 1
else:
status = 0
class MultiProcess(Thread):
def __init__(self, action, delay):
Thread.__init__(self)
self.action = action
self.delay = delay
def run(self):
ProcessAction(self.action, self.delay)
def prt_message(msg, status, mode):
if status == 0:
print(msg)
else:
if mode == 'info':
logger.info(msg)
elif mode == 'debug':
logger.debug(msg)
elif mode == 'error':
logger.error(msg)
def getCurrentQ():
Season = {'Q1': ['01', '02', '03'], 'Q2': ['04', '05', '06'], 'Q3': ['07', '08', '09'], 'Q4': ['10', '11', '12']}
for i in range(1,5):
if strftime('%m') in Season['Q'+str(i)]:
return('Q' + str(i))
def checkFail(path):
FAIL_LIST = []
for each_file in listdir(path):
if 'FAIL' in each_file:
FAIL_LIST.append(each_file)
prt_message('Check Fail Count: {} fail items found.'.format(str(len(FAIL_LIST))), status, 'debug')
return(len(FAIL_LIST))
def getProgram(fileReg, dir):
for each_file in listdir(dir):
if search(fileReg, each_file):
return(join(dir, each_file))
def getMonthDay(y, m):
tmp_list = []
for i in range(6):
try:
for each_d in monthcalendar(int(y), int(m))[i]:
if each_d != 0:
tmp_list.append(each_d)
except IndexError:
pass
return(tmp_list)
def getProcesses():
EnumObjects(None, None, 0, 1)
(items, instance) = EnumObjectItems(None, None, "Process", -1)
return(instance)
def ProcessAction(action, delay):
sleep(delay)
if action == 'automation':
system(Automation)
elif action == 'keyControl':
prt_message('Starting control automation...', status, 'info')
for each_key in ['{DOWN}', '{TAB}', '{ENTER}', 'Path', '{TAB}', '{TAB}', '{TAB}', '{ENTER}', ' ', '+{END}', '{TAB}', '{ENTER}', '{TAB}', 'InputDate', '{TAB}', '{ENTER}']:
sleep(2)
if each_key == 'Path':
kb.SendKeys(SavePath)
elif each_key == 'InputDate':
kb.SendKeys(date_range)
else:
kb.SendKeys(each_key)
sleep(250)
while(1):
if basename(Automation).replace('.exe', '') in getProcesses():
prt_message('Automation is exist, Enter to exit', status, 'info')
kb.SendKeys('{ENTER}')
else:
counter = counter + 1
if counter == len(date_list):
system('\"{} -s\"'.format(join(iSMART_PATH, basename(iSMART_PATH) + '.exe')))
sleep(3)
log_list = []
for each_log in listdir(iSMART_PATH):
if search('\w+.log$', each_log):
log_list.append(each_log)
with open(join(iSMART_PATH, log_list[0])) as rf:
for each_line in rf.read().split('\n'):
if 'erase count' in each_line:
prt_message('Drive erase count: ' + each_line, status, 'debug')
break
sleep(5)
def isAlive(logic=None, threadNum=0):
while(True):
if logic == 'lt':
if active_count() < threadNum:
break
elif logic == 'gt':
if active_count() > threadNum:
break
sleep(0.5)
prt_message("""
======================================================================
\t Welcome to auto check recording log script
======================================================================
\t\t Follow below listed caution:
[1] Put the automation tool in desktop follow as below example.
e.g., "Recording_log_check_Tool_for_vast_vx.x.x.exe"
[2] Put iSMART tool in path "C:\\Program Files (x86)\\VIVOTEK Inc\\
[3] Open VAST Playback program as necessary.
[4] Do not interrupt while script has been executing.
[5] Press keyboard "Ctrl + C" to exit.
[6] Date range format must be:
e.g., 2017/12/29
2017/12/29-2018/01/01 or 2017/02/28-2017/03/01
======================================================================""", status, 'info')
try:
while(1):
date_range = input('Please input date range e.g., [2000/01/01-2018/12/31]:\n ==> ')
d_lst1, d_lst2 = [], []
if search('^20\d{2}/\d{2}/\d{2}\-20\d{2}/\d{2}/\d{2}$', date_range):
date_range1, date_range2 = date_range.split('-')[0], date_range.split('-')[1]
y1, y2 = date_range1.split('/')[0], date_range2.split('/')[0]
m1, m2 = date_range1.split('/')[1], date_range2.split('/')[1]
d1, d2 = date_range1.split('/')[2], date_range2.split('/')[2]
if int(y1) > int(y2) or int(m1) > 12 or int(m2) > 12 or 0 in [int(m1), int(m2), int(d1), int(d2)]:
prt_message('\nInvalid date format', status, 'error')
continue
elif (y1 == y2 and int(m1) > int(m2)) or (y1 == y2 and m1 == m2 and int(d1) > int(d2)) or int(d1) > 31 or int(d2) > 31:
prt_message('\nInvalid date format', status, 'error')
continue
def DateSum1(d1):
for d in getMonthDay(y1, m1):
if d1[0] == '0':
d1 = d1[1]
if d >= int(d1):
if d < 10:
d = '0' + str(d)
d_lst1.append(y1 + '-' + m1 + '-' + str(d))
def DateSum2(d2):
for d in getMonthDay(y2, m2):
if d2[0] == '0':
d2 = d2[1]
if d <= int(d2):
if d < 10:
d = '0' + str(d)
d_lst2.append(y2 + '-' + m2 + '-' + str(d))
def DateSum3(w, x, y, z):
for i in range(x, y):
if i < 10:
fr_i = '0' + str(i)
else:
fr_i = str(i)
for j in getMonthDay(z, i):
if j < 10:
j = '0' + str(j)
w.append(z + '-' + fr_i + '-' + str(j))
#case1: 2017/12/29-2018/01/04 or 2018/01/29-2018/02/04
if (y1 < y2 and m1 == '12' and m2 == '01') or (y1 == y2 and int(m2) - int(m1) == 1):
DateSum1(d1)
DateSum2(d2)
#case2: 2017/01/10-2017/05/04
elif y1 == y2 and int(m2) - int(m1) > 1:
DateSum1(d1)
DateSum3(d_lst1, int(m1)+1, int(m2), y1)
DateSum2(d2)
#case3: 2017/10/10-2018/02/04 or 2017/12/05-2018/03/05 or 2017/10/05-2018/01/05
elif (y1 < y2 and m1 != '12' and m2 != '01') or (y1 < y2 and m1 == '12' and m2 != '01') or (y1 < y2 and m1 != '12' and m2 == '01'):
prt_message('this case', status, 'debug')
DateSum1(d1)
DateSum3(d_lst1, int(m1)+1, 13, y1)
DateSum3(d_lst2, 1, int(m2), y2)
DateSum2(d2)
#case4: 2018/01/09-2018/01/20
elif y1 == y2 and m1 == m2:
for d in getMonthDay(y1, m1):
if d1[0] == '0':
d1 = d1[1]
if d >= int(d1) and d <= int(d2):
if d < 10:
d = '0' + str(d)
d_lst1.append(y1 + '-' + m1 + '-' + str(d))
break
elif search('^20\d{2}/\d{2}/\d{2}$', date_range):
date_range = date_range.replace('/', '-')
d_lst1.append(date_range)
break
else:
prt_message('\nInvalid date format', status, 'error')
date_list = d_lst1 + d_lst2
prt_message(date_list, status, 'debug')
for each_date in date_list:
date_range, iSMART_PATH = each_date, getProgram('^iSMART\w+', r'C:\Program Files (x86)\VIVOTEK Inc')
prt_message('Check Date: ' + date_range, status, 'debug')
thread_1 = MultiProcess('automation', 0)
thread_2 = MultiProcess('keyControl', 5)
SavePath = join(join(Desktop, strftime('%Y') + ' ' + getCurrentQ()), date_range.split('-')[1] + date_range.split('-')[2])
prt_message(SavePath, status, 'debug')
Automation = getProgram('^Recording_log_check_Tool_for_vast_\w+', Desktop)
prt_message('Execute Automation: ' + basename(Automation), status, 'debug')
if not getProgram('^Recording_log_check_Tool_for_vast_\w+', Desktop):
prt_message('\nRecording log check Tool is not found', status, 'error')
raise Exception
if 'VMSPlayback' not in getProcesses():
prt_message('\nVAST Playback program is not executed', status, 'error')
raise Exception
prt_message('Log Save Path: ' + SavePath, status, 'error')
if not isdir(SavePath):
prt_message('Detect directory is not exists, create new one.\nDirectory: ' + SavePath, status, 'info')
mkdir(SavePath)
if basename(Automation).replace('.exe', '') in getProcesses():
prt_message('Detect automation {} is executing in system, kill the process down.'.format(basename(Automation)), status, 'info')
system('taskkill /f /im ' + basename(Automation))
thread_1.start()
thread_2.start()
isAlive('lt', 3)
if len(listdir(SavePath)) >= 64 and checkFail(SavePath) == 0:
prt_message('\n***** Date: {} check continues recording completed! *****\n'.format(date_range), status, 'info')
else:
prt_message('\n***** Date: {} check continues recording failured. *****\n'.format(date_range), status, 'error')
except Exception:
print('', end='')
except FileNotFoundError as ferr:
prt_message(str(ferr), status, 'error')
except KeyboardInterrupt:
prt_message('\nUser press Ctrl+C to exit.', status, 'info')
sleep(2)