[SOLVED] Console_entry point scripts with access to app?

I have often some add-ons with some scripts that are run outside the control of the standard zeoclient as
bin/zeoclient run myscript. I want these scripts to be configured as console_scripts and created in the bin folder...the configuration is not the problem...the question is how to get hold of the app object...I know that this was possible somehow ...after after almost 21 years with Zope, we are all getting old and forgetful :speak_no_evil:

https://docs.plone.org/develop/plone/misc/commandline.html#running-scripts
or what mean you with "hold"?

Generated console_scripts are configured with the Python interpreter as shebang. Stuff run with bin/instance run script.py have access to app...that's what I need for the generated console_scripts.

Maybe I miss the point here but "app" is just there. You can do:

portal = app.Plone

My personal cheat sheet is in Plone itself, the first ~60 lines here:

It's not exactly what you asked for but it is the closest thing I know of: you can use an entry point provided by Zope:

In setup.py:

    entry_points={
        'zopectl.command': [
            'myscript = my.project.myscript:main'
        ]
    },

Then you'll be able to run this script like this:

bin/instance myscript [args...]

Your myscript.py should look like this:

from zope.component.hooks import setSite
import sys

def main(app, args):
    print 'Got the following args: {}'.format(args)
    portal = app['Plone']  # If your Plone site id is 'Plone'
    setSite(portal)  # You still have to setup things similar to regular "bin/instance run..." type of script.
    do_stuff(portal)

# The following lines are a trick to allow running directly with `bin/instance run script.py` if you want.
if 'app' in locals():
    main(app, sys.argv)
3 Likes

All wrong :slight_smile:

I don't want to start my console scripts as

bin/instance run bin/myscript.py

but as

bin/myscript.py.

bin/instance run... injects the app somehow into the underlying myscript.py.

But calling bin/myscript just uses the standard Python interpreter (as shebang) without direct access to app. So either I need to magic to get app myself...I think the question is: how to open a ZODB connection myself for getting hold the app object...I think this question is a shame for the former Zope release manager :slight_smile:

1 Like

Maybe this helps?

The key is passing in your zope.conf so the app can be set up.

arguments = '${instance:location}/etc/zope.conf',[...]

The ConsoleScript class uses it like this:

        starter = Zope2.Startup.run.configure(config_file)
        self.app = Zope2.app()

see https://github.com/syslabcom/slc.zopescript/blob/master/src/slc/zopescript/script.py#L73

2 Likes

Thanks, this was it^^

Plone Foundation Code of Conduct