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

This commit is contained in:
Mads Jensen 2019-11-29 06:37:23 +01:00 committed by ansibot
parent deb0cbbf73
commit d335d7a62c
2 changed files with 12 additions and 16 deletions

View file

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

View file

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