views:

315

answers:

2

I need to run a simple script after the modules and programs have been installed. I'm having a little trouble finding straight-forward documentation on how to do this. It looks like I need to inherit from distutils.command.install, override some methods and add this object to the setup script. The specifics are a bit hazy though and it seems like a lot of effort for such a simple hook. Does anyone know an easy way to do this?

+3  A: 

I dug through distutils source for a day to learn enough about it to make a bunch of custom commands. It's not pretty, but it does work.

import distutils.core
import distutils.command.install
...
class my_install(distutils.command.install):
    def run(self):
        distutils.command.install.run(self)
        # Custom stuff here
        # distutils.command.install actually has some nice helper methods
        # and interfaces. I strongly suggest reading the docstrings.
...
distutils.core.setup(..., cmdclass=dict(install=my_install), ...)

`

Joe
Thanks, Joe. I already found out and posted a similar answer. You were earlier though, so enjoy the green :)
blokkie
+1  A: 

OK, I figured it out. The idea is basically to extend one of the distutils commands and overwrite the run method. To tell distutils to use the new class you can use the cmdclass variable.

from distutils.core import setup
from distutils.command.install_data import install_data

class post_install(install_data):
    def run(self):
        # Call parent 
        install_data.run(self)
        # Execute commands
        print "Running"

setup(name="example",
      cmdclass={"install_data": post_install},
      ...
      )

Hope this will help someone else.

blokkie