2
# largely taken from python examples
3
# http://docs.python.org/library/email-examples.html
8
# For guessing MIME type based on file name extension
11
from email import encoders
12
from email.message import Message
13
from email.mime.base import MIMEBase
14
from email.mime.multipart import MIMEMultipart
15
from email.mime.text import MIMEText
16
from optparse import OptionParser
21
starts_with_mappings={
22
'#include' : 'text/x-include-url',
23
'#!' : 'text/x-shellscript',
24
'#cloud-config' : 'text/cloud-config',
25
'#upstart-job' : 'text/upstart-job',
26
'#part-handler' : 'text/part-handler',
27
'#cloud-boothook' : 'text/cloud-boothook'
30
def get_type(fname,deftype):
35
for str,mtype in starts_with_mappings.items():
36
if line.startswith(str):
42
outer = MIMEMultipart()
43
#outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
44
#outer['To'] = COMMASPACE.join(opts.recipients)
45
#outer['From'] = opts.sender
46
#outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
48
parser = OptionParser()
50
parser.add_option("-o", "--output", dest="output",
51
help="write output to FILE [default %default]", metavar="FILE",
53
parser.add_option("-z", "--gzip", dest="compress", action="store_true",
54
help="compress output", default=False)
55
parser.add_option("-d", "--default", dest="deftype",
56
help="default mime type [default %default]", default="text/plain")
57
parser.add_option("--delim", dest="delim",
58
help="delimiter [default %default]", default=":")
60
(options, args) = parser.parse_args()
63
parser.error("Must give file list see '--help'")
66
t = arg.split(options.delim, 1)
71
mtype = get_type(path,options.deftype)
73
maintype, subtype = mtype.split('/', 1)
74
if maintype == 'text':
76
# Note: we should handle calculating the charset
77
msg = MIMEText(fp.read(), _subtype=subtype)
81
msg = MIMEBase(maintype, subtype)
82
msg.set_payload(fp.read())
84
# Encode the payload using Base64
85
encoders.encode_base64(msg)
87
# Set the filename parameter
88
msg.add_header('Content-Disposition', 'attachment',
89
filename=os.path.basename(path))
93
if options.output is "-":
96
ofile = file(options.output,"wb")
99
gfile = gzip.GzipFile(fileobj=ofile, filename = options.output )
100
gfile.write(outer.as_string())
103
ofile.write(outer.as_string())
107
if __name__ == '__main__':