From 5cce319568f0deef99d868459fb7dd17d4cec528 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 17 May 2015 15:12:06 +0200 Subject: Bus.chown can use usernames and groupnames, as well as using the group of the owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/bus.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/bus.py b/src/bus.py index fd6257f..b07fe82 100644 --- a/src/bus.py +++ b/src/bus.py @@ -232,14 +232,16 @@ class Bus: return message - def chown(self, owner : int = None, group : int = None): + def chown(self, owner = None, group = None): ''' Change the ownership of a bus `os.stat` can be used of the bus's associated file to get the bus's ownership - @param owner:int? The user ID of the bus's new owner, if `None`, keep current - @param group:int? The group ID of the bus's new group, if `None`, keep current + @param owner:int|str? The user ID or username of the bus's new owner, + if `None`, keep current + @param group:int|str|...? The group ID or groupname of the bus's new group, + if `None`, keep current, `...` to use the owner's group ''' from native_bus import bus_chown_wrapped if (owner is None) or (group is None): @@ -247,6 +249,15 @@ class Bus: attr = stat(self.pathname) if owner is None: owner = attr.st_uid if group is None: group = attr.st_gid + if isinstance(owner, str): + import pwd + owner = pwd.getpwnam(owner).pw_uid + if isinstance(group, str): + import grp + group = grp.getgrnam(group).gr_gid + elif group is ...: + import pwd + group = pwd.getpwuid(owner).pw_gid (r, e) = bus_chown_wrapped(self.pathname, owner, group) if r == -1: raise self.__oserror(e) -- cgit v1.2.3-70-g09d2