comparison mercurial/bundle2.py @ 20802:520df53ad26a

bundle2: a very first version of bundle2 unbundler This changeset introduce an unbundler class to match the bundle2 bundler. It is currently able to unbundle an empty bundle2 only and will gain more feature at the same pace than the bundler. It also comes with its special extension command in test.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Tue, 18 Mar 2014 14:28:42 -0700
parents 9c5183cb9bca
children 88db3e615319
comparison
equal deleted inserted replaced
20801:9c5183cb9bca 20802:520df53ad26a
58 (size = 0) this is interpreted as the end of stream marker. 58 (size = 0) this is interpreted as the end of stream marker.
59 59
60 Currently forced to 0 in the current state of the implementation 60 Currently forced to 0 in the current state of the implementation
61 """ 61 """
62 62
63 import util
64 import changegroup
65
66
63 _magicstring = 'HG20' 67 _magicstring = 'HG20'
64 68
65 class bundle20(object): 69 class bundle20(object):
66 """represent an outgoing bundle2 container 70 """represent an outgoing bundle2 container
67 71
80 yield '\0\0' 84 yield '\0\0'
81 # no support for parts 85 # no support for parts
82 # to be obviously fixed soon. 86 # to be obviously fixed soon.
83 assert not self._parts 87 assert not self._parts
84 yield '\0\0' 88 yield '\0\0'
89
90 class unbundle20(object):
91 """interpret a bundle2 stream
92
93 (this will eventually yield parts)"""
94
95 def __init__(self, fp):
96 # assume the magic string is ok and drop it
97 # to be obviously fixed soon.
98 self._fp = fp
99 self._readexact(4)
100
101 def _unpack(self, format):
102 """unpack this struct format from the stream"""
103 data = self._readexact(struct.calcsize(format))
104 return _unpack(format, data)
105
106 def _readexact(self, size):
107 """read exactly <size> bytes from the stream"""
108 return changegroup.readexactly(self._fp, size)
109
110 @util.propertycache
111 def params(self):
112 """dictionnary of stream level parameters"""
113 paramsize = self._readexact(2)
114 assert paramsize == '\0\0'
115 return {}
116
117 def __iter__(self):
118 """yield all parts contained in the stream"""
119 # make sure param have been loaded
120 self.params
121 part = self._readpart()
122 while part is not None:
123 yield part
124 part = self._readpart()
125
126 def _readpart(self):
127 """return None when an end of stream markers is reach"""
128 headersize = self._readexact(2)
129 assert headersize == '\0\0'
130 return None
131
132
133