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