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,17 +308,17 @@ 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()
os.chmod(mock_sh_path, 0o755) os.chmod(mock_sh_path, 0o755)
os.chmod(iso_template_path, 0o755) os.chmod(iso_template_path, 0o755)
@ -890,9 +890,9 @@ 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(
boot_iso=boot_iso, boot_iso=boot_iso,
isoname=isoname, isoname=isoname,
@ -900,9 +900,9 @@ 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
make_image = '{} {}'.format( make_image = '{} {}'.format(
@ -934,21 +934,21 @@ 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()
os.chmod(mock_sh_path, 0o755) os.chmod(mock_sh_path, 0o755)
os.chmod(iso_template_path, 0o755) os.chmod(iso_template_path, 0o755)
@ -1273,39 +1273,39 @@ 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
for upda in update: for upda in update:
if fnmatch(zm, upda): if fnmatch(zm, upda):
#print(f'updating: {zm} {upda}') #print(f'updating: {zm} {upda}')
replace = True replace = True
break break
for excl in exclude: for excl in exclude:
if fnmatch(zm, excl): if fnmatch(zm, excl):
#print(f'ignoring: {zm} {excl}') #print(f'ignoring: {zm} {excl}')
found = True found = True
break break
if found: if found:
continue continue
mcmd = "-update" if replace else "-map" mcmd = "-update" if replace else "-map"
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:
# if fnmatch(zl, excl): # if fnmatch(zl, excl):
# found = True # found = True
# 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,26 +65,27 @@ 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: while True:
logger.error("Could not open file %s: %s" % (path, e)) chunk = input_file.read(8192)
if not chunk:
break
checksum.update(chunk)
input_file.close()
except IOError as exc:
logger.error("Could not open file %s: %s", path, exc)
return False return False
while True:
chunk = input_file.read(8192)
if not chunk:
break
checksum.update(chunk)
input_file.close()
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,