diff options
author | Sandro Tosi <sandro.tosi@gmail.com> | 2011-08-22 21:28:27 (GMT) |
---|---|---|
committer | Sandro Tosi <sandro.tosi@gmail.com> | 2011-08-22 21:28:27 (GMT) |
commit | d902a14dd0ed29bda061f3c0e4cfe355f2763462 (patch) | |
tree | 37633787b7ecbbd8d71e558ee92429c1248d44d0 /Lib/shutil.py | |
parent | 6f2a683a0c4ec0b3fe3ed840336853ebf26004c0 (diff) | |
download | cpython-d902a14dd0ed29bda061f3c0e4cfe355f2763462.zip cpython-d902a14dd0ed29bda061f3c0e4cfe355f2763462.tar.gz cpython-d902a14dd0ed29bda061f3c0e4cfe355f2763462.tar.bz2 |
#12191: add shutil.chown() to change user and/or group owner of a given path also specifying their names.
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r-- | Lib/shutil.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index 2955b04..4b75262 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -790,3 +790,34 @@ elif os.name == 'nt': total, free = nt._getdiskusage(path) used = total - free return _ntuple_diskusage(total, used, free) + +def chown(path, user=None, group=None): + """Change owner user and group of the given path. + + user and group can be the uid/gid or the user/group names, and in that case, + they are converted to their respective uid/gid. + """ + + if user is None and group is None: + raise ValueError("user and/or group must be set") + + _user = user + _group = group + + # -1 means don't change it + if user is None: + _user = -1 + # user can either be an int (the uid) or a string (the system username) + elif isinstance(user, str): + _user = _get_uid(user) + if _user is None: + raise LookupError("no such user: {!r}".format(user)) + + if group is None: + _group = -1 + elif not isinstance(group, int): + _group = _get_gid(group) + if _group is None: + raise LookupError("no such group: {!r}".format(group)) + + os.chown(path, _user, _group) |