[PATCH v2 09/10] parsemail: Convert to a management command

Daniel Axtens dja at axtens.net
Sun Aug 28 15:21:02 AEST 2016


> +from email import message_from_file
> +import logging

> +from optparse import make_option

This is throwing a warning:

RemovedInDjango110Warning: OptionParser usage for Django management commands is deprecated, use ArgumentParser instead

Likewise with the other management command.

Could you flick over to argparse please?

Regards,
Daniel

> +import sys
> +
> +from django.core.management import base
> +
> +from patchwork.parser import parse_mail
> +
> +logger = logging.getLogger(__name__)
> +
> +
> +class Command(base.BaseCommand):
> +    help = 'Parse an mbox file and store any patch/comment found'
> +    args = '<infile>'  # Django < 1.8 compatibility
> +    option_list = base.BaseCommand.option_list + (
> +        make_option(
> +            '--list-id',
> +            help='mailing list ID. If not supplied, this will be extracted '
> +            'from the mail headers.'
> +        ),
> +    )
> +
> +    def handle(self, *args, **options):
> +        # Attempt to parse the path if provided, and fallback to stdin if not
> +        if args:
> +            logger.info('Parsing mail loaded by filename')
> +            with open(args[0]) as file_:
> +                mail = message_from_file(file_)
> +        else:
> +            logger.info('Parsing mail loaded from stdin')
> +            mail = message_from_file(sys.stdin)
> +
> +        try:
> +            result = parse_mail(mail, options['list_id'])
> +            if result:
> +                sys.exit(0)
> +            logger.warning('Failed to parse mail.')
> +            sys.exit(1)
> +        except Exception as exc:
> +            logger.exception('Error when parsing incoming email',
> +                             extra={'mail': mail.as_string()})
> diff --git a/patchwork/parser.py b/patchwork/parser.py
> index 1805df8..51de997 100644
> --- a/patchwork/parser.py
> +++ b/patchwork/parser.py
> @@ -42,7 +42,7 @@ _hunk_re = re.compile(r'^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
>  _filename_re = re.compile(r'^(---|\+\+\+) (\S+)')
>  list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list']
>  
> -LOGGER = logging.getLogger(__name__)
> +logger = logging.getLogger(__name__)
>  
>  
>  def normalise_space(str):
> @@ -599,7 +599,7 @@ def parse_mail(mail, list_id=None):
>  
>      hint = mail.get('X-Patchwork-Hint', '').lower()
>      if hint == 'ignore':
> -        LOGGER.debug("Ignoring email due to 'ignore' hint")
> +        logger.debug("Ignoring email due to 'ignore' hint")
>          return
>  
>      if list_id:
> @@ -608,7 +608,7 @@ def parse_mail(mail, list_id=None):
>          project = find_project_by_header(mail)
>  
>      if project is None:
> -        LOGGER.error('Failed to find a project for email')
> +        logger.error('Failed to find a project for email')
>          return
>  
>      # parse content
> @@ -651,7 +651,7 @@ def parse_mail(mail, list_id=None):
>              delegate=delegate,
>              state=find_state(mail))
>          patch.save()
> -        LOGGER.debug('Patch saved')
> +        logger.debug('Patch saved')
>  
>          return patch
>      elif x == 0:  # (potential) cover letters
> @@ -683,7 +683,7 @@ def parse_mail(mail, list_id=None):
>                  submitter=author,
>                  content=message)
>              cover_letter.save()
> -            LOGGER.debug('Cover letter saved')
> +            logger.debug('Cover letter saved')
>  
>              return cover_letter
>  
> @@ -704,7 +704,7 @@ def parse_mail(mail, list_id=None):
>          submitter=author,
>          content=message)
>      comment.save()
> -    LOGGER.debug('Comment saved')
> +    logger.debug('Comment saved')
>  
>      return comment
>  
> diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
> index b78ed4b..a32710f 100644
> --- a/patchwork/settings/base.py
> +++ b/patchwork/settings/base.py
> @@ -125,6 +125,8 @@ STATICFILES_DIRS = [
>  # Third-party application settings
>  #
>  
> +# rest_framework
> +
>  try:
>      # django rest framework isn't a standard package in most distros, so
>      # don't make it compulsory
> @@ -136,17 +138,65 @@ try:
>  except ImportError:
>      pass
>  
> -#
> -# Third-party application settings
> -#
> -
> -# rest_framework
>  
>  REST_FRAMEWORK = {
>      'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning'
>  }
>  
>  #
> +# Logging settings
> +#
> +
> +LOGGING = {
> +    'version': 1,
> +    'disable_existing_loggers': False,
> +    'formatters': {
> +        'email': {
> +            'format': '== Mail\n\n%(mail)s\n\n== Traceback\n',
> +        },
> +    },
> +    'filters': {
> +        'require_debug_false': {
> +            '()': 'django.utils.log.RequireDebugFalse',
> +        },
> +        'require_debug_true': {
> +            '()': 'django.utils.log.RequireDebugTrue',
> +        },
> +    },
> +    'handlers': {
> +        'console': {
> +            'level': 'DEBUG',
> +            'filters': ['require_debug_true'],
> +            'class': 'logging.StreamHandler',
> +        },
> +        'mail_admins': {
> +            'level': 'ERROR',
> +            'filters': ['require_debug_false'],
> +            'class': 'django.utils.log.AdminEmailHandler',
> +            'formatter': 'email',
> +            'include_html': True,
> +        },
> +    },
> +    'loggers': {
> +        'django': {
> +            'handlers': ['console'],
> +            'level': 'INFO',
> +            'propagate': True,
> +        },
> +        'patchwork.parser': {
> +            'handlers': ['console'],
> +            'level': 'DEBUG',
> +            'propagate': False,
> +        },
> +        'patchwork.management.commands': {
> +            'handlers': ['console', 'mail_admins'],
> +            'level': 'INFO',
> +            'propagate': True,
> +        },
> +    },
> +}
> +
> +#
>  # Patchwork settings
>  #
>  
> -- 
> 2.7.4
>
> _______________________________________________
> Patchwork mailing list
> Patchwork at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 859 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/patchwork/attachments/20160828/9a793af5/attachment.sig>


More information about the Patchwork mailing list