[275] | 1 | import os |
---|
| 2 | import time |
---|
| 3 | from tarfile import TarInfo, DIRTYPE |
---|
| 4 | from StringIO import StringIO |
---|
| 5 | |
---|
| 6 | # TarballExportContext don't write dirs in tarball and we need to fix this |
---|
| 7 | |
---|
| 8 | #security.declareProtected( ManagePortal, 'writeDataFile' ) |
---|
| 9 | def 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 | |
---|
| 38 | from Products.GenericSetup.context import TarballExportContext |
---|
| 39 | TarballExportContext.writeDataFile = writeDataFile |
---|
| 40 | |
---|
| 41 | from Products.GenericSetup.context import SKIPPED_FILES, SKIPPED_SUFFIXES |
---|
| 42 | |
---|
| 43 | def 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 | |
---|
| 78 | from Products.GenericSetup.context import TarballImportContext |
---|
| 79 | TarballImportContext.listDirectory = listDirectory |
---|