Product SiteDocumentation Site

16.6.4.2. Coding A Sample Callback

The following code shows a valid sample callback for upgrading and installing packages.
# Global file descriptor for the callback.
rpmtsCallback_fd = None
def runCallback(reason, amount, total, key, client_data):
global rpmtsCallback_fd
if reason == rpm.RPMCALLBACK_INST_OPEN_FILE:
print "Opening file. ", reason, amount, total, key, client_data
rpmtsCallback_fd = os.open(client_data, os.O_RDONLY)
return rpmtsCallback_fd
elif reason == rpm.RPMCALLBACK_INST_START:
print "Closing file. ", reason, amount, total, key, client_data
os.close(rpmtsCallback_fd)
This callback assumes that the call to addInstall passed client data of the package file name. This callback ignores the client_data passed to the run method, but this is a perfect slot for passing an object. You can use this, for example, to avoid having a global variable for the file descriptor.