RF: try/except/finally in a single statement (while at it)

since we support now python >= 2.6
pull/1187/head
Yaroslav Halchenko 2015-09-12 12:56:28 -04:00
parent 9ebf01293b
commit 85b298e49c
1 changed files with 21 additions and 21 deletions

View File

@ -560,10 +560,10 @@ class CommandAction(ActionBase):
return True return True
_cmd_lock.acquire() _cmd_lock.acquire()
try: # Try wrapped within another try needed for python version < 2.5 try:
stdout = tempfile.TemporaryFile(suffix=".stdout", prefix="fai2ban_") stdout = tempfile.TemporaryFile(suffix=".stdout", prefix="fai2ban_")
stderr = tempfile.TemporaryFile(suffix=".stderr", prefix="fai2ban_") stderr = tempfile.TemporaryFile(suffix=".stderr", prefix="fai2ban_")
try:
popen = subprocess.Popen( popen = subprocess.Popen(
realCmd, stdout=stdout, stderr=stderr, shell=True, realCmd, stdout=stdout, stderr=stderr, shell=True,
preexec_fn=os.setsid # so that killpg does not kill our process preexec_fn=os.setsid # so that killpg does not kill our process
@ -584,7 +584,7 @@ class CommandAction(ActionBase):
os.killpg(pgid, signal.SIGKILL) # Kill the process os.killpg(pgid, signal.SIGKILL) # Kill the process
time.sleep(0.1) time.sleep(0.1)
retcode = popen.poll() retcode = popen.poll()
except OSError, e: except OSError as e:
logSys.error("%s -- failed with %s" % (realCmd, e)) logSys.error("%s -- failed with %s" % (realCmd, e))
finally: finally:
_cmd_lock.release() _cmd_lock.release()