diff options
author | Stephen Finucane <stephen.finucane@intel.com> | 2015-04-15 17:03:47 +0100 |
---|---|---|
committer | Stephen Finucane <stephen.finucane@intel.com> | 2015-11-26 21:15:37 +0000 |
commit | 9a1802aa49299384e68d0fd2a3dd46c0885b29eb (patch) | |
tree | 643549ac69f147a98846ec274253c6b099e9dabf | |
parent | 545c062e794715c30e1e77245a70303ac28a9da3 (diff) | |
download | patchwork-9a1802aa49299384e68d0fd2a3dd46c0885b29eb.tar patchwork-9a1802aa49299384e68d0fd2a3dd46c0885b29eb.tar.gz |
bin/parsemail: Use logging instead of prints
This is more useful for helpful dialog messages. Without this it is
necessary to add prints to figure out why patches aren't parsed.
Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
-rwxr-xr-x | patchwork/bin/parsemail.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/patchwork/bin/parsemail.py b/patchwork/bin/parsemail.py index 0562a45..513daef 100755 --- a/patchwork/bin/parsemail.py +++ b/patchwork/bin/parsemail.py @@ -39,6 +39,16 @@ from patchwork.models import ( Patch, Project, Person, Comment, State, get_default_initial_patch_state) from patchwork.parser import parse_patch +LOGGER = logging.getLogger(__name__) + +VERBOSITY_LEVELS = { + 'debug': logging.DEBUG, + 'info': logging.INFO, + 'warning': logging.WARNING, + 'error': logging.ERROR, + 'critical': logging.CRITICAL +} + list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list'] @@ -94,6 +104,7 @@ def find_project_by_header(mail): return project + def find_author(mail): from_header = clean_header(mail.get('From')) @@ -385,16 +396,20 @@ def parse_mail(mail, list_id=None): """ # some basic sanity checks if 'From' not in mail: + LOGGER.debug("Ignoring patch due to missing 'From'") return 0 if 'Subject' not in mail: + LOGGER.debug("Ignoring patch due to missing 'Subject'") return 0 if 'Message-Id' not in mail: + LOGGER.debug("Ignoring patch due to missing 'Message-Id'") return 0 hint = mail.get('X-Patchwork-Hint', '').lower() if hint == 'ignore': + LOGGER.debug("Ignoring patch due to 'ignore' hint") return 0 if list_id: @@ -403,7 +418,7 @@ def parse_mail(mail, list_id=None): project = find_project_by_header(mail) if project is None: - print("no project found") + LOGGER.error('Failed to find a project for patch') return 0 msgid = mail.get('Message-Id').strip() @@ -472,6 +487,11 @@ def main(args): logger = setup_error_handler() parser = argparse.ArgumentParser() + def list_logging_levels(): + """Give a summary of all available logging levels.""" + return sorted(VERBOSITY_LEVELS.keys(), + key=lambda x: VERBOSITY_LEVELS[x]) + parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='input mbox file (a filename ' 'or stdin)') @@ -479,9 +499,13 @@ def main(args): group = parser.add_argument_group('Mail parsing configuration') group.add_argument('--list-id', help='mailing list ID. If not supplied ' 'this will be extracted from the mail headers.') + group.add_argument('--verbosity', choices=list_logging_levels(), + help='debug level', default=logging.INFO) args = vars(parser.parse_args()) + logging.basicConfig(level=args['verbosity']) + mail = message_from_file(args['infile']) try: return parse_mail(mail, args['list_id']) |