This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: shutil.chown function enhancement
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add shutil.chowntree
View: 13033
Assigned To: Nosy List: SilentGhost, YoSTEALTH, r.david.murray
Priority: normal Keywords: patch

Created on 2015-12-03 20:27 by YoSTEALTH, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
shutil.patch YoSTEALTH, 2015-12-03 23:07 review
Repositories containing patches
https://hg.python.org/cpython/file/3.5/Lib/shutil.py
Messages (6)
msg255834 - (view) Author: (YoSTEALTH) * Date: 2015-12-03 20:27
A very simple but useful enhancement for shutil.chown function

Currently "shutil.chown" function effects only one directory or file user/group permission by adding "recursive" parameter it can easily effect all sub-directories/files

Source: https://hg.python.org/cpython/file/3.5/Lib/shutil.py#l1007


# Current:
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)


# Enhanced:

import os.path


# Internal Function
def _dir_walk(path):
    ''' Get All Directories & Files'''

    for dir_path, dir_names, file_names in os.walk(path):

        # Directories
        for dir_name in dir_names:
            yield os.path.join(dir_path, dir_name)

        # Files
        for file_name in file_names:
            yield os.path.join(dir_path, file_name)

def chown(path, user=None, group=None, recursive=False):

    """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))

    # Default Do First
    if not recursive:
        os.chown(path, _user, _group)
    else:
        for recursive_path in _dir_walk(path):
            os.chown(recursive_path, _user, _group)


hope this helps :)
msg255836 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-12-03 20:34
Please submit this as a patch, and provide an explanation of the change you are proposing.
msg255837 - (view) Author: (YoSTEALTH) * Date: 2015-12-03 20:39
I am new to this, not exactly sure how to go about doing that!
msg255839 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-12-03 20:54
Well, the prose description of the enhancement is just words added to this issue :)

For the patch, you can use the unix 'diff -u' command or, if you want to get more involved in contributing, read the instructions for checking out the repository in docs.python.org/devguide, check it out, apply your changes, and then do 'hg diff' to produce the patch file.  Either way, you attach the patch file to the issue with the 'File: browse' button.
msg255844 - (view) Author: (YoSTEALTH) * Date: 2015-12-03 23:07
Thanks David.

I hope this is what a patch file should be! Attached.
msg255846 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-12-03 23:40
You still didn't describe in prose what your proposed enhancement is, but looking at the patch I can now see you are proposing to add a recursive flag.

This has already been proposed and discussed, and the conclusion was a separate function would be better (like remove versus rmtree).  The proposed chowntree patch appears to be in limbo; perhaps you could pick it up and move it forward.  See issue 13033 for details.
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 69976
2015-12-03 23:40:19r.david.murraysetstatus: open -> closed
superseder: Add shutil.chowntree
messages: + msg255846

resolution: duplicate
stage: resolved
2015-12-03 23:07:12YoSTEALTHsetfiles: + shutil.patch
keywords: + patch
messages: + msg255844
2015-12-03 21:12:29SilentGhostsetnosy: + SilentGhost
2015-12-03 20:54:03r.david.murraysetmessages: + msg255839
2015-12-03 20:39:09YoSTEALTHsetmessages: + msg255837
2015-12-03 20:34:16r.david.murraysetnosy: + r.david.murray

messages: + msg255836
versions: + Python 3.6, - Python 3.5
2015-12-03 20:27:33YoSTEALTHcreate