]> code.delx.au - offlineimap/commitdiff
Applied pre/post sync hooks
authorJohn Goerzen <jgoerzen@complete.org>
Wed, 1 Oct 2008 05:03:04 +0000 (00:03 -0500)
committerJohn Goerzen <jgoerzen@complete.org>
Wed, 1 Oct 2008 05:03:04 +0000 (00:03 -0500)
Patch from Sylvain FORET in refs #71

offlineimap/accounts.py
offlineimap/ui/Blinkenlights.py
offlineimap/ui/Machine.py
offlineimap/ui/UIBase.py

index a86485d88d5cf2bd4f8bd27e47c73ad9ad75df77..36a7665bcfdadb3cfa6dca7d0f7f70ac039804ca 100644 (file)
@@ -21,6 +21,7 @@ from offlineimap.ui import UIBase
 from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
 from threading import Event
 import os
+from subprocess import Popen, PIPE
 
 def getaccountlist(customconfig):
     return customconfig.getsectionlist('Account')
@@ -121,6 +122,9 @@ class AccountSynchronizationMixin:
         # We don't need an account lock because syncitall() goes through
         # each account once, then waits for all to finish.
 
+        hook = self.getconf('presynchook', '')
+        self.callhook(hook)
+
         quickconfig = self.getconfint('quick', 0)
         if quickconfig < 0:
             quick = True
@@ -161,6 +165,23 @@ class AccountSynchronizationMixin:
             remoterepos.holdordropconnections()
         finally:
             pass
+
+        hook = self.getconf('postsynchook', '')
+        self.callhook(hook)
+
+    def callhook(self, cmd):
+        if not cmd:
+            return
+        try:
+            self.ui.callhook("Calling hook: " + cmd)
+            p = Popen(cmd, shell=True,
+                      stdin=PIPE, stdout=PIPE, stderr=PIPE,
+                      close_fds=True)
+            r = p.communicate()
+            self.ui.callhook("Hook stdout: %s\nHook stderr:%s\n" % r)
+            self.ui.callhook("Hook return code: %d" % p.returncode)
+        except:
+            self.ui.warn("Exception occured while calling hook")
     
 class SyncableAccount(Account, AccountSynchronizationMixin):
     pass
index 69823513117139b5042536e93f7335b1231161b3..577f60186c4f8b98d28bbe30eaed3465a1ab4705 100644 (file)
@@ -127,6 +127,10 @@ class BlinkenBase:
             return tf
         finally:
             s.tflock.release()
+
+    def callhook(s, msg):
+        s.gettf().setcolor('white')
+        s.__class__.__bases__[-1].callhook(s, msg)
             
     def sleep(s, sleepsecs):
         s.gettf().setcolor('red')
index d02bbbc173804ea7cb654eaa5a8a713e201ba9f1..0a07e3eda55e6c8b0c218138baccf58854acad06 100644 (file)
@@ -175,3 +175,5 @@ class MachineUI(UIBase):
     def init_banner(s):
         s._printData('initbanner', offlineimap.version.banner)
 
+    def callhook(s, msg):
+        s._printData('callhook', msg)
index d0e92e6a07716933f6801144235284ba5b6c7a8c..ffdbc782705483e3fd927aac191a60de77ca3896 100644 (file)
@@ -322,6 +322,12 @@ class UIBase:
         s.delThreadDebugLog(thread)
         s.unregisterthread(thread)
 
+    ################################################## Hooks
+
+    def callhook(s, msg):
+        if s.verbose >= 0:
+            s._msg(msg)
+
     ################################################## Other
 
     def sleep(s, sleepsecs):