[1416] | 1 | import os |
---|
[679] | 2 | import tempfile |
---|
| 3 | |
---|
[282] | 4 | from zope.interface import implements |
---|
[679] | 5 | from zope.annotation import IAnnotations |
---|
[282] | 6 | |
---|
| 7 | from collective.transmogrifier.interfaces import ITransmogrifier |
---|
| 8 | from collective.transmogrifier.transmogrifier import _load_config, constructPipeline |
---|
[679] | 9 | from collective.transmogrifier.transmogrifier import configuration_registry |
---|
[282] | 10 | |
---|
| 11 | from Products.GenericSetup.context import TarballExportContext, TarballImportContext |
---|
| 12 | from Products.GenericSetup.interfaces import IFilesystemImporter |
---|
| 13 | |
---|
| 14 | from quintagroup.transmogrifier.writer import WriterSection |
---|
| 15 | from quintagroup.transmogrifier.reader import ReaderSection |
---|
[679] | 16 | from quintagroup.transmogrifier.configview import ANNOKEY |
---|
[282] | 17 | |
---|
| 18 | EXPORT_CONFIG = 'export' |
---|
| 19 | IMPORT_CONFIG = 'import' |
---|
| 20 | |
---|
[679] | 21 | CONFIGFILE = None |
---|
| 22 | def registerPersistentConfig(site, type_): |
---|
| 23 | """ Try to get persistent pipeline configuration of given type (export or import) |
---|
| 24 | and register it for use with transmogrifier. |
---|
| 25 | """ |
---|
| 26 | global CONFIGFILE |
---|
| 27 | anno = IAnnotations(site) |
---|
| 28 | key = '%s.%s' % (ANNOKEY, type_) |
---|
| 29 | config = anno.has_key(key) and anno[key] or None |
---|
| 30 | |
---|
| 31 | # unregister old config |
---|
| 32 | name = 'persitent-%s' % type_ |
---|
| 33 | if name in configuration_registry._config_ids: |
---|
| 34 | configuration_registry._config_ids.remove(name) |
---|
| 35 | del configuration_registry._config_info[name] |
---|
| 36 | |
---|
| 37 | # register new |
---|
| 38 | if config is not None: |
---|
| 39 | title = description = u'Persistent %s pipeline' |
---|
| 40 | tf = tempfile.NamedTemporaryFile('w+t', suffix='.cfg') |
---|
| 41 | tf.write(config) |
---|
| 42 | tf.seek(0) |
---|
| 43 | CONFIGFILE = tf |
---|
| 44 | configuration_registry.registerConfiguration(name, title, description, tf.name) |
---|
| 45 | return name |
---|
| 46 | else: |
---|
| 47 | return None |
---|
| 48 | |
---|
[282] | 49 | def exportSiteStructure(context): |
---|
| 50 | transmogrifier = ITransmogrifier(context.getSite()) |
---|
| 51 | |
---|
| 52 | # we don't use transmogrifer's __call__ method, because we need to do |
---|
| 53 | # some modification in pipeline sections |
---|
| 54 | |
---|
[679] | 55 | config_name = registerPersistentConfig(context.getSite(), 'export') |
---|
| 56 | if config_name is None: |
---|
| 57 | transmogrifier._raw = _load_config(EXPORT_CONFIG) |
---|
| 58 | else: |
---|
| 59 | transmogrifier._raw = _load_config(config_name) |
---|
| 60 | global CONFIGFILE |
---|
| 61 | CONFIGFILE = None |
---|
[282] | 62 | transmogrifier._data = {} |
---|
| 63 | |
---|
| 64 | options = transmogrifier._raw['transmogrifier'] |
---|
| 65 | sections = options['pipeline'].splitlines() |
---|
| 66 | pipeline = constructPipeline(transmogrifier, sections) |
---|
| 67 | |
---|
| 68 | last_section = pipeline.gi_frame.f_locals['self'] |
---|
| 69 | |
---|
| 70 | # if 'quintagroup.transmogrifier.writer' section's export context is |
---|
| 71 | # tarball replace it with given function argument |
---|
| 72 | while hasattr(last_section, 'previous'): |
---|
| 73 | if isinstance(last_section, WriterSection) and \ |
---|
| 74 | isinstance(last_section.export_context, TarballExportContext): |
---|
| 75 | last_section.export_context = context |
---|
| 76 | last_section = last_section.previous |
---|
| 77 | # end cycle if we get empty starter section |
---|
| 78 | if type(last_section) == type(iter(())): |
---|
| 79 | break |
---|
| 80 | last_section = last_section.gi_frame.f_locals['self'] |
---|
| 81 | |
---|
| 82 | # Pipeline execution |
---|
| 83 | for item in pipeline: |
---|
| 84 | pass # discard once processed |
---|
| 85 | |
---|
| 86 | def importSiteStructure(context): |
---|
| 87 | |
---|
| 88 | transmogrifier = ITransmogrifier(context.getSite()) |
---|
| 89 | |
---|
| 90 | # we don't use transmogrifer's __call__ method, because we need to do |
---|
| 91 | # some modification in pipeline sections |
---|
| 92 | |
---|
[679] | 93 | config_name = registerPersistentConfig(context.getSite(), 'import') |
---|
| 94 | if config_name is None: |
---|
| 95 | transmogrifier._raw = _load_config(IMPORT_CONFIG) |
---|
| 96 | else: |
---|
| 97 | transmogrifier._raw = _load_config(config_name) |
---|
| 98 | global CONFIGFILE |
---|
| 99 | CONFIGFILE = None |
---|
[282] | 100 | transmogrifier._data = {} |
---|
| 101 | |
---|
[1416] | 102 | # this function is also called when adding Plone site, so call standard handler |
---|
| 103 | path = '' |
---|
| 104 | if 'reader' in transmogrifier._raw: |
---|
| 105 | path = transmogrifier._raw['reader'].get('path', '') |
---|
| 106 | if not context.readDataFile('.objects.xml', subdir=os.path.join(path, 'structure')): |
---|
| 107 | try: |
---|
| 108 | from Products.GenericSetup.interfaces import IFilesystemImporter |
---|
| 109 | IFilesystemImporter(context.getSite()).import_(context, 'structure', True) |
---|
| 110 | except ImportError: |
---|
| 111 | pass |
---|
| 112 | return |
---|
| 113 | |
---|
[282] | 114 | options = transmogrifier._raw['transmogrifier'] |
---|
| 115 | sections = options['pipeline'].splitlines() |
---|
| 116 | pipeline = constructPipeline(transmogrifier, sections) |
---|
| 117 | |
---|
| 118 | last_section = pipeline.gi_frame.f_locals['self'] |
---|
| 119 | |
---|
| 120 | # if 'quintagroup.transmogrifier.writer' section's export context is |
---|
| 121 | # tarball replace it with given function argument |
---|
| 122 | while hasattr(last_section, 'previous'): |
---|
| 123 | if isinstance(last_section, ReaderSection) and \ |
---|
| 124 | isinstance(last_section.import_context, TarballImportContext): |
---|
| 125 | last_section.import_context = context |
---|
| 126 | last_section = last_section.previous |
---|
| 127 | # end cycle if we get empty starter section |
---|
| 128 | if type(last_section) == type(iter(())): |
---|
| 129 | break |
---|
| 130 | last_section = last_section.gi_frame.f_locals['self'] |
---|
| 131 | |
---|
| 132 | # Pipeline execution |
---|
| 133 | for item in pipeline: |
---|
| 134 | pass # discard once processed |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | class PloneSiteImporter(object): |
---|
| 138 | """ Importer of plone site. |
---|
| 139 | """ |
---|
| 140 | implements(IFilesystemImporter) |
---|
| 141 | |
---|
| 142 | def __init__(self, context): |
---|
| 143 | self.context = context |
---|
| 144 | |
---|
| 145 | def import_(self, import_context, subdir="structure", root=False): |
---|
| 146 | # When performing import steps we need to use standart importing adapter, |
---|
| 147 | # if 'object.xml' file is absent in 'structure' directory of the profile. |
---|
| 148 | # This may be because it is the base plone profile or extension profile, that has |
---|
| 149 | # structure part in other format. |
---|
| 150 | |
---|
| 151 | objects_xml = import_context.readDataFile('.objects.xml', subdir) |
---|
| 152 | if objects_xml is not None: |
---|
| 153 | importSiteStructure(import_context) |
---|
| 154 | else: |
---|
| 155 | from Products.CMFCore.exportimport.content import StructureFolderWalkingAdapter |
---|
| 156 | StructureFolderWalkingAdapter(self.context).import_(import_context, "structure", True) |
---|