Author: ratchet
Date: 2008-11-13 15:17:04 +0000 (Thu, 13 Nov 2008)
New Revision: 23521

Modified:
   trunk/apps/pyFreenetHg/FreenetHg.py
Log:
Notifier class which will handle all notifications.
implementation of status callback method from pyFreenet
callback status for fcp-bundle


Modified: trunk/apps/pyFreenetHg/FreenetHg.py
===================================================================
--- trunk/apps/pyFreenetHg/FreenetHg.py 2008-11-13 14:23:22 UTC (rev 23520)
+++ trunk/apps/pyFreenetHg/FreenetHg.py 2008-11-13 15:17:04 UTC (rev 23521)
@@ -101,6 +101,43 @@

         return result

+
+class Notifier(object):
+    """This object handles all notifications of repository updates or bundle 
inserts"""
+
+    def __init__(self, ui, notify_data, autorun=False):
+
+        self.ui = ui
+        self.notify_data = notify_data
+
+        if autorun:
+            self.notify()
+
+    def notify(self):
+        uiw = self.ui.walkconfig()
+        trigger = self.ui.config('freenethg','notify')
+
+        for section, key, value in uiw:
+            if 'notify_' in section and key == 'type' and 
section.replace('notify_','') in trigger:
+                m = getattr(self, value)
+                m(section)
+
+    def fmsnntp(self, config_section):
+        fms_host = self.ui.config(config_section,'fmshost')
+        fms_port = self.ui.config(config_section,'fmsport')
+        fms_user = self.ui.config(config_section,'fmsuser')
+        fms_groups = self.ui.config(config_section,'fmsgroups')
+
+        if fms_host and fms_port and fms_user and fms_groups:
+
+            server = FMS_NNTP(fms_host, fms_user, fms_groups, int(fms_port))
+            result = server.post_updatestatic(self.notify_data['uri'])
+            server.quit()
+
+            print "NNTP result: %s" % str(result)
+
+
+
 class myFCP(fcp.FCPNode):

     def putraw(self, id, rawcmd, async=False):
@@ -133,6 +170,25 @@
         self.verbosity = fcp.DETAIL
         return ticket.wait()

+    def put_static(self, id, rawcmd):
+        """putraw with status callback method"""
+        opts = dict()
+        opts['async'] = True
+        opts['rawcmd'] = rawcmd
+        opts['callback'] = self.put_status
+
+        ticket = self._submitCmd(id, "", **opts)
+        ticket.wait()
+        return ticket.result
+
+    def put_status(self, status, value):
+        """prints the status messages from jobticket"""
+        if status == 'pending' and value['header'] == 'SimpleProgress':
+            print "Succeeded: %d  -  Required: %d  -  Total: %d  -  Failed: %d 
 -  Final: %s" % (value['Succeeded'], value['Required'], value['Total'], 
value['FatallyFailed'], value['FinalizedTotal'])
+        if status == 'successful':
+            print "inserted successfully: %s" % value
+
+
 class _static_composer(object):
     """
     a helper class to compose the ClientPutComplexDir
@@ -253,13 +309,14 @@
     #node = fcp.FCPNode(verbosity=fcp.DETAIL)
     node = _make_node(**opts)

-    insertresult = node.put(data=bundledata, priority=1, 
mimetype="mercurial/bundle")
+    print "insert now. this may take a while..."

+    insertresult = node.put(data=bundledata, priority=1, 
mimetype="mercurial/bundle", async=True, callback=node.put_status)
+    insertresult.wait()
+
     node.shutdown()

-    print "bundle inserted: " + insertresult

-
 def fcp_unbundle(ui, repo, uri , **opts):
     # The doc string below will show up in hg help
     """unbundle from CHK/USK"""
@@ -339,14 +396,14 @@

     try:
         #testresult = node.putraw(id, cmd + composer.getRawCmd())
-        testresult = node.putraw2(id, cmd + composer.getRawCmd())
+        testresult = node.put_static(id, cmd + composer.getRawCmd())
     except fcp.node.FCPException, e:
         print e

     node.shutdown()

-    if testresult:
-        print "success: " + testresult
+#    if testresult:
+#        print "success: " + testresult
         # here method for automated fms posts should be called

 def fcp_createstatic(ui, repo, uri=None, **opts):
@@ -393,33 +450,21 @@
     print "site composer done."
     print "insert now. this may take a while..."

-    testresult = None
+    result = None

     try:
-        #testresult = node.putraw(id, cmd + composer.getRawCmd())
-        testresult = node.putraw2(id, cmd + composer.getRawCmd())
+        result = node.put_static(id, cmd + composer.getRawCmd())
     except fcp.node.FCPException, e:
         print e

     node.shutdown()

-    if testresult:
-        print "success: " + testresult
+    if result:

-        fms_host = ui.config('freenethg','fmshost')
-        fms_port = ui.config('freenethg','fmsport')
-        fms_user = ui.config('freenethg','fmsuser')
-        fms_groups = ui.config('freenethg','fmsgroups')
+        notify_data = {'uri':result}
+        notifier = Notifier(ui, notify_data, autorun=True)

-        if fms_host and fms_port and fms_user and fms_groups:

-            server = FMS_NNTP(fms_host, fms_user, fms_groups, int(fms_port))
-            result = server.post_updatestatic(testresult)
-            server.quit()
-
-            print "NNTP result: %s" % str(result)
-
-
 def updatestatic_hook2(ui, repo, hooktype, node=None, source=None, **kwargs):
     """update static """



Reply via email to