/
context.py
692 lines (541 loc) · 20 KB
/
context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
##############################################################################
#
# Copyright (c) 2004 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
""" Various context implementations for export / import of configurations.
Wrappers representing the state of an import / export operation.
"""
import logging
import os
import time
from io import BytesIO
from tarfile import DIRTYPE
from tarfile import TarFile
from tarfile import TarInfo
import six
from AccessControl.class_init import InitializeClass
from AccessControl.Permissions import view
from AccessControl.SecurityInfo import ClassSecurityInfo
from Acquisition import Implicit
from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
from Acquisition import aq_self
from DateTime.DateTime import DateTime
from OFS.DTMLDocument import DTMLDocument
from OFS.Folder import Folder
from OFS.Image import File
from OFS.Image import Image
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
from Products.PythonScripts.PythonScript import PythonScript
from zope.interface import implementer
from .interfaces import SKIPPED_FILES
from .interfaces import SKIPPED_SUFFIXES
from .interfaces import IChunkableExportContext
from .interfaces import IChunkableImportContext
from .interfaces import IExportContext
from .interfaces import IImportContext
from .interfaces import ISetupEnviron
from .interfaces import IWriteLogger
from .permissions import ManagePortal
@implementer(IWriteLogger)
class Logger:
def __init__(self, id, messages):
"""Initialize the logger with a name and an optional level.
"""
self._id = id
self._messages = messages
self._logger = logging.getLogger('GenericSetup.%s' % id)
def debug(self, msg, *args, **kwargs):
"""Log 'msg % args' with severity 'DEBUG'.
"""
self.log(logging.DEBUG, msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
"""Log 'msg % args' with severity 'INFO'.
"""
self.log(logging.INFO, msg, *args, **kwargs)
def warning(self, msg, *args, **kwargs):
"""Log 'msg % args' with severity 'WARNING'.
"""
self.log(logging.WARNING, msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
"""Log 'msg % args' with severity 'ERROR'.
"""
self.log(logging.ERROR, msg, *args, **kwargs)
def exception(self, msg, *args):
"""Convenience method for logging an ERROR with exception information.
"""
self.error(msg, *args, **{'exc_info': 1})
def critical(self, msg, *args, **kwargs):
"""Log 'msg % args' with severity 'CRITICAL'.
"""
self.log(logging.CRITICAL, msg, *args, **kwargs)
def log(self, level, msg, *args, **kwargs):
"""Log 'msg % args' with the integer severity 'level'.
"""
self._messages.append((level, self._id, msg))
self._logger.log(level, msg, *args, **kwargs)
@implementer(ISetupEnviron)
class SetupEnviron(Implicit):
"""Context for body im- and exporter.
"""
security = ClassSecurityInfo()
def __init__(self):
self._should_purge = True
@security.protected(ManagePortal)
def getLogger(self, name):
"""Get a logger with the specified name, creating it if necessary.
"""
return logging.getLogger('GenericSetup.%s' % name)
@security.protected(ManagePortal)
def shouldPurge(self):
"""When installing, should the existing setup be purged?
"""
return self._should_purge
InitializeClass(SetupEnviron)
class BaseContext(SetupEnviron):
security = ClassSecurityInfo()
def __init__(self, tool, encoding):
self._tool = tool
self._site = aq_parent(aq_inner(tool))
self._loggers = {}
self._messages = []
self._encoding = encoding
self._should_purge = True
@security.protected(ManagePortal)
def getSite(self):
""" See ISetupContext.
"""
return aq_self(self._site)
@security.protected(ManagePortal)
def getSetupTool(self):
""" See ISetupContext.
"""
return self._tool
@security.protected(ManagePortal)
def getEncoding(self):
""" See ISetupContext.
"""
return self._encoding
@security.protected(ManagePortal)
def getLogger(self, name):
""" See ISetupContext.
"""
return self._loggers.setdefault(name, Logger(name, self._messages))
@security.protected(ManagePortal)
def listNotes(self):
""" See ISetupContext.
"""
return self._messages[:]
@security.protected(ManagePortal)
def clearNotes(self):
""" See ISetupContext.
"""
self._messages[:] = []
InitializeClass(BaseContext)
@implementer(IChunkableImportContext)
class DirectoryImportContext(BaseContext):
security = ClassSecurityInfo()
def __init__(self, tool, profile_path, should_purge=False,
encoding=None):
BaseContext.__init__(self, tool, encoding)
self._profile_path = profile_path
self._should_purge = bool(should_purge)
@security.protected(ManagePortal)
def openDataFile(self, filename, subdir=None):
""" See IImportContext.
"""
if subdir is None:
full_path = os.path.join(self._profile_path, filename)
else:
full_path = os.path.join(self._profile_path, subdir, filename)
if not os.path.exists(full_path):
return None
return open(full_path, 'rb')
@security.protected(ManagePortal)
def readDataFile(self, filename, subdir=None):
""" See IImportContext.
"""
result = None
file = self.openDataFile(filename, subdir)
if file is not None:
result = file.read()
file.close()
return result
@security.protected(ManagePortal)
def getLastModified(self, path):
""" See IImportContext.
"""
full_path = os.path.join(self._profile_path, path)
if not os.path.exists(full_path):
return None
return DateTime(os.path.getmtime(full_path))
@security.protected(ManagePortal)
def isDirectory(self, path):
""" See IImportContext.
"""
full_path = os.path.join(self._profile_path, path)
if not os.path.exists(full_path):
return None
return os.path.isdir(full_path)
@security.protected(ManagePortal)
def listDirectory(self, path, skip=SKIPPED_FILES,
skip_suffixes=SKIPPED_SUFFIXES):
""" See IImportContext.
"""
if path is None:
path = ''
full_path = os.path.join(self._profile_path, path)
if not os.path.exists(full_path) or not os.path.isdir(full_path):
return None
names = []
for name in os.listdir(full_path):
if name in skip:
continue
if [s for s in skip_suffixes if name.endswith(s)]:
continue
names.append(name)
return names
InitializeClass(DirectoryImportContext)
@implementer(IChunkableExportContext)
class DirectoryExportContext(BaseContext):
security = ClassSecurityInfo()
def __init__(self, tool, profile_path, encoding=None):
BaseContext.__init__(self, tool, encoding)
self._profile_path = profile_path
@security.protected(ManagePortal)
def openDataFile(self, filename, content_type, subdir=None):
""" See IChunkableExportContext.
"""
if subdir is None:
prefix = self._profile_path
else:
prefix = os.path.join(self._profile_path, subdir)
full_path = os.path.join(prefix, filename)
if not os.path.exists(prefix):
os.makedirs(prefix)
return open(full_path, 'wb')
@security.protected(ManagePortal)
def writeDataFile(self, filename, text, content_type, subdir=None):
""" See IExportContext.
"""
if isinstance(text, six.text_type):
encoding = self.getEncoding() or 'utf-8'
text = text.encode(encoding)
file = self.openDataFile(filename, content_type, subdir)
file.write(text)
file.close()
InitializeClass(DirectoryExportContext)
@implementer(IImportContext)
class TarballImportContext(BaseContext):
security = ClassSecurityInfo()
def __init__(self, tool, archive_bits, encoding=None, should_purge=False):
BaseContext.__init__(self, tool, encoding)
self._archive_stream = BytesIO(archive_bits)
self._archive = TarFile.open('foo.bar', 'r:gz', self._archive_stream)
self._should_purge = bool(should_purge)
def readDataFile(self, filename, subdir=None):
""" See IImportContext.
"""
if subdir is not None:
filename = '/'.join((subdir, filename))
try:
file = self._archive.extractfile(filename)
except KeyError:
return None
return file.read()
def getLastModified(self, path):
""" See IImportContext.
"""
info = self._getTarInfo(path)
return info and DateTime(info.mtime) or None
def isDirectory(self, path):
""" See IImportContext.
"""
info = self._getTarInfo(path)
if info is not None:
return info.isdir()
def listDirectory(self, path, skip=SKIPPED_FILES,
skip_suffixes=SKIPPED_SUFFIXES):
""" See IImportContext.
"""
if path is None: # root is special case: no leading '/'
path = ''
else:
if not self.isDirectory(path):
return None
if not path.endswith('/'):
path = path + '/'
pfx_len = len(path)
names = []
for info in self._archive.getmembers():
name = info.name.rstrip('/')
if name == path or not name.startswith(path):
continue
name = name[pfx_len:]
if '/' in name:
# filter out items in subdirs
continue
if name in skip:
continue
if [s for s in skip_suffixes if name.endswith(s)]:
continue
names.append(name)
return names
def shouldPurge(self):
""" See IImportContext.
"""
return self._should_purge
def _getTarInfo(self, path):
if path.endswith('/'):
path = path[:-1]
try:
return self._archive.getmember(path)
except KeyError:
pass
try:
return self._archive.getmember(path + '/')
except KeyError:
return None
InitializeClass(TarballImportContext)
@implementer(IExportContext)
class TarballExportContext(BaseContext):
security = ClassSecurityInfo()
def __init__(self, tool, encoding=None):
BaseContext.__init__(self, tool, encoding)
timestamp = time.gmtime()
archive_name = ('setup_tool-%4d%02d%02d%02d%02d%02d.tar.gz'
% timestamp[:6])
self._archive_stream = BytesIO()
self._archive_filename = archive_name
self._archive = TarFile.open(archive_name, 'w:gz',
self._archive_stream)
@security.protected(ManagePortal)
def writeDataFile(self, filename, text, content_type, subdir=None):
""" See IExportContext.
"""
if subdir is not None:
filename = '/'.join((subdir, filename))
parents = filename.split('/')[:-1]
while parents:
path = '/'.join(parents) + '/'
if path not in self._archive.getnames():
info = TarInfo(path)
info.type = DIRTYPE
# tarfile.filemode(0o755) == '-rwxr-xr-x'
info.mode = 0o755
info.mtime = time.time()
self._archive.addfile(info)
parents.pop()
info = TarInfo(filename)
if isinstance(text, six.text_type):
encoding = self.getEncoding() or 'utf-8'
text = text.encode(encoding)
if isinstance(text, six.binary_type):
stream = BytesIO(text)
info.size = len(text)
else:
# Assume text is a an instance of a class like
# Products.Archetypes.WebDAVSupport.PdataStreamIterator,
# as in the case of ATFile
stream = text.file
info.size = text.size
info.mtime = time.time()
self._archive.addfile(info, stream)
@security.protected(ManagePortal)
def getArchive(self):
""" Close the archive, and return it as a big string.
"""
self._archive.close()
return self._archive_stream.getvalue()
@security.protected(ManagePortal)
def getArchiveFilename(self):
""" Close the archive, and return it as a big string.
"""
return self._archive_filename
InitializeClass(TarballExportContext)
@implementer(IExportContext)
class SnapshotExportContext(BaseContext):
security = ClassSecurityInfo()
def __init__(self, tool, snapshot_id, encoding=None):
BaseContext.__init__(self, tool, encoding)
self._snapshot_id = snapshot_id
@security.protected(ManagePortal)
def writeDataFile(self, filename, text, content_type, subdir=None):
""" See IExportContext.
"""
if subdir is not None:
filename = '/'.join((subdir, filename))
sep = filename.rfind('/')
if sep != -1:
subdir = filename[:sep]
filename = filename[sep+1:]
if six.PY2 and isinstance(text, six.text_type):
encoding = self.getEncoding() or 'utf-8'
text = text.encode(encoding)
folder = self._ensureSnapshotsFolder(subdir)
# MISSING: switch on content_type
ob = self._createObjectByType(filename, text, content_type)
folder._setObject(str(filename), ob) # No Unicode IDs!
# Tighten the View permission on the new object.
# Only the owner and Manager users may view the log.
# file_ob = self._getOb(name)
ob.manage_permission(view, ('Manager', 'Owner'), 0)
@security.protected(ManagePortal)
def getSnapshotURL(self):
""" See IExportContext.
"""
return '%s/%s' % (self._tool.absolute_url(), self._snapshot_id)
@security.protected(ManagePortal)
def getSnapshotFolder(self):
""" See IExportContext.
"""
return self._ensureSnapshotsFolder()
#
# Helper methods
#
@security.private
def _createObjectByType(self, name, body, content_type):
encoding = self.getEncoding() or 'utf-8'
if six.PY2 and isinstance(body, six.text_type):
body = body.encode(encoding)
if name.endswith('.py'):
ob = PythonScript(name)
ob.write(body)
return ob
if name.endswith('.dtml'):
ob = DTMLDocument('', __name__=name)
ob.munge(body)
return ob
if content_type in ('text/html', 'text/xml'):
return ZopePageTemplate(name, body, content_type=content_type)
if isinstance(body, six.text_type):
body = body.encode(encoding)
if content_type[:6] == 'image/':
return Image(name, '', body, content_type=content_type)
return File(name, '', body, content_type=content_type)
@security.private
def _ensureSnapshotsFolder(self, subdir=None):
""" Ensure that the appropriate snapshot folder exists.
"""
path = ['snapshots', self._snapshot_id]
if subdir is not None:
path.extend(subdir.split('/'))
current = self._tool
for element in path:
if element not in current.objectIds():
# No Unicode IDs!
current._setObject(str(element), Folder(element))
current = current._getOb(element)
current.manage_permission(view, ('Manager', 'Owner'), 0)
else:
current = current._getOb(element)
return current
InitializeClass(SnapshotExportContext)
@implementer(IImportContext)
class SnapshotImportContext(BaseContext):
security = ClassSecurityInfo()
def __init__(self, tool, snapshot_id, should_purge=False, encoding=None):
BaseContext.__init__(self, tool, encoding)
self._snapshot_id = snapshot_id
self._encoding = encoding
self._should_purge = bool(should_purge)
@security.protected(ManagePortal)
def readDataFile(self, filename, subdir=None):
""" See IImportContext.
"""
if subdir is not None:
filename = '/'.join((subdir, filename))
sep = filename.rfind('/')
if sep != -1:
subdir = filename[:sep]
filename = filename[sep+1:]
try:
snapshot = self._getSnapshotFolder(subdir)
object = snapshot._getOb(filename)
except (AttributeError, KeyError):
return None
if isinstance(object, File):
# OFS File Object have only one way to access the raw
# data directly, __str__. The code explicitly forbids
# to store unicode, so str() is safe here
data = six.binary_type(aq_base(object.data))
else:
data = object.read()
if isinstance(data, six.text_type):
data = data.encode('utf-8')
return data
@security.protected(ManagePortal)
def getLastModified(self, path):
""" See IImportContext.
"""
try:
snapshot = self._getSnapshotFolder()
object = snapshot.restrictedTraverse(path)
except (AttributeError, KeyError):
return None
else:
mtime = getattr(object, '_p_mtime', None)
if mtime is None:
# test hook
mtime = getattr(object, '_faux_mod_time', None)
if mtime is None:
return DateTime()
return DateTime(mtime)
@security.protected(ManagePortal)
def isDirectory(self, path):
""" See IImportContext.
"""
try:
snapshot = self._getSnapshotFolder()
object = snapshot.restrictedTraverse(str(path))
except (AttributeError, KeyError):
return None
else:
folderish = getattr(object, 'isPrincipiaFolderish', False)
return bool(folderish)
@security.protected(ManagePortal)
def listDirectory(self, path, skip=(), skip_suffixes=()):
""" See IImportContext.
"""
try:
snapshot = self._getSnapshotFolder()
subdir = snapshot.restrictedTraverse(path)
except (AttributeError, KeyError):
return None
else:
if not getattr(subdir, 'isPrincipiaFolderish', False):
return None
names = []
for name in subdir.objectIds():
if name in skip:
continue
if [s for s in skip_suffixes if name.endswith(s)]:
continue
names.append(name)
return names
@security.protected(ManagePortal)
def shouldPurge(self):
""" See IImportContext.
"""
return self._should_purge
#
# Helper methods
#
@security.private
def _getSnapshotFolder(self, subdir=None):
""" Return the appropriate snapshot (sub)folder.
"""
path = ['snapshots', self._snapshot_id]
if subdir is not None:
path.extend(subdir.split('/'))
return self._tool.restrictedTraverse(path)
InitializeClass(SnapshotImportContext)