[275] | 1 | import os.path |
---|
| 2 | import time |
---|
| 3 | |
---|
| 4 | from zope.interface import classProvides, implements |
---|
| 5 | |
---|
| 6 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
| 7 | from collective.transmogrifier.utils import defaultMatcher |
---|
| 8 | |
---|
| 9 | from Products.GenericSetup import context |
---|
| 10 | from Products.CMFCore import utils |
---|
| 11 | |
---|
| 12 | class WriterSection(object): |
---|
| 13 | classProvides(ISectionBlueprint) |
---|
| 14 | implements(ISection) |
---|
| 15 | |
---|
| 16 | def __init__(self, transmogrifier, name, options, previous): |
---|
| 17 | self.previous = previous |
---|
| 18 | self.context = transmogrifier.context |
---|
| 19 | |
---|
| 20 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
| 21 | self.fileskey = defaultMatcher(options, 'files-key', name, 'files') |
---|
| 22 | |
---|
| 23 | if 'prefix' in options: |
---|
| 24 | self.prefix = options['prefix'].strip() |
---|
| 25 | else: |
---|
| 26 | self.prefix = '' |
---|
| 27 | |
---|
| 28 | context_type = options.get('context', 'tarball').strip() |
---|
| 29 | |
---|
| 30 | setup_tool = utils.getToolByName(self.context, 'portal_setup') |
---|
| 31 | if context_type == 'directory': |
---|
| 32 | profile_path = options.get('path', '') |
---|
| 33 | self.export_context = context.DirectoryExportContext(setup_tool, profile_path) |
---|
| 34 | elif context_type == 'tarball': |
---|
| 35 | self.export_context = context.TarballExportContext(setup_tool) |
---|
| 36 | elif context_type == 'snapshot': |
---|
| 37 | items = ('snapshot',) + time.gmtime()[:6] |
---|
| 38 | snapshot_id = '%s-%4d%02d%02d%02d%02d%02d' % items |
---|
| 39 | self.export_context = context.SnapshotExportContext(setup_tool, snapshot_id) |
---|
| 40 | else: |
---|
| 41 | self.export_context = context.TarballExportContext(setup_tool) |
---|
| 42 | |
---|
| 43 | def __iter__(self): |
---|
| 44 | for item in self.previous: |
---|
| 45 | pathkey = self.pathkey(*item.keys())[0] |
---|
| 46 | fileskey = self.fileskey(*item.keys())[0] |
---|
| 47 | |
---|
| 48 | if not (pathkey and fileskey): # path doesn't exist or no data to write |
---|
| 49 | yield item; continue |
---|
| 50 | |
---|
| 51 | item['_export_context'] = self.export_context |
---|
| 52 | |
---|
| 53 | path = item[pathkey] |
---|
| 54 | |
---|
| 55 | item_path = os.path.join(self.prefix, path) |
---|
| 56 | item_path.rstrip('/') |
---|
| 57 | |
---|
| 58 | for k, v in item[fileskey].items(): |
---|
| 59 | self.export_context.writeDataFile(v['name'], v['data'], 'text/xml', subdir=item_path) |
---|
| 60 | |
---|
| 61 | yield item |
---|