cpython/Doc/includes/email-unpack.py

69 lines
1.8 KiB
Python
Raw Normal View History

2007-08-15 11:28:01 -03:00
#!/usr/bin/env python
"""Unpack a MIME message into a directory of files."""
import os
import sys
import email
import errno
import mimetypes
from optparse import OptionParser
def main():
parser = OptionParser(usage="""\
Unpack a MIME message into a directory of files.
Usage: %prog [options] msgfile
""")
parser.add_option('-d', '--directory',
type='string', action='store',
help="""Unpack the MIME message into the named
directory, which will be created if it doesn't already
exist.""")
opts, args = parser.parse_args()
if not opts.directory:
parser.print_help()
sys.exit(1)
try:
msgfile = args[0]
except IndexError:
parser.print_help()
sys.exit(1)
try:
os.mkdir(opts.directory)
except OSError, e:
# Ignore directory exists error
Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r72558 | benjamin.peterson | 2009-05-11 01:52:09 +0200 (Mo, 11 Mai 2009) | 1 line sys.setdefaultencoding() strikes me as a bad example ........ r72745 | benjamin.peterson | 2009-05-17 16:16:29 +0200 (So, 17 Mai 2009) | 1 line ignore .rst files in sphinx its self ........ r72750 | benjamin.peterson | 2009-05-17 18:59:27 +0200 (So, 17 Mai 2009) | 1 line chop off slash ........ r72876 | benjamin.peterson | 2009-05-23 22:59:09 +0200 (Sa, 23 Mai 2009) | 1 line remove mention of old ctypes version ........ r73042 | benjamin.peterson | 2009-05-30 05:10:52 +0200 (Sa, 30 Mai 2009) | 1 line no fdatasync on macos ........ r73045 | georg.brandl | 2009-05-30 09:26:04 +0200 (Sa, 30 Mai 2009) | 1 line #6146: fix markup bug. ........ r73046 | georg.brandl | 2009-05-30 09:31:25 +0200 (Sa, 30 Mai 2009) | 1 line Use preferred form of raising exceptions. ........ r73047 | georg.brandl | 2009-05-30 12:33:23 +0200 (Sa, 30 Mai 2009) | 1 line Fix some more small markup problems. ........ r73048 | georg.brandl | 2009-05-30 12:34:25 +0200 (Sa, 30 Mai 2009) | 1 line Fix markup problem. ........ r73069 | benjamin.peterson | 2009-05-31 02:42:42 +0200 (So, 31 Mai 2009) | 1 line fix signature ........ r73089 | andrew.kuchling | 2009-06-01 02:14:19 +0200 (Mo, 01 Jun 2009) | 1 line The class for regexes isn't called RegexObject any more; correct the text ........ r73163 | georg.brandl | 2009-06-03 09:25:35 +0200 (Mi, 03 Jun 2009) | 1 line Use the preferred form of raise statements in the docs. ........ r73186 | georg.brandl | 2009-06-03 23:21:09 +0200 (Mi, 03 Jun 2009) | 1 line #6174: fix indentation in code example. ........ r73213 | georg.brandl | 2009-06-04 12:15:57 +0200 (Do, 04 Jun 2009) | 1 line #5967: note that the C slicing APIs do not support negative indices. ........ r73215 | georg.brandl | 2009-06-04 12:22:31 +0200 (Do, 04 Jun 2009) | 1 line #6176: fix man page section for flock(2). ........ r73217 | georg.brandl | 2009-06-04 12:27:21 +0200 (Do, 04 Jun 2009) | 1 line #6175: document that inet_aton supports alternate input formats with less than three dots. ........ r73257 | georg.brandl | 2009-06-06 19:50:05 +0200 (Sa, 06 Jun 2009) | 1 line #6211: elaborate a bit on ways to call the function. ........ r73258 | georg.brandl | 2009-06-06 19:51:31 +0200 (Sa, 06 Jun 2009) | 1 line #6204: use a real reference instead of "see later". ........ r73260 | georg.brandl | 2009-06-06 20:21:58 +0200 (Sa, 06 Jun 2009) | 1 line #6224: s/JPython/Jython/, and remove one link to a module nine years old. ........
2009-10-27 11:19:50 -03:00
if e.errno != errno.EEXIST:
2007-08-15 11:28:01 -03:00
raise
fp = open(msgfile)
msg = email.message_from_file(fp)
fp.close()
counter = 1
for part in msg.walk():
# multipart/* are just containers
if part.get_content_maintype() == 'multipart':
continue
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
filename = part.get_filename()
if not filename:
2007-10-13 10:20:03 -03:00
ext = mimetypes.guess_extension(part.get_content_type())
2007-08-15 11:28:01 -03:00
if not ext:
# Use a generic bag-of-bits extension
ext = '.bin'
filename = 'part-%03d%s' % (counter, ext)
counter += 1
fp = open(os.path.join(opts.directory, filename), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
if __name__ == '__main__':
main()