[275] | 1 | import os.path |
---|
| 2 | |
---|
| 3 | from zope.interface import classProvides, implements |
---|
[415] | 4 | from zope.annotation.interfaces import IAnnotations |
---|
[275] | 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 |
---|
[415] | 14 | from quintagroup.transmogrifier.logger import VALIDATIONKEY |
---|
[319] | 15 | |
---|
[275] | 16 | class ReaderSection(object): |
---|
| 17 | classProvides(ISectionBlueprint) |
---|
| 18 | implements(ISection) |
---|
| 19 | |
---|
| 20 | def __init__(self, transmogrifier, name, options, previous): |
---|
| 21 | self.previous = previous |
---|
| 22 | self.context = transmogrifier.context |
---|
[282] | 23 | self.options = options |
---|
[275] | 24 | |
---|
[415] | 25 | self.anno = IAnnotations(transmogrifier) |
---|
| 26 | self.storage = self.anno.setdefault(VALIDATIONKEY, []) |
---|
| 27 | |
---|
[275] | 28 | self.pathkey = options.get('path-key', '_path').strip() |
---|
| 29 | self.fileskey = options.get('files-key', '_files').strip() |
---|
| 30 | self.contextkey = options.get('context-key', '_import_context').strip() |
---|
| 31 | |
---|
| 32 | if 'prefix' in options: |
---|
| 33 | self.prefix = options['prefix'].strip() |
---|
| 34 | self.prefix = self.prefix.strip('/') |
---|
| 35 | else: |
---|
| 36 | self.prefix = '' |
---|
| 37 | |
---|
| 38 | context_type = options.get('context', 'tarball').strip() |
---|
| 39 | if context_type not in ['directory', 'tarball', 'snapshot']: |
---|
| 40 | context_type = 'tarball' |
---|
| 41 | |
---|
| 42 | path = options.get('path', '').strip() |
---|
| 43 | |
---|
| 44 | setup_tool = utils.getToolByName(self.context, 'portal_setup') |
---|
| 45 | if context_type == 'directory': |
---|
| 46 | self.import_context = context.DirectoryImportContext(setup_tool, path) |
---|
| 47 | elif context_type == 'tarball': |
---|
| 48 | if os.path.exists(path): |
---|
| 49 | archive = file(path, 'rb') |
---|
| 50 | archive_bits = archive.read() |
---|
| 51 | archive.close() |
---|
| 52 | else: |
---|
| 53 | archive_bits = '' |
---|
| 54 | self.import_context = context.TarballImportContext(setup_tool, archive_bits) |
---|
| 55 | elif context_type == 'snapshot': |
---|
| 56 | self.import_context = context.SnapshotImportContext(setup_tool, path) |
---|
| 57 | |
---|
| 58 | def walk(self, top): |
---|
| 59 | names = self.import_context.listDirectory(top) |
---|
[415] | 60 | if names is None: |
---|
| 61 | names = [] |
---|
[275] | 62 | yield self.readFiles(top, names) |
---|
| 63 | for name in names: |
---|
| 64 | name = os.path.join(top, name) |
---|
| 65 | if self.import_context.isDirectory(name): |
---|
| 66 | for i in self.walk(name): |
---|
| 67 | yield i |
---|
| 68 | |
---|
| 69 | def readFiles(self, top, names): |
---|
| 70 | path = top[len(self.prefix):] |
---|
| 71 | path = path.lstrip('/') |
---|
| 72 | item = {self.pathkey: path} |
---|
| 73 | for name in names: |
---|
| 74 | full_name = os.path.join(top, name) |
---|
| 75 | if self.import_context.isDirectory(full_name): continue |
---|
[282] | 76 | section = self.options.get(name, name).strip() |
---|
[275] | 77 | files = item.setdefault(self.fileskey, {}) |
---|
| 78 | files[section] = { |
---|
| 79 | 'name': name, |
---|
| 80 | 'data': self.import_context.readDataFile(name, top) |
---|
| 81 | } |
---|
| 82 | return item |
---|
| 83 | |
---|
| 84 | def __iter__(self): |
---|
| 85 | for item in self.previous: |
---|
| 86 | yield item |
---|
| 87 | |
---|
| 88 | for item in self.walk(self.prefix): |
---|
| 89 | # add import context to item (some next section may use it) |
---|
| 90 | item[self.contextkey] = self.import_context |
---|
[415] | 91 | self.storage.append(item[self.pathkey]) |
---|
[275] | 92 | yield item |
---|
[415] | 93 | |
---|
| 94 | if VALIDATIONKEY in self.anno: |
---|
| 95 | del self.anno[VALIDATIONKEY] |
---|