[PATCH 17/17] Move actual synchronization into a startsync function

Sebastian Spaeth Sebastian at SSpaeth.de
Mon Nov 29 16:02:25 GMT 2010


Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 offlineimap/init.py |  125 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 68 insertions(+), 57 deletions(-)

diff --git a/offlineimap/init.py b/offlineimap/init.py
index f76fb5f..b7961a8 100644
--- a/offlineimap/init.py
+++ b/offlineimap/init.py
@@ -40,8 +40,8 @@ class OfflineImap:
                                 line arguments to be used rather than using sys.argv."""
         self._lockfile = None       # lockfile path when locked
         self._pidfile  = None       # pidfile path when locked
-        self.parse_commandline(cmdline_opts)
-
+        (options, args)= self.parse_commandline(cmdline_opts)
+        self.startsync(options)
 
     def lock(self):
         """Create lock file and exit if not possible.
@@ -264,10 +264,25 @@ class OfflineImap:
                     self._config.set(section, "folderfilter", folderfilter)
                     self._config.set(section, "folderincludes", folderincludes)
 
+
+        if options.logfile:
+            sys.stderr = OfflineImap.ui.logfile
+    
+        socktimeout = self._config.getdefaultint("general", "socktimeout", 0)
+        if socktimeout > 0:
+            socket.setdefaulttimeout(socktimeout)
+
+        return (options, args)
+
+
+    def startsync(self, options):
+        """Do a sync on all configured accounts"""
+
+        #lock instance
         self.lock()
         self.write_pidfile(
             os.path.join(self._config.getmetadatadir(), "pid"))
-    
+
         def sigterm_handler(self, signum, frame):
             # die immediately
             ui = BaseUI.getglobalui()
@@ -275,72 +290,68 @@ class OfflineImap:
 
         signal.signal(signal.SIGTERM,sigterm_handler)
 
-        try:
-            if options.logfile:
-                sys.stderr = OfflineImap.ui.logfile
-    
-            socktimeout = self._config.getdefaultint("general", "socktimeout", 0)
-            if socktimeout > 0:
-                socket.setdefaulttimeout(socktimeout)
-    
-            activeaccounts = self._config.get("general", "accounts")
-            if options.accounts:
-                activeaccounts = options.accounts
-            activeaccounts = activeaccounts.replace(" ", "")
-            activeaccounts = activeaccounts.split(",")
-            allaccounts = accounts.AccountHashGenerator(self._config)
+        #Figure out which accounts to sync
+        activeaccounts = self._config.get("general", "accounts")
+        if options.accounts:
+            activeaccounts = options.accounts
+        activeaccounts = activeaccounts.replace(" ", "")
+        activeaccounts = activeaccounts.split(",")
+        allaccounts = accounts.AccountHashGenerator(self._config)
     
-            syncaccounts = []
-            for account in activeaccounts:
-                if account not in allaccounts:
-                    if len(allaccounts) == 0:
-                        errormsg = 'The account "%s" does not exist because no accounts are defined!'%account
-                    else:
-                        errormsg = 'The account "%s" does not exist.  Valid accounts are:'%account
-                        for name in allaccounts.keys():
-                            errormsg += '\n%s'%name
-                    OfflineImap.ui.terminate(1, errortitle = 'Unknown Account "%s"'%account, errormsg = errormsg)
-                if account not in syncaccounts:
-                    syncaccounts.append(account)
+        syncaccounts = []
+        for account in activeaccounts:
+            if account not in allaccounts:
+                if len(allaccounts) == 0:
+                    errormsg = 'The account "%s" does not exist because no accounts are defined!'%account
+                else:
+                    errormsg = 'The account "%s" does not exist.  Valid accounts are:'%account
+                    for name in allaccounts.keys():
+                        errormsg += '\n%s'%name
+                OfflineImap.ui.terminate(1, errortitle = 'Unknown Account "%s"'%account, errormsg = errormsg)
+            if account not in syncaccounts:
+                syncaccounts.append(account)
     
-            server = None
-            remoterepos = None
-            localrepos = None
+        server = None
+        remoterepos = None
+        localrepos = None
     
-            if options.singlethreading:
-                threadutil.initInstanceLimit("ACCOUNTLIMIT", 1)
-            else:
-                threadutil.initInstanceLimit("ACCOUNTLIMIT",
-                                             self._config.getdefaultint("general", "maxsyncaccounts", 1))
+        #How many accounts do we want to check at the same time
+        if options.singlethreading:
+            threadutil.initInstanceLimit("ACCOUNTLIMIT", 1)
+        else:
+            threadutil.initInstanceLimit("ACCOUNTLIMIT",
+                                         self._config.getdefaultint("general", "maxsyncaccounts", 1))
     
-            for reposname in self._config.getsectionlist('Repository'):
-                for instancename in ["FOLDER_" + reposname,
-                                     "MSGCOPY_" + reposname]:
-                    if options.singlethreading:
-                        threadutil.initInstanceLimit(instancename, 1)
-                    else:
-                        threadutil.initInstanceLimit(instancename,
-                                                     self._config.getdefaultint('Repository ' + reposname, "maxconnections", 1))
-            siglisteners = []
-
-            def sig_handler(signum, frame):
-                if signum == signal.SIGUSR1:
+        #How many connections/threads to this repository at the same time?
+        for reposname in self._config.getsectionlist('Repository'):
+            for instancename in ["FOLDER_" + reposname,
+                                 "MSGCOPY_" + reposname]:
+                if options.singlethreading:
+                    threadutil.initInstanceLimit(instancename, 1)
+                else:
+                    threadutil.initInstanceLimit(instancename,
+                                                 self._config.getdefaultint('Repository ' + reposname, "maxconnections", 1))
+        siglisteners = []
+
+        def sig_handler(signum, frame):
+            if signum == signal.SIGUSR1:
                     # tell each account to do a full sync asap
                     signum = (1,)
-                elif signum == signal.SIGHUP:
+            elif signum == signal.SIGHUP:
                     # tell each account to die asap
                     signum = (2,)
-                elif signum == signal.SIGUSR2:
+            elif signum == signal.SIGUSR2:
                     # tell each account to do a full sync asap, then die
                     signum = (1, 2)
                 # one listener per account thread (up to maxsyncaccounts)
-                for listener in siglisteners:
-                    for sig in signum:
-                        listener.put_nowait(sig)
-            signal.signal(signal.SIGHUP,sig_handler)
-            signal.signal(signal.SIGUSR1,sig_handler)
-            signal.signal(signal.SIGUSR2,sig_handler)
+            for listener in siglisteners:
+                for sig in signum:
+                    listener.put_nowait(sig)
+        signal.signal(signal.SIGHUP,sig_handler)
+        signal.signal(signal.SIGUSR1,sig_handler)
+        signal.signal(signal.SIGUSR2,sig_handler)
     
+        try:
             threadutil.initexitnotify()
             t = ExitNotifyThread(target=syncmaster.syncitall,
                                  name='Sync Runner',
-- 
1.7.1





More information about the OfflineIMAP-project mailing list