Skip to content

Commit db51023

Browse files
committed
Only remove folders that contain no files recursively
Otherwise 'include/libfoo/foo.h' would be removed
1 parent bf663f7 commit db51023

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

easybuild/framework/easyblock.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
from easybuild.tools.filetools import diff_files, download_file, encode_class_name, extract_file
7676
from easybuild.tools.filetools import find_backup_name_candidate, get_source_tarball_from_git, is_alt_pypi_url
7777
from easybuild.tools.filetools import is_sha256_checksum, mkdir, move_file, move_logs, read_file, remove_dir
78-
from easybuild.tools.filetools import remove_file, rmtree2, verify_checksum, weld_paths, write_file
78+
from easybuild.tools.filetools import remove_file, rmtree2, verify_checksum, weld_paths, write_file, is_empty_folder
7979
from easybuild.tools.hooks import BUILD_STEP, CLEANUP_STEP, CONFIGURE_STEP, EXTENSIONS_STEP, FETCH_STEP, INSTALL_STEP
8080
from easybuild.tools.hooks import MODULE_STEP, PACKAGE_STEP, PATCH_STEP, PERMISSIONS_STEP, POSTITER_STEP, POSTPROC_STEP
8181
from easybuild.tools.hooks import PREPARE_STEP, READY_STEP, SANITYCHECK_STEP, SOURCE_STEP, TEST_STEP, TESTCASES_STEP
@@ -1305,15 +1305,14 @@ def make_module_req(self):
13051305
if path and not self.dry_run:
13061306
paths = sorted(glob.glob(path))
13071307
if paths and key in keys_requiring_files:
1308-
self.log.info("Only retaining paths for %s that include at least one file: %s", key, paths)
1309-
# only retain paths that include at least one file
1310-
retained_paths = []
1311-
for path in paths:
1312-
full_path = os.path.join(self.installdir, path)
1313-
if os.path.isdir(full_path):
1314-
if any(os.path.isfile(os.path.join(full_path, x)) for x in os.listdir(full_path)):
1315-
retained_paths.append(path)
1316-
self.log.info("Retained paths for %s: %s", key, retained_paths)
1308+
# only retain paths that contain at least one file
1309+
retained_paths = [
1310+
path for path in paths
1311+
if os.path.isdir(os.path.join(self.installdir, path))
1312+
and not is_empty_folder(os.path.join(self.installdir, path))
1313+
]
1314+
self.log.info("Only retaining paths for %s that contain at least one file: %s -> %s",
1315+
key, paths, retained_paths)
13171316
paths = retained_paths
13181317
else:
13191318
# empty string is a valid value here (i.e. to prepend the installation prefix, cfr $CUDA_HOME)

easybuild/tools/filetools.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,14 @@ def search_file(paths, query, short=False, ignore_dirs=None, silent=False, filen
681681
return var_defs, hits
682682

683683

684+
def is_empty_folder(folder_path):
685+
"""Return true if the given folder does not contain any files"""
686+
for _root, _dirs, files in os.walk(folder_path):
687+
if files:
688+
return False
689+
return True
690+
691+
684692
def find_eb_script(script_name):
685693
"""Find EasyBuild script with given name (in easybuild/scripts subdirectory)."""
686694
filetools, eb_dir = __file__, None
@@ -746,7 +754,7 @@ def calc_block_checksum(path, algorithm):
746754
try:
747755
# in hashlib, blocksize is a class parameter
748756
blocksize = algorithm.blocksize * 262144 # 2^18
749-
except AttributeError as err:
757+
except AttributeError:
750758
blocksize = 16777216 # 2^24
751759
_log.debug("Using blocksize %s for calculating the checksum" % blocksize)
752760

test/framework/filetools.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,32 @@ def test_search_file(self):
17241724
for pattern in ['*foo', '(foo', ')foo', 'foo)', 'foo(']:
17251725
self.assertErrorRegex(EasyBuildError, "Invalid search query", ft.search_file, [test_ecs], pattern)
17261726

1727+
def test_is_empty_folder(self):
1728+
tmpdir = tempfile.mkdtemp()
1729+
self.assertTrue(ft.is_empty_folder(tmpdir))
1730+
# Containing a folder is still empty
1731+
subdir = tempfile.mkdtemp(dir=tmpdir)
1732+
self.assertTrue(ft.is_empty_folder(tmpdir))
1733+
# A file in a subfolder makes it non-empty
1734+
sub_filename = os.path.join(subdir, 'testfile.h')
1735+
ft.write_file(sub_filename, '')
1736+
self.assertFalse(ft.is_empty_folder(tmpdir))
1737+
# A file in the folder makes it non-empty
1738+
filename = os.path.join(tmpdir, 'testfile.h')
1739+
ft.write_file(filename, '')
1740+
self.assertFalse(ft.is_empty_folder(tmpdir))
1741+
# And even when the file in the subfolder or the subfolder is removed
1742+
# as 'filename' stays
1743+
os.remove(sub_filename)
1744+
self.assertFalse(ft.is_empty_folder(tmpdir))
1745+
os.rmdir(subdir)
1746+
self.assertFalse(ft.is_empty_folder(tmpdir))
1747+
# And true again when the file is removed
1748+
os.remove(filename)
1749+
self.assertTrue(ft.is_empty_folder(tmpdir))
1750+
# Cleanup
1751+
os.rmdir(tmpdir)
1752+
17271753
def test_find_eb_script(self):
17281754
"""Test find_eb_script function."""
17291755
self.assertTrue(os.path.exists(ft.find_eb_script('rpath_args.py')))

0 commit comments

Comments
 (0)