Skip to content

Commit

Permalink
rename filetools.readlink to filetools.resolve_path, use os.path.real…
Browse files Browse the repository at this point in the history
…path, enhance test for symlink & resolve_path
  • Loading branch information
boegel committed Mar 23, 2017
1 parent e9c97e9 commit 7282896
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
3 changes: 1 addition & 2 deletions easybuild/framework/easyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
30 changes: 21 additions & 9 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit 7282896

Please sign in to comment.