diff --git a/easybuild/framework/easyblock.py b/easybuild/framework/easyblock.py index 5b38a1a4a7..ea393446c2 100644 --- a/easybuild/framework/easyblock.py +++ b/easybuild/framework/easyblock.py @@ -2081,11 +2081,10 @@ def _set_module_as_default(self): sets the default module version except if we are in dry run. """ - mod_folderpath = os.path.dirname(self.module_generator.get_module_filepath()) - if self.dry_run: dry_run_msg("Marked %s v%s as default version" % (self.name, self.version)) else: + mod_folderpath = os.path.dirname(self.module_generator.get_module_filepath()) self.module_generator.set_as_default(mod_folderpath, self.version) def cleanup_step(self): diff --git a/easybuild/tools/filetools.py b/easybuild/tools/filetools.py index 3b56ac65a6..acd0644ad4 100644 --- a/easybuild/tools/filetools.py +++ b/easybuild/tools/filetools.py @@ -171,18 +171,18 @@ def write_file(path, txt, append=False, forced=False): raise EasyBuildError("Failed to write to %s: %s", path, err) -def readlink(symlink_path): +def resolve_path(path): """ - Read symlink target at the specified path to the given path. + Return fully resolved path for given path. - :param symlink_path: symlink file path + :param path: path that (maybe) contains symlinks """ try: - target_symlink_path = os.readlink(symlink_path) + resolved_path = os.path.realpath(path) except OSError as err: - raise EasyBuildError("Get target of symlink %s failed: %s", symlink_path, err) + raise EasyBuildError("Resolving path %s failed: %s", path, err) - return target_symlink_path + return resolved_path def symlink(source_path, symlink_path, use_abspath_source=True): diff --git a/easybuild/tools/module_generator.py b/easybuild/tools/module_generator.py index 8e2daead71..3711c7605f 100644 --- a/easybuild/tools/module_generator.py +++ b/easybuild/tools/module_generator.py @@ -41,7 +41,7 @@ from easybuild.tools.build_log import EasyBuildError from easybuild.tools.config import build_option, get_module_syntax, install_path -from easybuild.tools.filetools import mkdir, readlink, read_file, remove_file, symlink, write_file +from easybuild.tools.filetools import mkdir, read_file, remove_file, resolve_path, symlink, write_file from easybuild.tools.modules import modules_tool from easybuild.tools.utilities import quote_str @@ -719,7 +719,7 @@ def set_as_default(self, module_folder_path, module_version): default_filepath = os.path.join(module_folder_path, 'default') if os.path.islink(default_filepath): - link_target = readlink(default_filepath) + link_target = resolve_path(default_filepath) remove_file(default_filepath) self.log.info("Removed default version marking from %s.", link_target) elif os.path.exists(default_filepath): diff --git a/test/framework/filetools.py b/test/framework/filetools.py index 9b0c653226..efcf3eb9ca 100644 --- a/test/framework/filetools.py +++ b/test/framework/filetools.py @@ -355,23 +355,35 @@ def test_is_readable(self): os.chmod(test_file, 0) self.assertFalse(ft.is_readable(test_file)) - def test_symlink_readlink_file(self): - """Test read link target file""" + def test_symlink_resolve_path(self): + """Test symlink and resolve_path function""" # write_file and read_file tests are elsewhere. so not getting their states - fp = os.path.join(self.test_prefix, 'test.txt') - txt = "test_my_link_file" - ft.write_file(fp, txt) + test_dir = os.path.join(os.path.realpath(self.test_prefix), 'test') + ft.mkdir(test_dir) + + link_dir = os.path.join(self.test_prefix, 'linkdir') + ft.symlink(test_dir, link_dir) + self.assertTrue(os.path.islink(link_dir)) + self.assertTrue(os.path.exists(link_dir)) + + test_file = os.path.join(link_dir, 'test.txt') + ft.write_file(test_file, "test123") - link = os.path.join(self.test_prefix, 'test.link') # creating the link file - ft.symlink(fp, link) + link = os.path.join(self.test_prefix, 'test.link') + ft.symlink(test_file, link) # checking if file is symlink self.assertTrue(os.path.islink(link)) + self.assertTrue(os.path.exists(link_dir)) + + self.assertTrue(os.path.samefile(os.path.join(self.test_prefix, 'test', 'test.txt'), link)) - # reading link target and comparing to file name - self.assertEqual(os.path.realpath(fp), os.path.realpath(ft.readlink(link))) + # test resolve_path + self.assertEqual(test_dir, ft.resolve_path(link_dir)) + self.assertEqual(os.path.join(os.path.realpath(self.test_prefix), 'test', 'test.txt'), ft.resolve_path(link)) + self.assertEqual(ft.read_file(link), "test123") def test_remove_symlinks(self): """Test remove valid and invalid symlinks"""