Use context manager for mssql and postgresql modules. (#65224)

pull/65359/head
Mads Jensen 5 years ago committed by ansibot
parent deb0cbbf73
commit d335d7a62c

@ -122,8 +122,7 @@ def db_delete(conn, cursor, db):
def db_import(conn, cursor, module, db, target): def db_import(conn, cursor, module, db, target):
if os.path.isfile(target): if os.path.isfile(target):
backup = open(target, 'r') with open(target, 'r') as backup:
try:
sqlQuery = "USE [%s]\n" % db sqlQuery = "USE [%s]\n" % db
for line in backup: for line in backup:
if line is None: if line is None:
@ -135,8 +134,6 @@ def db_import(conn, cursor, module, db, target):
sqlQuery += line sqlQuery += line
cursor.execute(sqlQuery) cursor.execute(sqlQuery)
conn.commit() conn.commit()
finally:
backup.close()
return 0, "import successful", "" return 0, "import successful", ""
else: else:
return 1, "cannot find target file", "cannot find target file" return 1, "cannot find target file", "cannot find target file"

@ -285,18 +285,17 @@ class PgHba(object):
self.comment = [] self.comment = []
# read the pg_hbafile # read the pg_hbafile
try: try:
file = open(self.pg_hba_file, 'r') with open(self.pg_hba_file, 'r') as file:
for line in file: for line in file:
line = line.strip() line = line.strip()
# uncomment # uncomment
if '#' in line: if '#' in line:
line, comment = line.split('#', 1) line, comment = line.split('#', 1)
self.comment.append('#' + comment) self.comment.append('#' + comment)
try: try:
self.add_rule(PgHbaRule(line=line)) self.add_rule(PgHbaRule(line=line))
except PgHbaRuleError: except PgHbaRuleError:
pass pass
file.close()
self.unchanged() self.unchanged()
except IOError: except IOError:
pass pass

Loading…
Cancel
Save