[Grok-dev] howto run python scripts with grok application
Alberto Berti
alberto at metapensiero.it
Sun Jan 3 11:29:33 EST 2010
>>>>> "Lacko" == Lacko Roman <rlacko at gratex.com> writes:
Lacko> Hi, In zope2 it is possible to run custom scripts with
Lacko> command like "bin/instance run myscript.py" (In myscript.py i
Lacko> want to import some object to ZODB)
i do this by preparing stuff this whay:
* code the script as a normal package module, with its "main()" entry
point to do the usual cmdline processing and the startup of zope
configuration, which is, ripped from other code:
import zope.app.wsgi
db = zope.app.wsgi.config(zope_conf_filepath)
then get your application as it was looked up by publishing
connection = db.open()
from zope.app.publication.zopepublication import ZopePublication
root = connection.root()[ZopePublication.root_name]
application = root[application_name]
from zope.app.component.hooks import setSite
setSite(application)
# do your job with the application
import transaction
transaction.commit()
* add an entry to you buildout.cfg so that an executable will be
created for you under bin/ which has all the imports needed and that
all what it does is to call your main()
[your_script_name]
recipe = z3c.recipe.dev:script
eggs = needed_egg1 needed_egg2
module = the.library.import.path.to.your.module
method = main
then add your_script_name to the parts entry;
* The app should have a zeo setup so that you can have both 2 processes
accessing the database, the wsgi web app and the process script. This
is optional, otherwise you have to stop the web app and then run your
script.
hope it helps
Alberto
More information about the Grok-dev
mailing list