~ubuntu-branches/ubuntu/saucy/cloud-utils/saucy

« back to all changes in this revision

Viewing changes to .pc/sync-to-trunk.patch/bin/write-mime-multipart

  • Committer: Scott Moser
  • Date: 2013-02-04 16:59:40 UTC
  • Revision ID: smoser@ubuntu.com-20130204165940-05chdao305tlwza8
* sync to trunk at revno 216
  * support for GPT partitions in growpart via sgdisk (LP: #1087526)
  * depend on wget and ca-certificates for ubuntu-cloudimg-query 
    (LP: #1062671)
  * fix sfdisk parsing (LP: #1007415)

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
    '#cloud-config-archive': 'text/cloud-config-archive',
 
26
    '#upstart-job': 'text/upstart-job',
 
27
    '#part-handler': 'text/part-handler',
 
28
    '#cloud-boothook': 'text/cloud-boothook'
 
29
}
 
30
 
 
31
 
 
32
def get_type(fname, deftype):
 
33
    f = file(fname, "rb")
 
34
    line = f.readline()
 
35
    f.close()
 
36
    rtype = deftype
 
37
 
 
38
    # slist is sorted longest first
 
39
    slist = sorted(starts_with_mappings.keys(), key=lambda e: 0 - len(e))
 
40
    for sstr in slist:
 
41
        if line.startswith(sstr):
 
42
            rtype = starts_with_mappings[sstr]
 
43
            break
 
44
    return(rtype)
 
45
 
 
46
 
 
47
def main():
 
48
    outer = MIMEMultipart()
 
49
    #outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
 
50
    #outer['To'] = COMMASPACE.join(opts.recipients)
 
51
    #outer['From'] = opts.sender
 
52
    #outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
 
53
 
 
54
    parser = OptionParser()
 
55
 
 
56
    parser.add_option("-o", "--output", dest="output",
 
57
        help="write output to FILE [default %default]", metavar="FILE",
 
58
        default="-")
 
59
    parser.add_option("-z", "--gzip", dest="compress", action="store_true",
 
60
        help="compress output", default=False)
 
61
    parser.add_option("-d", "--default", dest="deftype",
 
62
        help="default mime type [default %default]", default="text/plain")
 
63
    parser.add_option("--delim", dest="delim",
 
64
        help="delimiter [default %default]", default=":")
 
65
 
 
66
    (options, args) = parser.parse_args()
 
67
 
 
68
    if (len(args)) < 1:
 
69
        parser.error("Must give file list see '--help'")
 
70
 
 
71
    for arg in args:
 
72
        t = arg.split(options.delim, 1)
 
73
        path = t[0]
 
74
        if len(t) > 1:
 
75
            mtype = t[1]
 
76
        else:
 
77
            mtype = get_type(path, options.deftype)
 
78
 
 
79
        maintype, subtype = mtype.split('/', 1)
 
80
        if maintype == 'text':
 
81
            fp = open(path)
 
82
            # Note: we should handle calculating the charset
 
83
            msg = MIMEText(fp.read(), _subtype=subtype)
 
84
            fp.close()
 
85
        else:
 
86
            fp = open(path, 'rb')
 
87
            msg = MIMEBase(maintype, subtype)
 
88
            msg.set_payload(fp.read())
 
89
            fp.close()
 
90
            # Encode the payload using Base64
 
91
            encoders.encode_base64(msg)
 
92
 
 
93
        # Set the filename parameter
 
94
        msg.add_header('Content-Disposition', 'attachment',
 
95
            filename=os.path.basename(path))
 
96
 
 
97
        outer.attach(msg)
 
98
 
 
99
    if options.output is "-":
 
100
        ofile = sys.stdout
 
101
    else:
 
102
        ofile = file(options.output, "wb")
 
103
 
 
104
    if options.compress:
 
105
        gfile = gzip.GzipFile(fileobj=ofile, filename=options.output)
 
106
        gfile.write(outer.as_string())
 
107
        gfile.close()
 
108
    else:
 
109
        ofile.write(outer.as_string())
 
110
 
 
111
    ofile.close()
 
112
 
 
113
if __name__ == '__main__':
 
114
    main()
 
115
 
 
116
# vi: ts=4 expandtab