source: products/quintagroup.transmogrifier/trunk/quintagroup/transmogrifier/patches.py @ 275

Last change on this file since 275 was 275, checked in by mylan, 18 years ago

Removed file/folder

File size: 2.3 KB
Line 
1import os
2import time
3from tarfile import TarInfo, DIRTYPE
4from StringIO import StringIO
5
6# TarballExportContext don't write dirs in tarball and we need to fix this
7
8#security.declareProtected( ManagePortal, 'writeDataFile' )
9def writeDataFile( self, filename, text, content_type, subdir=None ):
10
11    """ See IExportContext.
12    """
13    mod_time = time.time()
14
15    if subdir is not None:
16        elements = subdir.split('/')
17        parents = filter(None, elements)
18        while parents:
19            dirname = os.path.join(*parents)
20            try:
21                self._archive.getmember(dirname+'/')
22            except KeyError:
23                info = TarInfo(dirname)
24                info.size = 0
25                info.mtime = mod_time
26                info.type = DIRTYPE
27                self._archive.addfile(info, StringIO())
28            parents = parents[:-1]
29
30        filename = '/'.join( ( subdir, filename ) )
31
32    stream = StringIO( text )
33    info = TarInfo( filename )
34    info.size = len( text )
35    info.mtime = mod_time
36    self._archive.addfile( info, stream )
37
38from Products.GenericSetup.context import TarballExportContext
39TarballExportContext.writeDataFile = writeDataFile
40
41from Products.GenericSetup.context import SKIPPED_FILES, SKIPPED_SUFFIXES
42
43def listDirectory(self, path, skip=SKIPPED_FILES,
44                    skip_suffixes=SKIPPED_SUFFIXES):
45
46    """ See IImportContext.
47    """
48    if path is None:  # root is special case:  no leading '/'
49        path = ''
50    elif path:
51        if not self.isDirectory(path):
52            return None
53
54        if not path.endswith('/'):
55            path = path + '/'
56
57    pfx_len = len(path)
58
59    names = []
60    for name in self._archive.getnames():
61        if name == path or not name.startswith(path):
62            continue
63        name = name[pfx_len:]
64        if name.count('/') > 1:
65            continue
66        if '/' in name and not name.endswith('/'):
67            continue
68        if name in skip:
69            continue
70        if [s for s in skip_suffixes if name.endswith(s)]:
71            continue
72        # directories have trailing '/' character and we need to remove it
73        names.rstrip('/')
74        names.append(name)
75
76    return names
77
78from Products.GenericSetup.context import TarballImportContext
79TarballImportContext.listDirectory = listDirectory
Note: See TracBrowser for help on using the repository browser.