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.py36
1 files changed, 34 insertions, 2 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py
index 9d8c552e05..066eca5bd3 100644
--- a/lib/spack/llnl/util/filesystem.py
+++ b/lib/spack/llnl/util/filesystem.py
@@ -455,8 +455,10 @@ def working_dir(dirname, **kwargs):
orig_dir = os.getcwd()
os.chdir(dirname)
- yield
- os.chdir(orig_dir)
+ try:
+ yield
+ finally:
+ os.chdir(orig_dir)
@contextmanager
@@ -605,6 +607,36 @@ def ancestor(dir, n=1):
return parent
+def get_single_file(directory):
+ fnames = os.listdir(directory)
+ if len(fnames) != 1:
+ raise ValueError("Expected exactly 1 file, got {0}"
+ .format(str(len(fnames))))
+ return fnames[0]
+
+
+@contextmanager
+def temp_cwd():
+ tmp_dir = tempfile.mkdtemp()
+ try:
+ with working_dir(tmp_dir):
+ yield tmp_dir
+ finally:
+ shutil.rmtree(tmp_dir)
+
+
+@contextmanager
+def temp_rename(orig_path, temp_path):
+ same_path = os.path.realpath(orig_path) == os.path.realpath(temp_path)
+ if not same_path:
+ shutil.move(orig_path, temp_path)
+ try:
+ yield
+ finally:
+ if not same_path:
+ shutil.move(temp_path, orig_path)
+
+
def can_access(file_name):
"""True if we have read/write access to the file."""
return os.access(file_name, os.R_OK | os.W_OK)