Author: saces
Date: 2008-11-02 20:52:37 +0000 (Sun, 02 Nov 2008)
New Revision: 23288

Modified:
   trunk/apps/pyFreenetHg/FreenetHg.py
Log:
fcp-unbundle implementation + fixes from Ratchet

Modified: trunk/apps/pyFreenetHg/FreenetHg.py
===================================================================
--- trunk/apps/pyFreenetHg/FreenetHg.py 2008-11-02 18:48:55 UTC (rev 23287)
+++ trunk/apps/pyFreenetHg/FreenetHg.py 2008-11-02 20:52:37 UTC (rev 23288)
@@ -12,41 +12,41 @@
 import sys
 import dircache
 import fcp
- 
+
 from mercurial import hg
 from mercurial import commands
 from mercurial import repo,cmdutil,util,ui,revlog,node
 from mercurial.node import bin

 class myFCP(fcp.FCPNode):
-    
+
     def putraw(self, id, rawcmd, async=False):
         """
         Inserts a raw command.
         This is intended for testing and development, not for common use
-        
+
         Arguments:
             - id - job id, must be the same as identifier in raw command (if 
any)
-            - rawcmd - data passed as is to the node 
+            - rawcmd - data passed as is to the node
             - async - whether to do the job asynchronously, returning a job 
ticket
               object (default False)
         """
-         
+
         opts = {}
         opts['async'] = async
         opts['rawcmd'] = rawcmd
-        
+
         return self._submitCmd(id, "", **opts)
-    
+
     def putraw2(self, id, rawcmd):
         """
         mine ;) verbosity hacking
         do not print the command
         """
-        
+
         self.verbosity = fcp.INFO
-        ticket = self.putraw(id, rawcmd, True)  
-        ticket.waitTillReqSent()                           
+        ticket = self.putraw(id, rawcmd, True)
+        ticket.waitTillReqSent()
         self.verbosity = fcp.DETAIL
         return ticket.wait()

@@ -58,15 +58,15 @@
     #@+node:__init__
     def __init__(self, repo):
         """ """
-        
+
         self._rootdir = repo.url()[5:] + '/.hg/'
         self._index = 0;
         self._fileitemlist = {}
         self._databuff = ''
         self._cmdbuff = ''
-        
+
         a = dircache.listdir(self._rootdir)
-        
+
         for s in a:
             if s == 'hgrc':
                 pass # it may contains private/local config!! -> forbitten
@@ -76,9 +76,9 @@
                 pass # called from hook, ignore
             else:
                 self._addItem('', s)
-                
+
         self._parseDir('store')
-       
+
     def _parseDir(self, dir):
         a = dircache.listdir(self._rootdir + dir)
         dircache.annotate(self._rootdir + dir, a)
@@ -86,35 +86,35 @@
             if s[-1:] == '/':
                 self._parseDir(dir + '/' + s[:-1])
             elif s[-4:] == 'lock':
-               pass # called from hook, ignore
+                pass # called from hook, ignore
             else:
                 self._addItem(dir, s)
-            
+
     def _addItem(self, dir, filename):
         """ """
         if dir != "":
             dir = dir + '/'
-        
+
         virtname = dir + filename
         realname = self._rootdir + virtname
-        
+
         f = open(realname)
         content = f.read()
         f.close()
-        
+
         self._databuff = self._databuff + content;
         idx = str(self._index)
-            
+
         self._cmdbuff = self._cmdbuff + "Files."+idx+".Name=.hg/" + virtname + 
'\n'
         self._cmdbuff = self._cmdbuff + "Files."+idx+".UploadFrom=direct" + 
'\n'
         self._cmdbuff = self._cmdbuff + 
"Files."+idx+".Metadata.ContentType=text/plain" + '\n'
-        self._cmdbuff = self._cmdbuff + "Files."+idx+".DataLength=" + 
str(len(content)) + '\n'    
-        
+        self._cmdbuff = self._cmdbuff + "Files."+idx+".DataLength=" + 
str(len(content)) + '\n'
+
         self._index = self._index + 1
-        
+
     def getCmd(self):
         return self._cmdbuff + "Data\n"
-    
+
     def getRawCmd(self):
         return self._cmdbuff + "Data\n" + self._databuff

@@ -125,47 +125,79 @@
 # don't start with a dash.  If no default value is given in the parameter list,
 # they are required
 def fcp_bundle(ui, repo, **opts):
