From 6bd56719ee966abb767d51020d75d70ffa16b406 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 4 Jun 2017 17:06:48 +0300 Subject: Add logging of what happens during importing mail --- distixlib/plugins/import_mail_plugin.py | 34 ++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/distixlib/plugins/import_mail_plugin.py b/distixlib/plugins/import_mail_plugin.py index 8375d73..8b850c6 100644 --- a/distixlib/plugins/import_mail_plugin.py +++ b/distixlib/plugins/import_mail_plugin.py @@ -19,6 +19,7 @@ import contextlib import email import imaplib +import logging import mailbox import os import re @@ -79,6 +80,8 @@ class ImportMailPlugin(cliapp.Plugin): def import_mail(self, args): repo_dirname, mail_filename, keyvalue = self._parse_command_line(args) + logging.info('Import mail from a file: %r', mail_filename) + key, value = self._parse_keyvalue(keyvalue) msg = self._read_mail_message(mail_filename) @@ -89,17 +92,22 @@ class ImportMailPlugin(cliapp.Plugin): filenames = self._import_msg_to_ticket_store(context, msg, key, value) if filenames: + logging.info('Commit new/changed files: %s', filenames) context.repo.commit_changes(filenames, self.commit_msg) def import_mbox(self, args): + logging.info('Importing mbox') self._import_folder(args, mailbox.mbox) def import_maildir(self, args): def maildir_factory(filename): return mailbox.Maildir(filename, factory=None) + logging.info('Importing maildir') self._import_folder(args, maildir_factory) def import_imap(self, args): + logging.info('Importing IMAP') + if args: key, value = self._parse_keyvalue(args[0]) else: @@ -116,6 +124,8 @@ class ImportMailPlugin(cliapp.Plugin): repo_rules = _RepoRules() repo_rules.add_rules(cp) + logging.info( + 'Connecting to IMAP servers %s as %s', imap_server, username) imap = imaplib.IMAP4_SSL(imap_server) imap.login(username, password) imap.select('INBOX') @@ -123,14 +133,17 @@ class ImportMailPlugin(cliapp.Plugin): _, data = imap.search(None, 'ALL') for msgnum in data[0].split(): print 'importing msg', msgnum + logging.info('Importing message %s', msgnum) _, data = imap.fetch(msgnum, '(RFC822)') _, text = data[0] repo_url = repo_rules.find_url(text) if repo_url: print ' to repo', repo_url + logging.info('Importing to repository %s', repo_url) msg = email.message_from_string(text) self._import_msg_into_repo( collection, repo_url, msg, key, value) + logging.info('Removing message from IMAP INBOX') imap.store(msgnum, '+FLAGS', '(\\Deleted)') imap.expunge() @@ -151,6 +164,7 @@ class ImportMailPlugin(cliapp.Plugin): filenames = self._import_msg_to_ticket_store(context, msg, key, value) if filenames: + logging.info('Committing new/changed files: %s', filenames) context.repo.commit_changes(filenames, self.commit_msg) collection.push(repo_url) @@ -158,6 +172,8 @@ class ImportMailPlugin(cliapp.Plugin): def _import_folder(self, args, folder_factory): repo_dirname, folder_filename, keyvalue = self._parse_command_line( args) + logging.info('Import mail folders %s', folder_filename) + key, value = self._parse_keyvalue(keyvalue) folder = folder_factory(folder_filename) @@ -177,8 +193,10 @@ class ImportMailPlugin(cliapp.Plugin): progress.next_msg() filenames += self._import_msg_to_ticket_store( context, msg, key, value) + filenames += context.store.save_changes() if filenames: + logging.info('Committing new/changed files: %s', filenames) context.repo.commit_changes(filenames, self.commit_msg) def _parse_command_line(self, args): @@ -194,7 +212,10 @@ class ImportMailPlugin(cliapp.Plugin): def _parse_keyvalue(self, keyvalue): if keyvalue is None: return None, None - return keyvalue.split('=', 1) + key, value = keyvalue.split('=', 1) + logging.info( + 'Set %s=%s on all tickets created/changed by import', key, value) + return key, value def _read_mail_message(self, mail_filename): with open(mail_filename) as f: @@ -209,15 +230,22 @@ class ImportMailPlugin(cliapp.Plugin): if referenced_ids: for ticket_id in referenced_ids: - if not self._contains_message(context.store, ticket_id, msg): + if self._contains_message(context.store, ticket_id, msg): + logging.info( + 'Ticket %s already contains the mail', ticket_id) + else: + logging.info('Importing mail into ticket %s', ticket_id) ticket = context.store.get_ticket(ticket_id) ticket.add_message(msg) self._set_key_value(ticket, key, value) context.cache.add_msg_ids_for_ticket_id( ticket.get_ticket_id(), msg_ids) else: - if not self._is_already_imported(context, msg): + if self._is_already_imported(context, msg): + logging.info('Mail haa already been imported') + else: new_ticket = self._create_ticket_from_msg(context.repo, msg) + logging.info('Created new ticket %s', new_ticket) self._set_key_value(new_ticket, key, value) context.cache.add_msg_ids_for_ticket_id( new_ticket.get_ticket_id(), msg_ids) -- cgit v1.2.1