mercurial/changegroup.py
changeset 1981 736b6c96bbbc
child 1997 802e8a029d99
equal deleted inserted replaced
1980:dfb796786337 1981:736b6c96bbbc
       
     1 """
       
     2 changegroup.py - Mercurial changegroup manipulation functions
       
     3 
       
     4  Copyright 2006 Matt Mackall <mpm@selenic.com>
       
     5 
       
     6 This software may be used and distributed according to the terms
       
     7 of the GNU General Public License, incorporated herein by reference.
       
     8 """
       
     9 import struct
       
    10 from demandload import *
       
    11 demandload(globals(), "util")
       
    12 
       
    13 def getchunk(source):
       
    14     """get a chunk from a changegroup"""
       
    15     d = source.read(4)
       
    16     if not d:
       
    17         return ""
       
    18     l = struct.unpack(">l", d)[0]
       
    19     if l <= 4:
       
    20         return ""
       
    21     d = source.read(l - 4)
       
    22     if len(d) < l - 4:
       
    23         raise util.Abort(_("premature EOF reading chunk"
       
    24                            " (got %d bytes, expected %d)")
       
    25                           % (len(d), l - 4))
       
    26     return d
       
    27 
       
    28 def chunkiter(source):
       
    29     """iterate through the chunks in source"""
       
    30     while 1:
       
    31         c = getchunk(source)
       
    32         if not c:
       
    33             break
       
    34         yield c
       
    35 
       
    36 def genchunk(data):
       
    37     """build a changegroup chunk"""
       
    38     header = struct.pack(">l", len(data)+ 4)
       
    39     return "%s%s" % (header, data)
       
    40 
       
    41 def closechunk():
       
    42     return struct.pack(">l", 0)
       
    43