I am at a real loss for what to do. My boss wants me to devise a way to extract user data from a Plone installation and I don't know how to do it. The best option I've found so far is the following piece of Python code. Below it is the executed code from the console. Can anyone tell me what I'm doing wrong? I am no coder and have no experience with Python at all. Is there some easier way to do what I'm trying to do that anyone knows of? Any suggestions would help, thanks.


***********************************Here is the unexecuted code:


#!/usr/bin/python2.3


#
# Authors - Stephan February, Michael Clark
#
# License - LGPL - http://www.gnu.org/licenses/lgpl.txt
#


import sys

#set your zope directory as appropriate
sys.path.append('/usr/lib/zope/lib/python/')

from ZODB import FileStorage, DB

# set this to point to your ZODB
# NOTE: Zope/Plone needs to be stopped first
#
storage = FileStorage.FileStorage('/var/lib/zope/data/')
db = DB(storage)
connection = db.open()
root = connection.root()

#obtain a list of all acl_users for the plone site
userList = root.items()[0][1]
.getUsers()

portalData = root.items()[0][1]

#obtain a portal_membership tool
mtool = root.items()[0][1]

#the following loop will print user information as:
# username, password, emailaddress, fullname,<list of user's roles>
for user in userList:
user_wrapper = mtool.getMemberById( user.getUserName() )
email = user_wrapper.getProperty('email')
fullname = user_wrapper.getProperty('fullname')

print("'%s','%s','%s','%s'\"%s\" ") % (user, user._getPassword(),
email, fullname, user.getRoles())

db.close()


***************************************Now the executed code:


Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:0Cool [MSC v.1310 32 bit (Intel)] o
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> #!/usr/bin/python2.3
...
>>>
>>> #
... # Authors - Stephan February, Michael Clark
... #
... # License - LGPL - http://www.gnu.org/licenses/lgpl.txt
... #
...
>>>
>>> import sys
>>>
>>> #set your zope directory as appropriate
... sys.path.append('/usr/lib/zope/lib/python/')
>>>
>>> from ZODB import FileStorage, DB
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ZODB
>>>
>>> # set this to point to your ZODB
... # NOTE: Zope/Plone needs to be stopped first
... #
... storage = FileStorage.FileStorage('/var/lib/zope/data/')
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
NameError: name 'FileStorage' is not defined
>>> db = DB(storage)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'DB' is not defined
>>> connection = db.open()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'db' is not defined
>>> root = connection.root()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'connection' is not defined
>>>
>>> #obtain a list of all acl_users for the LUGS plone site
... userList = root.items()[0][1]
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'root' is not defined
>>> .getUsers()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'getUsers'
>>>
>>> portalData = root.items()[0][1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'root' is not defined
>>>
>>> #obtain a portal_membership tool
... mtool = root.items()[0][1]
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'root' is not defined
>>>
>>> #the following loop will print user information as:
... # username, password, emailaddress, fullname,<list of user's roles>
... for user in userList:
... user_wrapper = mtool.getMemberById( user.getUserName() )
... email = user_wrapper.getProperty('email')
... fullname = user_wrapper.getProperty('fullname')
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name 'userList' is not defined
>>> print("'%s','%s','%s','%s'\"%s\" ") % (user, user._getPassword(),
File "<stdin>", line 1
print("'%s','%s','%s','%s'\"%s\" ") % (user, user._getPassword(),
^
IndentationError: unexpected indent
>>> email, fullname, user.getRoles())
File "<stdin>", line 1
email, fullname, user.getRoles())
^
SyntaxError: invalid syntax
>>>
>>> db.close()

***************************************

My configuration is as follows:

Python 2.5.1
Zope 2.9.5
Plone 2.5.2

Dani AI

Generated

Brief, practical summary: three separate issues are colliding in the examples posted by . was correct that the interpreter/environment matters (the import failure for ZODB typically means you aren’t running the same Python that Plone/Zope uses). The Windows error you later saw is almost always caused by backslash escapes in the filename (e.g. \v becomes a control character), not by FileStorage itself. ’s suggestion (run code inside Zope as an external method) is a valid alternative but has security/maintenance trade‑offs.

Concrete, ordered checks to resolve this quickly:

  • Run the script with the same Python that runs Zope/Plone (or start Zope in debug mode) so ZODB and other Zope packages are already on sys.path. Adding the Plone/Zope lib/python directory to sys.path is safer than trying to guess individual package paths.
  • Verify the Data.fs path really exists and isn’t being mangled by escapes. On Windows either escape backslashes, use forward slashes, or use a raw string. For a quick test:
import os
fs = r"C:\Program Files\Plone 2\Data\var\data.fs"
print os.path.exists(fs)
  • Stop Zope before opening Data.fs directly (FileStorage expects exclusive access) or use an approach that runs inside the running instance (debug/external method) so you don’t need to stop the server.
  • Don’t rely on fragile indexing of root.items()[0]—locate the Plone site by name or detect the object with the portal_membership/acl_users attributes so your code works reliably.

A few final notes: formatting/indentation mistakes and misplaced parentheses in interactive pastes will cause the syntax/indentation errors you saw. Also, authentication backends normally store hashed passwords, so don’t expect plaintext passwords from the DB.

Recommended Answers

All 3 Replies

Given:#!/usr/bin/python2.3
...
>>> from ZODB import FileStorage, DB
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ZODB
>>>
...
My configuration is as follows:

Python 2.5.1
It looks like you problem stems from not having the right version of ZODB installed in the right place, or not running the right version of Python. Shouldn't the top line read #!/usr/bin/python2.5?

Okay, I made some changes now, and this is what happens.....


>>> import sys
>>>
>>> #set your zope directory as appropriate
... sys.path.append('C:\Program Files\Plone 2\Data\var')
>>> sys.path.append('C:\Program Files\Plone 2\Zope\lib\python\ZODB')
>>>
>>> from ZODB import FileStorage, DB
>>>
>>> # set this to point to your ZODB
... # NOTE: Zope/Plone needs to be stopped first
... #
...
>>> storage = FileStorage.FileStorage ('C:\Program Files\Plone 2\Data\var\data.f
s')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Program Files\Plone 2\Zope\lib\python\ZODB\FileStorage\
", line 112, in __init__
self._lock_file = LockFile(file_name + '.lock')
File "C:\Program Files\Plone 2\Zope\lib\python\ZODB\lock_", line 60, in
__init__
self._fp = open(path, 'w+')
IOError: [Errno 2] No such file or directory: 'C:\\Program Files\\Plone 2\\Data\
x0bar\\data.fs.lock'
>>> db = DB(storage)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'storage' is not defined
>>> connection = db.open()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'db' is not defined
>>> root = connection.root()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'connection' is not defined
>>>

Any suggestions?

i think you are working with zope/plone environment, so my suggession is to use external method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.