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