Changeset 756
- Timestamp:
- 03/03/11 16:58:21 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Products.ecmigration/trunk/Products/ecmigration/migrate.py
r755 r756 39 39 import os 40 40 import tarfile 41 import tempfile 41 42 42 43 … … 75 76 self.tar_archive = tarfile.open(archname, mode) 76 77 77 def addFile(self, fn, finfo, lmod): 78 """ Write a file to the archive """ 79 data = Pickle.dumps(finfo, -1) 78 def addFileFromString(self, fn, data, lmod): 79 """ Write a file from a string to the archive """ 80 80 tinfo = tarfile.TarInfo(fn) 81 81 tinfo.size = len(data) … … 84 84 # Circumvent caching for large file handling 85 85 self.tar_archive.members = [] 86 print fn 87 88 def addFileFromDisk(self, fn, arcname): 89 """ Write a file from the filesystemto the archive """ 90 self.tar_archive.add(fn, arcname) 91 print arcname 86 92 87 93 def getNextFileInfo(self): … … 124 130 'About':'about', 125 131 'Help':'help', 132 'terms_of_use':'terms-of-use', 133 'privacy_policy':'privacy-policy' 126 134 } 127 135 … … 234 242 brains = self.context.portal_catalog(path={'query':'/', 'depth':2,},) 235 243 tf = TarArchiveManager(fn, 'w:bz2') 236 self.exportUsers( context,tf)244 self.exportUsers(tf) 237 245 self.exportObjects(brains, tf) 238 246 tf.close() 239 247 240 def exportUsers(self, portal,archive):241 fn = '%s/userinfo.ecmigration' % portal.getId()248 def exportUsers(self, archive): 249 fn = '%s/userinfo.ecmigration' %self.context.getId() 242 250 objstore = {'filename':fn, 'type':'UserInfo.ecmigration', 'users':{},} 243 for user in portal.acl_users.getUsers():251 for user in self.context.acl_users.getUsers(): 244 252 username = user.getName() 245 password = portal.acl_users.source_users._user_passwords[username]253 password = self.context.acl_users.source_users._user_passwords[username] 246 254 objstore['users'][username] = { 247 255 'password':password, … … 250 258 'roles':user.getRoles(), 251 259 } 252 archive.addFile(fn, objstore, float(DateTime())) 260 data = Pickle.dumps(objstore) 261 archive.addFileFromString(fn, data, float(DateTime())) 253 262 254 263 def exportObjects(self, brains, tf): … … 261 270 def exportObject(self, brain, archive): 262 271 """ Export an object """ 272 if 'Download this Course' == brain.Title: 273 return 263 274 fn = self._getPath(brain) 264 print fn265 objstore = { 'filename':fn }266 275 obj = brain.getObject() 267 self._storeData(obj, objstore) 276 # Get the metadata and store it 277 if obj.isPrincipiaFolderish: 278 self.exportMetadata(obj, fn + '.ecmigration.directory', archive) 279 else: 280 self.exportMetadata(obj, fn + '.ecmigration.metadata', archive) 281 self.exportFile(obj, fn, archive) 282 283 def exportMetadata(self, obj, fn, archive): 284 """ Export an object's metadata """ 285 objstore = {'filename':fn} 286 self._storeMetaData(obj, objstore) 268 287 lmod = obj.getRawModification_date() 269 if obj.isPrincipiaFolderish: 270 fn += '.ecmigration.directory' 271 try: 272 archive.addFile(fn, objstore, float(lmod)) 273 except TypeError: 274 import pdb; pdb.set_trace() 288 data = Pickle.dumps(objstore) 289 archive.addFileFromString(fn, data, float(lmod)) 290 291 def exportFile(self, obj, fn, archive): 292 """ Export the primary field of an object as a file in the archive """ 293 pfield = obj.getPrimaryField() 294 fd = pfield.get(obj) 295 data = None 296 if type(fd) == type(''): 297 data = fd #StringIO(fd) 298 if type(data) != type(''): 299 data = fd.data 300 if type(data) != type(''): 301 data = fd.data.data 302 # elif fd.meta_type in ['Image', 'File']: 303 # try: 304 # data = fd.data #StringIO(fd.data) 305 # except TypeError: 306 # data = fd.data.data #StringIO(fd.data.data) 307 tf = tempfile.NamedTemporaryFile() 308 tf.write(data) 309 #while 1: 310 # buf = data.read() 311 # if buf: 312 # tf.write(buf) 313 # else: 314 # break 315 tf.seek(0) 316 archive.addFileFromDisk(tf.name, fn) 317 tf.close() 275 318 276 319 def _getPath(self, brain): … … 280 323 return os.sep.join(fn[1:]) 281 324 282 def _store Data(self, obj, objstore):325 def _storeMetaData(self, obj, objstore): 283 326 """ Export Object Metadata """ 284 327 objstore['type'] = self._transformType(obj.portal_type) 285 328 # Get field data 329 pfield = obj.getPrimaryField() 286 330 objstore['fields'] = {} 287 331 for x in obj.Schema().fields(): 288 fid = x.getName() 289 if 'id' != fid: 290 if fid in ['file', 'image']: # and 'ATBlob' == obj.meta_type: 291 # Object is a file, must get data from the file 292 # In order to pickle it 293 objstore['fields'][fid] = x.get(obj).data 332 if x != pfield: 333 fid = x.getName() 334 if 'id' != fid: 335 pass # Do not store the ID, get it from the filename instead 336 if 'clearedCopyright' == fid: 337 # if clearedcopyright flag is stored as a field 338 # move it to annotations instead 339 objstore['clearedcopyright'] = x.get(obj) 294 340 else: 295 if 'clearedCopyright' == fid: 296 # if clearedcopyright flag is stored as a field 297 # move it to annotations instead 298 objstore['clearedcopyright'] = x.get(obj) 299 else: 300 # Use the usual method to get the field data 301 objstore['fields'][fid] = x.get(obj) 341 objstore['fields'][fid] = x.get(obj) 342 # # Use the usual method to get the field data 343 # data = x.get(obj) 344 # if getattr(data, 'data', None): 345 # pass 346 # else: 347 # objstore['fields'][fid] = data 348 # #objstore['fields'][fid] = x.get(obj) 349 # #if getattr(objstore['fields'][fid], 'data', None): 350 # # objstore['fields'][fid] = x.get(obj).data 351 # # if getattr(objstore['fields'][fid], 'data', None): 352 # # objstore['fields'][fid] = objstore['fields'][fid].data 302 353 # Get non field related data 303 354 objstore['owner'] = obj.getOwner().getId() … … 330 381 def _transformId(self, fn): 331 382 """ Change IDs """ 332 if len(fn) > 1 and self.id_transforms.has_key(fn[1]): 333 import pdb; pdb.set_trace() 334 fn[1] = self.id_transforms[fn[1]] 383 if len(fn) > 2 and self.id_transforms.has_key(fn[2]): 384 fn[2] = self.id_transforms[fn[2]] 385 if len(fn) > 3 and 'about' == fn[2] and 'index_html' == fn[3]: 386 fn[3] = 'abouttext_text' 387 if len(fn) > 3 and 'help' == fn[2] and 'index_html' == fn[3]: 388 fn[3] = 'help_text' 389 if len(fn) > 3 and self.id_transforms.has_key(fn[3]): 390 fn[3] = self.id_transforms[fn[3]]
Note: See TracChangeset
for help on using the changeset viewer.
