Comment on this script. Possible error in communication with list arg between functions

P

Phoe6

Hi all,
Part of my script is to check for pre-requisite rpms to be
installed.
If its installed, I just display the rpm version as in rpm database,
otherwise I output a message saying:
' rpm is not installed' and collect the rpm name in a list
(notInstalled).
At the end if the len(notInstalled) is greater than 0 then I display
them all asking it to be installed and exit the program.
My Script is below:
-------
#!/usr/bin/env python
import os
import sys



notInstalled = []



def checkForRpm(rpmname):
''' Check for the presence of the RPM. '''
cin,cout,cerr = os.popen3('rpm -q ' + rpmname)
output = cout.read()
global notInstalled
if len(output) <= 0:
print rpmname + ' not installed.'
notInstalled.append(rpmname)
else:
print output



def preReqCheckRpms():
''' Check for the required RPMS '''
listOfRpms =
['firefox','senthil','binutils','gcc','cpp','glibc-devel','glibc-headers','glibc-kernheaders','compat-db','compat-gcc','compat-gcc-c++','compat-libstdc++','compat-libstdc++-devel','gnome-libs','openmotif21','setarch']



for eachRpm in listOfRpms:
checkForRpm(eachRpm)
global notInstalled
if len(notInstalled) > 0:
print 'The following RPMS are not installed:'
for eachRpm in notInstalled:
print eachRpm
print 'Please install them for the installation to
continue.'
sys.exit(-1)






def main():
preReqCheckRpms()



if __name__ == '__main__':
main()
----

* This is NOT Working.
* notInstalled.append(rpmname) is not taking effect in
checkForRpm(rpmname)
* I dont know how to communicate the notInstalled list to
preReqCheckRpms.
 
J

Juho Schultz

Phoe6 said:
Hi all,
Part of my script is to check for pre-requisite rpms to be
installed.
If its installed, I just display the rpm version as in rpm database,
otherwise I output a message saying:
' rpm is not installed' and collect the rpm name in a list
(notInstalled).
At the end if the len(notInstalled) is greater than 0 then I display
them all asking it to be installed and exit the program.
My Script is below:
-------
def checkForRpm(rpmname):
''' Check for the presence of the RPM. '''
cin,cout,cerr = os.popen3('rpm -q ' + rpmname)
output = cout.read()
global notInstalled
if len(output) <= 0:
print rpmname + ' not installed.'
notInstalled.append(rpmname)
else:
print output



def preReqCheckRpms():
''' Check for the required RPMS '''
listOfRpms = ['firefox','senthil',]


for eachRpm in listOfRpms:
checkForRpm(eachRpm)
global notInstalled
if len(notInstalled) > 0:
print 'The following RPMS are not installed:'
for eachRpm in notInstalled:
print eachRpm
print 'Please install them for the installation to
continue.'
sys.exit(-1)
* This is NOT Working.
* notInstalled.append(rpmname) is not taking effect in
checkForRpm(rpmname)
* I dont know how to communicate the notInstalled list to
preReqCheckRpms.

I think return values should be used for communication between
functions. Maybe something like this could work for you (not tested).

def checkForRpm(rpmname):
# <cut start of function>
# Strings with 0 lenght are False
if output:
print output
else:
print rpmname + ' is not installed'
return output

def checkPreReqs():
missingRpms = []
for requiredRpm in listOfRpms:
if not checkForRpm(requiredRpm):
missingRpms.append(requiredRpm)
# you could also do this by a list comprehension
missingRpms = [reqRpm for reqRpm in listOfRpms if not
checkForRpm(reqRpm)]
# or you could use the builtin function filter() - see
filter.__doc__ for that

# now, list of lenght 0 is False.
if missingRpms:
# print error messages, exit.
 
P

Phoe6

Juho said:
I think return values should be used for communication between
functions. Maybe something like this could work for you (not tested).

def checkForRpm(rpmname):
# <cut start of function>
# Strings with 0 lenght are False
if output:
print output
else:
print rpmname + ' is not installed'
return output

def checkPreReqs():
missingRpms = []
for requiredRpm in listOfRpms:
if not checkForRpm(requiredRpm):
missingRpms.append(requiredRpm)
# you could also do this by a list comprehension
missingRpms = [reqRpm for reqRpm in listOfRpms if not
checkForRpm(reqRpm)]
# or you could use the builtin function filter() - see
filter.__doc__ for that

Thanks Juho. I got the logic to workout the problem and I was able to
solve it. There was bit alteration of my code was required. Its working
now. Thanks.
---
def checkForRpm(rpmname):
''' Check for the presence of the RPM. '''
cin,cout,cerr = os.popen3('rpm -q ' + rpmname)
rpmStatus = cout.read()
print rpmStatus,
return rpmStatus



def preReqCheckRpms():
''' Check for the required RPMS '''
listOfRpms =
['firefox','senthil','binutils','gcc','cpp','glibc-devel','glibc-headers','glibc-kernheaders','compat-db','compat-gcc','compat-gcc-c++','compat-libstdc++','compat-libstdc++-devel','gnome-libs','openmotif21','setarch']
missingRpms = []
for requiredRpm in listOfRpms:
if checkForRpm(requiredRpm).find('is not installed') >
0:
missingRpms.append(requiredRpm)
if missingRpms:
print 'The following RPMS are not installed:'
for eachMissingRpm in missingRpms:
print eachMissingRpm
print 'Please install them for the installation to
continue.'
sys.exit(-1)



---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top