Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-64978: Add chown() to pathlib.Path #31212

Closed
wants to merge 7 commits into from

Conversation

y0urself
Copy link

@y0urself y0urself commented Feb 8, 2022

This PR adds chown() and lchown() to the pathlib library.

As for chmod and other existing functions, this is achieved by using os.chown() within the new functions.

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@y0urself

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@y0urself y0urself force-pushed the fix-issue-20779 branch 2 times, most recently from 6d47346 to 55b70c0 Compare February 8, 2022 11:52
@y0urself y0urself marked this pull request as ready for review February 8, 2022 11:55
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@y0urself y0urself force-pushed the fix-issue-20779 branch 2 times, most recently from 196cf81 to 9032f1b Compare February 8, 2022 13:47
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@JelleZijlstra
Copy link
Member

@barneygale what do you think?

@barneygale
Copy link
Contributor

I'm supportive of adding chown() - it comes up semi-regularly in devops work.

I don't love lchown() - as you say @JelleZijlstra, it's two ways of spelling the same thing.

Couple more things to consider/discuss:

  • We already have owner() and group() methods, which return names, not IDs. Would it make sense to accept names here too?
  • We don't currently expose dir_fd in pathlib, why start here? :)

@y0urself y0urself requested a review from brettcannon as a code owner May 3, 2022 05:44
@y0urself y0urself force-pushed the fix-issue-20779 branch from ea24cfb to 5e367ac Compare May 3, 2022 05:46
@brettcannon brettcannon changed the title bpo-20779: Add chown() and lchown() to pathlib.Path gh-64978: Add chown() and lchown() to pathlib.Path May 5, 2022
@brettcannon
Copy link
Member

I'm fine with adding chown().

I also concur on not wanting lchown().

No opinion on the name/ID question.

I don't quite follow the dir_fd comment as I don't see that in the PR.

Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@y0urself
Copy link
Author

y0urself commented May 6, 2022

I don't quite follow the dir_fd comment as I don't see that in the PR.

I have already removed the dir_fd option.

@y0urself y0urself changed the title gh-64978: Add chown() and lchown() to pathlib.Path gh-64978: Add chown() to pathlib.Path May 6, 2022
@y0urself y0urself force-pushed the fix-issue-20779 branch from 5e367ac to 06aebeb Compare May 6, 2022 06:17
@y0urself
Copy link
Author

y0urself commented May 6, 2022

I am not sure with owner/ group stuff. They return the current owner/group for the given Path object, but these parameters are the ones, we want to change with chown.
If we allow (new_)owner here, we would probably need to go with pwd.getpwnam('new_owner').pw_uid.

On the other hand I have removed lchown() from the PR and I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from brettcannon May 6, 2022 06:40
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@cpython-cla-bot
Copy link

cpython-cla-bot bot commented May 7, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@barneygale
Copy link
Contributor

barneygale commented May 7, 2022

Having given it more thought, and considering the context where this is likely to be used (devops-y scripts), I think supporting owner and group names (in addition to IDs) is important. I think the new method should call through to shutil.chown(), which already accepts int | str | None for each argument. I think the arguments should be spelled owner and group for symmetry with the Path methods. What do you reckon?

Co-authored-by: Barney Gale <barney.gale@gmail.com>
@y0urself
Copy link
Author

y0urself commented May 9, 2022

Having given it more thought, and considering the context where this is likely to be used (devops-y scripts), I think supporting owner and group names (in addition to IDs) is important. I think the new method should call through to shutil.chown(), which already accepts int | str | None for each argument. I think the arguments should be spelled owner and group for symmetry with the Path methods. What do you reckon?

We would need to drop the follow_symlinks flag, when we use shutil.chown().

@barneygale
Copy link
Contributor

Or add follow_symlinks to shutil.chown()

@brettcannon brettcannon self-requested a review May 11, 2022 22:56

Change the file ownership, like :func:`os.chown`.

This method normally follows symlinks. Some Unix flavours support changing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This method normally follows symlinks. Some Unix flavours support changing
This method follows symlinks by default. Some Unix flavours support changing

Comment on lines +1131 to +1133
"""
Change the owner and group id of path to the numeric uid and gid, like os.chown().
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow PEP 8.

Suggested change
"""
Change the owner and group id of path to the numeric uid and gid, like os.chown().
"""
"""Change the owner and group id of path to the numeric uid and gid, like os.chown()."""

Comment on lines +30 to +32
root_in_posix = False
if hasattr(os, 'geteuid'):
root_in_posix = (os.geteuid() == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
root_in_posix = False
if hasattr(os, 'geteuid'):
root_in_posix = (os.geteuid() == 0)
try:
root_in_posix = not os.geteuid()
except AttributeError:
root_in_posix = False

This looks to be a copy-and-paste from test_os.py, correct? If so, can you move this to https://github.com/python/cpython/tree/main/Lib/test/support somewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I can move it and use it from there as a helper module ...

@unittest.skipUnless(root_in_posix and len(all_users) > 1,
"test needs root privilege and more than one user")
def test_chown_with_root(self):
# original uid and gid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow PEP 8.

Suggested change
# original uid and gid
# The original uid and gid.

Comment on lines +24 to +28
try:
import pwd
all_users = [u.pw_uid for u in pwd.getpwall()]
except (ImportError, AttributeError):
all_users = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to be a copy-and-paste from test_os.py, correct? If so, can you move this to https://github.com/python/cpython/tree/main/Lib/test/support somewhere?

uid = p.stat().st_uid
gid = p.stat().st_gid

# get users and groups for testing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# get users and groups for testing
# Get users and groups for testing.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@brettcannon
Copy link
Member

I left a comment on the issue, but after thinking about it, right now there isn't really enough here to warrant taking this PR as it's just wrapping a function call in a method. Without some added benefit to being attached to Path (e.g. higher-level), having to support this API for decades is unfortunately not going to be worth it.

@AlexWaygood
Copy link
Member

Thanks for the PR, @y0urself. I'm afraid I'm going to close this as rejected, as I agree with Brett's comments, as well as @barneygale's and @CAM-Gerlach's remarks on the associated issue. However, I hope this doesn't discourage you from contributing to CPython in the future! We do appreciate the contribution :)

@AlexWaygood AlexWaygood closed this Jan 8, 2023
@anki-code
Copy link

anki-code commented Mar 18, 2023

A few words about one thing.
It will be super cool to have this method because there is awesome python-powered xonsh shell and Path objects are using in xonsh broadly. You can do this in the shell using path-strings:

pip install -U 'xonsh[full]'
xonsh

cd /tmp && echo world > hello.txt 

p'hello.txt'
# Path('hello.txt')
p'hello.txt'.read_text()
# 'world\n'

for f in p'/tmp'.glob('hello*'):
    print(f, f.exists())
# /tmp/hello.txt True

It will be cool to run p'hello.txt'.chown('root') instead of running subprocess chown root hello.txt (it's slower) or do import os etc.

I'm for reopening this issue and favor for enriching pathlib.

@CAM-Gerlach
Copy link
Member

@barneygale , as you're now officially the pathlib maintainer, what is your take on considering re-opening this PR and/or a revised one?

@barneygale
Copy link
Contributor

I've re-opened the issue, we could continue discussion there if that works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.