cpython: d1fd0f0f8e68 (original) (raw)
Mercurial > cpython
changeset 72039:d1fd0f0f8e68
#12191: add shutil.chown() to change user and/or group owner of a given path also specifying their names. [#12191]
Sandro Tosi sandro.tosi@gmail.com | |
---|---|
date | Mon, 22 Aug 2011 23:28:27 +0200 |
parents | e3be2941c834 |
children | a1267968f6ed |
files | Doc/library/os.rst Doc/library/shutil.rst Lib/shutil.py Lib/test/test_shutil.py Misc/NEWS |
diffstat | 5 files changed, 110 insertions(+), 0 deletions(-)[+] [-] Doc/library/os.rst 3 Doc/library/shutil.rst 14 Lib/shutil.py 31 Lib/test/test_shutil.py 59 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -1530,6 +1530,9 @@ Files and Directories Change the owner and group id of path to the numeric uid and gid. To leave one of the ids unchanged, set it to -1.
- See :func:
shutil.chown
for a higher-level function that accepts names in - addition to numeric ids. + Availability: Unix.
--- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -183,6 +183,20 @@ Directory and files operations Availability: Unix, Windows. +.. function:: chown(path, user=None, group=None) +
- Change owner user and/or group of the given path. +
- user can be a system user name or a uid; the same applies to group. At
- least one argument is required. +
- See also :func:
os.chown
, the underlying function. + - Availability: Unix. +
- .. versionadded:: 3.3 +
+ .. exception:: Error This exception collects exceptions that are raised during a multi-file
--- 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):
- 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.
- """
-1 means don't change it
- if user is None:
_user = -1[](#l3.23)
user can either be an int (the uid) or a string (the system username)
- elif isinstance(user, str):
_user = _get_uid(user)[](#l3.26)
if _user is None:[](#l3.27)
raise LookupError("no such user: {!r}".format(user))[](#l3.28)
- if group is None:
_group = -1[](#l3.31)
- elif not isinstance(group, int):
_group = _get_gid(group)[](#l3.33)
if _group is None:[](#l3.34)
raise LookupError("no such group: {!r}".format(group))[](#l3.35)
--- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -712,6 +712,65 @@ class TestShutil(unittest.TestCase): self.assertGreaterEqual(usage.total, usage.used) self.assertGreater(usage.total, usage.free)
- @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
- @unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown')
- def test_chown(self):
# cleaned-up automatically by TestShutil.tearDown method[](#l4.11)
dirname = self.mkdtemp()[](#l4.12)
filename = tempfile.mktemp(dir=dirname)[](#l4.13)
write_file(filename, 'testing chown function')[](#l4.14)
with self.assertRaises(ValueError):[](#l4.16)
shutil.chown(filename)[](#l4.17)
with self.assertRaises(LookupError):[](#l4.19)
shutil.chown(filename, user='non-exising username')[](#l4.20)
with self.assertRaises(LookupError):[](#l4.22)
shutil.chown(filename, group='non-exising groupname')[](#l4.23)
with self.assertRaises(TypeError):[](#l4.25)
shutil.chown(filename, b'spam')[](#l4.26)
with self.assertRaises(TypeError):[](#l4.28)
shutil.chown(filename, 3.14)[](#l4.29)
uid = os.getuid()[](#l4.31)
gid = os.getgid()[](#l4.32)
def check_chown(path, uid=None, gid=None):[](#l4.34)
s = os.stat(filename)[](#l4.35)
if uid is not None:[](#l4.36)
self.assertEqual(uid, s.st_uid)[](#l4.37)
if gid is not None:[](#l4.38)
self.assertEqual(gid, s.st_gid)[](#l4.39)
shutil.chown(filename, uid, gid)[](#l4.41)
check_chown(filename, uid, gid)[](#l4.42)
shutil.chown(filename, uid)[](#l4.43)
check_chown(filename, uid)[](#l4.44)
shutil.chown(filename, user=uid)[](#l4.45)
check_chown(filename, uid)[](#l4.46)
shutil.chown(filename, group=gid)[](#l4.47)
check_chown(filename, gid)[](#l4.48)
shutil.chown(dirname, uid, gid)[](#l4.50)
check_chown(dirname, uid, gid)[](#l4.51)
shutil.chown(dirname, uid)[](#l4.52)
check_chown(dirname, uid)[](#l4.53)
shutil.chown(dirname, user=uid)[](#l4.54)
check_chown(dirname, uid)[](#l4.55)
shutil.chown(dirname, group=gid)[](#l4.56)
check_chown(dirname, gid)[](#l4.57)
user = pwd.getpwuid(uid)[0][](#l4.59)
group = grp.getgrgid(gid)[0][](#l4.60)
shutil.chown(filename, user, group)[](#l4.61)
check_chown(filename, uid, gid)[](#l4.62)
shutil.chown(dirname, user, group)[](#l4.63)
check_chown(dirname, uid, gid)[](#l4.64)
+ class TestMove(unittest.TestCase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -1144,6 +1144,9 @@ Library