[Grok-dev] howto run python scripts with grok application

Lacko Roman rlacko at gratex.com
Mon Jan 4 02:37:25 EST 2010


Thanks Alberto,

this is what i needed :) it would be great to have this on grok page as simple tutorial.

BTW: Do you know gocept.runner ? Could it be used for such task ?

Thanks
- Roman

________________________________________
From: grok-dev-bounces at zope.org [grok-dev-bounces at zope.org] On Behalf Of Alberto Berti [alberto at metapensiero.it]
Sent: Sunday, January 03, 2010 5:29 PM
To: grok-dev at zope.org
Subject: Re: [Grok-dev] howto run python scripts with grok application

>>>>> "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

_______________________________________________
Grok-dev mailing list
Grok-dev at zope.org
https://mail.zope.org/mailman/listinfo/grok-dev


More information about the Grok-dev mailing list