+ print 'done'
+
+def __import_file(filename, options, patch = None):
+ """Import a patch from a file or standard input
+ """
+ if filename:
+ f = file(filename)
+ else:
+ f = sys.stdin
+
+ if options.mail:
+ try:
+ msg = email.message_from_file(f)
+ except Exception, ex:
+ raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
+ message, author_name, author_email, author_date, diff = \
+ __parse_mail(msg)
+ else:
+ message, author_name, author_email, author_date, diff = \
+ __parse_patch(f)
+
+ if filename:
+ f.close()
+
+ if patch:
+ pname = patch
+ else:
+ pname = filename
+
+ __create_patch(pname, message, author_name, author_email,
+ author_date, diff, options)
+
+def __import_series(filename, options):
+ """Import a series of patches
+ """
+ applied = crt_series.get_applied()
+
+ if filename:
+ f = file(filename)
+ patchdir = os.path.dirname(filename)
+ else:
+ f = sys.stdin
+ patchdir = ''
+
+ for line in f:
+ patch = re.sub('#.*$', '', line).strip()
+ if not patch:
+ continue
+ patchfile = os.path.join(patchdir, patch)
+ patch = __replace_slashes_with_dashes(patch);
+
+ __import_file(patchfile, options, patch)
+
+ if filename:
+ f.close()
+
+def __import_mbox(filename, options):
+ """Import a series from an mbox file
+ """
+ if filename:
+ f = file(filename, 'rb')
+ else:
+ f = StringIO(sys.stdin.read())
+
+ try:
+ mbox = UnixMailbox(f, email.message_from_file)
+ except Exception, ex:
+ raise CmdException, 'error parsing the mbox file: %s' % str(ex)
+
+ for msg in mbox:
+ message, author_name, author_email, author_date, diff = \
+ __parse_mail(msg)
+ __create_patch(None, message, author_name, author_email,
+ author_date, diff, options)
+
+ f.close()
+
+def __import_url(url, options):
+ """Import a patch from a URL
+ """
+ import urllib
+ import tempfile
+
+ if not url:
+ parser.error('URL argument required')
+
+ patch = os.path.basename(urllib.unquote(url))
+ filename = os.path.join(tempfile.gettempdir(), patch)
+ urllib.urlretrieve(url, filename)
+ __import_file(filename, options)
+
+def func(parser, options, args):
+ """Import a GNU diff file as a new patch
+ """
+ if len(args) > 1:
+ parser.error('incorrect number of arguments')
+
+ check_local_changes()
+ check_conflicts()
+ check_head_top_equal()
+
+ if len(args) == 1:
+ filename = args[0]
+ else:
+ filename = None
+
+ if options.series:
+ __import_series(filename, options)
+ elif options.mbox:
+ __import_mbox(filename, options)
+ elif options.url:
+ __import_url(filename, options)
+ else:
+ __import_file(filename, options)
+