Skip to content

Commit 90a6817

Browse files
committed
Handle extracting the same file to the same destination
When a source is added multiple times the content after the 2nd extraction will be the same and no new files/folders are detected. So `extract_file` would return the parent folder. However if there was a clear result after the first extraction the same result should be returned. This works trivially until something else is extracted to the same folder between the 2 extraction operations.
1 parent 5cc1038 commit 90a6817

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

easybuild/tools/filetools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,12 @@ def extract_file(fn, dest, cmd=None, extra_options=None, overwrite=False, forced
508508
previous_paths = set(_get_paths_purged(abs_dest))
509509
run_shell_cmd(cmd, in_dry_run=forced, hidden=not trace)
510510
new_paths = set(_get_paths_purged(abs_dest)) - previous_paths
511-
if len(new_paths) == 1:
511+
if len(new_paths) == 0:
512+
_log.warning(f"No new folder/files found after extraction of {fn} in {abs_dest}, "
513+
f"verify that the same file is not extracted multiple times! "
514+
f"Existing paths: {', '.join(previous_paths)}")
515+
base_dir = find_base_dir() # Fallback
516+
elif len(new_paths) == 1:
512517
new_path = new_paths.pop()
513518
if os.path.isdir(new_path):
514519
# note: find_base_dir also changes into the base dir!

test/framework/filetools.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,9 +2622,18 @@ def test_extract_file(self):
26222622
with self.mocked_stdout_stderr():
26232623
path = ft.extract_file(toy_tarball, extraction_path, change_into_dir=False)
26242624
self.assertTrue(os.path.samefile(path, toy_path))
2625+
# Extracting the same tarball again detects the same folder
2626+
path = ft.extract_file(toy_tarball, extraction_path, change_into_dir=False)
2627+
self.assertTrue(os.path.samefile(path, toy_path))
2628+
26252629
path = ft.extract_file(bar_tarball, extraction_path, change_into_dir=False)
26262630
self.assertTrue(os.path.samefile(path, os.path.join(extraction_path, 'bar-0.0')))
26272631

2632+
# Extracting the same tarball again now can't detect the same folder as there is another one next to it.
2633+
# Should fall back to the parent dir
2634+
path = ft.extract_file(toy_tarball, extraction_path, change_into_dir=False)
2635+
self.assertTrue(os.path.samefile(path, extraction_path))
2636+
26282637
# Contains just a file
26292638
path = ft.extract_file(file_tarball, extraction_path, change_into_dir=False)
26302639
self.assertTrue(os.path.samefile(path, extraction_path))

0 commit comments

Comments
 (0)