1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Rewrite with "try ... finally" instead of "with" statement to support Python 2.4.

This commit is contained in:
Hiroaki Nakamura 2013-08-25 07:44:18 +09:00
parent 3b1d7d88b5
commit 4f91238e34

View file

@ -145,16 +145,22 @@ class DebianStrategy(GenericStrategy):
def get_permanent_hostname(self): def get_permanent_hostname(self):
try: try:
with open(self.HOSTNAME_FILE) as f: f = open(self.HOSTNAME_FILE)
try:
return f.read().split() return f.read().split()
finally:
f.close()
except Exception, err: except Exception, err:
self.module.fail_json(msg="failed to read hostname: %s" % self.module.fail_json(msg="failed to read hostname: %s" %
str(err)) str(err))
def set_permanent_hostname(self, name): def set_permanent_hostname(self, name):
try: try:
with open(self.HOSTNAME_FILE, 'w+') as f: f = open(self.HOSTNAME_FILE, 'w+')
try:
f.write("%s\n" % name) f.write("%s\n" % name)
finally:
f.close()
except Exception, err: except Exception, err:
self.module.fail_json(msg="failed to update hostname: %s" % self.module.fail_json(msg="failed to update hostname: %s" %
str(err)) str(err))
@ -180,11 +186,14 @@ class RedHatStrategy(GenericStrategy):
def get_permanent_hostname(self): def get_permanent_hostname(self):
try: try:
with open(self.NETWORK_FILE, 'rb') as f: f = open(self.NETWORK_FILE, 'rb')
try:
for line in f.readlines(): for line in f.readlines():
if line.startswith('HOSTNAME'): if line.startswith('HOSTNAME'):
k, v = line.split('=') k, v = line.split('=')
return v.strip() return v.strip()
finally:
f.close()
except Exception, err: except Exception, err:
self.module.fail_json(msg="failed to read hostname: %s" % self.module.fail_json(msg="failed to read hostname: %s" %
str(err)) str(err))
@ -192,14 +201,20 @@ class RedHatStrategy(GenericStrategy):
def set_permanent_hostname(self, name): def set_permanent_hostname(self, name):
try: try:
lines = [] lines = []
with open(self.NETWORK_FILE, 'rb') as f: f = open(self.NETWORK_FILE, 'rb')
try:
for line in f.readlines(): for line in f.readlines():
if line.startswith('HOSTNAME'): if line.startswith('HOSTNAME'):
lines.append("HOSTNAME=%s\n" % name) lines.append("HOSTNAME=%s\n" % name)
else: else:
lines.append(line) lines.append(line)
with open(self.NETWORK_FILE, 'w+') as f: finally:
f.close()
f = open(self.NETWORK_FILE, 'w+')
try:
f.writelines(lines) f.writelines(lines)
finally:
f.close()
except Exception, err: except Exception, err:
self.module.fail_json(msg="failed to update hostname: %s" % self.module.fail_json(msg="failed to update hostname: %s" %
str(err)) str(err))