Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] check to make sure that .zip files exist before trying to load from them. #1777

Merged
merged 8 commits into from
Jan 17, 2022
5 changes: 5 additions & 0 deletions src/sourmash/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ def save(self, path):
def load(cls, location, traverse_yield_all=False, use_manifest=True):
"Class method to load a zipfile."
from .sbt_storage import ZipStorage

# we can only load from existing zipfiles in this method.
if not os.path.exists(location):
raise FileNotFoundError(location)

storage = ZipStorage(location)
return cls(storage, traverse_yield_all=traverse_yield_all,
use_manifest=use_manifest)
Expand Down
10 changes: 8 additions & 2 deletions src/sourmash/sourmash_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,14 @@ def _load_zipfile(filename, **kwargs):
db = None
if filename.endswith('.zip'):
traverse_yield_all = kwargs['traverse_yield_all']
db = ZipFileLinearIndex.load(filename,
traverse_yield_all=traverse_yield_all)
try:
db = ZipFileLinearIndex.load(filename,
traverse_yield_all=traverse_yield_all)
except FileNotFoundError as exc:
# turn this into a ValueError => proper exception handling by
# _load_database.
raise ValueError(exc)

return db


Expand Down
12 changes: 12 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,18 @@ def test_index_same_md5sum_sbt_zipstorage(c):
assert len([f for f in zout.namelist() if f.startswith(".sbt.zzz/")]) == 5


def test_zipfile_does_not_exist(runtmp):
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.sourmash('sig', 'describe', 'no-exist.zip')

# old behavior, pre PR #1777
assert 'FileNotFoundError: SOURMASH-MANIFEST.csv' not in str(exc)
assert not os.path.exists(runtmp.output('no-exist.zip'))

# correct behavior
assert "ERROR: Error while reading signatures from 'no-exist.zip'." in str(exc)


@utils.in_thisdir
def test_zipfile_protein_command_search(c):
# test command-line search/gather of zipfile with protein sigs
Expand Down