-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Conversation
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 MissingOur records indicate the following people have not signed the CLA: 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 You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
6d47346
to
55b70c0
Compare
196cf81
to
9032f1b
Compare
No support for WindowsPath
c72a2f3
to
f9a611b
Compare
@barneygale what do you think? |
I'm supportive of adding I don't love Couple more things to consider/discuss:
|
chown()
and lchown()
to pathlib.Path
chown()
and lchown()
to pathlib.Path
I'm fine with adding I also concur on not wanting No opinion on the name/ID question. I don't quite follow the |
Misc/NEWS.d/next/Library/2022-02-08-12-26-20.bpo-20779.Gha5Fa.rst
Outdated
Show resolved
Hide resolved
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 already removed the |
chown()
and lchown()
to pathlib.Path
chown()
to pathlib.Path
I am not sure with On the other hand I have removed |
Thanks for making the requested changes! @brettcannon: please review the changes made to this pull request. |
Misc/NEWS.d/next/Library/2022-02-08-12-26-20.bpo-20779.Gha5Fa.rst
Outdated
Show resolved
Hide resolved
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 |
Co-authored-by: Barney Gale <barney.gale@gmail.com>
We would need to drop the |
Or add |
|
||
Change the file ownership, like :func:`os.chown`. | ||
|
||
This method normally follows symlinks. Some Unix flavours support changing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method normally follows symlinks. Some Unix flavours support changing | |
This method follows symlinks by default. Some Unix flavours support changing |
""" | ||
Change the owner and group id of path to the numeric uid and gid, like os.chown(). | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow PEP 8.
""" | |
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().""" |
root_in_posix = False | ||
if hasattr(os, 'geteuid'): | ||
root_in_posix = (os.geteuid() == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow PEP 8.
# original uid and gid | |
# The original uid and gid. |
try: | ||
import pwd | ||
all_users = [u.pw_uid for u in pwd.getpwall()] | ||
except (ImportError, AttributeError): | ||
all_users = [] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# get users and groups for testing | |
# Get users and groups for testing. |
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 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 |
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 :) |
A few words about one thing. 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 I'm for reopening this issue and favor for enriching pathlib. |
@barneygale , as you're now officially the pathlib maintainer, what is your take on considering re-opening this PR and/or a revised one? |
I've re-opened the issue, we could continue discussion there if that works? |
This PR adds
chown()
andlchown()
to thepathlib
library.As for
chmod
and other existing functions, this is achieved by usingos.chown()
within the new functions.