From ead002225548c88f400451b765935d3f516191ae Mon Sep 17 00:00:00 2001 From: Nehal J Wani Date: Mon, 12 Dec 2016 17:57:29 +0530 Subject: [PATCH] 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. --- .../web_infrastructure/jenkins_plugin.py | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/lib/ansible/modules/web_infrastructure/jenkins_plugin.py b/lib/ansible/modules/web_infrastructure/jenkins_plugin.py index 832dbe060a..41568357f1 100644 --- a/lib/ansible/modules/web_infrastructure/jenkins_plugin.py +++ b/lib/ansible/modules/web_infrastructure/jenkins_plugin.py @@ -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(