LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-25-2005, 09:35 AM   #1
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Rep: Reputation: 30
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.
 
Old 02-25-2005, 11:26 AM   #2
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
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.
 
Old 02-25-2005, 05:49 PM   #3
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
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.
 
Old 02-27-2005, 12:53 AM   #4
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
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.
 
Old 02-27-2005, 07:42 AM   #5
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
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
 
Old 02-27-2005, 07:57 AM   #6
APB_4
Member
 
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298

Rep: Reputation: 31
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unmerged python in gentoo system! ascentsoft Gentoo 5 11-02-2008 04:12 PM
alternative to versioning system rblampain Linux - Security 8 09-13-2005 07:12 PM
Fedora System-Python gone :( Xiiph Linux - Software 1 04-02-2005 02:49 PM
python os.system arguments randomx Programming 2 03-03-2005 03:54 PM
Checking passwords and system clock in Python 1337 Twinkie Programming 9 10-31-2004 06:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration