diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2018-10-03 21:56:22 +0900 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2018-10-03 22:03:52 +0900 |
commit | 262e789fca6950bc136a87f9a475d6cf7dd1faa3 (patch) | |
tree | 9c6edb1a8143d327cfb816afde02acbc55afd733 | |
parent | 013a8ad47f612b799e51e24181b122f04faf22ff (diff) | |
download | buildstream-262e789fca6950bc136a87f9a475d6cf7dd1faa3.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.py | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py index efecdc2e5..1bb504470 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, |