[Plone-IT] Plone & mechanize (REPOST)

luigi scarso luigi.scarso a gmail.com
Gio 29 Gen 2009 15:44:43 UTC


2009/1/26 Fabrizio Reale <fabrizio.reale a redomino.com>

> Ciao,
> > Per il momento sto testando il mio "approccio alla zopectl" .
>
> Mi fai capire, per favore, come mai ti piace questa strada?
> Io l'ho usata solo per debug e quindi sono interessato ad
> eventuali vantaggi che ho trascurato.
>


Questo "fa" il task di cui ho bisogno:
1) crea una folder   (localArgs.arg[1])
2) ci mette dentro un file (localArgs.arg[2])
3) ci mette dentro un file (localArgs.arg[3])

L'ho testato in 10 task concorrenti con  un desktop Dell
AMD Athlon(tm) 64 X2 Dual Core Processor 4800+
cpu MHz         : 1000.000
cache size      : 512 KB
1GByte di memoria

e non "perde" pezzi.

Il decorator
entryExit(f)
dovrebbe isolare la parte di gestione dell'accesso a ZODB
dalle funzioni che fanno il lavoro
(mkdir, copy)

La chiarezza e' tutto dire.
#
#
#
#


import sys
import os
##
import AccessControl.SecurityManagement
from pprint import pprint as pprint
from ZODB.POSException import ConflictError
from Testing import makerequest
import transaction
import random
import time
## localArgs  is from upload-zopectl.sh
## really bad way to pass arguments ?
import localArgs



## Utility
def _last_node_frompath(node,path):
    node = site
    for attr in path.split('/'):
        if len(attr) == 0: continue
        node = getattr(node,attr)
    return node



## Decorator
def entryExit(f):
    def new_f(*argv,**kv):
        Limit = 30
        last_t = 1.0
        for j in range(0,Limit):
            try:
                #savepoint = transaction.savepoint()
                print " ** Entering", f.__name__
                res = f(*argv,**kv)
                print " ** Exited", f.__name__
                return res
            except ConflictError:
                #savepoint.rollback()
                if (j+1) == Limit: raise
                else:      pass
            except Exception ,e :
                #savepoint.rollback()
                print "Exception",e ,
                last_t = random.uniform(1.1,2.1) * last_t
                print " sleep", last_t
                time.sleep( last_t )
    new_f.__name__ = f.__name__
    return new_f

#hmm no decorator
#@entryExit
#
def login_admin(app):
    site = makerequest.makerequest(app)
    admin = site.acl_users.getUser('admin')
    AccessControl.SecurityManagement.newSecurityManager(None,admin)
    return site


@entryExit
def logout():
    pass



@entryExit
def mkdir(site ,path='/Plone/Members/testuser',title='sample02',desc='foo'):
    site._p_jar.sync()
    node = site
    for attr in path.split('/'):
        if len(attr) == 0: continue
        node = getattr(node,attr)
    folder = node.invokeFactory("Folder", id=title,title=title)
    node = getattr(node,title)
    node.reindexObject()
    transaction.commit()
    site._p_jar.sync()
    return folder


@entryExit
def copy(site
,path='/Plone/Members/testuser/sample02',title='sample',src=''):
    site._p_jar.sync()
    folder = _last_node_frompath(site,path)
    content_type = os.path.splitext(title)[1]
    ##
    if content_type == '.pdf' :

folder.invokeFactory('File',id=title,title=title,format='application/pdf',content_type='application/pdf',)
        print 'pdf'
    elif content_type == '.swf' :

folder.invokeFactory('File',id=title,title=title,format='application/x-shockwave-flash',content_type='application/x-shockwave-flash',)
        print 'swf'
    else:
        folder.invokeFactory('File',id=title,title=title,)
    file1 = open(src,'rb').read()
    content = getattr(folder,title)
    content.edit(file=file1)
    if content_type == '.swf' :
        content.setFilename(title)
    content.reindexObject()
    transaction.commit()
    site._p_jar.sync()
    return content


###############
from Zope2.Startup.run import configure
configure(os.environ['CONFIG_FILE'])
import Zope2
#
print "TRY ",localArgs.arg[1]
app = None
Limit = 30
last_t = 1.0
for j in range(0,Limit):
    try:
        print "Try app : " ,j
        app = Zope2.app()
        break
    except ConflictError:
        if (j+1) == Limit: raise
        else:      pass
    except Exception ,e :
        print "Exception",e ,
        last_t = random.uniform(1.1,2.1) * last_t
        print " sleep", last_t
        time.sleep( last_t )
#
site = login_admin(app)
pprint( site.Plone )
basepath = '/Plone/Members/testuser'
title = localArgs.arg[1]
folder = mkdir(site=site, path=basepath, title=title, desc='')
#
path=basepath+'/'+title
#
target_file = localArgs.arg[2]
dest = os.path.basename(target_file)
copy(site=site, path=path, title=dest,src=target_file)
#
target_file = localArgs.arg[3]
dest = os.path.basename(target_file)
copy(site=site, path=path, title=dest,src=target_file)





-- 
luigi
-------------- parte successiva --------------
Un allegato HTML  stato rimosso...
URL: <http://lists.plone.org/pipermail/plone-plone-it/attachments/20090129/1effdc15/attachment.html>


Maggiori informazioni sulla lista Plone-IT