aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-05-17 15:12:06 +0200
committerMattias Andrée <maandree@operamail.com>2015-05-17 15:12:06 +0200
commit5cce319568f0deef99d868459fb7dd17d4cec528 (patch)
treec8c78d0706ad2e15fd1a289b61f12d1abce67890
parentBus.chown can keep current owner or group + Bus.chmod can use a permission mask to keep or remove permissions (diff)
downloadpython-bus-5cce319568f0deef99d868459fb7dd17d4cec528.tar.gz
python-bus-5cce319568f0deef99d868459fb7dd17d4cec528.tar.bz2
python-bus-5cce319568f0deef99d868459fb7dd17d4cec528.tar.xz
Bus.chown can use usernames and groupnames, as well as using the group of the owner
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/bus.py17
1 files changed, 14 insertions, 3 deletions
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)