Patchwork [07,of,13] largefiles: move protocol conversion into getlfile and make it an iterable

login
register
mail settings
Submitter Mads Kiilerich
Date April 16, 2013, 2:43 a.m.
Message ID <9e00da40bd5995bf07c7.1366080204@mk-desktop>
Download mbox | patch
Permalink /patch/1336/
State Accepted
Commit 6614e5e24e666c66f0fb7a06e3b8dc241c9f3f83
Headers show

Comments

Mads Kiilerich - April 16, 2013, 2:43 a.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1366069599 -7200
#      Tue Apr 16 01:46:39 2013 +0200
# Node ID 9e00da40bd5995bf07c7cb3df2b152ab34fb2603
# Parent  1de0e7b65b58a91e7b78474f5aacb8bfef0e66df
largefiles: move protocol conversion into getlfile and make it an iterable

Avoid the intermediate limitreader and filechunkiter between getlfile and
copyandhash - return the right protocol and put the complexity where it better
can be managed.

Patch

diff --git a/hgext/largefiles/proto.py b/hgext/largefiles/proto.py
--- a/hgext/largefiles/proto.py
+++ b/hgext/largefiles/proto.py
@@ -114,6 +114,7 @@ 
                         _('putlfile failed (unexpected response):'), ret)
 
         def getlfile(self, sha):
+            """returns an iterable with the chunks of the file with sha sha"""
             stream = self._callstream("getlfile", sha=sha)
             length = stream.readline()
             try:
@@ -121,7 +122,12 @@ 
             except ValueError:
                 self._abort(error.ResponseError(_("unexpected response:"),
                                                 length))
-            return (length, stream)
+
+            # Mercurial does not close its SSH connections after writing a stream
+            infile = lfutil.limitreader(stream, length)
+            for chunk in util.filechunkiter(infile, 128 * 1024):
+                yield chunk
+            infile.close()
 
         @batchable
         def statlfile(self, sha):
diff --git a/hgext/largefiles/remotestore.py b/hgext/largefiles/remotestore.py
--- a/hgext/largefiles/remotestore.py
+++ b/hgext/largefiles/remotestore.py
@@ -58,7 +58,7 @@ 
                                'statlfile (%r)' % stat)
 
         try:
-            length, infile = self._get(hash)
+            chunks = self._get(hash)
         except urllib2.HTTPError, e:
             # 401s get converted to util.Aborts; everything else is fine being
             # turned into a StoreError
@@ -71,14 +71,7 @@ 
         except IOError, e:
             raise basestore.StoreError(filename, hash, self.url, str(e))
 
-        # Mercurial does not close its SSH connections after writing a stream
-        if length is not None:
-            infile = lfutil.limitreader(infile, length)
-        try:
-            return lfutil.copyandhash(util.filechunkiter(infile, 128 * 1024),
-                                      tmpfile)
-        finally:
-            infile.close()
+        return lfutil.copyandhash(chunks, tmpfile)
 
     def _verifyfile(self, cctx, cset, contents, standin, verified):
         filename = lfutil.splitstandin(standin)