Skip to content

Commit

Permalink
archive: explicitly add entry for top-level dirs when making archives…
Browse files Browse the repository at this point in the history
… in 'make_archive_tgz' & 'make_archive_multitgz'.
  • Loading branch information
pjbriggs committed Feb 7, 2025
1 parent 534fae8 commit 85bba5e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
31 changes: 27 additions & 4 deletions ngsarchiver/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,17 @@ def make_archive_tgz(base_name,root_dir,base_dir=None,ext="tar.gz",
compresslevel=compresslevel)
with tarfile.open(archive_name,'w:gz',compresslevel=compresslevel) \
as tgz:
# Add entry for top-level directory
if base_dir:
arcname = base_dir
else:
arcname = "."
try:
tgz.add(d.path, arcname=arcname, recursive=False)
except Exception as ex:
raise NgsArchiverException(f"{d.path}: unable to add top-level "
f"directory to archive: {ex}")
# Add contents
for o in d.walk():
if include_files and o not in include_files:
continue
Expand Down Expand Up @@ -2154,16 +2165,28 @@ def make_archive_multitgz(base_name,root_dir,base_dir=None,
"""
d = Directory(root_dir)
max_size = convert_size_to_bytes(size)
indx = 0
archive_name = None
archive_list = []
tgz = None
if d.is_empty:
# Special case: directory is empty
logger.warning(f"{d.path}: creating archive for empty directory")
archive_name = "%s.00.%s" % (base_name, ext)
return [make_empty_archive(archive_name, root_dir, base_dir=base_dir,
compresslevel=compresslevel)]
# Initialise tar archive and add entry for top-level directory
indx = 0
archive_name = "%s.%02d.%s" % (base_name, indx, ext)
tgz = tarfile.open(archive_name,'w:gz',
compresslevel=compresslevel)
archive_list = [archive_name]
if base_dir:
arcname = base_dir
else:
arcname = "."
try:
tgz.add(d.path, arcname=arcname, recursive=False)
except Exception as ex:
raise NgsArchiverException(f"{d.path}: unable to add top-level "
f"directory to archive: {ex}")
# Add the directory contents
for o in d.walk():
if include_files and o not in include_files:
continue
Expand Down
23 changes: 16 additions & 7 deletions ngsarchiver/test/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5168,7 +5168,7 @@ def test_make_archive_dir_multiple_subarchives_empty_toplevel_subdir(self):
with tarfile.open(os.path.join(archive_dir, "subdir2.tar.gz"),
"r:gz") as tgz:
members = tgz.getnames()
self.assertEqual(["example/subdir2/."], members)
self.assertEqual(["example/subdir2"], members)
# Check file list
with open(os.path.join(archive_dir, "ARCHIVE_FILELIST.txt"), "rt") as fp:
for line in fp:
Expand Down Expand Up @@ -5615,7 +5615,7 @@ def test_make_archive_dir_multi_volume_multiple_subarchives_empty_toplevel_subdi
with tarfile.open(os.path.join(archive_dir, "subdir2.00.tar.gz"),
"r:gz") as tgz:
members = tgz.getnames()
self.assertEqual(["example/subdir2/."], members)
self.assertEqual(["example/subdir2"], members)
# Check file list
with open(os.path.join(archive_dir, "ARCHIVE_FILELIST.txt"), "rt") as fp:
for line in fp:
Expand Down Expand Up @@ -5935,7 +5935,8 @@ def test_make_archive_tgz(self):
# Check archive exists
self.assertTrue(os.path.exists(test_archive_path))
# Check archive contains only expected members
expected = set(("ex1.txt",
expected = set((".",
"ex1.txt",
"subdir",
"subdir/ex2.txt",))
members = set()
Expand Down Expand Up @@ -5965,7 +5966,8 @@ def test_make_archive_tgz_with_base_dir(self):
# Check archive exists
self.assertTrue(os.path.exists(test_archive_path))
# Check archive contains only expected members
expected = set(("example/ex1.txt",
expected = set(("example",
"example/ex1.txt",
"example/subdir",
"example/subdir/ex2.txt",))
members = set()
Expand Down Expand Up @@ -6001,7 +6003,8 @@ def test_make_archive_tgz_with_include_files(self):
# Check archive exists
self.assertTrue(os.path.exists(test_archive_path))
# Check archive contains only expected members
expected = set(("ex1.txt",
expected = set((".",
"ex1.txt",
"subdir",
"subdir/ex2.txt",))
members = set()
Expand Down Expand Up @@ -6039,7 +6042,8 @@ def test_make_archive_tgz_with_exclude_files(self):
# Check archive exists
self.assertTrue(os.path.exists(test_archive_path))
# Check archive contains only expected members
expected = set(("ex1.txt",
expected = set((".",
"ex1.txt",
"subdir",
"subdir/ex2.txt",))
members = set()
Expand Down Expand Up @@ -6072,7 +6076,8 @@ def test_make_archive_tgz_non_default_compression_level(self):
# Check archive exists
self.assertTrue(os.path.exists(test_archive_path))
# Check archive contains only expected members
expected = set(("ex1.txt",
expected = set((".",
"ex1.txt",
"subdir",
"subdir/ex2.txt",))
members = set()
Expand Down Expand Up @@ -6116,6 +6121,7 @@ def test_make_archive_multitgz(self):
test_archive_paths)
# Check archives contains only expected members
expected = set(example_dir.list())
expected.add(".")
members = set()
for test_archive_path in test_archive_paths:
# Check archive exists
Expand Down Expand Up @@ -6155,6 +6161,7 @@ def test_make_archive_multitgz_with_base_dir(self):
test_archive_paths)
# Check archives contains only expected members
expected = set(example_dir.list(prefix="example"))
expected.add("example")
members = set()
for test_archive_path in test_archive_paths:
# Check archive exists
Expand Down Expand Up @@ -6201,6 +6208,7 @@ def test_make_archive_multitgz_with_include_files(self):
test_archive_paths)
# Check archives contains only expected members
expected = set(overlay_dir.list())
expected.add(".")
members = set()
for test_archive_path in test_archive_paths:
# Check archive exists
Expand Down Expand Up @@ -6284,6 +6292,7 @@ def test_make_archive_multitgz_non_default_compression_level(self):
test_archive_paths)
# Check archives contains only expected members
expected = set(example_dir.list())
expected.add(".")
members = set()
for test_archive_path in test_archive_paths:
# Check archive exists
Expand Down

0 comments on commit 85bba5e

Please sign in to comment.