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,17 +55,21 @@ 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)
for patch in patches: try:
patchfn = os.path.join(tmpoutdir, patch) for patch in patches:
tar.add(patchfn, arcname=patch) patchfn = os.path.join(tmpoutdir, patch)
tar.close() tar.add(patchfn, arcname=patch)
finally:
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")
with open(os.path.join(tmpoutdir, patches[0]), "rb") as patchfile: try:
shutil.copyfileobj(patchfile, tmppatchfile) with open(os.path.join(tmpoutdir, patches[0]), "rb") as patchfile:
tmppatchfile.close() shutil.copyfileobj(patchfile, tmppatchfile)
finally:
tmppatchfile.close()
ret = tmppatchname ret = tmppatchname
shutil.rmtree(tmpoutdir) shutil.rmtree(tmpoutdir)

View File

@ -1457,25 +1457,28 @@ 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
f.seek(int(start)) try:
# We need to escape this or else things that look like tags in the output f.seek(int(start))
# will be interpreted as such by the browser # We need to escape this or else things that look like tags in the output
data = escape(f.read()) # will be interpreted as such by the browser
response = HttpResponse(data) data = escape(f.read())
if result.ready(): response = HttpResponse(data)
response['Task-Done'] = '1' if result.ready():
updateobj = get_object_or_404(Update, task_id=task_id) response['Task-Done'] = '1'
response['Task-Duration'] = utils.timesince2(updateobj.started, updateobj.finished) updateobj = get_object_or_404(Update, task_id=task_id)
response['Task-Progress'] = 100 response['Task-Duration'] = utils.timesince2(updateobj.started, updateobj.finished)
if result.info: response['Task-Progress'] = 100
if isinstance(result.info, dict): if result.info:
response['Task-Result'] = result.info.get('retcode', None) if isinstance(result.info, dict):
else: response['Task-Result'] = result.info.get('retcode', None)
response['Task-Result'] = -1 else:
else: response['Task-Result'] = -1
response['Task-Done'] = '0' else:
preader = utils.ProgressReader(settings.TASK_LOG_DIR, task_id) response['Task-Done'] = '0'
response['Task-Progress'] = preader.read() preader = utils.ProgressReader(settings.TASK_LOG_DIR, task_id)
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,27 +102,28 @@ 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:
res = get_recipe_maintainer(line, logger) line = line.strip()
if res: res = get_recipe_maintainer(line, logger)
(pn, name, email) = res if res:
qry = Recipe.objects.filter(pn = pn, layerbranch = layerbranch) (pn, name, email) = res
qry = Recipe.objects.filter(pn = pn, layerbranch = layerbranch)
if qry: if qry:
m = Maintainer.create_or_update(name, email) m = Maintainer.create_or_update(name, email)
rm = RecipeMaintainer() rm = RecipeMaintainer()
rm.recipe = qry[0] rm.recipe = qry[0]
rm.maintainer = m rm.maintainer = m
rm.history = rms rm.history = rms
rm.save() rm.save()
logger.debug("%s: Change maintainer to %s in commit %s." % \ logger.debug("%s: Change maintainer to %s in commit %s." % \
(pn, m.name, commit)) (pn, m.name, commit))
else: else:
logger.debug("%s: Not found in %s." % \ logger.debug("%s: Not found in %s." % \
(pn, layerbranch)) (pn, layerbranch))
# set missing recipes to no maintainer # set missing recipes to no maintainer
for recipe in layerbranch.recipe_set.all(): for recipe in layerbranch.recipe_set.all():