Skip to content

Commit f8a1da6

Browse files
committed
add copy_files function in filetools module
1 parent 307abbe commit f8a1da6

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

easybuild/tools/filetools.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ def copy_file(path, target_path, force_in_dry_run=False):
17441744
17451745
:param path: the original filepath
17461746
:param target_path: path to copy the file to
1747-
:param force_in_dry_run: force running the command during dry run
1747+
:param force_in_dry_run: force copying of file during dry run
17481748
"""
17491749
if not force_in_dry_run and build_option('extended_dry_run'):
17501750
dry_run_msg("copied file %s to %s" % (path, target_path))
@@ -1767,6 +1767,28 @@ def copy_file(path, target_path, force_in_dry_run=False):
17671767
raise EasyBuildError("Failed to copy file %s to %s: %s", path, target_path, err)
17681768

17691769

1770+
def copy_files(paths, target_dir, force_in_dry_run=False):
1771+
"""
1772+
Copy list of files to specified target directory (which is created if it doesn't exist yet).
1773+
1774+
:param filepaths: list of files to copy
1775+
:param target_dir: target directory to copy files into
1776+
:param force_in_dry_run: force copying of files during dry run
1777+
"""
1778+
if not force_in_dry_run and build_option('extended_dry_run'):
1779+
dry_run_msg("copied files %s to %s" % (paths, target_dir))
1780+
else:
1781+
if os.path.exists(target_dir):
1782+
if os.path.isdir(target_dir):
1783+
_log.info("Copying easyconfigs into existing directory %s...", target_dir)
1784+
else:
1785+
raise EasyBuildError("%s exists but is not a directory", target_dir)
1786+
else:
1787+
mkdir(target_dir, parents=True)
1788+
for path in paths:
1789+
copy_file(path, target_dir)
1790+
1791+
17701792
def copy_dir(path, target_path, force_in_dry_run=False, **kwargs):
17711793
"""
17721794
Copy a directory from specified location to specified location

test/framework/filetools.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ def test_apply_patch(self):
13241324
self.assertErrorRegex(EasyBuildError, "Couldn't apply patch file", ft.apply_patch, toy_patch, path)
13251325

13261326
def test_copy_file(self):
1327-
""" Test copy_file """
1327+
"""Test copy_file function."""
13281328
testdir = os.path.dirname(os.path.abspath(__file__))
13291329
to_copy = os.path.join(testdir, 'easyconfigs', 'test_ecs', 't', 'toy', 'toy-0.0.eb')
13301330
target_path = os.path.join(self.test_prefix, 'toy.eb')
@@ -1385,8 +1385,44 @@ def test_copy_file(self):
13851385
self.assertTrue(ft.read_file(to_copy) == ft.read_file(target_path))
13861386
self.assertEqual(txt, '')
13871387

1388+
def test_copy_files(self):
1389+
"""Test copy_files function."""
1390+
test_ecs = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'easyconfigs', 'test_ecs')
1391+
toy_ec = os.path.join(test_ecs, 't', 'toy', 'toy-0.0.eb')
1392+
toy_ec_txt = ft.read_file(toy_ec)
1393+
bzip2_ec = os.path.join(test_ecs, 'b', 'bzip2', 'bzip2-1.0.6-GCC-4.9.2.eb')
1394+
bzip2_ec_txt = ft.read_file(bzip2_ec)
1395+
1396+
# copying a single file to a non-existing directory
1397+
target_dir = os.path.join(self.test_prefix, 'target_dir1')
1398+
ft.copy_files([toy_ec], target_dir)
1399+
copied_toy_ec = os.path.join(target_dir, 'toy-0.0.eb')
1400+
self.assertTrue(os.path.exists(copied_toy_ec))
1401+
self.assertEqual(ft.read_file(copied_toy_ec), toy_ec_txt)
1402+
1403+
# copying a single file to an existing directory
1404+
ft.copy_files([bzip2_ec], target_dir)
1405+
copied_bzip2_ec = os.path.join(target_dir, 'bzip2-1.0.6-GCC-4.9.2.eb')
1406+
self.assertTrue(os.path.exists(copied_bzip2_ec))
1407+
self.assertEqual(ft.read_file(copied_bzip2_ec), bzip2_ec_txt)
1408+
1409+
# copying multiple files to a non-existing directory
1410+
target_dir = os.path.join(self.test_prefix, 'target_dir_multiple')
1411+
ft.copy_files([toy_ec, bzip2_ec], target_dir)
1412+
copied_toy_ec = os.path.join(target_dir, 'toy-0.0.eb')
1413+
self.assertTrue(os.path.exists(copied_toy_ec))
1414+
self.assertEqual(ft.read_file(copied_toy_ec), toy_ec_txt)
1415+
copied_bzip2_ec = os.path.join(target_dir, 'bzip2-1.0.6-GCC-4.9.2.eb')
1416+
self.assertTrue(os.path.exists(copied_bzip2_ec))
1417+
self.assertEqual(ft.read_file(copied_bzip2_ec), bzip2_ec_txt)
1418+
1419+
# copying files to an existing target that is not a directory results in an error
1420+
self.assertTrue(os.path.isfile(copied_toy_ec))
1421+
error_pattern = "/toy-0.0.eb exists but is not a directory"
1422+
self.assertErrorRegex(EasyBuildError, error_pattern, ft.copy_files, [bzip2_ec], copied_toy_ec)
1423+
13881424
def test_copy_dir(self):
1389-
"""Test copy_file"""
1425+
"""Test copy_dir function."""
13901426
testdir = os.path.dirname(os.path.abspath(__file__))
13911427
to_copy = os.path.join(testdir, 'easyconfigs', 'test_ecs', 'g', 'GCC')
13921428

0 commit comments

Comments
 (0)