Python: rather than permission denied, can you get an OS specific password challenge?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Python: rather than permission denied, can you get an OS specific password challenge?
In my python script, I need to copy files from one place to another. I know that the majority of the time, the destination will require the user to sudo or be the owner of the destination dir. Rather than get a simple "permission denied" message, is there a way to get a password challenge dialog to display (assuming running X or OSX, etc) so that the user can sudo/elevate so the file(s) can be copied w/o changing permissions on the dest?
First off, I have to tell you I don't know python; however, you might be able to translate this from bash to python because I think the general concept applies:
Code:
source=( 'some' 'list' 'of' 'files' )
dest='/home/you/some-dir'
[ -d "$dest" ] || exit 1
if [ ! -w "$dest" ]; then
sudo -u "$( stat -c%U "$dest" )" cp -v "${source[@]}" -t "$dest"
else
cp -v "${source[@]}" -t "$dest"
fi
Thanks Kevin, but what I'm after is getting an OS-specific dialog asking for a password to pop-up. For example, when I open Synaptic on Debian/Ubuntu, I get asked for a password. The same thing often happens for update manager or something of the sort. If, in OSX, I try to copy a file (with the finder) from my homedir to, say, /Applications, it brings up a password dialog. I know those are already X applications, but I was hoping for some sort of way to spawn that from a python script... maybe using an os.system("cp --elevate_privileges /foo /bar") or something like that.
At the end of the day, I'm writing in some functionality into an existing wxPython application that will copy files from one place on a computer to another & would like to be able to do more than tell the user they don't have write permissions to the area they are trying to copy to.
In terms of portability the problem for Python developers would
be that they need to cater for a whole host WM/DEs, with a variety
of possible choices to get the dialog you desire. There's no generic
built in X11 way to do that, they'll be usinge kdesu, kdesudo, the
gnome equivalents, or even use something as simple as x11-ssh-askpass.
In terms of portability the problem for Python developers would
be that they need to cater for a whole host WM/DEs, with a variety
of possible choices to get the dialog you desire. There's no generic
built in X11 way to do that, they'll be usinge kdesu, kdesudo, the
gnome equivalents, or even use something as simple as x11-ssh-askpass.
Cheers,
Tink
And if none of those GUI options are available, you can always fake it with an xterm:
Code:
xterm -e 'sudo -u user cp srcdest'
Kevin Barry
PS I think this is the first Programming thread I've seen with > 3,000 views that wasn't over 5 years old. It must make a good search result.
I'm disappointed that no one suggested just running the python script with Audi in the first place... Unless of course you're programming a GUI (for some god awful reason) in which case either os or sys should have the equivalent to C's setuid. It's dangerous, but it works.
Ian John Locke II, mind sharing some info on "Audi"? Google searching for "Audi Authentication" or "Python Audi" doesn't bring back much in the way of info that isn't related to Audi Automobiles. Do you have a link?
Ian John Locke II, mind sharing some info on "Audi"? Google searching for "Audi Authentication" or "Python Audi" doesn't bring back much in the way of info that isn't related to Audi Automobiles. Do you have a link?
Har, phone auto "correction". I meant simply running the script with sudo. If you were concerned about doing it across multiple machines fabric (fabfile.org iirc) and paramiko are a must. (I think paramiko relies on pycrypto so that's three things you'd need to download & install)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.