Skip to content

Commit

Permalink
Merge pull request #57 from fls-bioinformatics-core/search-fix-duplic…
Browse files Browse the repository at this point in the history
…ated-results

'extract' command: fix bug with duplicated matches
  • Loading branch information
pjbriggs authored Dec 12, 2024
2 parents 8517e27 + 7dffada commit 45773c7
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ngsarchiver/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ def search(self,name=None,path=None,case_insensitive=False):
if not name and not path:
# Nothing to do
return
matches = set()
if case_insensitive:
if name:
name = name.lower()
Expand All @@ -1121,10 +1122,14 @@ def search(self,name=None,path=None,case_insensitive=False):
else:
p_ = p
if name:
if fnmatch.fnmatch(os.path.basename(p_),name):
if fnmatch.fnmatch(os.path.basename(p_),name) and \
m not in matches:
matches.add(m)
yield m
if path:
if fnmatch.fnmatch(p_,path):
if fnmatch.fnmatch(p_,path) and \
m not in matches:
matches.add(m)
yield m

def extract_files(self,name,extract_dir=None,include_path=False):
Expand Down
108 changes: 108 additions & 0 deletions ngsarchiver/test/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,13 @@ def test_archivedirectory_single_subarchive(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -1567,6 +1574,13 @@ def test_archivedirectory_multiple_subarchives(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -1705,6 +1719,13 @@ def test_archivedirectory_multiple_subarchives_and_file(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -1834,6 +1855,11 @@ def test_archivedirectory_multi_volume_single_subarchive(self):
path="example/subdir*/ex1.txt")]),
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2011,6 +2037,13 @@ def test_archivedirectory_multi_volume_multiple_subarchives(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2205,6 +2238,13 @@ def test_archivedirectory_multi_volume_multiple_subarchives_and_file(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2327,6 +2367,13 @@ def test_archivedirectory_with_external_symlink(self):
path="example_external_symlinks/subdir*/*symlink1.txt")]),
["example_external_symlinks/subdir1/symlink1.txt",
"example_external_symlinks/subdir2/external_symlink1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example_external_symlinks/ex1.txt",
"example_external_symlinks/subdir1/ex1.txt",
"example_external_symlinks/subdir2/ex1.txt",
"example_external_symlinks/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2482,6 +2529,13 @@ def test_archivedirectory_with_broken_symlink(self):
path="example_broken_symlinks/subdir*/*symlink1.txt")]),
["example_broken_symlinks/subdir1/symlink1.txt",
"example_broken_symlinks/subdir2/broken_symlink1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example_broken_symlinks/ex1.txt",
"example_broken_symlinks/subdir1/ex1.txt",
"example_broken_symlinks/subdir2/ex1.txt",
"example_broken_symlinks/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2637,6 +2691,13 @@ def test_legacy_archivedirectory_single_subarchive(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2760,6 +2821,13 @@ def test_legacy_archivedirectory_multiple_subarchives(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -2892,6 +2960,13 @@ def test_legacy_archivedirectory_multiple_subarchives_and_file(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -3015,6 +3090,11 @@ def test_legacy_archivedirectory_multi_volume_single_subarchive(self):
path="example/subdir*/ex1.txt")]),
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -3186,6 +3266,13 @@ def test_legacy_archivedirectory_multi_volume_multiple_subarchives(self):
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -3374,6 +3461,13 @@ def test_legacy_archivedirectory_multi_volume_multiple_subarchives_and_file(self
["example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example/ex1.txt",
"example/subdir1/ex1.txt",
"example/subdir2/ex1.txt",
"example/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -3490,6 +3584,13 @@ def test_legacy_archivedirectory_with_external_symlink(self):
path="example_external_symlinks/subdir*/*symlink1.txt")]),
["example_external_symlinks/subdir1/symlink1.txt",
"example_external_symlinks/subdir2/external_symlink1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example_external_symlinks/ex1.txt",
"example_external_symlinks/subdir1/ex1.txt",
"example_external_symlinks/subdir2/ex1.txt",
"example_external_symlinks/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down Expand Up @@ -3640,6 +3741,13 @@ def test_legacy_archivedirectory_with_broken_symlink(self):
path="example_broken_symlinks/subdir*/*symlink1.txt")]),
["example_broken_symlinks/subdir1/symlink1.txt",
"example_broken_symlinks/subdir2/broken_symlink1.txt"])
self.assertEqual(sorted([x.path for x in a.search(
name="ex1.*",
path="*/ex1.txt")]),
["example_broken_symlinks/ex1.txt",
"example_broken_symlinks/subdir1/ex1.txt",
"example_broken_symlinks/subdir2/ex1.txt",
"example_broken_symlinks/subdir3/ex1.txt"])
# Verify archive
self.assertTrue(a.verify_archive())
# Unpack
Expand Down

0 comments on commit 45773c7

Please sign in to comment.