diff --git a/ngsarchiver/archive.py b/ngsarchiver/archive.py index cb7773a..e306f53 100644 --- a/ngsarchiver/archive.py +++ b/ngsarchiver/archive.py @@ -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 @@ -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 diff --git a/ngsarchiver/test/test_archive.py b/ngsarchiver/test/test_archive.py index d3f7980..b4887fb 100644 --- a/ngsarchiver/test/test_archive.py +++ b/ngsarchiver/test/test_archive.py @@ -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: @@ -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: @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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 @@ -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 @@ -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 @@ -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