Extending os.chown() to accept user/group names
Hi all, before opening an issue to track the request, I'd like to ask advice here about this: extend os.chown() to accept even user/group names instead of just uid and gid. On a Unix system, you can call chown command passing either id or names, so it seems (to me at least) natural to expect os.chown() to behave similarly; but that's not the case. I can see os module wants to be a thin wrapper around OS syscalls and chown(2) accepts only uid/gid as input, so what would be best: extend os.chown() or provide a chown() function in shutil module for this purpose? Thanks in advance, -- Sandro Tosi (aka morph, morpheus, matrixhasu) My website: http://matrixhasu.altervista.org/ Me at Debian: http://wiki.debian.org/SandroTosi
On May 25, 2011, at 10:24 AM, Sandro Tosi wrote:
before opening an issue to track the request, I'd like to ask advice here about this: extend os.chown() to accept even user/group names instead of just uid and gid.
On a Unix system, you can call chown command passing either id or names, so it seems (to me at least) natural to expect os.chown() to behave similarly; but that's not the case.
I can see os module wants to be a thin wrapper around OS syscalls and chown(2) accepts only uid/gid as input, so what would be best: extend os.chown() or provide a chown() function in shutil module for this purpose?
I think it would be a nice feature, and I can see the conflict. OT1H you want to keep os.chown() a thin wrapper, but OTOH you'd rather not have to add a new, arguably more difficult to discover, function. Given those two choices, I still think I'd come down on adding a new function and shutil.chown() seems an appropriate place for it. Cheers, -Barry
On Wed, 25 May 2011 09:41:46 -0400
Barry Warsaw
On May 25, 2011, at 10:24 AM, Sandro Tosi wrote:
before opening an issue to track the request, I'd like to ask advice here about this: extend os.chown() to accept even user/group names instead of just uid and gid.
On a Unix system, you can call chown command passing either id or names, so it seems (to me at least) natural to expect os.chown() to behave similarly; but that's not the case.
I can see os module wants to be a thin wrapper around OS syscalls and chown(2) accepts only uid/gid as input, so what would be best: extend os.chown() or provide a chown() function in shutil module for this purpose?
I think it would be a nice feature, and I can see the conflict. OT1H you want to keep os.chown() a thin wrapper, but OTOH you'd rather not have to add a new, arguably more difficult to discover, function. Given those two choices, I still think I'd come down on adding a new function and shutil.chown() seems an appropriate place for it.
+1 for shutil.chown(). Regards Antoine.
On Wed, May 25, 2011 at 15:58, Antoine Pitrou
On Wed, 25 May 2011 09:41:46 -0400 Barry Warsaw
wrote: I think it would be a nice feature, and I can see the conflict. OT1H you want to keep os.chown() a thin wrapper, but OTOH you'd rather not have to add a new, arguably more difficult to discover, function. Given those two choices, I still think I'd come down on adding a new function and shutil.chown() seems an appropriate place for it.
+1 for shutil.chown().
and so shutil.chown() be it: http://bugs.python.org/issue12191 Currently, only the function for a single file is implemented, let's look later what to do for a recursive one. Cheers, -- Sandro Tosi (aka morph, morpheus, matrixhasu) My website: http://matrixhasu.altervista.org/ Me at Debian: http://wiki.debian.org/SandroTosi
On Wed, May 25, 2011 at 15:41, Barry Warsaw
I think it would be a nice feature, and I can see the conflict. OT1H you want to keep os.chown() a thin wrapper, but OTOH you'd rather not have to add a new, arguably more difficult to discover, function. Given those two choices, I still think I'd come down on adding a new function and shutil.chown() seems an appropriate place for it.
Right. Please add a mention of shutil.chown() to the os.chown() docs, though. Cheers, Dirkjan
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
Le mercredi 25 mai 2011 à 18:46 +0200, Charles-François Natali a écrit :
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag. I would prefer a "map-like" function to "apply" a function on all files of a directory. Something like shutil.apply_recursive(shutil.chown)... ... maybe with options to choose between deep-first search and breadth-first search, filter (filenames, file size, files only, directories only, other attributes?), directory before files (may be need for chmod(0o000)), etc. Victor
On 5/25/2011 1:17 PM, Victor Stinner wrote:
Le mercredi 25 mai 2011 à 18:46 +0200, Charles-François Natali a écrit :
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag. I would prefer a "map-like" function to "apply" a function on all files of a directory. Something like shutil.apply_recursive(shutil.chown)...
... maybe with options to choose between deep-first search and breadth-first search, filter (filenames, file size, files only, directories only, other attributes?), directory before files (may be need for chmod(0o000)), etc.
You can do all of this with an appropriate application of os.walk(). Eric.
Victor Stinner wrote:
Le mercredi 25 mai 2011 à 18:46 +0200, Charles-François Natali a écrit :
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag. I would prefer a "map-like" function to "apply" a function on all files of a directory. Something like shutil.apply_recursive(shutil.chown)...
FWIW, the chown program (in GNU coreutils at least) has a -R flag for recursive operation, and I've found it *extremely* useful on many situations. Petri
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag. I would prefer a "map-like" function to "apply" a function on all files of a directory. Something like shutil.apply_recursive(shutil.chown)...
I was also thinking about this possibility. The advantage is that we could factor-out the recursive walk logic to make it available for other functions (chown, chmod...). It doesn't map well to the Unix command, though.
You can do all of this with an appropriate application of os.walk().
Then, I wonder why shutil.copytree and shutil.rmtree are provided. Recursive rm/copy/chown/chmod are extremely useful in system administration scripts. Furthermore, it's not as simple as it seems because of symlinks, see for example http://bugs.python.org/issue4489 .
2011/5/26 Charles-François Natali
Then, I wonder why shutil.copytree and shutil.rmtree are provided. Recursive rm/copy/chown/chmod are extremely useful in system administration scripts. Furthermore, it's not as simple as it seems because of symlinks, see for example http://bugs.python.org/issue4489
Rather than a fixed binary flag, I would suggest following the precedent of copytree and rmtree, and provide recursive functionality as a separate shutil function (i.e. shutil.chmodtree, shutil.chowntree). As noted, while these *can* be written manually, it is convenient to have the logic for handling symlinks dealt with for you, as well as not having to look up the particular incantation for correctly linking os.walk and the relevant operations. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
Nick Coghlan wrote:
2011/5/26 Charles-François Natali
: Then, I wonder why shutil.copytree and shutil.rmtree are provided. Recursive rm/copy/chown/chmod are extremely useful in system administration scripts. Furthermore, it's not as simple as it seems because of symlinks, see for example http://bugs.python.org/issue4489
Rather than a fixed binary flag, I would suggest following the precedent of copytree and rmtree, and provide recursive functionality as a separate shutil function (i.e. shutil.chmodtree, shutil.chowntree).
+1
As noted, while these *can* be written manually, it is convenient to have the logic for handling symlinks dealt with for you, as well as not having to look up the particular incantation for correctly linking os.walk and the relevant operations.
This is exactly what I meant when saying that the -R option to chown and chmod shell commands is useful. I *could* do it without them, but writing the same logic every time with error handling would be cumbersome. Petri
With regards to http://bugs.python.org/issue11457 What should the name of the (seconds, nanoseconds) tuple be? st_atim, st_ctim and st_mtim has bee suggested and is what the POSIX specification uses. This is confusingly similar to the existing st_atime, st_ctime and st_mtime. Also, should it be that these attributes are always defined (and just have 0 for nanoseconds if the OS doesn't support it) or should it be that they are only defined if the OS supports nanosecond precision. If they are always defined, it would make usage simpler. Cheers Ross
On Fri, Jun 3, 2011 at 12:34 PM, Ross Lagerwall
.. What should the name of the (seconds, nanoseconds) tuple be? st_atim, st_ctim and st_mtim has bee suggested and is what the POSIX specification uses. This is confusingly similar to the existing st_atime, st_ctime and st_mtime.
Still, inventing new names would make it even more confusing. +1 for POSIX names.
Also, should it be that these attributes are always defined (and just have 0 for nanoseconds if the OS doesn't support it) or should it be that they are only defined if the OS supports nanosecond precision. If they are always defined, it would make usage simpler.
+1 to have them always defined.
What should the name of the (seconds, nanoseconds) tuple be?
See my comment: -1 on having such a tuple in the first place. We have the decimal type to represent arbitrary-precision time stamps.
st_atim, st_ctim and st_mtim has bee suggested and is what the POSIX specification uses. This is confusingly similar to the existing st_atime, st_ctime and st_mtime.
That the POSIX spec uses it is IMO a strong argument.
Also, should it be that these attributes are always defined (and just have 0 for nanoseconds if the OS doesn't support it)
Definitely. There may be a day when ps, fs, or as resolution comes along. It would be good if a) the fields are always available, and b) have "unspecified"/"best-effort" precision, to accomodate future changes. I wish the decimal type would have been available in 2.3, when I changed the fields to be doubles instead of longs... Regards, Martin
2011/5/26 Victor Stinner
Le mercredi 25 mai 2011 à 18:46 +0200, Charles-François Natali a écrit :
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag. I would prefer a "map-like" function to "apply" a function on all files of a directory. Something like shutil.apply_recursive(shutil.chown)...
... maybe with options to choose between deep-first search and breadth-first search, filter (filenames, file size, files only, directories only, other attributes?), directory before files (may be need for chmod(0o000)), etc.
Pass an iterable to shutil.chown()? Then you could call it like: shutil.chown(os.walk(path)) Then of course you have the difficulty of wanting to pass either an iterator or a single path - probably prefer two functions e.g.: shutil.chown(path) shutil.chown_many(iter) Tim Delaney
Le mercredi 25 mai 2011 à 18:46 +0200, Charles-François Natali a écrit :
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag.
I think shutil.chown should aim to mimic chown(1). At least GNU chown has a -R flag (not sure about POSIX chown), and it's useful in practice. Regards, Martin
Wiadomość napisana przez Martin v. Löwis w dniu 2011-06-01, o godz. 08:48:
Le mercredi 25 mai 2011 à 18:46 +0200, Charles-François Natali a écrit :
While we're at it, adding a "recursive" argument to this shutil.chown could also be useful.
I don't like the idea of a recursive flag.
I think shutil.chown should aim to mimic chown(1). At least GNU chown has a -R flag (not sure about POSIX chown), and it's useful in practice.
cp(1) and rm(1) also have "-r", still there are `shutil.copytree` and `shutil.rmtree`. I would like to keep that consistency, e.g. to have `shutil.chown` and `shutil.chowntree` as previously discussed by Charles-François, Nick and Petri. -- Best regards, Łukasz Langa Senior Systems Architecture Engineer IT Infrastructure Department Grupa Allegro Sp. z o.o.
participants (14)
-
"Martin v. Löwis"
-
Alexander Belopolsky
-
Antoine Pitrou
-
Barry Warsaw
-
Charles-François Natali
-
Dirkjan Ochtman
-
Eric Smith
-
Nick Coghlan
-
Petri Lehtinen
-
Ross Lagerwall
-
Sandro Tosi
-
Tim Delaney
-
Victor Stinner
-
Łukasz Langa