Make the script more stable (exception catching etc)

This commit is contained in:
Bernhard F.W. Gschaider 2014-07-09 17:55:36 +02:00 committed by Dominik Christ
parent c05eac90c8
commit 93ca2b71ee

View file

@ -63,6 +63,9 @@ else:
write(arg)
write(end)
def printDebug(*args):
print_(*args,file=sys.stderr)
# the actual work starts here
from os import environ,path
@ -77,7 +80,7 @@ verbose="FOAM_NEW_STARTUP_DEBUG" in environ
# Location of this script
here=path.dirname(path.abspath(sys.argv[0]))
if verbose:
print_("Using scripts in",here)
printDebug("Using scripts in",here)
# For which shell
destShell=sys.argv[1]
@ -108,26 +111,31 @@ cmd+='echo "=== End"'
cmd="bash -c '"+cmd+"'"
if verbose:
print_("Cmd:",cmd)
printDebug("Cmd:",cmd)
# Execute the shell commands
if sys.version_info<(2,6):
# for old machines (RHEL 5)
raus,rein = popen4(cmd)
ret=0 # standin for a proper implementation
else:
p = Popen(cmd, shell=True,
stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
(rein,raus)=(p.stdin,p.stdout)
ret=p.wait()
lines=[l.strip().decode() for l in raus.readlines()]
rein.close()
raus.close()
if verbose:
print_("========= Script output start")
if verbose or ret!=0:
printDebug("========= Script output start")
for l in lines:
print_(l)
print_("========= Script output end")
printDebug(l)
printDebug("========= Script output end")
if ret!=0:
printDebug("Command return code",ret,". There seems to be a problem")
def extractVariables(lines):
vars={}
@ -180,6 +188,7 @@ def splitPaths(orig):
new[k]=orig[k]
return new
try:
vars=splitPaths(
changedVars(extractVariables(lines[lines.index("=== Export pre")+1:
lines.index("=== Alias pre")]),
@ -192,10 +201,18 @@ aliases=changedVars(extractAliases(lines[lines.index("=== Alias post")+1:
scriptOutput=lines[lines.index("=== Script")+1:
lines.index("=== Export post")]
except Exception:
e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
err, detail, tb = sys.exc_info()
printDebug("Output of",cmd,"not successfully parsed")
import traceback
traceback.print_exception(err,detail,tb,file=sys.stderr)
sys.exit(-1)
# Pass output of the script to stderr for debugging
if len(scriptOutput)>0:
for l in scriptOutput:
print_("Script output:",l,file=sys.stderr)
print_(l,file=sys.stderr)
class ShellConvert(object):
def __call__(self,vars,aliases):
@ -218,6 +235,9 @@ class BashConvert(ShellConvert):
return "alias %s='%s'" % (n,v)
class CshConvert(ShellConvert):
def __init__(self,sName="csh"):
self.shellName=sName
def toVar(self,n,v):
if type(v)==list:
val=":".join(v)
@ -229,7 +249,7 @@ class CshConvert(ShellConvert):
return result
def toAlias(self,n,v):
val=v.replace(" . ","source ").replace("bash","csh")
val=v.replace(" . ","source ").replace("bash",self.shellName)
if val.find("unset ")>=0:
val=val.replace("unset ","unsetenv ")
# Make sure that more than one export is possible and no wrong = is replaced
@ -239,6 +259,10 @@ class CshConvert(ShellConvert):
val=val[:pos]+val[pos:].replace("="," ",1)
return "alias %s '%s'" % (n,val)
class TcshConvert(CshConvert):
def __init__(self):
CshConvert.__init__(self,"tcsh")
class FishConvert(ShellConvert):
def toVar(self,n,v):
if type(v)==list:
@ -272,8 +296,35 @@ shells={"bash" : BashConvert,
"zsh" : ZshConvert,
"fish" : FishConvert,
"csh" : CshConvert,
"tcsh" : CshConvert}
"tcsh" : TcshConvert}
result=shells[destShell]()(vars,aliases)
try:
converter=shells[destShell]()
except KeyError:
printDebug("No converter for shell",destShell,
"Available:"," ".join(shells.keys()))
sys.exit(-1)
try:
result=converter(vars,aliases)
except Exception:
e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
err, detail, tb = sys.exc_info()
printDebug("Problem while converting output for",destShell)
import traceback
traceback.print_exception(err,detail,tb,file=sys.stderr)
sys.exit(-1)
try:
open(path.abspath(sys.argv[0])+"."+destShell,"w").write(result)
except OSError:
# We are not allowed to write here. Try tmp
try:
open(path.join("/tmp",
path.basename(sys.argv[0])+"."+destShell),"w").write(result)
except OSError:
# Nobody wants this
pass
# This is the only part that goes to stdout to be sourced by the calling script
print_(result)