diff options
author | Scott Moser <smoser@nelson> | 2010-01-06 16:17:57 -0500 |
---|---|---|
committer | Scott Moser <smoser@nelson> | 2010-01-06 16:17:57 -0500 |
commit | a280ac3087deed1a7e9ca5f561094291d0b62ffe (patch) | |
tree | e7a0c170b073b42db7f55eeab368b05772127f0b | |
parent | baf494858f0a9e8514d66cf3371dc6135b411c26 (diff) | |
download | vyos-cloud-init-a280ac3087deed1a7e9ca5f561094291d0b62ffe.tar.gz vyos-cloud-init-a280ac3087deed1a7e9ca5f561094291d0b62ffe.zip |
add tools/ directory and 'write-mime-multipart'
write-mime-multipart text/x-shellscript path/filename.sh \
text/x-cloud-config my.yaml \
> my.userdata
-rwxr-xr-x | tools/write-mime-multipart | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tools/write-mime-multipart b/tools/write-mime-multipart new file mode 100755 index 00000000..99cfe299 --- /dev/null +++ b/tools/write-mime-multipart @@ -0,0 +1,67 @@ +#!/usr/bin/python + +"""Send the contents of a directory as a MIME message.""" + +import os +import sys +import smtplib +# For guessing MIME type based on file name extension +import mimetypes + +from optparse import OptionParser + +from email import encoders +from email.message import Message +from email.mime.base import MIMEBase +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +COMMASPACE = ', ' + + +def main(): + outer = MIMEMultipart() + #outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory) + #outer['To'] = COMMASPACE.join(opts.recipients) + #outer['From'] = opts.sender + #outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' + + if len(sys.argv) % 2 != 1 or len(sys.argv) == 1: + print "Usage: prog content-type file1 [ content-type2 file2 [ ... ] ]" + sys.exit(1) + + pos = 1 + while pos < len(sys.argv): + ctype = sys.argv[pos] + path = sys.argv[pos+1] + pos=pos+2 + + maintype, subtype = ctype.split('/', 1) + if maintype == 'text': + fp = open(path) + # Note: we should handle calculating the charset + msg = MIMEText(fp.read(), _subtype=subtype) + fp.close() + else: + fp = open(path, 'rb') + msg = MIMEBase(maintype, subtype) + msg.set_payload(fp.read()) + fp.close() + # Encode the payload using Base64 + encoders.encode_base64(msg) + + # Set the filename parameter + msg.add_header('Content-Disposition', 'attachment', + filename=os.path.basename(path)) + + if ctype is None: + # No guess could be made, or the file is encoded (compressed), so + # use a generic bag-of-bits type. + ctype = 'application/octet-stream' + outer.attach(msg) + # Now send or store the message + #composed = outer.as_string() + sys.stdout.write(outer.as_string()) + +if __name__ == '__main__': + main() |