Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-25-2005, 09:35 AM
|
#1
|
Member
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317
Rep:
|
python alternative to os.system()
Hey Guys,
say I have this command in a loop where pkg contains the path to a package to be installed on a slackware system
Code:
packages = os.listdir(download_folder)
for pkg in packages:
cmd = "su -c 'upgradepkg --install-new " + download_folder + "/" + pkg + "'"
os.system(cmd)
That will prompt for a password and install packages from download_folder
How can I do this in a way where I could prompt for the password and then send the password in where it's needed. I can't seem to get the syntax right to use pipe, and popen tells me that su must be run in a terminal.
|
|
|
02-25-2005, 11:26 AM
|
#2
|
Senior Member
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503
Rep:
|
You just want to put in your password once and then it installs everything else right? I'd guess that because your in the for loop your getting prompted for the password everytime. Did you try putting the su -c outside of the for loop:
Code:
packages = os.listdir(download_folder)
os.system("su -")
# it will prompt you for your password
for pkg in packages:
cmd = "upgradepkg --install-new " + download_folder + "/" + pkg
os.system(cmd)
I haven't tested this out and it may not work but its just a thought.
|
|
|
02-25-2005, 05:49 PM
|
#3
|
Member
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317
Original Poster
Rep:
|
I'va already done similar things, but that's not what I was looking for. The rest of my program get all user input using dialog. I want to avoid using the console and run the command in a pipe of some type.
|
|
|
02-27-2005, 12:53 AM
|
#4
|
Member
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601
Rep:
|
Not sure, but maybe this will help:
Code:
import commands
foo = commands.getoutput( 'ls' ) # executed as 2 > &1
The output of running that shell command gets placed into the variable foo, rather than printed to stdout.
Well, it's an alternative to os.system() anyhow. 
|
|
|
02-27-2005, 07:42 AM
|
#5
|
Member
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317
Original Poster
Rep:
|
That's pretty good, but maybe I need to explain myself better. Here's the function I want to modify. It comes from my project getpkg
http://sourceforge.net/projects/getpkg/
What I have:
Code:
def install_downloaded_pkgs(d):
global download_folder
global desc_folder
downloaded = os.listdir(download_folder)
if len(downloaded) == 0:
d.infobox("Download folder is empty")
return
if len(downloaded) > 0:
heading = "Choose package(s) to Install"
maxLen = len(heading)
downloaded_list = []
for dl in downloaded:
if len(dl) > maxLen: maxLen = len(dl)
downloaded_list.append((dl,"",0))
inst_choice = d.checklist(heading,downloaded_list,maxLen+12,len(downloaded)+6,len(downloaded))
if len(inst_choice) > 0:
print "Installing packages requires root access."
print "Please enter your password."
inst_cmd = "su -c 'upgradepkg --install-new"
for pkg in inst_choice:
inst_cmd += " " + download_folder + "/" + pkg
inst_cmd += "'"
os.system(inst_cmd)
inst_pkglist = get_installed_pkgs(d)
os.unlink(inst_path)
output = open(inst_path,'w')
for pkg in inst_pkglist:
output.write(str(pkg) + "\n")
output.close()
return
The above code uses a dialog class that I wrote to get all input/output using the linux dialog utility. The only time the program dumps the user back to the console is when installing packages. I want to prompt the user for a password and show the user the package description file while the file is being installed.
What I want.
Code:
def install_downloaded_pkgs(d):
global download_folder
global desc_folder
global inst_path
downloaded = os.listdir(download_folder)
if len(downloaded) == 0:
d.infobox("Download folder is empty")
return
if len(downloaded) > 0:
heading = "Choose package(s) to Install"
maxLen = len(heading)
downloaded_list = []
for dl in downloaded:
if len(dl) > maxLen: maxLen = len(dl)
downloaded_list.append((dl,"",0))
inst_choice = d.checklist(heading,downloaded_list,maxLen+12,len(downloaded)+6,len(downloaded))
if len(inst_choice) > 0:
msg = "Installing packages requires root access.\n"
msg += "Please enter your password."
while 1:
pw = d.passwordbox(msg,40,10,1)
if pw != "": break
for pkg in inst_choice:
descFile = desc_folder + "/" + pkg.replace(".tgz",".txt")
been_inst_path = "/var/log/packages/" + pkg.replace(".tgz","")
d.infobox(descFile,100,20)
inst_cmd += "su -c 'upgradepkg --install-new " + download_folder + "/" + pkg + "'"
# need commands here to send inst_cmd + pw
# want to run in background
while 1:
if os.path.exists(been_inst_path): break
time.sleep(0.5)
inst_pkglist = get_installed_pkgs(d)
os.unlink(inst_path)
output = open(inst_path,'w')
for pkg in inst_pkglist:
output.write(str(pkg) + "\n")
output.close()
return
|
|
|
02-27-2005, 07:57 AM
|
#6
|
Member
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298
Rep:
|
Just got to say thanks to johnMG for the import commands bit. It was just what I needed to make my small MD5 checking script My program
|
|
|
All times are GMT -5. The time now is 12:23 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|