Stack trace when loading ZEXP from a script

Hi folks.

I'm at a loss.

I'm trying to automate the import of some .zexp files from a script. Essentially the script does:

zexpFile = '/path/to/import.zexp'
_EXTENSION_IDS = [
   'plonetheme.barceloneta:default',
   'plone.app.caching:default',
   'mysite.policy:default'
]
app = makerequest.makerequest(app)
acl_users = app.acl_users
setSecurityPolicy(PermissiveSecurityPolicy())
newSecurityManager(None, OmnipotentUser().__of__(acl_users))
portal = addPloneSite(
    app,
    'mysite',
    u'My Site',
    u'Welcome to my site.',
    extension_ids=_EXTENSION_IDS,
    setup_content=False
)
portal._importObjectFromFile(zexpFile)

I run it using bin/instance-debug run. This fails with the following stack trace:

Traceback (most recent call last):
  File "support/setupSite.py", line 117, in _loadZEXPFiles
    portal._importObjectFromFile(zexpFile)
  File "/Users/kelly/.buildout/eggs/Zope2-2.13.27-py2.7.egg/OFS/ObjectManager.py", line 648, in _importObjectFromFile
    self._setObject(id, ob, set_owner=set_owner)
  File "/Users/kelly/.buildout/eggs/Zope2-2.13.27-py2.7.egg/OFS/ObjectManager.py", line 358, in _setObject
    notify(ObjectAddedEvent(ob, self, id))
  File "/Users/kelly/.buildout/eggs/zope.event-3.5.2-py2.7.egg/zope/event/__init__.py", line 31, in notify
    subscriber(event)
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/event.py", line 27, in dispatch
    component_subscribers(event, None)
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/_api.py", line 139, in subscribers
    return sitemanager.subscribers(objects, interface)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/registry.py", line 442, in subscribers
    return self.adapters.subscribers(objects, provided)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/adapter.py", line 607, in subscribers
    subscription(*objects)
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/event.py", line 36, in objectEventNotify
    component_subscribers((event.object, event), None)
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/_api.py", line 139, in subscribers
    return sitemanager.subscribers(objects, interface)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/registry.py", line 442, in subscribers
    return self.adapters.subscribers(objects, provided)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/adapter.py", line 607, in subscribers
    subscription(*objects)
  File "/Users/kelly/.buildout/eggs/Zope2-2.13.27-py2.7.egg/OFS/subscribers.py", line 113, in dispatchObjectMovedEvent
    dispatchToSublocations(ob, event)
  File "/Users/kelly/.buildout/eggs/zope.container-3.11.2-py2.7-macosx-10.14-x86_64.egg/zope/container/contained.py", line 153, in dispatchToSublocations
    for ignored in zope.component.subscribers((sub, event), None):
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/_api.py", line 139, in subscribers
    return sitemanager.subscribers(objects, interface)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/registry.py", line 442, in subscribers
    return self.adapters.subscribers(objects, provided)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/adapter.py", line 607, in subscribers
    subscription(*objects)
  File "/Users/kelly/.buildout/eggs/Zope2-2.13.27-py2.7.egg/OFS/subscribers.py", line 113, in dispatchObjectMovedEvent
    dispatchToSublocations(ob, event)
  File "/Users/kelly/.buildout/eggs/zope.container-3.11.2-py2.7-macosx-10.14-x86_64.egg/zope/container/contained.py", line 153, in dispatchToSublocations
    for ignored in zope.component.subscribers((sub, event), None):
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/_api.py", line 139, in subscribers
    return sitemanager.subscribers(objects, interface)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/registry.py", line 442, in subscribers
    return self.adapters.subscribers(objects, provided)
  File "/Users/kelly/.buildout/eggs/zope.interface-4.4.3-py2.7-macosx-10.14-x86_64.egg/zope/interface/adapter.py", line 607, in subscribers
    subscription(*objects)
  File "/Users/kelly/.buildout/eggs/plone.app.linkintegrity-3.3.7-py2.7.egg/plone/app/linkintegrity/handlers.py", line 108, in modifiedContent
    links = retriever.retrieveLinks()
  File "/Users/kelly/.buildout/eggs/plone.app.linkintegrity-3.3.7-py2.7.egg/plone/app/linkintegrity/retriever.py", line 57, in retrieveLinks
    fti = getUtility(IDexterityFTI, name=self.context.portal_type)
  File "/Users/kelly/.buildout/eggs/zope.component-4.4.1-py2.7.egg/zope/component/_api.py", line 169, in getUtility
    raise ComponentLookupError(interface, name)
ComponentLookupError: (<InterfaceClass plone.dexterity.interfaces.IDexterityFTI>, 'Document')

What's perplexing, though, is that if I just do bin/instance-debug fg and use the ZMI's "Import/export" button, I can import the same ZEXP file just fine.

What am I missing that's preventing the script from working?

This exception indicates that you are not "in the context of a Plone portal".

Use zope.components.hooks.setSite to enter a portal context. In your case above setSite(portal).

Dieter: this is hilarious.

The first thing I type by hand whenever I start bin/instance-debug debug is:

from zope.component.hooks import setSite; setSite(app.whateverportal)

And yet I completely neglected that in my script!

In any case, thanks for the fix. It's working now.

1 Like

Maybe this would help too:

bin/instance-debug -O siteid fg

where siteid is the ID of the site inside the Zope root

1 Like

There's a -O option? I had no idea.

Wouldn't it be nice if --help mentioned it? :wink: All I get is:

zopectl -- control Zope using zdaemon.

Usage: zopectl [options] [action [arguments]]

Options:
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands action [arguments] -- see below

I don’t know if it’s in zopectl but it is in ZEO clients

What do you get from --help from your ZEO client?

(My output is from the script generated by plone.recipe.zope2instance.)

Just curious.