source: products/quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/patches.py @ 1413

Last change on this file since 1413 was 1195, checked in by koval, 15 years ago

now package is compatible with plone 2.5

File size: 3.3 KB
Line 
1import os
2import time
3from tarfile import TarInfo, DIRTYPE
4from StringIO import StringIO
5
6try:
7    from Products.GenericSetup import profile_registry, EXTENSION
8except ImportError:
9    from Products.CMFSetup import profile_registry, EXTENSION
10
11if 'quintagroup.transmogrifier:default' not in profile_registry.listProfiles():
12    profile_path = os.path.join(os.path.split(__file__)[0], 'profiles/default')
13    profile_registry.registerProfile("default",
14                                     "Transmogrifier",
15                                     "Export/import the site's structure and content.",
16                                     profile_path,
17                                     #"quintagroup.transmogrifier",
18                                     profile_type=EXTENSION
19                                    )
20
21# TarballExportContext don't write dirs in tarball and we need to fix this
22def writeDataFile( self, filename, text, content_type, subdir=None ):
23    mod_time = time.time()
24
25    if subdir is not None:
26        elements = subdir.split('/')
27        parents = filter(None, elements)
28        while parents:
29            dirname = os.path.join(*parents)
30            try:
31                self._archive.getmember(dirname+'/')
32            except KeyError:
33                info = TarInfo(dirname)
34                info.size = 0
35                info.mode = 509
36                info.mtime = mod_time
37                info.type = DIRTYPE
38                self._archive.addfile(info, StringIO())
39            parents = parents[:-1]
40
41        filename = '/'.join( ( subdir, filename ) )
42
43    stream = StringIO( text )
44    info = TarInfo( filename )
45    info.size = len( text )
46    info.mode = 436
47    info.mtime = mod_time
48    self._archive.addfile( info, stream )
49
50
51try:
52    from Products.GenericSetup.context import TarballExportContext
53    TarballExportContext.writeDataFile = writeDataFile
54except ImportError:
55    from Products.CMFSetup.context import TarballExportContext
56    TarballExportContext.writeDataFile = writeDataFile
57
58try:
59    from Products.GenericSetup.context import SKIPPED_FILES, SKIPPED_SUFFIXES
60except ImportError:
61    SKIPPED_FILES, SKIPPED_SUFFIXES = (), ()
62
63def listDirectory(self, path, skip=SKIPPED_FILES,
64                    skip_suffixes=SKIPPED_SUFFIXES):
65
66    """ See IImportContext.
67    """
68    if path is None:  # root is special case:  no leading '/'
69        path = ''
70    elif path:
71        if not self.isDirectory(path):
72            return None
73
74        if not path.endswith('/'):
75            path = path + '/'
76
77    pfx_len = len(path)
78
79    names = []
80    for name in self._archive.getnames():
81        if name == path or not name.startswith(path):
82            continue
83        name = name[pfx_len:]
84        if name.count('/') > 1:
85            continue
86        if '/' in name and not name.endswith('/'):
87            continue
88        if name in skip:
89            continue
90        if [s for s in skip_suffixes if name.endswith(s)]:
91            continue
92        # directories have trailing '/' character and we need to remove it
93        name = name.rstrip('/')
94        names.append(name)
95
96    return names
97
98try:
99    from Products.GenericSetup.context import TarballImportContext
100    TarballImportContext.listDirectory = listDirectory
101except ImportError:
102    # we don't have TarballImportContext, because setup_tool don't support importing
103    # profiles from tarball
104    pass
Note: See TracBrowser for help on using the repository browser.