~ubuntu-on-ec2/ubuntu-on-ec2/cloud-utils

« back to all changes in this revision

Viewing changes to write-mime-multipart

  • Committer: Scott Moser
  • Date: 2011-01-11 14:40:17 UTC
  • Revision ID: smoser@ubuntu.com-20110111144017-2x02h7z32836la8f
move write-mime-multipart from cloud-init to cloud-init-multipart
in cloud-utils

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# largely taken from python examples
 
3
# http://docs.python.org/library/email-examples.html
 
4
 
 
5
import os
 
6
import sys
 
7
import smtplib
 
8
# For guessing MIME type based on file name extension
 
9
import mimetypes
 
10
 
 
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
 
17
import gzip
 
18
 
 
19
COMMASPACE = ', '
 
20
 
 
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'
 
28
}
 
29
 
 
30
def get_type(fname,deftype):
 
31
    f = file(fname,"rb")
 
32
    line = f.readline()
 
33
    f.close()
 
34
    rtype = deftype
 
35
    for str,mtype in starts_with_mappings.items():
 
36
        if line.startswith(str):
 
37
            rtype = mtype
 
38
            break
 
39
    return(rtype)
 
40
 
 
41
def main():
 
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'
 
47
 
 
48
    parser = OptionParser()
 
49
    
 
50
    parser.add_option("-o", "--output", dest="output",
 
51
        help="write output to FILE [default %default]", metavar="FILE", 
 
52
        default="-")
 
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=":")
 
59
 
 
60
    (options, args) = parser.parse_args()
 
61
 
 
62
    if (len(args)) < 1:
 
63
        parser.error("Must give file list see '--help'")
 
64
 
 
65
    for arg in args:
 
66
        t = arg.split(options.delim, 1)
 
67
        path=t[0]
 
68
        if len(t) > 1:
 
69
            mtype = t[1]
 
70
        else:
 
71
            mtype = get_type(path,options.deftype)
 
72
 
 
73
        maintype, subtype = mtype.split('/', 1)
 
74
        if maintype == 'text':
 
75
            fp = open(path)
 
76
            # Note: we should handle calculating the charset
 
77
            msg = MIMEText(fp.read(), _subtype=subtype)
 
78
            fp.close()
 
79
        else:
 
80
            fp = open(path, 'rb')
 
81
            msg = MIMEBase(maintype, subtype)
 
82
            msg.set_payload(fp.read())
 
83
            fp.close()
 
84
            # Encode the payload using Base64
 
85
            encoders.encode_base64(msg)
 
86
 
 
87
        # Set the filename parameter
 
88
        msg.add_header('Content-Disposition', 'attachment',
 
89
            filename=os.path.basename(path))
 
90
 
 
91
        outer.attach(msg)
 
92
 
 
93
    if options.output is "-":
 
94
        ofile = sys.stdout
 
95
    else:
 
96
        ofile = file(options.output,"wb")
 
97
        
 
98
    if options.compress:
 
99
        gfile = gzip.GzipFile(fileobj=ofile, filename = options.output )
 
100
        gfile.write(outer.as_string())
 
101
        gfile.close()
 
102
    else:
 
103
        ofile.write(outer.as_string())
 
104
 
 
105
    ofile.close()
 
106
 
 
107
if __name__ == '__main__':
 
108
    main()