1 # -*- coding: utf-8 -*-
2 from argparse
import ArgumentParser
4 from AccessControl
import getSecurityManager
5 from Testing
.makerequest
import makerequest
6 from zope
.globalrequest
import setRequest
7 from zope
.site
.hooks
import setSite
8 from ZODB
.POSException
import ConflictError
9 from Products
.Photo
.cache
import aggregateIndex
12 GET_RI_SIGNATURE
= (('size', 1), ('keepAspectRatio', 2))
15 def main(app
, portal_path
, userid
, skipfile
) :
16 portal
= app
.unrestrictedTraverse(portal_path
)
17 portal
= makerequest(portal
)
18 setRequest(portal
.REQUEST
)
20 user
= portal
.acl_users
.getUser(userid
)
21 sm
= getSecurityManager()
22 sm
._context
.user
= user
24 absSkipFilePath
= os
.path
.abspath(os
.path
.expanduser(skipfile
))
25 if os
.path
.exists(absSkipFilePath
) :
26 skipFile
= open(absSkipFilePath
, 'r+')
27 skip
= filter(None, skipFile
.readlines())
28 _skipDict
= dict([(path
.strip(), True) for path
in skip
])
31 skipFile
= open(absSkipFilePath
, 'w')
34 toSkip
= _skipDict
.has_key
37 thumb_size
= portal
.thumb_size
38 ctool
= portal
.portal_catalog
39 brains
= ctool
.unrestrictedSearchResults(portal_type
='Photo', tiles_available
=1)
40 brains
= [b
for b
in brains
if not toSkip(b
.getPath())]
44 for i
, b
in enumerate(brains
) :
46 p
= b
._unrestrictedGetObject
()
48 print '%d/%d: %s' % (i
+1, len(brains
), p
.absolute_url())
51 if hasattr(p
, 'thumbnail'):
52 print 'make thumbnail'
53 delattr(p
, 'thumbnail')
54 p
.thumb_width
= thumb_size
55 p
.thumb_height
= thumb_size
59 for size
in ((500, 500), (600, 600), (800, 800)) :
60 index
= aggregateIndex(GET_RI_SIGNATURE
, (size
, True))
61 if p
._methodResultsCache
['_getResizedImage'].has_key(index
) :
62 del p
._methodResultsCache
['_getResizedImage'][index
]
63 print 'resize at', size
64 p
._getResizedImage
(size
, True)
67 zMin
= p
.tiles_min_zoom
68 zMax
= p
.tiles_max_zoom
69 zStep
= p
.tiles_step_zoom
70 levels
= range(zMin
, zMax
+ zStep
, zStep
)
71 zooms
= [l
/100. for l
in levels
]
73 if p
.tileGenerationLock
.locked() :
74 print 'skip %s: already tiling.' % p
.absolute_url()
77 p
.tileGenerationLock
.acquire()
82 print 'tiling at', zoom
84 rppm
= ppm
.resize(ratio
=zoom
)
87 p
._makeTilesAt
(zoom
, rppm
)
92 except UnboundLocalError : pass
93 p
.tileGenerationLock
.release()
96 delattr(p
, '_v__methodResultsCache')
97 except AttributeError:
100 _skipDict
[path
] = True
101 skipFile
.write('%s\n' % path
)
103 except ConflictError
:
104 print 'Resync after ZODB ConflicError'
107 brains
= ctool
.unrestrictedSearchResults(portal_type
='Photo', tiles_available
=1)
108 brains
= [b
for b
in brains
if not toSkip(b
.getPath())]
111 except KeyboardInterrupt:
114 p
.tiles_available
= 1
116 p
.reindexObject(idxs
=['tiles_available'])
119 print 'queue finished.'
123 print 'Objects deleted during processing'
125 brains
= ctool
.unrestrictedSearchResults(portal_type
='Photo', tiles_available
=1)
126 brains
= [b
for b
in brains
if not toSkip(b
.getPath())]
128 except ConflictError
:
129 print 'Resync after ZODB ConflicError'
132 brains
= ctool
.unrestrictedSearchResults(portal_type
='Photo', tiles_available
=1)
133 brains
= [b
for b
in brains
if not toSkip(b
.getPath())]
135 except KeyboardInterrupt:
142 if __name__
== '__main__':
143 parser
= ArgumentParser(description
="Thumbnails regeneration")
144 parser
.add_argument('portal_path', help='portal object path')
145 parser
.add_argument('userid', help='zope user id')
146 parser
.add_argument('--skipfile',
149 default
='skipfile.txt',
150 help="File that log work's progress.")
151 args
= parser
.parse_args()