Wednesday 2 April 2008

Adapting Python setuptools to Execute Post-installation Steps

I've been experimenting with the Python setuptools package for sometime now to enable customisations for the NERC DataGrid Security installation (eggifying the Python OpenSSL package M2Crypto).  For NDG Security, a wrapper script ndg-security-install.py to easy_install allows for a custom M2Crypto build and for the fact that at the time the Python Twisted package was not available as an egg.

I now want to make the installation easier still. There's a config directory for the server egg that needs to be set-up post egg install. This a manual process but I've now incorporated this step into ndg-security-install.py. A copy is made of the ndg.security.server.conf package from the egg to a separate location - default is /etc/ndg/security/conf

To do this I need to know how to get a handle on the directory path ndg.security.server.conf contained in the latest egg. Digging into the setuptools pkg_resources package this turns out to be easy:


eggConfigDir = pkg_resources.resource_filename('ndg.security.server', 
'conf')



... but I then wanted some capability to avoid trashing a pre-existing conf/ from a previous installation. I've done this by installing the new conf/ with a suffix corresponding to the latest NDG Security server egg version. To get the version:


# Get distribution version info
serverDistro = pkg_resources.get_distribution('ndg-security-server')
configDirVers = "%s.%s" % (self.opt.configDir, serverDistro.version)


but when tested I noticed that it was picking up the wrong version - the version of the LAST install not the NEW one from the update. Moving my pkg_resources import to immediately BEFORE the version check fixed this!! ...


import pkg_resources
eggConfigDir = pkg_resources.resource_filename('ndg.security.server', 
'conf')
# Get distribution version info
serverDistro = pkg_resources.get_distribution('ndg-security-server')
configDirVers = "%s.%s" % (self.opt.configDir, serverDistro.version)
# Get distribution version info
serverDistro = pkg_resources.get_distribution('ndg-security-server')
configDirVers = "%s.%s" % (self.opt.configDir, serverDistro.version)


Previously I had my import at the top of the file. pkg_resources must check for the current egg version at import. The code to install the updated packages occurs of course at a later point in the execution.

No comments: