Skip to content

Commit cf41bdb

Browse files
committed
py3k: Use contexts for file I/O in fwfetcher.
Signed-off-by: Elliott Sales de Andrade <[email protected]>
1 parent a30d29e commit cf41bdb

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/fwfetcher.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ def dump_png(infile, pnglen, maxlen, pngid):
154154
if nice_open_file(outname):
155155
buf = infile.read(pnglen)
156156
print("Writing PNG file", outname)
157-
outfile = open(outname, "wb")
158-
print(buf, end=' ', file=outfile)
159-
outfile.close()
157+
with open(outname, "wb") as outfile:
158+
print(buf, end=' ', file=outfile)
160159
else:
161160
print("PNG image %s too large (%i instead of maximal %i bytes), "
162161
"file not written." % (pngid, pnglen, maxlen))
@@ -350,9 +349,8 @@ def fill_directory(infile, txtfile, contents, firstclust, makedir, start,
350349
startclust += 1
351350
adstart += 0x1000
352351
filelen -= 0x1000
353-
outfile = open(outname, "wb")
354-
print(buf, end=' ', file=outfile)
355-
outfile.close()
352+
with open(outname, "wb") as outfile:
353+
print(buf, end=' ', file=outfile)
356354

357355
do_utime(outname, dati2, dati1)
358356

@@ -526,9 +524,9 @@ def getFileOrURL(filename, url):
526524
# If so, return its contents. If not, download it, save it, and return its
527525
# contents.
528526
try:
529-
f = open(filename)
530-
print("Found", filename, "cached on disk, using local copy")
531-
retval = f.read()
527+
with open(filename) as f:
528+
print("Found", filename, "cached on disk, using local copy")
529+
retval = f.read()
532530
return retval
533531
except IOError:
534532
pass
@@ -545,19 +543,18 @@ def getFileOrURL(filename, url):
545543
print("Reading response...")
546544
retval = response.read()
547545
# Save downloaded file to disk
548-
f = open(filename, "wb")
549-
f.write(retval)
550-
f.close()
546+
with open(filename, "wb") as f:
547+
f.write(retval)
551548
print("done, saved to", filename)
552549
return retval
553550

554551
def extractPirsFromZip(systemupdate):
555552
print("Extracting $SystemUpdate/FFFE07DF00000001 from system update "
556553
"file...")
557554
updatefile = StringIO.StringIO(systemupdate)
558-
z = zipfile.ZipFile(updatefile)
559-
# print(z.namelist())
560-
pirs = z.open("$SystemUpdate/FFFE07DF00000001").read()
555+
with zipfile.ZipFile(updatefile) as z:
556+
# print(z.namelist())
557+
pirs = z.open("$SystemUpdate/FFFE07DF00000001").read()
561558
print("done.")
562559
return pirs
563560

0 commit comments

Comments
 (0)