summaryrefslogtreecommitdiff
path: root/buildstream/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'buildstream/utils.py')
-rw-r--r--buildstream/utils.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py
index efecdc2e5..34c0f898b 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -496,7 +496,7 @@ def get_bst_version():
@contextmanager
def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
- errors=None, newline=None, closefd=True, opener=None):
+ errors=None, newline=None, closefd=True, opener=None, tempdir=None):
"""Save a file with a temporary name and rename it into place when ready.
This is a context manager which is meant for saving data to files.
@@ -523,8 +523,9 @@ def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
# https://bugs.python.org/issue8604
assert os.path.isabs(filename), "The utils.save_file_atomic() parameter ``filename`` must be an absolute path"
- dirname = os.path.dirname(filename)
- fd, tempname = tempfile.mkstemp(dir=dirname)
+ if tempdir is None:
+ tempdir = os.path.dirname(filename)
+ fd, tempname = tempfile.mkstemp(dir=tempdir)
os.close(fd)
f = open(tempname, mode=mode, buffering=buffering, encoding=encoding,
@@ -556,6 +557,9 @@ def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
#
# Get the disk usage of a given directory in bytes.
#
+# This function assumes that files do not inadvertantly
+# disappear while this function is running.
+#
# Arguments:
# (str) The path whose size to check.
#