summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/filesystem.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/spack/llnl/util/filesystem.py')
-rw-r--r--lib/spack/llnl/util/filesystem.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py
index 4a708b639a..015eeb9aa1 100644
--- a/lib/spack/llnl/util/filesystem.py
+++ b/lib/spack/llnl/util/filesystem.py
@@ -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', 'remove_dead_links']
+ 'FileFilter', 'change_sed_delimiter', 'is_exe', 'force_symlink', 'remove_dead_links', 'remove_linked_tree']
import os
import sys
@@ -353,4 +353,20 @@ def remove_dead_links(root):
if os.path.islink(path):
real_path = os.path.realpath(path)
if not os.path.exists(real_path):
- os.unlink(path) \ No newline at end of file
+ os.unlink(path)
+
+def remove_linked_tree(path):
+ """
+ Removes a directory and its contents. If the directory is a symlink, follows the link and removes the real
+ directory before removing the link.
+
+ Args:
+ path: directory to be removed
+
+ """
+ if os.path.exists(path):
+ if os.path.islink(path):
+ shutil.rmtree(os.path.realpath(path), True)
+ os.unlink(path)
+ else:
+ shutil.rmtree(path, True)