-     # The doc string below will show up in hg help
-     """write bundle to CHK/USK
-     the bundel will be inserted as CHK@ if no uri is given
-     see hg help bundle for bundle options
-     """
-     
-     # setup fcp stuff
-     # set server, port
-     
-     # make tempfile
-     tmpfd, tmpfname = tempfile.mkstemp('fcpbundle')
-     
-     #delete it, the bundle function don't like prexisting files
-     os.remove(tmpfname)    
-     
-     #create bundle file, call the origin bundle funcrion
-     commands.bundle(ui, repo, tmpfname, **opts)
-     
-     #read the bundle
-     f = open(tmpfname, 'r+')
-     bundledata = f.read()
-     
-     #delete the tempfile again
-     os.remove(tmpfname) 
+    # The doc string below will show up in hg help
+    """write bundle to CHK/USK
+    the bundel will be inserted as CHK@ if no uri is given
+    see hg help bundle for bundle options
+    """

-     # now insert the data as chk (first step implementation)
-     #node = fcp.FCPNode(verbosity=fcp.DETAIL)
-     node = _make_node(**opts)
-     
-     insertresult = node.put(data=bundledata, priority=1, 
mimetype="mercurial/bundle");
-     
-     node.shutdown();
-     
-     print "bundle inserted: " + insertresult
-     
+    # setup fcp stuff
+    # set server, port
+    if not opts.get('fcphost'):
+        opts['fcphost'] = ui.config('freenethg', 'fcphost')
+    if not opts.get('fcpport'):
+        opts['fcpport'] = ui.config('freenethg', 'fcpport')

-def fcp_unbundle(ui, repo, node, **opts):
-     # The doc string below will show up in hg help
-     """unbundle from CHK/USK (not implemented jet)"""
-     #commands.unbundle(ui, repo, "Filename", **opts)
+    # make tempfile
+    tmpfd, tmpfname = tempfile.mkstemp('fcpbundle')

+    #delete it, the bundle function don't like prexisting files
+    os.remove(tmpfname)
+
+    #create bundle file, call the origin bundle funcrion
+    commands.bundle(ui, repo, tmpfname, **opts)
+
+    #read the bundle
+    f = open(tmpfname, 'r+')
+    bundledata = f.read()
+
+    #delete the tempfile again
+    os.remove(tmpfname)
+
+    # now insert the data as chk (first step implementation)
+    #node = fcp.FCPNode(verbosity=fcp.DETAIL)
+    node = _make_node(**opts)
+
+    insertresult = node.put(data=bundledata, priority=1, 
mimetype="mercurial/bundle");
+
+    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"""
+
+    # set server, port
+    if not opts.get('fcphost'):
+        opts['fcphost'] = ui.config('freenethg', 'fcphost')
+    if not opts.get('fcpport'):
+        opts['fcpport'] = ui.config('freenethg', 'fcpport')
+
+    # make tempfile
+    tmpfd, tmpfname = tempfile.mkstemp('fcpbundle')
+
+    node = _make_node(**opts)
+
+    unbundle = None
+
+    try:
+        unbundle = node.get(uri, priority=1, maxretries=5)
+    except fcp.node.FCPException, e:
+        print e
+
+    node.shutdown();
+
+    if unbundle:
+        changes = unbundle[1]
+        f = open(tmpfname, 'wb')
+        f.write(changes)
+        f.close()
+        commands.unbundle(ui, repo, tmpfname, **opts)
+
+    os.remove(tmpfname)
+
 def _make_node(**opts):
     fcpopts = {}
     fcpopts['verbosity'] = fcp.INFO
@@ -177,11 +209,11 @@
         fcpopts['port'] = port
     #return node2.FCPNode(**fcpopts)
     return myFCP(**fcpopts)
-    
+
 def fcp_makestatic(ui, repo, uri=None, **opts):
     """put the repo into freenet for access via static-http
     """
-    
+
     id = "freenethgid" + str(int(time.time() * 1000000))

     config_uri = ui.config('freenethg', 'inserturi')
@@ -198,133 +230,133 @@
         opts['fcpport'] = ui.config('freenethg', 'fcpport')
     cmd = "ClientPutComplexDir\n" + "URI=" + uri + "\nIdentifier=" + id
     cmd = cmd + "\nVerbosity=-1\nPriorityClass=1\n"
-    
-    composer = _static_composer(repo)   
-    
+
+    composer = _static_composer(repo)
+
     print "Debug: " + cmd + composer.getCmd()
-    
-    print "site composer done." 
+
+    print "site composer done."
     print "insert now. this may take a while..."
-    
+
     node = _make_node(**opts)
-    
+
     #testresult = node.putraw(id, cmd + composer.getRawCmd())
     testresult = node.putraw2(id, cmd + composer.getRawCmd())
-    
+
     node.shutdown();
-    
+
     print "success? " + testresult
-    
+
 def fcp_createstatic(ui, repo, uri=None, **opts):
-    """put the repo into freenet for access via static-http, updatedable (not 
implemented jet)   
+    """put the repo into freenet for access via static-http, updatedable (not 
implemented jet)
     """
