Author: elliss
Update of /cvs/docs/rpm-guide
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv918
Added Files:
rpm-guide-programming-python-en.xml
Log Message:
--- NEW FILE rpm-guide-programming-python-en.xml ---
<!-- $Id: -->
<chapter id="ch-rpm-programming-python">
<title>Programming RPM with Python</title>
<para>
Copyright (c) 2005 by Eric Foster-Johnson. This material may be
distributed only subject to the terms and conditions set forth in
the Open Publication License, v1.0 or later (the latest version is
presently available at
http://www.opencontent.org/openpub/).
</para>
<para/>
<para>
In This Chapter
</para>
<para>
*Using the RPM with Python
</para>
<para>
*Installing the necessary modules
</para>
<para>
*Programming with the RPM database
</para>
<para>
*Programming with RPM files
</para>
<para>
*Installing packages programmatically
</para>
<para>
P
</para>
<sect1>
<title>Setting Up a Python Development Environment</title>
<para>
Setting up a Python development environment is much the same as
setting up a C programming environment. You need to install a set
of packages for general Python development, install a package that
provides the Python API to the RPM system, and choose a program
for editing your Python scripts.
</para>
<para>
Cross Reference
</para>
<para>
Appendix H covers Linux text editors and development tools.
</para>
<para>
If you want to make a graphical user interface in your Python
programs, you need to install a separate Python package.
</para>
<sect2>
<title>Installing the base Python packages</title>
<para>
The base Python package needed for developing applications is
python. For RPM usage, you should install Python 2.2, not Python
1.5. That���s because the RPM bindings for Python are moving to
support only 2.2 and higher releases.
</para>
<para>
The Python package for RPM access is rpm-python. Install these
as you would any other packages.
</para>
<para>
Cross Reference
</para>
<para>
Chapter 4 covers installing packages.
</para>
</sect2>
<sect2>
<title>Using Python for graphics</title>
<para>
Python supports a number of different toolkits for creating
graphical user interfaces. You need one of these toolkits if you
want to create Python applications that sport a user interface
instead of command-line tools. Among the most popular toolkits
are PyGKT, PyQt, and Tkinter.
</para>
<para>
*PyGTK is a binding between Python and the GTK+ toolkit used by
the GNOME desktop, one of two main desktop environments for
Linux. (KDE is the other main desktop environment.) The Red Hat
redhat-config-packages program uses PyGTK and sports a very
good-looking user interface.
</para>
<para>
PyGTK provides full access to the GTK+ widgets such as menus,
dialog windows, and buttons. Install the pygtk2 module for
PyGTK. For more on PyGTK, see
www.daa.com.au/~james/pygtk/.
</para>
<para>
*PyQt connects Python scripts to the Qt C++ user interface
toolkit. Qt forms the base library used by the KDE desktop
environment and KDE applications. As with PyGTK, PyQt allows you
to access the rich widget set provided by the library.
</para>
<para>
Install the PyQt package for PyQt. For more on PyQt, see
www.riverbankcomputing.co.uk/pyqt/.
</para>
<para>
*Tkinter is considered a standard part of Python and is based on
the Tk (pronounced teekay) toolkit from the Tcl scripting
language. The main advantages of Tkinter are that it is
considered part of Python, meaning users are more likely to have
it, and Tkinter works on multiple platforms, including Windows.
</para>
<para>
The main drawback of Tkinter is that the widget sets are not as
rich as PyQt or PyGTK. For more on Tkinter, see
www.python.org/topics/tkinter/.
</para>
<para>
After you���ve set up your environment and installed all the
necessary packages, the next step is to start working with the
Python API for RPM.
</para>
</sect2>
</sect1>
<sect1>
<title>The Python API Hierarchy</title>
<para>
The RPM Python API provides a high-level abstraction into RPM
functionality divided into logical areas. Table 17-1 lists the
main RPM types. In most cases, you need to begin with rpm and
create a transaction set.
</para>
<para>
Table 17-1 Python types for RPM usage
</para>
<informaltable frame="all">
<tgroup cols="2">
<tbody>
<row>
<entry>
<para>
Class
</para>
</entry>
<entry>
<para>
Covers
</para>
</entry>
</row>
<row>
<entry>
<para>
rpm
</para>
</entry>
<entry>
<para>
RPM base module into RPM API
</para>
</entry>
</row>
<row>
<entry>
<para>
rpmts
</para>
</entry>
<entry>
<para>
Transaction sets
</para>
</entry>
</row>
<row>
<entry>
<para>
rpmte
</para>
</entry>
<entry>
<para>
Transaction elements, a package in a transaction set
</para>
</entry>
</row>
<row>
<entry>
<para>
rpmmi
</para>
</entry>
<entry>
[...2692 lines suppressed...]
# rpm -q jikes
</para>
<para>
jikes-1.17-1
</para>
<para/>
<para>
# python rpmupgrade.py jikes-1.18-1.i386.rpm
</para>
<para>
Upgrading jikes-1.18-1
</para>
<para>
This upgrade will install:
</para>
<para>
jikes-1.18-1
</para>
<para>
jikes-1.17-1
</para>
<para>
Running transaction (final step)...
</para>
<para>
Opening file. 4 0 0 jikes-1.18-1.i386.rpm 1
</para>
<para>
Closing file. 2 0 2854204 jikes-1.18-1.i386.rpm 1
</para>
<para/>
<para>
# rpm -q jikes
</para>
<para>
jikes-1.18-1
</para>
<para>
This example shows that the package was upgraded after running
the rpmupgrade.py script. Note that with an upgrade, the
original package, jikes-1.17-1 in this case, is also added to
the transaction set. With an install, this is not the case.
That���s because the original package is removed as part of
the transaction.
</para>
<para>
If you run this script as a non-root user, you will likely see
an error like the following:
</para>
<para>
$ python rpmupgrade.py jikes-1.18-1.i386.rpm
</para>
<para>
Upgrading jikes-1.18-1
</para>
<para>
This upgrade will install:
</para>
<para>
jikes-1.18-1
</para>
<para>
jikes-1.17-1
</para>
<para>
Running transaction (final step)...
</para>
<para>
error: cannot get exclusive lock on /var/lib/rpm/Packages
</para>
<para>
error: cannot open Packages index using db3 - Operation not
permitted (1)
</para>
<para>
error: cannot open Packages database in /var/lib/rpm
</para>
<para>
If a package has a dependency on a file such as a shared
library, you will see output like the following:
</para>
<para>
# python rpmupgrade.py jikes-1.17-glibc2.2-1.i386.rpm
jpilot-0_97-1_i386.rpm
</para>
<para>
Upgrading jikes-1.17-1
</para>
<para>
Upgrading jpilot-0.97-1
</para>
<para>
Must find file [ libpisock.so.3 ]
</para>
<para>
Error: Unresolved dependencies, transaction failed.
</para>
<para>
(('jpilot', '0.97', '1'), ('libpisock.so.3',
None), 0, None,
0)
</para>
<para>
If a package has a dependency on another package, you will see
output like the following:
</para>
<para>
# python rpmupgrade.py eruby-devel-0.9.8-2.i386.rpm
</para>
<para>
Upgrading eruby-devel-0.9.8-2
</para>
<para>
Must find package [ eruby-libs - 0.9.8 ]
</para>
<para>
Error: Unresolved dependencies, transaction failed.
</para>
<para>
(('eruby-devel', '0.9.8', '2'), ('eruby-libs',
'0.9.8'), 8,
None, 0)
</para>
</sect3>
</sect2>
</sect1>
<sect1>
<title>Where to Go from Here</title>
<para>
The RPM bindings for Python are documented along with the C
programming API. On a Red Hat Linux system, look in the file
/usr/share/doc/rpm-devel-4.1/apidocs/html/group__python.html to
see the start of the Python-specific documentation.
</para>
<para>
Note that much of this online documentation covers the C functions
that provide the Python bindings, not the Python API itself. But,
if you examine the online information on objects listed as
classes, such as rpmts, you can find the Python-specific
documentation.
</para>
<para>
Furthermore, if you look into the .c files that make up the Python
bindings, you can find PyMethodDef structure tables. These tables
provide useful glimpses into the Python API.
</para>
<para>
To learn more about programming in Python, install the python-docs
package. The python-docs package has a large set of online
documentation for Python, including the official Python Tutorial.
With Red Hat Linux, start at
/usr/share/doc/python-docs-2.2.1/html/tut/tut.html.
</para>
<para>
Cross Reference
</para>
<para>
Other tutorials are available at
http://diveintopython.org for the
Dive Into Python tutorial for experienced programmers, and at
http://py.vaults.ca/parnassus/apyllo.py/935043691.636055170 for
the Vaults of Parnassus listing of tutorials.
</para>
</sect1>
<sect1>
<title>Summary</title>
<para>
This chapter introduces the high-level RPM API for Python
programming. You can use this API from Python scripts to perform
RPM functionality, just as you can write C programs using the RPM
C API covered in Chapter 16.
</para>
<para>
In general, the Python API is simpler and requires fewer code
statements than the corresponding functionality in the C API.
</para>
<para>
Just about all of your work with the Python API requires a
transaction set, which you can get by calling rpm.TransactionSet.
</para>
<para>
To query the RPM database, call dbMatch on the transaction set
object. To install or upgrade packages, call addInstall, check,
order, and run on the transaction set.
</para>
<para>
The next chapter switches to another language for accessing the
RPM system: Perl. With the rich set of APIs, you can write your
RPM programs in C, Python, Perl, or any language that can call on
code written in one of these languages.
</para>
</sect1>
</chapter>
<!--
Local variables:
mode: xml
sgml-parent-document:("rpm-guide-en.xml" "book" "chapter")
fill-column: 72
End:
-->