| 1 |
#!/usr/bin/python3 |
| 2 |
# largely taken from python examples |
| 3 |
# http://docs.python.org/library/email-examples.html |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
|
| 8 |
from email import encoders |
| 9 |
from email.mime.base import MIMEBase |
| 10 |
from email.mime.multipart import MIMEMultipart |
| 11 |
from email.mime.text import MIMEText |
| 12 |
from optparse import OptionParser # pylint: disable=W0402 |
| 13 |
import gzip |
| 14 |
|
| 15 |
COMMASPACE = ", " |
| 16 |
|
| 17 |
starts_with_mappings = { |
| 18 |
"#include": "text/x-include-url", |
| 19 |
"#include-once": "text/x-include-once-url", |
| 20 |
"#!": "text/x-shellscript", |
| 21 |
"#cloud-config": "text/cloud-config", |
| 22 |
"#cloud-config-archive": "text/cloud-config-archive", |
| 23 |
"#upstart-job": "text/upstart-job", |
| 24 |
"#part-handler": "text/part-handler", |
| 25 |
"#cloud-boothook": "text/cloud-boothook", |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
def try_decode(data): |
| 30 |
try: |
| 31 |
return (True, data.decode()) |
| 32 |
except UnicodeDecodeError: |
| 33 |
return (False, data) |
| 34 |
|
| 35 |
|
| 36 |
def get_type(fname, deftype): |
| 37 |
rtype = deftype |
| 38 |
|
| 39 |
with open(fname, "rb") as f: |
| 40 |
(can_be_decoded, line) = try_decode(f.readline()) |
| 41 |
|
| 42 |
if can_be_decoded: |
| 43 |
# slist is sorted longest first |
| 44 |
slist = sorted(list(starts_with_mappings.keys()), key=lambda e: 0 - len(e)) |
| 45 |
for sstr in slist: |
| 46 |
if line.startswith(sstr): |
| 47 |
rtype = starts_with_mappings[sstr] |
| 48 |
break |
| 49 |
else: |
| 50 |
rtype = "application/octet-stream" |
| 51 |
|
| 52 |
return rtype |
| 53 |
|
| 54 |
|
| 55 |
def main(): |
| 56 |
outer = MIMEMultipart() |
| 57 |
parser = OptionParser() |
| 58 |
|
| 59 |
parser.add_option( |
| 60 |
"-o", |
| 61 |
"--output", |
| 62 |
dest="output", |
| 63 |
help="write output to FILE [default %default]", |
| 64 |
metavar="FILE", |
| 65 |
default="-", |
| 66 |
) |
| 67 |
parser.add_option( |
| 68 |
"-z", |
| 69 |
"--gzip", |
| 70 |
dest="compress", |
| 71 |
action="store_true", |
| 72 |
help="compress output", |
| 73 |
default=False, |
| 74 |
) |
| 75 |
parser.add_option( |
| 76 |
"-d", |
| 77 |
"--default", |
| 78 |
dest="deftype", |
| 79 |
help="default mime type [default %default]", |
| 80 |
default="text/plain", |
| 81 |
) |
| 82 |
parser.add_option( |
| 83 |
"--delim", dest="delim", help="delimiter [default %default]", default=":" |
| 84 |
) |
| 85 |
|
| 86 |
(options, args) = parser.parse_args() |
| 87 |
|
| 88 |
if (len(args)) < 1: |
| 89 |
parser.error("Must give file list see '--help'") |
| 90 |
|
| 91 |
for arg in args: |
| 92 |
t = arg.split(options.delim, 1) |
| 93 |
path = t[0] |
| 94 |
if len(t) > 1: |
| 95 |
mtype = t[1] |
| 96 |
else: |
| 97 |
mtype = get_type(path, options.deftype) |
| 98 |
|
| 99 |
maintype, subtype = mtype.split("/", 1) |
| 100 |
if maintype == "text": |
| 101 |
with open(path, encoding="utf-8") as fp: |
| 102 |
# Note: we should handle calculating the charset |
| 103 |
msg = MIMEText(fp.read(), _subtype=subtype) |
| 104 |
else: |
| 105 |
with open(path, "rb") as fp: |
| 106 |
msg = MIMEBase(maintype, subtype) |
| 107 |
msg.set_payload(fp.read()) |
| 108 |
# Encode the payload using Base64 |
| 109 |
encoders.encode_base64(msg) |
| 110 |
|
| 111 |
# Set the filename parameter |
| 112 |
msg.add_header( |
| 113 |
"Content-Disposition", "attachment", filename=os.path.basename(path) |
| 114 |
) |
| 115 |
|
| 116 |
outer.attach(msg) |
| 117 |
|
| 118 |
if options.output == "-": |
| 119 |
if hasattr(sys.stdout, "buffer"): |
| 120 |
# We want to write bytes not strings |
| 121 |
ofile = sys.stdout.buffer |
| 122 |
else: |
| 123 |
ofile = sys.stdout |
| 124 |
_write_output(ofile, outer, options) |
| 125 |
else: |
| 126 |
with open(options.output, "wb") as ofile: |
| 127 |
_write_output(ofile, outer, options) |
| 128 |
|
| 129 |
|
| 130 |
def _write_output(ofile, outer, options): |
| 131 |
"""Write the MIME message to the output file.""" |
| 132 |
if options.compress: |
| 133 |
with gzip.GzipFile(fileobj=ofile, filename=options.output) as gfile: |
| 134 |
gfile.write(outer.as_string().encode()) |
| 135 |
else: |
| 136 |
ofile.write(outer.as_string().encode()) |
| 137 |
|
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
main() |
| 141 |
|
| 142 |
# vi: ts=4 expandtab |