Use try...finally or with to ensure files get closed

Best practices state that you should use a mechanism that ensures files
get closed in case of any error, so let's do that.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2019-01-15 11:35:25 +13:00
parent 24739d9369
commit 9c65bf254e
4 changed files with 59 additions and 53 deletions

View File

@ -55,16 +55,20 @@ def generate_patches(tinfoil, fetchdir, changeset, outputdir):
(tmptarfd, tmptarname) = tempfile.mkstemp('.tar.gz', 'bulkchange-', outputdir) (tmptarfd, tmptarname) = tempfile.mkstemp('.tar.gz', 'bulkchange-', outputdir)
tmptarfile = os.fdopen(tmptarfd, "wb") tmptarfile = os.fdopen(tmptarfd, "wb")
tar = tarfile.open(None, "w:gz", tmptarfile) tar = tarfile.open(None, "w:gz", tmptarfile)
try:
for patch in patches: for patch in patches:
patchfn = os.path.join(tmpoutdir, patch) patchfn = os.path.join(tmpoutdir, patch)
tar.add(patchfn, arcname=patch) tar.add(patchfn, arcname=patch)
finally:
tar.close() tar.close()
ret = tmptarname ret = tmptarname
elif len(patches) == 1: elif len(patches) == 1:
(tmppatchfd, tmppatchname) = tempfile.mkstemp('.patch', 'bulkchange-', outputdir) (tmppatchfd, tmppatchname) = tempfile.mkstemp('.patch', 'bulkchange-', outputdir)
tmppatchfile = os.fdopen(tmppatchfd, "wb") tmppatchfile = os.fdopen(tmppatchfd, "wb")
try:
with open(os.path.join(tmpoutdir, patches[0]), "rb") as patchfile: with open(os.path.join(tmpoutdir, patches[0]), "rb") as patchfile:
shutil.copyfileobj(patchfile, tmppatchfile) shutil.copyfileobj(patchfile, tmppatchfile)
finally:
tmppatchfile.close() tmppatchfile.close()
ret = tmppatchname ret = tmppatchname

View File

@ -1457,6 +1457,7 @@ def task_log_view(request, task_id):
f = open(os.path.join(settings.TASK_LOG_DIR, 'task_%s.log' % task_id), 'rb') f = open(os.path.join(settings.TASK_LOG_DIR, 'task_%s.log' % task_id), 'rb')
except FileNotFoundError: except FileNotFoundError:
raise Http404 raise Http404
try:
f.seek(int(start)) f.seek(int(start))
# We need to escape this or else things that look like tags in the output # We need to escape this or else things that look like tags in the output
# will be interpreted as such by the browser # will be interpreted as such by the browser
@ -1476,6 +1477,8 @@ def task_log_view(request, task_id):
response['Task-Done'] = '0' response['Task-Done'] = '0'
preader = utils.ProgressReader(settings.TASK_LOG_DIR, task_id) preader = utils.ProgressReader(settings.TASK_LOG_DIR, task_id)
response['Task-Progress'] = preader.read() response['Task-Progress'] = preader.read()
finally:
f.close()
return response return response
def task_stop_view(request, task_id): def task_stop_view(request, task_id):

View File

@ -69,15 +69,13 @@ def search_package_in_distros(pkglst_dir, recipe, data):
else: else:
pn = recipe_name pn = recipe_name
f = open(os.path.join(pkglst_dir, distro_file), "r") with open(os.path.join(pkglst_dir, distro_file), "r") as f:
for line in f: for line in f:
(pkg, section) = line.split(":") (pkg, section) = line.split(":")
if pn == pkg: if pn == pkg:
distro_complete = distro + "-" + section[:-1] distro_complete = distro + "-" + section[:-1]
distros[distro_complete] = pn distros[distro_complete] = pn
f.close()
break break
f.close()
return distros return distros

View File

@ -102,8 +102,9 @@ def maintainers_inc_history(options, logger, maintplan, layerbranch, repodir, la
utils.runcmd("git checkout %s -f" % commit, utils.runcmd("git checkout %s -f" % commit,
repodir, logger=logger) repodir, logger=logger)
lines = [line.strip() for line in open(maintainers_full_path)] with open(maintainers_full_path, 'r') as f:
for line in lines: for line in f:
line = line.strip()
res = get_recipe_maintainer(line, logger) res = get_recipe_maintainer(line, logger)
if res: if res:
(pn, name, email) = res (pn, name, email) = res