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

Juho Schultz juho.schultz at pp.inet.fi
Mon Jul 24 15:01:56 EDT 2006


Phoe6 wrote:
> 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.
>
> --
> Senthil

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.

--
Juho Schultz




More information about the Python-list mailing list