forked from andaok/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseclass.py
More file actions
106 lines (82 loc) · 3.01 KB
/
baseclass.py
File metadata and controls
106 lines (82 loc) · 3.01 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
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
'''
Created on Nov 23, 2012
@author: wye
Copyright @ 2011 - 2012 Cloudiya Tech . Inc
'''
import os
import sys
import time
import pickle
import logging
import operator
import subprocess
#######################################
class execCmd():
""" Execute command """
def __init__(self,cmd,logger,killsig=-1,killtime=0):
"""
killsig , default is unset
killtime, default is unset
"""
self.cmd = cmd
self.log = logger
self.killsig = killsig
self.killtime = killtime
self.exception = None
self.exit = None
self.stdout = None
self.stderr = None
self.status = None
def exeCmd(self):
self.log.debug("Start run cmd : %s "%self.cmd)
cmdobj = subprocess.Popen(self.cmd,shell=True,stderr=subprocess.PIPE)
done = False
while not done and self.killtime > 0:
time.sleep(0.2)
if cmdobj.poll():
done = True
self.killtime -=0.2
if not done and self.killsig != -1:
try:
os.kill(cmdobj.pid, self.killsig)
except OSError,e:
self.log.error("Kill cmd fail : %s "%self.cmd)
self.exception = e
status = cmdobj.wait()
if os.WIFSIGNALED(status):
self.exit = "SIGNAL: " + str(os.WTERMSIG(status))
elif os.WIFEXITED(status):
self.exit = str(os.WEXITSTATUS(status))
else:
self.exit = "UNKNOWN"
self.status = status
self.stderr = cmdobj.stderr.read()
cmdobj.stderr.close()
self.log.debug("Cmd run end : %s "%self.cmd)
#######################################
def run(cmdlist,logger,QuitFlag=True,Accelerate=False):
if len(cmdlist) > 0:
for cmd in cmdlist:
CmdObj = execCmd(cmd,logger)
CmdObj.exeCmd()
if CmdObj.status != 0 and not QuitFlag:
logger.error("Cmd : \"%s\" ,Exit code : %s"%(cmd,CmdObj.exit))
logger.error("Cmd : \"%s\" ,Stderr : %s"%(cmd,CmdObj.stderr))
logger.error("Mapper fetch a error ,but mapper don't quit")
if CmdObj.status != 0 and QuitFlag:
logger.error("Cmd : \"%s\" ,Exit code : %s"%(cmd,CmdObj.exit))
logger.error("Cmd : \"%s\" ,Stderr : %s"%(cmd,CmdObj.stderr))
logger.error("Mapper fail,quit!")
sys.exit()
########################################
def getlog(VideoFileNameAlias,logfile="/tmp/videohandle.log"):
logger = logging.Logger(VideoFileNameAlias)
hdlr = logging.FileHandler(logfile)
formatter = logging.Formatter("%(asctime)s -- [ %(name)s ] -- %(levelname)s -- %(message)s")
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.NOTSET)
return logger
########################################