start using with open within reason 1/2

This commit is contained in:
Louis Abel 2023-06-04 04:09:15 -07:00
parent 8f5a8292ff
commit ee439d8af5
Signed by untrusted user: label
GPG Key ID: B37E62D143879B36
2 changed files with 71 additions and 70 deletions

View File

@ -308,15 +308,15 @@ class IsoBuild:
bugurl=self.bugurl,
)
mock_iso_entry = open(mock_iso_path, "w+")
with open(mock_iso_path, "w+") as mock_iso_entry:
mock_iso_entry.write(mock_iso_template_output)
mock_iso_entry.close()
mock_sh_entry = open(mock_sh_path, "w+")
with open(mock_sh_path, "w+") as mock_sh_entry:
mock_sh_entry.write(mock_sh_template_output)
mock_sh_entry.close()
iso_template_entry = open(iso_template_path, "w+")
with open(iso_template_path, "w+") as iso_template_entry:
iso_template_entry.write(iso_template_output)
iso_template_entry.close()
@ -890,7 +890,7 @@ class IsoBuild:
if opts['use_xorrisofs']:
# Generate a xorriso compatible dialog
xp = open(grafts)
with open(grafts) as xp:
xorpoint = xp.read()
xp.close()
xorriso_template_output = xorriso_template.render(
@ -900,7 +900,7 @@ class IsoBuild:
graft=xorpoint,
arch=arch,
)
xorriso_template_entry = open(xorriso_template_path, "w+")
with open(xorriso_template_path, "w+") as xorriso_template_entry:
xorriso_template_entry.write(xorriso_template_output)
xorriso_template_entry.close()
opts['graft_points'] = xorriso_template_path
@ -934,19 +934,19 @@ class IsoBuild:
arch=arch
)
mock_iso_entry = open(mock_iso_path, "w+")
with open(mock_iso_path, "w+") as mock_iso_entry:
mock_iso_entry.write(mock_iso_template_output)
mock_iso_entry.close()
mock_sh_entry = open(mock_sh_path, "w+")
with open(mock_sh_path, "w+") as mock_sh_entry:
mock_sh_entry.write(mock_sh_template_output)
mock_sh_entry.close()
iso_template_entry = open(iso_template_path, "w+")
with open(iso_template_path, "w+") as iso_template_entry:
iso_template_entry.write(iso_template_output)
iso_template_entry.close()
iso_readme_entry = open(iso_readme_path, "w+")
with open(iso_readme_path, "w+") as iso_readme_entry:
iso_readme_entry.write(iso_readme_template_output)
iso_readme_entry.close()
@ -1273,7 +1273,7 @@ class IsoBuild:
# We check first if a file needs to be updated first before relying on
# the boot.iso manifest to exclude a file
if self.iso_map['xorrisofs']:
fx = open(xorrspath, "w")
with open(xorrspath, "w") as fx:
for zm in sorted(result, key=Idents.sorting):
found = False
replace = False
@ -1293,9 +1293,9 @@ class IsoBuild:
fx.write("%s %s %s\n" % (mcmd, u[zm], zm))
fx.close()
else:
fh = open(filepath, "w")
self.log.info(Color.WARN + 'Nothing should be excluded in legacy ' +
'genisoimage. Ignoring exclude list.')
with open(filepath, "w") as fh:
self.log.info('%sNothing should be excluded in legacy ' \
'genisoimage. Ignoring exclude list.', Color.WARN)
for zl in sorted(result, key=Idents.sorting):
#found = False
#for excl in exclude:
@ -1304,7 +1304,7 @@ class IsoBuild:
# break
#if found:
# continue
fh.write("%s=%s\n" % (zl, u[zl]))
fh.write(f"{zl}={u[zl]}\n")
fh.close()
def run_pull_iso_images(self):

View File

@ -65,15 +65,11 @@ class Shared:
try:
checksum = hashlib.new(hashtype)
except ValueError:
logger.error("Invalid hash type: %s" % hashtype)
logger.error("Invalid hash type: %s", hashtype)
return False
try:
input_file = open(path, "rb")
except IOError as e:
logger.error("Could not open file %s: %s" % (path, e))
return False
with open(path, "rb") as input_file:
while True:
chunk = input_file.read(8192)
if not chunk:
@ -81,10 +77,15 @@ class Shared:
checksum.update(chunk)
input_file.close()
except IOError as exc:
logger.error("Could not open file %s: %s", path, exc)
return False
stat = os.stat(path)
base = os.path.basename(path)
# This emulates our current syncing scripts that runs stat and
# sha256sum and what not with a very specific output.
# pylint: disable=consider-using-f-string
return "# %s: %s bytes\n%s (%s) = %s\n" % (
base,
stat.st_size,