_cleanup_dead_links : factored method into a function and put it in llnl.filesystem

This commit is contained in:
alalazo 2016-03-02 16:19:32 +01:00
parent 9001b9ed3c
commit 901e4851b9
2 changed files with 21 additions and 18 deletions

View File

@ -25,7 +25,7 @@
__all__ = ['set_install_permissions', 'install', 'install_tree', 'traverse_tree',
'expand_user', 'working_dir', 'touch', 'touchp', 'mkdirp',
'force_remove', 'join_path', 'ancestor', 'can_access', 'filter_file',
'FileFilter', 'change_sed_delimiter', 'is_exe', 'force_symlink']
'FileFilter', 'change_sed_delimiter', 'is_exe', 'force_symlink', 'remove_dead_links']
import os
import sys
@ -235,7 +235,7 @@ def touchp(path):
def force_symlink(src, dest):
try:
os.symlink(src, dest)
except OSError, e:
except OSError as e:
os.remove(dest)
os.symlink(src, dest)
@ -339,3 +339,18 @@ def traverse_tree(source_root, dest_root, rel_path='', **kwargs):
if order == 'post':
yield (source_path, dest_path)
def remove_dead_links(root):
"""
Removes any dead link that is present in root
Args:
root: path where to search for dead links
"""
for file in os.listdir(root):
path = join_path(root, file)
if os.path.islink(path):
real_path = os.path.realpath(path)
if not os.path.exists(real_path):
os.unlink(path)

View File

@ -98,7 +98,7 @@ def __init__(self, url_or_fetch_strategy, **kwargs):
# Try to construct here a temporary name for the stage directory
# If this is a named stage, then construct a named path.
self.path = join_path(spack.stage_path, self.name)
# Flag to decide whether to delete the stage folder on exit or not
self.delete_on_exit = True
def __enter__(self):
@ -113,20 +113,17 @@ def __enter__(self):
"""
# Create the top-level stage directory
mkdirp(spack.stage_path)
self._cleanup_dead_links()
remove_dead_links(spack.stage_path)
# If this is a temporary stage, them make the temp directory
# If a tmp_root exists then create a directory there and then link it in the stage area,
# otherwise create the stage directory in self.path
if self.tmp_root:
if self._need_to_create_path():
tmp_dir = tempfile.mkdtemp('', STAGE_PREFIX, self.tmp_root)
os.symlink(tmp_dir, self.path)
# if we're not using a tmp dir, create the stage directly in the
# stage dir, rather than linking to it.
else:
if self._need_to_create_path():
mkdirp(self.path)
# Make sure we can actually do something with the stage we made.
ensure_access(self.path)
@ -151,15 +148,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if self.delete_on_exit:
self.destroy()
def _cleanup_dead_links(self):
"""Remove any dead links in the stage directory."""
for file in os.listdir(spack.stage_path):
path = join_path(spack.stage_path, file)
if os.path.islink(path):
real_path = os.path.realpath(path)
if not os.path.exists(real_path):
os.unlink(path)
def _need_to_create_path(self):
"""Makes sure nothing weird has happened since the last time we
looked at path. Returns True if path already exists and is ok.