Use context manager in yum module. (#65376)

This commit is contained in:
Mads Jensen 2020-04-14 09:21:22 +02:00 committed by GitHub
parent 8077d8e401
commit eaf648e600
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 11 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- packaging_yum - replace legacy file handling with a file manager.

View file

@ -506,19 +506,11 @@ class YumRepo(object):
if len(self.repofile.sections()): if len(self.repofile.sections()):
# Write data into the file # Write data into the file
try: try:
fd = open(self.params['dest'], 'w') with open(self.params['dest'], 'w') as fd:
self.repofile.write(fd)
except IOError as e: except IOError as e:
self.module.fail_json( self.module.fail_json(
msg="Cannot open repo file %s." % self.params['dest'], msg="Problems handling file %s." % self.params['dest'],
details=to_native(e))
self.repofile.write(fd)
try:
fd.close()
except IOError as e:
self.module.fail_json(
msg="Cannot write repo file %s." % self.params['dest'],
details=to_native(e)) details=to_native(e))
else: else:
# Remove the file if there are not repos # Remove the file if there are not repos