Linux - python vs sh

David M. Cook davecook at nowhere.net
Thu Jul 17 05:37:42 EDT 2003


In article <mailman.1058429007.4582.python-list at python.org>, Krisztian Kepes
wrote:

> Hi !
> 
> I want to list my rpm database to file with this:
> 
> #!/bin/sh
> 
> file="./allrpm.txt"
> nr=1
> for rpmname in `rpm -qa`
> do
>   echo '>>>>>>>>>>>>>>>' >>$file
>   echo "$nr." >>$file
>   echo "Name: $rpmname" >>$file
>   echo "" >>$file
>   info=`rpm -qi $rpmname`
>   echo "$info" >>$file
>   echo "" >>$file
>   nr=`expr $nr + 1`
> done  

You can get the same type of output using rpm format strings:

rpm -qa --queryformat ">>>>>>>>>>>>\nName: %{NAME}\n%{SUMMARY}\n"

For a listing of query tags, use

rpm --querytags

> But it have two error:
> 1. the $nr is not incremented

I usually use 

nr=$((nr+1))

> 2. the rpm detailed info is not writed to the file

Don't see why that's happening.

> I want to change it to py code. I see an perl code:
> 
> @rpmlist= rpm -qa 
> It is get the rpm's output.
> 
> But how I get the rpm's output in my py program ?
> list= rpm -qa 
> ????

You can use the commands module:

from commands import getstatusoutput

status, output = getstatusoutput("rpm -qa")

if status==0:
   #do stuff with output
else:
     print "There was an error:", output
   
Also take a look at the popen2 module.

There is also an rpm module that Red Hat uses for up2date.  There's not much
in the way of documentation, though.  There is a new book on RPM out that has
a chapter on rpm-python, but at $50 for a paperback I haven't bitten.

Dave Cook





More information about the Python-list mailing list