241 lines
7.4 KiB
Python
241 lines
7.4 KiB
Python
#! /usr/bin/env python
|
|
'''
|
|
X_X04SA_do_casave.py - Script to execute a casave command.
|
|
====================
|
|
|
|
Usage:
|
|
|
|
%s [<options>] -- <arguments-for-casave>
|
|
|
|
Description:
|
|
casave seems to be not 100%% reliable, at least when run from
|
|
spec via a "unix" command. It sometimes hangs up.
|
|
|
|
X_X04SA_do_casave.py will spawn a child process to run casave
|
|
with the given <arguments-for-casave>. If the process does
|
|
not terminate within the timeout (default = 5 secs), the
|
|
child process will be killed.
|
|
|
|
If the child process is killed, <arguments-for-casave> will
|
|
be parsed, looking for a -asciiout option. If it is found,
|
|
a file of the given name will be created containing the text
|
|
|
|
casave killed at <date> <time>
|
|
|
|
Options:
|
|
-h generate this help text.
|
|
-v display version information.
|
|
--debug turn on debug printout.
|
|
--timeout=<secs> specify a timeout. Default = 5 secs.
|
|
-- marks the start of args for casave.
|
|
'''
|
|
#---------------------------------------------------------------------------
|
|
import os, sys, commands
|
|
# Make sure we are running at least python level 2.
|
|
# CaChannel seems to give troubles otherwise!
|
|
try:
|
|
if sys.version[0:1] == "1":
|
|
python2 = commands.getoutput ("type -p python2")
|
|
if python2 == "":
|
|
print "\n\aThe default python version is", sys.version
|
|
print "and this script needs python level 2 or higher."
|
|
print " Python level 2 cannot be found."
|
|
sys.exit (1)
|
|
#endif
|
|
sys.argv.insert (0, python2)
|
|
os.execv (python2, sys.argv)
|
|
#endif
|
|
if sys.version[0:1] == "1":
|
|
print "\n\aThe loading of python level 2 seems to have failed!"
|
|
sys.exit (1)
|
|
#endif
|
|
except:
|
|
print "\n\aFailed to load python level 2!"
|
|
raise
|
|
#endtry
|
|
#---------------------------------------------------------------------------
|
|
import time
|
|
import getopt
|
|
import string
|
|
#---------------------------------------------------------------------------
|
|
#
|
|
def makeErrorReport (cmnd):
|
|
# ===============
|
|
os.system ("echo $(date) %s >> /exchange/share/X04SA/casaveErrors.log" % cmnd)
|
|
#enddef
|
|
#
|
|
#---------------------------------------------------------------------------
|
|
#
|
|
def show_usage ():
|
|
# ==========
|
|
#
|
|
base = os.path.basename (sys.argv[0])
|
|
print __doc__ % base
|
|
return
|
|
#enddef
|
|
#
|
|
#---------------------------------------------------------------------------
|
|
#
|
|
def show_version ():
|
|
# ============
|
|
#
|
|
cvsrepos = \
|
|
"$Source: /cvs/X/X04SA/App/scripts/X_X04SA_do_casave.py,v $"
|
|
cvsdate = "$Date: 2006/07/07 11:56:16 $"
|
|
cvsrevision = "$Revision: 1.2 $"
|
|
#
|
|
print
|
|
print "File: ", sys.argv[0]
|
|
#
|
|
l = len (cvsrepos)
|
|
print "Repository:", cvsrepos[9:l-1]
|
|
#
|
|
l = len (cvsdate)
|
|
print "Date: ", time.strftime ("%d-%b-%Y %H:%M:%S", \
|
|
time.strptime (cvsdate[7:l-1], "%Y/%m/%d %H:%M:%S"))
|
|
#
|
|
l = len (cvsrevision)
|
|
print "Revision: ", cvsrevision[11:l-1]
|
|
print
|
|
return
|
|
#enddef
|
|
#
|
|
#---------------------------------------------------------------------------
|
|
#---------------------------------------------------------------------------
|
|
#----------------------------------- The program starts here ---------------
|
|
#---------------------------------------------------------------------------
|
|
try:
|
|
red = chr (0x1b) + "[01;31m"
|
|
green = chr (0x1b) + "[01;32m"
|
|
blue = chr (0x1b) + "[01;34m"
|
|
off = chr (0x1b) + "[00m"
|
|
#
|
|
# Analyse the options
|
|
#
|
|
TimeOut = 5.0
|
|
|
|
Debug = 0
|
|
|
|
(opts, items) = getopt.getopt (sys.argv[1:], "h?v", \
|
|
("help", "debug", "timeout="))
|
|
for opt in opts:
|
|
if opt[0] == "-h": raise "Help"
|
|
if opt[0] == "-?": raise "Help"
|
|
if opt[0] == "--help": raise "Help"
|
|
if opt[0] == "-v": raise "Version"
|
|
if opt[0] == "--debug": Debug = 1
|
|
if opt[0] == "--timeout":
|
|
try:
|
|
TimeOut = float (opt[1])
|
|
except:
|
|
raise "BadTimeout"
|
|
#endtry
|
|
if TimeOut < 0.1: raise "BadTimeout"
|
|
#endif
|
|
#endfor
|
|
|
|
if Debug:
|
|
print "Arguments for casave are:"
|
|
for item in items:
|
|
print " ", item
|
|
#endfor
|
|
print "--- End of Arguments"
|
|
print "Time-out = %.1f secs" % TimeOut
|
|
#endif
|
|
|
|
cmnd = "casave"
|
|
for item in items:
|
|
cmnd = cmnd + " " + item
|
|
#endfor
|
|
if Debug:
|
|
print "casave command is:"
|
|
print " ", cmnd
|
|
#endif
|
|
pid = os.fork ()
|
|
if pid == 0:
|
|
status = os.system (cmnd)
|
|
if Debug: print "casave exit status = %d" % status
|
|
sys.exit (0)
|
|
else:
|
|
nLoops = int (TimeOut * 10)
|
|
if Debug: print "PID of casave = %d, # loops = %d" % (pid, nLoops)
|
|
for cnt in range (nLoops):
|
|
time.sleep (0.1)
|
|
(childPID, childStatus) = os.waitpid (pid, os.WNOHANG)
|
|
if childPID != 0:
|
|
if Debug: print "Status from casave = %d" % childStatus
|
|
sys.exit (childStatus)
|
|
#endif
|
|
#endfor
|
|
print "\a<<< %scasave has timed-out, killing all casave processes%s >>>" % (red, off)
|
|
makeErrorReport (cmnd)
|
|
#----------------------------------------------------------
|
|
# The following code is not completely solid ...
|
|
#
|
|
if Debug: print "Finding PID of process to kill ..."
|
|
killCmnd = ""
|
|
psCmnd = "ps -Af | grep -v grep | grep casave | grep %d" % pid
|
|
(status, op) = commands.getstatusoutput (psCmnd)
|
|
if status != 0:
|
|
if Debug: print "<<< %Cannot find any casave processes to kill.%s >>>" % (red, off)
|
|
pass
|
|
else:
|
|
psRecds = string.split (op, "\n")
|
|
if len (psRecds) == 0:
|
|
if Debug: print "<<< %sCannot find any casave processes to kill.%s >>>" % (red, off)
|
|
pass
|
|
elif len (psRecds) > 1:
|
|
if Debug: print "<<< %sCannot find a unique casave process to kill.%s >>>" % (red, off)
|
|
pass
|
|
else:
|
|
toks = string.split (psRecds[0])
|
|
if Debug: print psRecds[0]
|
|
casavePID = int (toks[1])
|
|
killCmnd = "kill -9 %d" % casavePID
|
|
#endif
|
|
#endif
|
|
if killCmnd == "":
|
|
if Debug: print "<<< %sKilling all casave processes.%s >>>" % (red, off)
|
|
killCmnd = "killall -KILL casave"
|
|
#endif
|
|
#----------------------------------------------------------
|
|
# Remove the next command when the above code has been made solid!
|
|
#
|
|
killCmnd = "killall -KILL casave"
|
|
#----------------------------------------------------------
|
|
if Debug: print "Kill command is \"%s\"" % killCmnd
|
|
os.system (killCmnd)
|
|
#endif
|
|
sys.exit (0)
|
|
|
|
#
|
|
except KeyboardInterrupt:
|
|
print "%sInterrupted!%s" % (blue, off)
|
|
sys.exit (0)
|
|
#
|
|
except SystemExit, value:
|
|
sys.exit (value)
|
|
#
|
|
except:
|
|
show_usage ()
|
|
sys.exit (0)
|
|
|
|
#--------------------------------------------------#
|
|
# emacs setup - force text mode to prevent emacs #
|
|
# from helping with the indentation #
|
|
# and tell emacs to use spaces when #
|
|
# tabbing. #
|
|
# Local Variables: #
|
|
# mode:text #
|
|
# indent-tabs-mode:nil #
|
|
# End: #
|
|
#--------------------------------------------------#
|
|
# Contact: D.Maden
|
|
# Intial version: Oct 2003
|
|
#
|
|
# $Author: maden $
|
|
# $Source: /cvs/X/X04SA/App/scripts/X_X04SA_do_casave.py,v $
|
|
# $Revision: 1.2 $
|
|
# $Date: 2006/07/07 11:56:16 $
|
|
#
|
|
#------------------------------------- End of X_X04SA_do_casave.py |