[Zope] Re: Accessing zope through command-line python

sean.upton@uniontrib.com sean.upton@uniontrib.com
Mon, 24 Feb 2003 10:37:51 -0800


In addition to using ZEO (absolute necessity), here are some other tips for
anyone doing out-of-process Zope scripts like this:

- Get to know how to deal with transactions in Zope within these
out-of-process scripts, because anything you do this way needs to be
committed when you are done.  Also, using transaction notes to place the
undo history in the right place in Zope is a good idea (example below).

- To run TTW code objects, you need to be able to:
	a. Manually create acquisition context for your
		objects using __of__()
	b. Create a new SecurityManager using an existing user

This is a generic example pieced together from some existing scripts that I
have that do this... not entirely tested, but should work:

#=============================
# An example of putting new content into 
# a CMF or Plone site in an out-of-process
# Zope instance in a Python module:
#=============================

import Zope
from AccessControl.SecurityManagement import newSecurityManager
app = Zope.app()
CMFSite = app['MyCMFSite']

#Create a security context (to run invokeFactory)
# Note: you would also need to do this to run TTW code objects
uf = app['acl_users']
zopeuser = uf.getUser('zope').__of__(uf) 
#user object wrapped in aq context of user folder
newSecurityManager(None,zopeuser)        
#new security manager to help invokeFactory run
# also needed to run TTW code (PythonScripts, ZPT, DTML)

CMFSite.invokeFactory(id='SomeNewDocument',type_name='Document')
obj = CMFSite['SomeNewDocument']

#do stuff to obj to put content in SomeNewDocument...

# Run some TTW Python script in a skins folder:
skinsfolder = CMFSite['portal_skins']['MySkinsFolder']
scriptobj = skinsfolder['MyScriptName'].__of__(CMFSite)
scriptobj('foo')

get_transaction().note('/MyCMFSite') 
#first note, path of object to put undo history on
get_transaction().note('Document created and populated with content')
#second note, just a description of what was done for the undo tab

get_transaction().commit()

#====================================

I'm working on putting together an out-of-process Zope howto, which I expect
to post in the next week or two on Zope.org, but in the meantime, feel free
to email me with any questions.

Sean

-----Original Message-----
From: Maik Jablonski [mailto:maik.jablonski@uni-bielefeld.de]
Sent: Monday, February 24, 2003 9:14 AM
To: zope@zope.org
Subject: [Zope] Re: Accessing zope through command-line python


Mark Gibson wrote:
> I understand this:
> 
> ----
> import Zope
> root=Zope.app()
> #do stuff now.
> 
> ----
> 
> However, if zope is running I receive an error:
> 
> 
>   File "/home/local/zope_2_6/lib/python/ZODB/lock_file.py", line 33, in 
> lock_file
>     raise error, (
> ZODB.POSException.StorageSystemError: Could not lock the database file.  
> There must be
> another process that has opened the file.
> 
> Is there a way to access a running zope instance in this way?

Hi,

you must ZEO for this tasks. Please read the Zope-Book => ZEO-Chapter.

A nice Howto is:

http://www.zope.org/Members/michel/HowTos/ZODB-How-To

-mj

-- 
German Zope User Group (DZUG) - http://www.dzug.org/



_______________________________________________
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )