1 | import os.path |
---|
2 | |
---|
3 | from zope.interface import classProvides, implements |
---|
4 | |
---|
5 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
6 | from collective.transmogrifier.utils import defaultMatcher |
---|
7 | |
---|
8 | from Products.GenericSetup import context |
---|
9 | from Products.CMFCore import utils |
---|
10 | |
---|
11 | # import monkey pathes for GS TarballContext |
---|
12 | import quintagroup.transmogrifier.patches |
---|
13 | |
---|
14 | class ReaderSection(object): |
---|
15 | classProvides(ISectionBlueprint) |
---|
16 | implements(ISection) |
---|
17 | |
---|
18 | def __init__(self, transmogrifier, name, options, previous): |
---|
19 | self.previous = previous |
---|
20 | self.context = transmogrifier.context |
---|
21 | self.options = options |
---|
22 | |
---|
23 | self.pathkey = options.get('path-key', '_path').strip() |
---|
24 | self.fileskey = options.get('files-key', '_files').strip() |
---|
25 | self.contextkey = options.get('context-key', '_import_context').strip() |
---|
26 | |
---|
27 | if 'prefix' in options: |
---|
28 | self.prefix = options['prefix'].strip() |
---|
29 | self.prefix = self.prefix.strip('/') |
---|
30 | else: |
---|
31 | self.prefix = '' |
---|
32 | |
---|
33 | context_type = options.get('context', 'tarball').strip() |
---|
34 | if context_type not in ['directory', 'tarball', 'snapshot']: |
---|
35 | context_type = 'tarball' |
---|
36 | |
---|
37 | path = options.get('path', '').strip() |
---|
38 | |
---|
39 | setup_tool = utils.getToolByName(self.context, 'portal_setup') |
---|
40 | if context_type == 'directory': |
---|
41 | self.import_context = context.DirectoryImportContext(setup_tool, path) |
---|
42 | elif context_type == 'tarball': |
---|
43 | if os.path.exists(path): |
---|
44 | archive = file(path, 'rb') |
---|
45 | archive_bits = archive.read() |
---|
46 | archive.close() |
---|
47 | else: |
---|
48 | archive_bits = '' |
---|
49 | self.import_context = context.TarballImportContext(setup_tool, archive_bits) |
---|
50 | elif context_type == 'snapshot': |
---|
51 | self.import_context = context.SnapshotImportContext(setup_tool, path) |
---|
52 | |
---|
53 | def walk(self, top): |
---|
54 | names = self.import_context.listDirectory(top) |
---|
55 | yield self.readFiles(top, names) |
---|
56 | for name in names: |
---|
57 | name = os.path.join(top, name) |
---|
58 | if self.import_context.isDirectory(name): |
---|
59 | for i in self.walk(name): |
---|
60 | yield i |
---|
61 | |
---|
62 | def readFiles(self, top, names): |
---|
63 | path = top[len(self.prefix):] |
---|
64 | path = path.lstrip('/') |
---|
65 | item = {self.pathkey: path} |
---|
66 | for name in names: |
---|
67 | full_name = os.path.join(top, name) |
---|
68 | if self.import_context.isDirectory(full_name): continue |
---|
69 | section = self.options.get(name, name).strip() |
---|
70 | files = item.setdefault(self.fileskey, {}) |
---|
71 | files[section] = { |
---|
72 | 'name': name, |
---|
73 | 'data': self.import_context.readDataFile(name, top) |
---|
74 | } |
---|
75 | return item |
---|
76 | |
---|
77 | def __iter__(self): |
---|
78 | for item in self.previous: |
---|
79 | yield item |
---|
80 | |
---|
81 | for item in self.walk(self.prefix): |
---|
82 | # add import context to item (some next section may use it) |
---|
83 | item[self.contextkey] = self.import_context |
---|
84 | yield item |
---|