Adding Console Scripts to a Plone Buildout

In a straight setupttools-based application, adding one or more console scripts is straightforward: you just declare them in the setup.py, and set setuptools build them for you. However, when using zc.buildout for Plone 3.0.x or 3.1.x, you have to arrange to get the non-eggified Zope2 products on the path, which is not easy. This recipe works for me.

  1. In the package's setup, declare the console script entry point as ususal:

    import os
    from setuptools import find_packages
    from setuptools import setup
    
    version = '0.1'
    
    setup(name='my.package',
          #...
          entry_points="""
            [console_scripts]
            do_something = my.package.scripts.do_something:main
          """,
         )
    
  2. Add a part to your buildout for the script(s) you want added to your bin directory:

    [parts]
    ...
    my_scripts
    
    [my_scripts]
    recipe = zc.recipe.egg
    eggs = ${instance:eggs}
    extra-paths = ${zope2:location}/lib/python
    arguments = '${instance:location}/etc/zope.conf'
    
  3. In the package, use the passed 'config_file' to get the root object:

    class DoSomething:
        config_file = 'etc/zope.conf'
        app = None
    
        def __init__(self, config_file, argv):
            if config_file is not None:
                self.config_file = config_file
            self.argv = argv
    
        def getApplication(self):
            from Zope2.Startup.run import configure
            configure(self.config_file)
            import Zope2
            return Zope2.app()
    
        def __call__(self):
            app = self.getApplication()
            # Do the actual work here, using self.app
    
    def main(config_file=None, argv=sys.argv):
        importer = DoSomething(config_file, argv)
        importer()
    
  4. Re-run buildout.