From 52913b50504352da24179d7efe6b6311cf6783f7 Mon Sep 17 00:00:00 2001 From: Joey Degges Date: Wed, 18 Aug 2021 17:25:12 -0700 Subject: [PATCH] Allow callers to specify desired uid/gid It can be useful for a file to be created atomically with the desired user and group. Let's allow callers to specify their desired uid/gid and change ownership of the intermediate temp file before it is moved into place. --- atomicwrites/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/atomicwrites/__init__.py b/atomicwrites/__init__.py index 623826e..e40a9f1 100644 --- a/atomicwrites/__init__.py +++ b/atomicwrites/__init__.py @@ -182,6 +182,13 @@ def get_fileobject(self, suffix="", prefix=tempfile.gettempprefix(), dir = os.path.normpath(os.path.dirname(self._path)) descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir) + + # If specified, change user and group. + uid = kwargs.pop('uid', -1) + gid = kwargs.pop('gid', -1) + if uid != -1 or gid != -1: + os.fchown(descriptor, uid, gid) + # io.open() will take either the descriptor or the name, but we need # the name later for commit()/replace_atomic() and couldn't find a way # to get the filename from the descriptor.