- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoCheckScript.py
More file actions
Latest commit
248 lines (243 loc) · 9.25 KB
/
Copy pathAutoCheckScript.py
File metadata and controls
248 lines (243 loc) · 9.25 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
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
fromreimportsearch
fromtimeimportsleep, strftime
fromwin32com.clientimportDispatch
fromos.pathimportjoin, isdir, basename
fromthreadingimportThread, active_count
fromosimportsystem, environ, mkdir, listdir
fromwin32pdhimportEnumObjects, EnumObjectItems
fromcalendarimportmonthcalendar, IllegalMonthError
fromloggingimportbasicConfig, 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]: ')
ifrequire_debugin ['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
classMultiProcess(Thread):
def__init__(self, action, delay):
Thread.__init__(self)
self.action=action
self.delay=delay
defrun(self):
ProcessAction(self.action, self.delay)
defprt_message(msg, status, mode):
ifstatus==0:
print(msg)
else:
ifmode=='info':
logger.info(msg)
elifmode=='debug':
logger.debug(msg)
elifmode=='error':
logger.error(msg)
defgetCurrentQ():
Season= {'Q1': ['01', '02', '03'], 'Q2': ['04', '05', '06'], 'Q3': ['07', '08', '09'], 'Q4': ['10', '11', '12']}
foriinrange(1,5):
ifstrftime('%m') inSeason['Q'+str(i)]:
return('Q'+str(i))
defcheckFail(path):
FAIL_LIST= []
foreach_fileinlistdir(path):
if'FAIL'ineach_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))
defgetProgram(fileReg, dir):
foreach_fileinlistdir(dir):
ifsearch(fileReg, each_file):
return(join(dir, each_file))
defgetMonthDay(y, m):
tmp_list= []
foriinrange(6):
try:
foreach_dinmonthcalendar(int(y), int(m))[i]:
ifeach_d!=0:
tmp_list.append(each_d)
exceptIndexError:
pass
return(tmp_list)
defgetProcesses():
EnumObjects(None, None, 0, 1)
(items, instance) =EnumObjectItems(None, None, "Process", -1)
return(instance)
defProcessAction(action, delay):
sleep(delay)
ifaction=='automation':
system(Automation)
elifaction=='keyControl':
prt_message('Starting control automation...', status, 'info')
foreach_keyin ['{DOWN}', '{TAB}', '{ENTER}', 'Path', '{TAB}', '{TAB}', '{TAB}', '{ENTER}', ' ', '+{END}', '{TAB}', '{ENTER}', '{TAB}', 'InputDate', '{TAB}', '{ENTER}']:
sleep(2)
ifeach_key=='Path':
kb.SendKeys(SavePath)
elifeach_key=='InputDate':
kb.SendKeys(date_range)
else:
kb.SendKeys(each_key)
sleep(250)
while(1):
ifbasename(Automation).replace('.exe', '') ingetProcesses():
prt_message('Automation is exist, Enter to exit', status, 'info')
kb.SendKeys('{ENTER}')
else:
counter=counter+1
ifcounter==len(date_list):
system('\"{} -s\"'.format(join(iSMART_PATH, basename(iSMART_PATH) +'.exe')))
sleep(3)
log_list= []
foreach_loginlistdir(iSMART_PATH):
ifsearch('\w+.log$', each_log):
log_list.append(each_log)
withopen(join(iSMART_PATH, log_list[0])) asrf:
foreach_lineinrf.read().split('\n'):
if'erase count'ineach_line:
prt_message('Drive erase count: '+each_line, status, 'debug')
break
sleep(5)
defisAlive(logic=None, threadNum=0):
while(True):
iflogic=='lt':
ifactive_count() <threadNum:
break
eliflogic=='gt':
ifactive_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= [], []
ifsearch('^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]
ifint(y1) >int(y2) orint(m1) >12orint(m2) >12or0in [int(m1), int(m2), int(d1), int(d2)]:
prt_message('\nInvalid date format', status, 'error')
continue
elif (y1==y2andint(m1) >int(m2)) or (y1==y2andm1==m2andint(d1) >int(d2)) orint(d1) >31orint(d2) >31:
prt_message('\nInvalid date format', status, 'error')
continue
defDateSum1(d1):
fordingetMonthDay(y1, m1):
ifd1[0] =='0':
d1=d1[1]
ifd>=int(d1):
ifd<10:
d='0'+str(d)
d_lst1.append(y1+'-'+m1+'-'+str(d))
defDateSum2(d2):
fordingetMonthDay(y2, m2):
ifd2[0] =='0':
d2=d2[1]
ifd<=int(d2):
ifd<10:
d='0'+str(d)
d_lst2.append(y2+'-'+m2+'-'+str(d))
defDateSum3(w, x, y, z):
foriinrange(x, y):
ifi<10:
fr_i='0'+str(i)
else:
fr_i=str(i)
forjingetMonthDay(z, i):
ifj<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<y2andm1=='12'andm2=='01') or (y1==y2andint(m2) -int(m1) ==1):
DateSum1(d1)
DateSum2(d2)
#case2: 2017/01/10-2017/05/04
elify1==y2andint(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<y2andm1!='12'andm2!='01') or (y1<y2andm1=='12'andm2!='01') or (y1<y2andm1!='12'andm2=='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
elify1==y2andm1==m2:
fordingetMonthDay(y1, m1):
ifd1[0] =='0':
d1=d1[1]
ifd>=int(d1) andd<=int(d2):
ifd<10:
d='0'+str(d)
d_lst1.append(y1+'-'+m1+'-'+str(d))
break
elifsearch('^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')
foreach_dateindate_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')
ifnotgetProgram('^Recording_log_check_Tool_for_vast_\w+', Desktop):
prt_message('\nRecording log check Tool is not found', status, 'error')
raiseException
if'VMSPlayback'notingetProcesses():
prt_message('\nVAST Playback program is not executed', status, 'error')
raiseException
prt_message('Log Save Path: '+SavePath, status, 'error')
ifnotisdir(SavePath):
prt_message('Detect directory is not exists, create new one.\nDirectory: '+SavePath, status, 'info')
mkdir(SavePath)
ifbasename(Automation).replace('.exe', '') ingetProcesses():
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)
iflen(listdir(SavePath)) >=64andcheckFail(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')
exceptException:
print('', end='')
exceptFileNotFoundErrorasferr:
prt_message(str(ferr), status, 'error')
exceptKeyboardInterrupt:
prt_message('\nUser press Ctrl+C to exit.', status, 'info')
sleep(2)