- 
+
     pass
- 
+
 def fcp_updatestatic(ui, repo, **opts):
-    """update the repo in freenet for access via static-http   
+    """update the repo in freenet for access via static-http
     """
-    
+
     updatestatic_hook(ui, repo, None, **opts)
-        
+
 def updatestatic_hook(ui, repo, hooktype, node=None, source=None, **kwargs):
     """update static """
-    
-    
+
+
     id = "freenethgid" + str(int(time.time() * 1000000))
     host = ui.config('freenethg', 'fcphost')
     port = ui.config('freenethg', 'fcpport')
-     
+
     uri = ui.config('freenethg', 'inserturi')
     #uri = "CHK@"
-   
+
     fcpopts = {}
     fcpopts['verbosity'] = fcp.INFO
     fcpopts['host'] = host
     fcpopts['port'] = port
+
     #fcpopts['logfunc'] = ui.log
-    
     node = myFCP(**fcpopts)
-         
+
     cmd = "ClientPutComplexDir\n" + "URI=" + uri + "\nIdentifier=" + id
     cmd = cmd + "\nVerbosity=-1\nPriorityClass=1\n"
-    
-    composer = _static_composer(repo)   
-    
+
+    composer = _static_composer(repo)
+
     print "Debug: " + cmd + composer.getCmd()
-    
-    print "site composer done." 
+
+    print "site composer done."
     print "insert now. this may take a while..."
-    
+
     #testresult = 'debug'
     #testresult = node.putraw(id, cmd + composer.getRawCmd())
     testresult = node.putraw2(id, cmd + composer.getRawCmd())
-    
+
     node.shutdown();
-    
+
     print "success? " + testresult
-    
+
 def updatestatic_hook2(ui, repo, hooktype, node=None, source=None, **kwargs):
     """update static """
-    
+
     # if ukw not set or empty throw an error
-    
+
     ukw = ui.config('freenethg', 'uploadkeyword')
-    
-    if not ukw: 
+
+    if not ukw:
         raise Exception, 'required config option ?uploadkeyword? not set'
-    
+
     if len(ukw.strip()) == 0:
         raise Exception, 'the keyword must contains at least one printable 
non-whitespace char'
-    
+
     comment = repo.changelog.read(bin(node))[4]
-    
+
     if ukw in comment:
         updatestatic_hook(ui, repo, hooktype, node, kwargs)

 def updatestatic_hook3(ui, repo, hooktype, node=None, source=None, **kwargs):
     """update static """
-    
+
     # if ukw not set or empty throw an error
-    
+
     ukw = ui.config('freenethg', 'uploadkeyword')
-    
-    if not ukw: 
+
+    if not ukw:
         raise ConfigError, 'required config option ?uploadkeyword? not set'

     # the keyword must contains at least one printable non-whitespace char
     # test_ukw = ukw.trim()
-    
+
     #if test_ukw == "":
     #    raise ConfigError, 'required config option ?uploadkeyword? cant be 
empty'
-    
+
     # if kw not set or empty throw an error
-    
+
     # message = getCommitMessage
     # if messege not contains kw
     #     nothing to do, return
-    
+
     # stp = checkNodeForSiteToolPlugin
     # if !stp     
     #    # SiteToolPlugin not installed, print a warning and fallback to 
simple plain isert
     #    updatestatic_hook(ui, repo, hooktype, node, source, kwargs)
-    
+
     # We expect lost inserts, so don't belive in the parent passed into hook
     # oldTip = getLatestFromFreenet
-    
+
     # filelist = getChangedFiles(oldTip, newTip)
-    
+
     # openSession (SiteToolPlugin)
-    
+
     # applyFiles(fileList)
-    
+
     # commitSession
-    
+
     # ende
-    
- 
-   
-         
+
+
+
+
 remoteopts = [
     ('e', 'ssh', '', 'specify ssh command to use'),
     ('', 'remotecmd', '', 'specify hg command to run on the remote side'),
@@ -347,7 +379,7 @@
        "fcp-unbundle": (fcp_unbundle,
                         [('u', 'update', None, 'update to new tip if 
changesets were unbundled'),
                          ] + fcpopts,
-                        'hg fcp-unbundle [-u] FREENETKEY'), 
+                        'hg fcp-unbundle [-u] FREENETKEY'),
        "fcp-makestatic": (fcp_makestatic,
                         [('', 'uri', None, 'use insert uri instead generate 
chk')
                          ] + fcpopts,
@@ -358,5 +390,5 @@
        #                 'hg fcp-createstatic'),  
        "fcp-updatestatic": (fcp_updatestatic,
                         [] + fcpopts,
-                        'hg fcp-updatestatic')  
+                        'hg fcp-updatestatic')
 }


Reply via email to