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

View File

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