summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2018-10-03 21:56:22 +0900
committerTristan Van Berkom <tristan.van.berkom@gmail.com>2018-10-03 13:44:02 +0000
commit11161f994b58db759a8f581b0fe39aa8e184407d (patch)
treec4fddb6e4a4b9277765002b40c3f228a3a73870f
parent0a1f8e3c41c0decc97a994b2b7da857a03585121 (diff)
downloadbuildstream-11161f994b58db759a8f581b0fe39aa8e184407d.tar.gz
utils.py: Give save_file_atomic() a tempdir argument
Allow callers to decide where the temporary file will be created.
-rw-r--r--buildstream/utils.py7
1 files changed, 4 insertions, 3 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py
index 1e04a31ed..7cff6c54b 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -502,7 +502,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.
@@ -529,8 +529,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,