datatracker/ietf/ipr/management/commands/process_email.py
Robert Sparks f8113cb862
fix: close open things (#5593)
* fix: close open things

* fix: clean up test created files

* fix: remove one close too many
2023-05-10 11:19:34 -05:00

52 lines
1.6 KiB
Python

# Copyright The IETF Trust 2014-2020, All Rights Reserved
# -*- coding: utf-8 -*-
import io
import sys
from textwrap import dedent
from django.core.management import CommandError
from ietf.utils.management.base import EmailOnFailureCommand
from ietf.ipr.mail import process_response_email
import debug # pyflakes:ignore
class Command(EmailOnFailureCommand):
help = ("Process incoming email responses to ipr mail")
msg_bytes = None
def add_arguments(self, parser):
super().add_arguments(parser)
parser.add_argument('--email-file', dest='email', help='File containing email (default: stdin)')
def handle(self, *args, **options):
email = options.get('email', None)
if email:
binary_input = io.open(email, 'rb')
self.msg_bytes = binary_input.read()
binary_input.close()
else:
self.msg_bytes = sys.stdin.buffer.read()
try:
process_response_email(self.msg_bytes)
except ValueError as e:
raise CommandError(e)
failure_subject = 'Error during ipr email processing'
failure_message = dedent("""\
An error occurred in the ipr process_email management command.
{error_summary}
""")
def make_failure_message(self, error, **extra):
msg = super().make_failure_message(error, **extra)
if self.msg_bytes is not None:
msg.add_attachment(
self.msg_bytes,
'application', 'octet-stream', # mime type
filename='original-message',
)
return msg