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

Open temp file only once

mkstemp() returns a tuple containing an OS-level handle to an open file
(as would be returned by os.open()) and the absolute pathname of that
file, in that order.

This patch makes sure that the fd opened by tempfile.mkstemp() is
re-used and closed properly.
This commit is contained in:
Nehal J Wani 2016-12-12 17:57:29 +05:30 committed by Toshio Kuratomi
parent 39af9b5a86
commit ead0022255

View file

@ -575,19 +575,10 @@ class JenkinsPlugin(object):
# Write the updates file
update_fd, updates_file = tempfile.mkstemp()
os.write(update_fd, r.read())
try:
fd = open(updates_file, 'wb')
except IOError:
e = get_exception()
self.module.fail_json(
msg="Cannot open the tmp updates file %s." % updates_file,
details=str(e))
fd.write(r.read())
try:
fd.close()
os.close(update_fd)
except IOError:
e = get_exception()
self.module.fail_json(
@ -651,25 +642,15 @@ class JenkinsPlugin(object):
def _write_file(self, f, data):
# Store the plugin into a temp file and then move it
tmp_f_tuple, tmp_f = tempfile.mkstemp()
try:
fd = open(tmp_f, 'wb')
except IOError:
e = get_exception()
self.module.fail_json(
msg='Cannot open the temporal plugin file %s.' % tmp_f,
details=str(e))
tmp_f_fd, tmp_f = tempfile.mkstemp()
if isinstance(data, str):
d = data
os.write(tmp_f_fd, data)
else:
d = data.read()
fd.write(d)
os.write(tmp_f_fd, data.read())
try:
fd.close()
os.close(tmp_f_fd)
except IOError:
e = get_exception()
self.module.fail_json(