File and console logging added for exception handling and information
[imap-restore-mail] / file_iterator.py
index 5c3512d..b597928 100644 (file)
@@ -6,6 +6,7 @@ Copyright (c) 2012 Intra2net AG
 
 import sys, os
 import re
+import logging
 
 MAIL_FILENAME = re.compile("^[0-9]+\.$")
 MBOXFILE_LINE = re.compile("^(.*?)\t(?:\d )?default[\t ](.*)$")
@@ -35,8 +36,9 @@ class FileIterator:
         try:
             with open(filename, "r") as msgfile:
                 message = msgfile.read()
-        except:
-            print("Could not open the e-mail file %s.", filename)
+        except IOError:
+            logging.warning("Could not open the e-mail file %s", filename)
+            raise
         return message
 
     def load_mailbox_list(self, mboxlistfile = ""):
@@ -49,8 +51,8 @@ class FileIterator:
                         linedata = MBOXFILE_LINE.match(line).groups()
                         #!!! test this condition
                         if len(linedata) == 0:
-                            print("Illegal line in mailbox list dump: %s" % line)
-                            sys.exit()
+                            logging.warning("Illegal line in mailbox list dump: %s" % line)
+                            continue
                         key = linedata[0]
                         aclstr = linedata[1]
                         
@@ -58,60 +60,58 @@ class FileIterator:
                         while(aclstr != ""):
                             acldata = ACL_STRING.match(aclstr).groups()
                             if len(acldata) == 0:
-                                print("Illegal line in mailbox list dump: %s" % line)
-                                sys.exit()                               
+                                logging.error("Illegal acl string in mailbox list dump: %s" % line)
+                                continue
                             aclstr = acldata[2]
                             acls[acldata[0]]=acldata[1]
 
                         mboxdb[key] = acls
-
             except IOError:
-                print("Could not open mboxlist file %s." % mboxlistfile)
+                logging.warning("Could not open mboxlist file %s" % mboxlistfile)
         return mboxdb
 
     def load_mails(self, filepath, mailpath):
         """Loads all e-mails from file hierarchy.
         This recursive generator always returns a tuple of
         the next found (e-mail, mailbox to store, internaldate)."""
-        #print("Entered directory %s -> %s." % (filepath, mailpath))
+        logging.debug("Entered directory %s -> %s" % (filepath, mailpath))
         try:
             filepath = os.path.abspath(filepath)
             os.chdir(filepath)
         except OSError:
-            print("Can't open the directory %s." % filepath)
+            logging.warning("Can't open the directory %s" % filepath)
             return
         # mark mailboxes that should be created
         self.created_mailboxes.append(mailpath)
         subpaths = os.listdir(filepath)
         for subpath in subpaths:
-            #print("Now checking subpath %s in %s" % (subpath, filepath))
             if subpath == "." or subpath == "..":
                 continue
             new_filepath = filepath + "/" + subpath
             if (os.path.isfile(new_filepath)):
                 if os.path.getsize(new_filepath) == 0:
-                    print("Skipping empty file %s." % subpath)
+                    logging.info("Skipping empty file %s" % subpath)
                 else:
                     if MAIL_FILENAME.match(subpath):
-                        #print("Injecting file %s." % subpath)
+                        logging.info("Injecting file %s" % subpath)
                         try:
                             message = self._message_read(new_filepath)
                             # suggest file modification date for internaldate
                             yield (message, mailpath, os.path.getmtime(new_filepath))
-                        except:
-                            print("Could not retrieve mail from file: %s" % new_filepath)                
+                        except IOError:
+                            logging.warning("Could not retrieve mail from the file %s" % new_filepath)
             else:
                 if os.path.isdir(new_filepath):
                     # cyrus ^ vs . storage replacement
                     subpath = subpath.replace("^", ".")
                     new_mailpath = mailpath + "/" + subpath
-                    #print("Inserting mails from directory %s into mailbox %s." % (new_filepath, new_mailpath))
+                    logging.debug("Inserting mails from directory %s into mailbox %s" % (new_filepath, new_mailpath))
                     # load_mails($mboxdbref, $origuser, $targetuser)
                     rcrs_generator = self.load_mails(new_filepath, new_mailpath)
                     # you enter the generator in the for loop
                     for rcr in rcrs_generator:
                         yield rcr
-                    #print("Done with directory %s and mailbox %s." % (new_filepath, new_mailpath))
+                    logging.debug("Done with directory %s and mailbox %s" % (new_filepath, new_mailpath))
         # mark mailboxes that need acl update
         self.acl_mailboxes.append(mailpath)
         return
\ No newline at end of file