summaryrefslogtreecommitdiff
path: root/tools/make-mime.py
diff options
context:
space:
mode:
authorVlastimil Holer <vlastimil.holer@gmail.com>2013-02-19 16:30:06 +0100
committerVlastimil Holer <vlastimil.holer@gmail.com>2013-02-19 16:30:06 +0100
commit6b0652745129808dc0669354cb3e0dc53962d6ea (patch)
tree6ec307c7c245cf68d28ef05e3f1a9f7d075ff8bc /tools/make-mime.py
parente18f0f8a382729cc7c9f8df3ad0573af7eeb8f47 (diff)
parent174bc39e6b2c1cac3f73f67f25fad87cab16fa42 (diff)
downloadvyos-cloud-init-6b0652745129808dc0669354cb3e0dc53962d6ea.tar.gz
vyos-cloud-init-6b0652745129808dc0669354cb3e0dc53962d6ea.zip
Merged trunk lp:cloud-init
Diffstat (limited to 'tools/make-mime.py')
-rwxr-xr-xtools/make-mime.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tools/make-mime.py b/tools/make-mime.py
new file mode 100755
index 00000000..72b29fb9
--- /dev/null
+++ b/tools/make-mime.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python
+
+import argparse
+import sys
+
+from email.mime.multipart import MIMEMultipart
+from email.mime.text import MIMEText
+
+KNOWN_CONTENT_TYPES = [
+ 'text/x-include-once-url',
+ 'text/x-include-url',
+ 'text/cloud-config-archive',
+ 'text/upstart-job',
+ 'text/cloud-config',
+ 'text/part-handler',
+ 'text/x-shellscript',
+ 'text/cloud-boothook',
+]
+
+
+def file_content_type(text):
+ try:
+ filename, content_type = text.split(":", 1)
+ return (open(filename, 'r'), filename, content_type.strip())
+ except:
+ raise argparse.ArgumentError("Invalid value for %r" % (text))
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-a", "--attach",
+ dest="files",
+ type=file_content_type,
+ action='append',
+ default=[],
+ required=True,
+ metavar="<file>:<content-type>",
+ help="attach the given file in the specified "
+ "content type")
+ args = parser.parse_args()
+ sub_messages = []
+ for i, (fh, filename, format_type) in enumerate(args.files):
+ contents = fh.read()
+ sub_message = MIMEText(contents, format_type, sys.getdefaultencoding())
+ sub_message.add_header('Content-Disposition',
+ 'attachment; filename="%s"' % (filename))
+ content_type = sub_message.get_content_type().lower()
+ if content_type not in KNOWN_CONTENT_TYPES:
+ sys.stderr.write(("WARNING: content type %r for attachment %s "
+ "may be incorrect!\n") % (content_type, i + 1))
+ sub_messages.append(sub_message)
+ combined_message = MIMEMultipart()
+ for msg in sub_messages:
+ combined_message.attach(msg)
+ print(combined_message)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main())