Skip to content

Commit

Permalink
fix remaining flushing/closing issues in ZipStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed May 11, 2021
1 parent dc36d00 commit fef4cf7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/sourmash/sbt_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,21 @@ def close(self):
# TODO: this is not ideal; checking for zipfile.fp is looking at
# internal implementation details from CPython...
if self.zipfile is not None or self.bufferzip is not None:
self.flush()
self.flush(keep_closed=True)
self.zipfile.close()
self.zipfile = None

def flush(self):
def flush(self, *, keep_closed=False):
# This is a bit complicated, but we have to deal with new data
# (if the original zipfile is read-only) and possible duplicates.

if self.bufferzip is None:
# The easy case: close (to force flushing) and reopen the zipfile
if self.zipfile is not None:
self.zipfile.close()
self.zipfile = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
if not keep_closed:
self.zipfile = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
else:
# The complicated one. Need to consider:
# - Is there data in the buffer?
Expand Down Expand Up @@ -230,14 +231,16 @@ def flush(self):
final_file.close()
os.unlink(self.path)
shutil.move(tempfile.name, self.path)
self.zipfile = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
if not keep_closed:
self.zipfile = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
elif new_data:
# Since there is no duplicated data, we can
# reopen self.zipfile in append mode and write the new data
self.zipfile.close()
zf = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
if not keep_closed:
zf = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
for item in new_data:
zf.writestr(item, self.bufferzip.read(item))
self.zipfile = zf
Expand Down

0 comments on commit fef4cf7

Please sign in to comment.