fixed try/except/finally to be 2.4 compatible

This commit is contained in:
Brian Coca 2015-05-11 14:36:11 -04:00
parent 8ab439498c
commit d501f50c7b

View file

@ -235,33 +235,34 @@ class LocalSocketThread(Thread):
while "\n" not in data: while "\n" not in data:
data += conn.recv(2048) data += conn.recv(2048)
try: try:
new_key = AesKey.Read(data.strip())
found = False
for key in self.server.key_list:
try:
new_key.Decrypt(key.Encrypt("foo"))
found = True
break
except:
pass
if not found:
vv("adding new key to the key list")
self.server.key_list.append(new_key)
conn.sendall("OK\n")
else:
vv("key already exists in the key list, ignoring")
conn.sendall("EXISTS\n")
# update the last event time so the server doesn't
# shutdown sooner than expected for new cliets
try: try:
self.server.last_event_lock.acquire() new_key = AesKey.Read(data.strip())
self.server.last_event = datetime.now() found = False
finally: for key in self.server.key_list:
self.server.last_event_lock.release() try:
except Exception, e: new_key.Decrypt(key.Encrypt("foo"))
vv("key loaded locally was invalid, ignoring (%s)" % e) found = True
conn.sendall("BADKEY\n") break
except:
pass
if not found:
vv("adding new key to the key list")
self.server.key_list.append(new_key)
conn.sendall("OK\n")
else:
vv("key already exists in the key list, ignoring")
conn.sendall("EXISTS\n")
# update the last event time so the server doesn't
# shutdown sooner than expected for new cliets
try:
self.server.last_event_lock.acquire()
self.server.last_event = datetime.now()
finally:
self.server.last_event_lock.release()
except Exception, e:
vv("key loaded locally was invalid, ignoring (%s)" % e)
conn.sendall("BADKEY\n")
finally: finally:
try: try:
conn.close() conn.close()
@ -588,21 +589,22 @@ def daemonize(module, password, port, timeout, minutes, use_ipv6, pid_file):
def timer_handler(signum, _): def timer_handler(signum, _):
try: try:
server.last_event_lock.acquire() try:
td = datetime.now() - server.last_event server.last_event_lock.acquire()
# older python timedelta objects don't have total_seconds(), td = datetime.now() - server.last_event
# so we use the formula from the docs to calculate it # older python timedelta objects don't have total_seconds(),
total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 # so we use the formula from the docs to calculate it
if total_seconds >= minutes * 60: total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
log("server has been idle longer than the timeout, shutting down") if total_seconds >= minutes * 60:
server.running = False log("server has been idle longer than the timeout, shutting down")
server.shutdown() server.running = False
else: server.shutdown()
# reschedule the check else:
vvvv("daemon idle for %d seconds (timeout=%d)" % (total_seconds,minutes*60)) # reschedule the check
signal.alarm(30) vvvv("daemon idle for %d seconds (timeout=%d)" % (total_seconds,minutes*60))
except: signal.alarm(30)
pass except:
pass
finally: finally:
server.last_event_lock.release() server.last_event_lock.release()
@ -704,14 +706,15 @@ def main():
# try to connect to the file socket for the daemon if it exists # try to connect to the file socket for the daemon if it exists
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try: try:
s.connect(SOCKET_FILE) try:
s.sendall(password + '\n') s.connect(SOCKET_FILE)
data = "" s.sendall(password + '\n')
while '\n' not in data: data = ""
data += s.recv(2048) while '\n' not in data:
res = data.strip() data += s.recv(2048)
except: res = data.strip()
module.fail_json(msg="failed to connect to the local socket file") except:
module.fail_json(msg="failed to connect to the local socket file")
finally: finally:
try: try:
s.close() s.close()