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