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
|
""" Attach relevant project files.
FIXME: there are no public project plugins for Envisage 3, yet. In any case,
this stuff should not be hard-coded, but extensible via extension points. The
code remains here because we can reuse the zip utility code in that extensible
rewrite.
"""
import logging
import os.path
from email import Encoders
from email.MIMEBase import MIMEBase
from enthought.traits.api import Any, HasTraits
logger = logging.getLogger(__name__)
class Attachments(HasTraits):
application = Any()
message = Any()
def __init__(self, message, **traits):
traits = traits.copy()
traits['message'] = message
super(Attachments, self).__init__(**traits)
# FIXME: all of the package_*() methods refer to deprecated project plugins.
def package_workspace(self):
if self.application is None:
pass
workspace = self.application.get_service('enthought.envisage.project.IWorkspace')
if workspace is not None:
dir = workspace.path
self._attach_directory(dir)
return
def package_single_project(self):
if self.application is None:
pass
single_project = self.application.get_service('enthought.envisage.single_project.ModelService')
if single_project is not None:
dir = single_project.location
self._attach_directory(dir)
def package_any_relevant_files(self):
self.package_workspace()
self.package_single_project()
return
def _attach_directory(self, dir):
relpath = os.path.basename(dir)
import zipfile
from cStringIO import StringIO
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
msg = MIMEBase(maintype, subtype)
file_object = StringIO()
zip = zipfile.ZipFile(file_object, 'w')
_append_to_zip_archive(zip, dir, relpath)
zip.close()
msg.set_payload(file_object.getvalue())
Encoders.encode_base64(msg) # Encode the payload using Base64
msg.add_header('Content-Disposition', 'attachment', filename='project.zip')
self.message.attach(msg)
file_object.close()
def _append_to_zip_archive(zip, dir, relpath):
""" Add all files in and below directory dir into zip archive"""
for filename in os.listdir(dir):
path = os.path.join(dir, filename)
if os.path.isfile(path):
name = os.path.join(relpath, filename)
zip.write(path, name)
logger.debug('adding %s to error report' % path)
else:
if filename != ".svn": # skip svn files if any
subdir = os.path.join(dir, filename)
_append_to_zip_archive(zip, subdir, os.path.join(relpath, filename))
return
|