Can you define an entry point in your Plone produc

2019-05-07 18:25发布

问题:

I have a batch job that I need to run occasionally against my Plone instance. It needs access to the code in my Plone product and other Plone code and to query the catalog. I've included the script in my Plone product and currently run it via

bin/instance run <path to script in eggs directory>

Obviously if a new version of my product comes along I need to change the path to point to the new version of the egg. What I'd like to do is define any entry point for the script in my product's setup.py and then use the buildout recipe like zc.recipe.egg so that I can just run

bin/myscript

How do I do this and still provide my script access to the top level app object and all the code installed in my Plone instance?

回答1:

As of Zope 2.13, you can register scripts for the zopectl.command entry point. These will be treated as new commands on the bin/instance controller script.

For example, the following will tie callables in your egg to commands:

[zopectl.command]
mybatch = example.egg.commands:mybatch

Your callable will be passed the root-level application object, and the remaining command line arguments:

def mybatch(app, args):
    site = app.mysiteid
    # remember to set up your site correctly (create request, call hooks, etc)

Use the args to implement command-line switches for your script.

See the Configuring and Running Zope documentation; note that your command names cannot use dashes (-) in the name.