##// END OF EJS Templates
bundle2: directly feed part to readbundle...
Pierre-Yves David -
r21020:5041163e default
parent child Browse files
Show More
@@ -1,662 +1,659 b''
1 # bundle2.py - generic container format to transmit arbitrary data.
1 # bundle2.py - generic container format to transmit arbitrary data.
2 #
2 #
3 # Copyright 2013 Facebook, Inc.
3 # Copyright 2013 Facebook, Inc.
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7 """Handling of the new bundle2 format
7 """Handling of the new bundle2 format
8
8
9 The goal of bundle2 is to act as an atomically packet to transmit a set of
9 The goal of bundle2 is to act as an atomically packet to transmit a set of
10 payloads in an application agnostic way. It consist in a sequence of "parts"
10 payloads in an application agnostic way. It consist in a sequence of "parts"
11 that will be handed to and processed by the application layer.
11 that will be handed to and processed by the application layer.
12
12
13
13
14 General format architecture
14 General format architecture
15 ===========================
15 ===========================
16
16
17 The format is architectured as follow
17 The format is architectured as follow
18
18
19 - magic string
19 - magic string
20 - stream level parameters
20 - stream level parameters
21 - payload parts (any number)
21 - payload parts (any number)
22 - end of stream marker.
22 - end of stream marker.
23
23
24 the Binary format
24 the Binary format
25 ============================
25 ============================
26
26
27 All numbers are unsigned and big endian.
27 All numbers are unsigned and big endian.
28
28
29 stream level parameters
29 stream level parameters
30 ------------------------
30 ------------------------
31
31
32 Binary format is as follow
32 Binary format is as follow
33
33
34 :params size: (16 bits integer)
34 :params size: (16 bits integer)
35
35
36 The total number of Bytes used by the parameters
36 The total number of Bytes used by the parameters
37
37
38 :params value: arbitrary number of Bytes
38 :params value: arbitrary number of Bytes
39
39
40 A blob of `params size` containing the serialized version of all stream level
40 A blob of `params size` containing the serialized version of all stream level
41 parameters.
41 parameters.
42
42
43 The blob contains a space separated list of parameters. parameter with value
43 The blob contains a space separated list of parameters. parameter with value
44 are stored in the form `<name>=<value>`. Both name and value are urlquoted.
44 are stored in the form `<name>=<value>`. Both name and value are urlquoted.
45
45
46 Empty name are obviously forbidden.
46 Empty name are obviously forbidden.
47
47
48 Name MUST start with a letter. If this first letter is lower case, the
48 Name MUST start with a letter. If this first letter is lower case, the
49 parameter is advisory and can be safefly ignored. However when the first
49 parameter is advisory and can be safefly ignored. However when the first
50 letter is capital, the parameter is mandatory and the bundling process MUST
50 letter is capital, the parameter is mandatory and the bundling process MUST
51 stop if he is not able to proceed it.
51 stop if he is not able to proceed it.
52
52
53 Stream parameters use a simple textual format for two main reasons:
53 Stream parameters use a simple textual format for two main reasons:
54
54
55 - Stream level parameters should remains simple and we want to discourage any
55 - Stream level parameters should remains simple and we want to discourage any
56 crazy usage.
56 crazy usage.
57 - Textual data allow easy human inspection of a the bundle2 header in case of
57 - Textual data allow easy human inspection of a the bundle2 header in case of
58 troubles.
58 troubles.
59
59
60 Any Applicative level options MUST go into a bundle2 part instead.
60 Any Applicative level options MUST go into a bundle2 part instead.
61
61
62 Payload part
62 Payload part
63 ------------------------
63 ------------------------
64
64
65 Binary format is as follow
65 Binary format is as follow
66
66
67 :header size: (16 bits inter)
67 :header size: (16 bits inter)
68
68
69 The total number of Bytes used by the part headers. When the header is empty
69 The total number of Bytes used by the part headers. When the header is empty
70 (size = 0) this is interpreted as the end of stream marker.
70 (size = 0) this is interpreted as the end of stream marker.
71
71
72 :header:
72 :header:
73
73
74 The header defines how to interpret the part. It contains two piece of
74 The header defines how to interpret the part. It contains two piece of
75 data: the part type, and the part parameters.
75 data: the part type, and the part parameters.
76
76
77 The part type is used to route an application level handler, that can
77 The part type is used to route an application level handler, that can
78 interpret payload.
78 interpret payload.
79
79
80 Part parameters are passed to the application level handler. They are
80 Part parameters are passed to the application level handler. They are
81 meant to convey information that will help the application level object to
81 meant to convey information that will help the application level object to
82 interpret the part payload.
82 interpret the part payload.
83
83
84 The binary format of the header is has follow
84 The binary format of the header is has follow
85
85
86 :typesize: (one byte)
86 :typesize: (one byte)
87
87
88 :typename: alphanumerical part name
88 :typename: alphanumerical part name
89
89
90 :partid: A 32bits integer (unique in the bundle) that can be used to refer
90 :partid: A 32bits integer (unique in the bundle) that can be used to refer
91 to this part.
91 to this part.
92
92
93 :parameters:
93 :parameters:
94
94
95 Part's parameter may have arbitraty content, the binary structure is::
95 Part's parameter may have arbitraty content, the binary structure is::
96
96
97 <mandatory-count><advisory-count><param-sizes><param-data>
97 <mandatory-count><advisory-count><param-sizes><param-data>
98
98
99 :mandatory-count: 1 byte, number of mandatory parameters
99 :mandatory-count: 1 byte, number of mandatory parameters
100
100
101 :advisory-count: 1 byte, number of advisory parameters
101 :advisory-count: 1 byte, number of advisory parameters
102
102
103 :param-sizes:
103 :param-sizes:
104
104
105 N couple of bytes, where N is the total number of parameters. Each
105 N couple of bytes, where N is the total number of parameters. Each
106 couple contains (<size-of-key>, <size-of-value) for one parameter.
106 couple contains (<size-of-key>, <size-of-value) for one parameter.
107
107
108 :param-data:
108 :param-data:
109
109
110 A blob of bytes from which each parameter key and value can be
110 A blob of bytes from which each parameter key and value can be
111 retrieved using the list of size couples stored in the previous
111 retrieved using the list of size couples stored in the previous
112 field.
112 field.
113
113
114 Mandatory parameters comes first, then the advisory ones.
114 Mandatory parameters comes first, then the advisory ones.
115
115
116 :payload:
116 :payload:
117
117
118 payload is a series of `<chunksize><chunkdata>`.
118 payload is a series of `<chunksize><chunkdata>`.
119
119
120 `chunksize` is a 32 bits integer, `chunkdata` are plain bytes (as much as
120 `chunksize` is a 32 bits integer, `chunkdata` are plain bytes (as much as
121 `chunksize` says)` The payload part is concluded by a zero size chunk.
121 `chunksize` says)` The payload part is concluded by a zero size chunk.
122
122
123 The current implementation always produces either zero or one chunk.
123 The current implementation always produces either zero or one chunk.
124 This is an implementation limitation that will ultimatly be lifted.
124 This is an implementation limitation that will ultimatly be lifted.
125
125
126 Bundle processing
126 Bundle processing
127 ============================
127 ============================
128
128
129 Each part is processed in order using a "part handler". Handler are registered
129 Each part is processed in order using a "part handler". Handler are registered
130 for a certain part type.
130 for a certain part type.
131
131
132 The matching of a part to its handler is case insensitive. The case of the
132 The matching of a part to its handler is case insensitive. The case of the
133 part type is used to know if a part is mandatory or advisory. If the Part type
133 part type is used to know if a part is mandatory or advisory. If the Part type
134 contains any uppercase char it is considered mandatory. When no handler is
134 contains any uppercase char it is considered mandatory. When no handler is
135 known for a Mandatory part, the process is aborted and an exception is raised.
135 known for a Mandatory part, the process is aborted and an exception is raised.
136 If the part is advisory and no handler is known, the part is ignored. When the
136 If the part is advisory and no handler is known, the part is ignored. When the
137 process is aborted, the full bundle is still read from the stream to keep the
137 process is aborted, the full bundle is still read from the stream to keep the
138 channel usable. But none of the part read from an abort are processed. In the
138 channel usable. But none of the part read from an abort are processed. In the
139 future, dropping the stream may become an option for channel we do not care to
139 future, dropping the stream may become an option for channel we do not care to
140 preserve.
140 preserve.
141 """
141 """
142
142
143 import util
143 import util
144 import struct
144 import struct
145 import urllib
145 import urllib
146 import string
146 import string
147 import StringIO
148
147
149 import changegroup
148 import changegroup
150 from i18n import _
149 from i18n import _
151
150
152 _pack = struct.pack
151 _pack = struct.pack
153 _unpack = struct.unpack
152 _unpack = struct.unpack
154
153
155 _magicstring = 'HG20'
154 _magicstring = 'HG20'
156
155
157 _fstreamparamsize = '>H'
156 _fstreamparamsize = '>H'
158 _fpartheadersize = '>H'
157 _fpartheadersize = '>H'
159 _fparttypesize = '>B'
158 _fparttypesize = '>B'
160 _fpartid = '>I'
159 _fpartid = '>I'
161 _fpayloadsize = '>I'
160 _fpayloadsize = '>I'
162 _fpartparamcount = '>BB'
161 _fpartparamcount = '>BB'
163
162
164 preferedchunksize = 4096
163 preferedchunksize = 4096
165
164
166 def _makefpartparamsizes(nbparams):
165 def _makefpartparamsizes(nbparams):
167 """return a struct format to read part parameter sizes
166 """return a struct format to read part parameter sizes
168
167
169 The number parameters is variable so we need to build that format
168 The number parameters is variable so we need to build that format
170 dynamically.
169 dynamically.
171 """
170 """
172 return '>'+('BB'*nbparams)
171 return '>'+('BB'*nbparams)
173
172
174 parthandlermapping = {}
173 parthandlermapping = {}
175
174
176 def parthandler(parttype):
175 def parthandler(parttype):
177 """decorator that register a function as a bundle2 part handler
176 """decorator that register a function as a bundle2 part handler
178
177
179 eg::
178 eg::
180
179
181 @parthandler('myparttype')
180 @parthandler('myparttype')
182 def myparttypehandler(...):
181 def myparttypehandler(...):
183 '''process a part of type "my part".'''
182 '''process a part of type "my part".'''
184 ...
183 ...
185 """
184 """
186 def _decorator(func):
185 def _decorator(func):
187 lparttype = parttype.lower() # enforce lower case matching.
186 lparttype = parttype.lower() # enforce lower case matching.
188 assert lparttype not in parthandlermapping
187 assert lparttype not in parthandlermapping
189 parthandlermapping[lparttype] = func
188 parthandlermapping[lparttype] = func
190 return func
189 return func
191 return _decorator
190 return _decorator
192
191
193 class unbundlerecords(object):
192 class unbundlerecords(object):
194 """keep record of what happens during and unbundle
193 """keep record of what happens during and unbundle
195
194
196 New records are added using `records.add('cat', obj)`. Where 'cat' is a
195 New records are added using `records.add('cat', obj)`. Where 'cat' is a
197 category of record and obj is an arbitraty object.
196 category of record and obj is an arbitraty object.
198
197
199 `records['cat']` will return all entries of this category 'cat'.
198 `records['cat']` will return all entries of this category 'cat'.
200
199
201 Iterating on the object itself will yield `('category', obj)` tuples
200 Iterating on the object itself will yield `('category', obj)` tuples
202 for all entries.
201 for all entries.
203
202
204 All iterations happens in chronological order.
203 All iterations happens in chronological order.
205 """
204 """
206
205
207 def __init__(self):
206 def __init__(self):
208 self._categories = {}
207 self._categories = {}
209 self._sequences = []
208 self._sequences = []
210 self._replies = {}
209 self._replies = {}
211
210
212 def add(self, category, entry, inreplyto=None):
211 def add(self, category, entry, inreplyto=None):
213 """add a new record of a given category.
212 """add a new record of a given category.
214
213
215 The entry can then be retrieved in the list returned by
214 The entry can then be retrieved in the list returned by
216 self['category']."""
215 self['category']."""
217 self._categories.setdefault(category, []).append(entry)
216 self._categories.setdefault(category, []).append(entry)
218 self._sequences.append((category, entry))
217 self._sequences.append((category, entry))
219 if inreplyto is not None:
218 if inreplyto is not None:
220 self.getreplies(inreplyto).add(category, entry)
219 self.getreplies(inreplyto).add(category, entry)
221
220
222 def getreplies(self, partid):
221 def getreplies(self, partid):
223 """get the subrecords that replies to a specific part"""
222 """get the subrecords that replies to a specific part"""
224 return self._replies.setdefault(partid, unbundlerecords())
223 return self._replies.setdefault(partid, unbundlerecords())
225
224
226 def __getitem__(self, cat):
225 def __getitem__(self, cat):
227 return tuple(self._categories.get(cat, ()))
226 return tuple(self._categories.get(cat, ()))
228
227
229 def __iter__(self):
228 def __iter__(self):
230 return iter(self._sequences)
229 return iter(self._sequences)
231
230
232 def __len__(self):
231 def __len__(self):
233 return len(self._sequences)
232 return len(self._sequences)
234
233
235 def __nonzero__(self):
234 def __nonzero__(self):
236 return bool(self._sequences)
235 return bool(self._sequences)
237
236
238 class bundleoperation(object):
237 class bundleoperation(object):
239 """an object that represents a single bundling process
238 """an object that represents a single bundling process
240
239
241 Its purpose is to carry unbundle-related objects and states.
240 Its purpose is to carry unbundle-related objects and states.
242
241
243 A new object should be created at the beginning of each bundle processing.
242 A new object should be created at the beginning of each bundle processing.
244 The object is to be returned by the processing function.
243 The object is to be returned by the processing function.
245
244
246 The object has very little content now it will ultimately contain:
245 The object has very little content now it will ultimately contain:
247 * an access to the repo the bundle is applied to,
246 * an access to the repo the bundle is applied to,
248 * a ui object,
247 * a ui object,
249 * a way to retrieve a transaction to add changes to the repo,
248 * a way to retrieve a transaction to add changes to the repo,
250 * a way to record the result of processing each part,
249 * a way to record the result of processing each part,
251 * a way to construct a bundle response when applicable.
250 * a way to construct a bundle response when applicable.
252 """
251 """
253
252
254 def __init__(self, repo, transactiongetter):
253 def __init__(self, repo, transactiongetter):
255 self.repo = repo
254 self.repo = repo
256 self.ui = repo.ui
255 self.ui = repo.ui
257 self.records = unbundlerecords()
256 self.records = unbundlerecords()
258 self.gettransaction = transactiongetter
257 self.gettransaction = transactiongetter
259 self.reply = None
258 self.reply = None
260
259
261 class TransactionUnavailable(RuntimeError):
260 class TransactionUnavailable(RuntimeError):
262 pass
261 pass
263
262
264 def _notransaction():
263 def _notransaction():
265 """default method to get a transaction while processing a bundle
264 """default method to get a transaction while processing a bundle
266
265
267 Raise an exception to highlight the fact that no transaction was expected
266 Raise an exception to highlight the fact that no transaction was expected
268 to be created"""
267 to be created"""
269 raise TransactionUnavailable()
268 raise TransactionUnavailable()
270
269
271 def processbundle(repo, unbundler, transactiongetter=_notransaction):
270 def processbundle(repo, unbundler, transactiongetter=_notransaction):
272 """This function process a bundle, apply effect to/from a repo
271 """This function process a bundle, apply effect to/from a repo
273
272
274 It iterates over each part then searches for and uses the proper handling
273 It iterates over each part then searches for and uses the proper handling
275 code to process the part. Parts are processed in order.
274 code to process the part. Parts are processed in order.
276
275
277 This is very early version of this function that will be strongly reworked
276 This is very early version of this function that will be strongly reworked
278 before final usage.
277 before final usage.
279
278
280 Unknown Mandatory part will abort the process.
279 Unknown Mandatory part will abort the process.
281 """
280 """
282 op = bundleoperation(repo, transactiongetter)
281 op = bundleoperation(repo, transactiongetter)
283 # todo:
282 # todo:
284 # - only create reply bundle if requested.
283 # - only create reply bundle if requested.
285 op.reply = bundle20(op.ui)
284 op.reply = bundle20(op.ui)
286 # todo:
285 # todo:
287 # - replace this is a init function soon.
286 # - replace this is a init function soon.
288 # - exception catching
287 # - exception catching
289 unbundler.params
288 unbundler.params
290 iterparts = iter(unbundler)
289 iterparts = iter(unbundler)
291 part = None
290 part = None
292 try:
291 try:
293 for part in iterparts:
292 for part in iterparts:
294 parttype = part.type
293 parttype = part.type
295 # part key are matched lower case
294 # part key are matched lower case
296 key = parttype.lower()
295 key = parttype.lower()
297 try:
296 try:
298 handler = parthandlermapping[key]
297 handler = parthandlermapping[key]
299 op.ui.debug('found a handler for part %r\n' % parttype)
298 op.ui.debug('found a handler for part %r\n' % parttype)
300 except KeyError:
299 except KeyError:
301 if key != parttype: # mandatory parts
300 if key != parttype: # mandatory parts
302 # todo:
301 # todo:
303 # - use a more precise exception
302 # - use a more precise exception
304 raise
303 raise
305 op.ui.debug('ignoring unknown advisory part %r\n' % key)
304 op.ui.debug('ignoring unknown advisory part %r\n' % key)
306 # consuming the part
305 # consuming the part
307 part.read()
306 part.read()
308 continue
307 continue
309
308
310 # handler is called outside the above try block so that we don't
309 # handler is called outside the above try block so that we don't
311 # risk catching KeyErrors from anything other than the
310 # risk catching KeyErrors from anything other than the
312 # parthandlermapping lookup (any KeyError raised by handler()
311 # parthandlermapping lookup (any KeyError raised by handler()
313 # itself represents a defect of a different variety).
312 # itself represents a defect of a different variety).
314 handler(op, part)
313 handler(op, part)
315 part.read()
314 part.read()
316 except Exception:
315 except Exception:
317 if part is not None:
316 if part is not None:
318 # consume the bundle content
317 # consume the bundle content
319 part.read()
318 part.read()
320 for part in iterparts:
319 for part in iterparts:
321 # consume the bundle content
320 # consume the bundle content
322 part.read()
321 part.read()
323 raise
322 raise
324 return op
323 return op
325
324
326 class bundle20(object):
325 class bundle20(object):
327 """represent an outgoing bundle2 container
326 """represent an outgoing bundle2 container
328
327
329 Use the `addparam` method to add stream level parameter. and `addpart` to
328 Use the `addparam` method to add stream level parameter. and `addpart` to
330 populate it. Then call `getchunks` to retrieve all the binary chunks of
329 populate it. Then call `getchunks` to retrieve all the binary chunks of
331 datathat compose the bundle2 container."""
330 datathat compose the bundle2 container."""
332
331
333 def __init__(self, ui):
332 def __init__(self, ui):
334 self.ui = ui
333 self.ui = ui
335 self._params = []
334 self._params = []
336 self._parts = []
335 self._parts = []
337
336
338 def addparam(self, name, value=None):
337 def addparam(self, name, value=None):
339 """add a stream level parameter"""
338 """add a stream level parameter"""
340 if not name:
339 if not name:
341 raise ValueError('empty parameter name')
340 raise ValueError('empty parameter name')
342 if name[0] not in string.letters:
341 if name[0] not in string.letters:
343 raise ValueError('non letter first character: %r' % name)
342 raise ValueError('non letter first character: %r' % name)
344 self._params.append((name, value))
343 self._params.append((name, value))
345
344
346 def addpart(self, part):
345 def addpart(self, part):
347 """add a new part to the bundle2 container
346 """add a new part to the bundle2 container
348
347
349 Parts contains the actuall applicative payload."""
348 Parts contains the actuall applicative payload."""
350 assert part.id is None
349 assert part.id is None
351 part.id = len(self._parts) # very cheap counter
350 part.id = len(self._parts) # very cheap counter
352 self._parts.append(part)
351 self._parts.append(part)
353
352
354 def getchunks(self):
353 def getchunks(self):
355 self.ui.debug('start emission of %s stream\n' % _magicstring)
354 self.ui.debug('start emission of %s stream\n' % _magicstring)
356 yield _magicstring
355 yield _magicstring
357 param = self._paramchunk()
356 param = self._paramchunk()
358 self.ui.debug('bundle parameter: %s\n' % param)
357 self.ui.debug('bundle parameter: %s\n' % param)
359 yield _pack(_fstreamparamsize, len(param))
358 yield _pack(_fstreamparamsize, len(param))
360 if param:
359 if param:
361 yield param
360 yield param
362
361
363 self.ui.debug('start of parts\n')
362 self.ui.debug('start of parts\n')
364 for part in self._parts:
363 for part in self._parts:
365 self.ui.debug('bundle part: "%s"\n' % part.type)
364 self.ui.debug('bundle part: "%s"\n' % part.type)
366 for chunk in part.getchunks():
365 for chunk in part.getchunks():
367 yield chunk
366 yield chunk
368 self.ui.debug('end of bundle\n')
367 self.ui.debug('end of bundle\n')
369 yield '\0\0'
368 yield '\0\0'
370
369
371 def _paramchunk(self):
370 def _paramchunk(self):
372 """return a encoded version of all stream parameters"""
371 """return a encoded version of all stream parameters"""
373 blocks = []
372 blocks = []
374 for par, value in self._params:
373 for par, value in self._params:
375 par = urllib.quote(par)
374 par = urllib.quote(par)
376 if value is not None:
375 if value is not None:
377 value = urllib.quote(value)
376 value = urllib.quote(value)
378 par = '%s=%s' % (par, value)
377 par = '%s=%s' % (par, value)
379 blocks.append(par)
378 blocks.append(par)
380 return ' '.join(blocks)
379 return ' '.join(blocks)
381
380
382 class unpackermixin(object):
381 class unpackermixin(object):
383 """A mixin to extract bytes and struct data from a stream"""
382 """A mixin to extract bytes and struct data from a stream"""
384
383
385 def __init__(self, fp):
384 def __init__(self, fp):
386 self._fp = fp
385 self._fp = fp
387
386
388 def _unpack(self, format):
387 def _unpack(self, format):
389 """unpack this struct format from the stream"""
388 """unpack this struct format from the stream"""
390 data = self._readexact(struct.calcsize(format))
389 data = self._readexact(struct.calcsize(format))
391 return _unpack(format, data)
390 return _unpack(format, data)
392
391
393 def _readexact(self, size):
392 def _readexact(self, size):
394 """read exactly <size> bytes from the stream"""
393 """read exactly <size> bytes from the stream"""
395 return changegroup.readexactly(self._fp, size)
394 return changegroup.readexactly(self._fp, size)
396
395
397
396
398 class unbundle20(unpackermixin):
397 class unbundle20(unpackermixin):
399 """interpret a bundle2 stream
398 """interpret a bundle2 stream
400
399
401 (this will eventually yield parts)"""
400 (this will eventually yield parts)"""
402
401
403 def __init__(self, ui, fp):
402 def __init__(self, ui, fp):
404 self.ui = ui
403 self.ui = ui
405 super(unbundle20, self).__init__(fp)
404 super(unbundle20, self).__init__(fp)
406 header = self._readexact(4)
405 header = self._readexact(4)
407 magic, version = header[0:2], header[2:4]
406 magic, version = header[0:2], header[2:4]
408 if magic != 'HG':
407 if magic != 'HG':
409 raise util.Abort(_('not a Mercurial bundle'))
408 raise util.Abort(_('not a Mercurial bundle'))
410 if version != '20':
409 if version != '20':
411 raise util.Abort(_('unknown bundle version %s') % version)
410 raise util.Abort(_('unknown bundle version %s') % version)
412 self.ui.debug('start processing of %s stream\n' % header)
411 self.ui.debug('start processing of %s stream\n' % header)
413
412
414 @util.propertycache
413 @util.propertycache
415 def params(self):
414 def params(self):
416 """dictionnary of stream level parameters"""
415 """dictionnary of stream level parameters"""
417 self.ui.debug('reading bundle2 stream parameters\n')
416 self.ui.debug('reading bundle2 stream parameters\n')
418 params = {}
417 params = {}
419 paramssize = self._unpack(_fstreamparamsize)[0]
418 paramssize = self._unpack(_fstreamparamsize)[0]
420 if paramssize:
419 if paramssize:
421 for p in self._readexact(paramssize).split(' '):
420 for p in self._readexact(paramssize).split(' '):
422 p = p.split('=', 1)
421 p = p.split('=', 1)
423 p = [urllib.unquote(i) for i in p]
422 p = [urllib.unquote(i) for i in p]
424 if len(p) < 2:
423 if len(p) < 2:
425 p.append(None)
424 p.append(None)
426 self._processparam(*p)
425 self._processparam(*p)
427 params[p[0]] = p[1]
426 params[p[0]] = p[1]
428 return params
427 return params
429
428
430 def _processparam(self, name, value):
429 def _processparam(self, name, value):
431 """process a parameter, applying its effect if needed
430 """process a parameter, applying its effect if needed
432
431
433 Parameter starting with a lower case letter are advisory and will be
432 Parameter starting with a lower case letter are advisory and will be
434 ignored when unknown. Those starting with an upper case letter are
433 ignored when unknown. Those starting with an upper case letter are
435 mandatory and will this function will raise a KeyError when unknown.
434 mandatory and will this function will raise a KeyError when unknown.
436
435
437 Note: no option are currently supported. Any input will be either
436 Note: no option are currently supported. Any input will be either
438 ignored or failing.
437 ignored or failing.
439 """
438 """
440 if not name:
439 if not name:
441 raise ValueError('empty parameter name')
440 raise ValueError('empty parameter name')
442 if name[0] not in string.letters:
441 if name[0] not in string.letters:
443 raise ValueError('non letter first character: %r' % name)
442 raise ValueError('non letter first character: %r' % name)
444 # Some logic will be later added here to try to process the option for
443 # Some logic will be later added here to try to process the option for
445 # a dict of known parameter.
444 # a dict of known parameter.
446 if name[0].islower():
445 if name[0].islower():
447 self.ui.debug("ignoring unknown parameter %r\n" % name)
446 self.ui.debug("ignoring unknown parameter %r\n" % name)
448 else:
447 else:
449 raise KeyError(name)
448 raise KeyError(name)
450
449
451
450
452 def __iter__(self):
451 def __iter__(self):
453 """yield all parts contained in the stream"""
452 """yield all parts contained in the stream"""
454 # make sure param have been loaded
453 # make sure param have been loaded
455 self.params
454 self.params
456 self.ui.debug('start extraction of bundle2 parts\n')
455 self.ui.debug('start extraction of bundle2 parts\n')
457 headerblock = self._readpartheader()
456 headerblock = self._readpartheader()
458 while headerblock is not None:
457 while headerblock is not None:
459 part = unbundlepart(self.ui, headerblock, self._fp)
458 part = unbundlepart(self.ui, headerblock, self._fp)
460 yield part
459 yield part
461 headerblock = self._readpartheader()
460 headerblock = self._readpartheader()
462 self.ui.debug('end of bundle2 stream\n')
461 self.ui.debug('end of bundle2 stream\n')
463
462
464 def _readpartheader(self):
463 def _readpartheader(self):
465 """reads a part header size and return the bytes blob
464 """reads a part header size and return the bytes blob
466
465
467 returns None if empty"""
466 returns None if empty"""
468 headersize = self._unpack(_fpartheadersize)[0]
467 headersize = self._unpack(_fpartheadersize)[0]
469 self.ui.debug('part header size: %i\n' % headersize)
468 self.ui.debug('part header size: %i\n' % headersize)
470 if headersize:
469 if headersize:
471 return self._readexact(headersize)
470 return self._readexact(headersize)
472 return None
471 return None
473
472
474
473
475 class bundlepart(object):
474 class bundlepart(object):
476 """A bundle2 part contains application level payload
475 """A bundle2 part contains application level payload
477
476
478 The part `type` is used to route the part to the application level
477 The part `type` is used to route the part to the application level
479 handler.
478 handler.
480 """
479 """
481
480
482 def __init__(self, parttype, mandatoryparams=(), advisoryparams=(),
481 def __init__(self, parttype, mandatoryparams=(), advisoryparams=(),
483 data=''):
482 data=''):
484 self.id = None
483 self.id = None
485 self.type = parttype
484 self.type = parttype
486 self.data = data
485 self.data = data
487 self.mandatoryparams = mandatoryparams
486 self.mandatoryparams = mandatoryparams
488 self.advisoryparams = advisoryparams
487 self.advisoryparams = advisoryparams
489
488
490 def getchunks(self):
489 def getchunks(self):
491 #### header
490 #### header
492 ## parttype
491 ## parttype
493 header = [_pack(_fparttypesize, len(self.type)),
492 header = [_pack(_fparttypesize, len(self.type)),
494 self.type, _pack(_fpartid, self.id),
493 self.type, _pack(_fpartid, self.id),
495 ]
494 ]
496 ## parameters
495 ## parameters
497 # count
496 # count
498 manpar = self.mandatoryparams
497 manpar = self.mandatoryparams
499 advpar = self.advisoryparams
498 advpar = self.advisoryparams
500 header.append(_pack(_fpartparamcount, len(manpar), len(advpar)))
499 header.append(_pack(_fpartparamcount, len(manpar), len(advpar)))
501 # size
500 # size
502 parsizes = []
501 parsizes = []
503 for key, value in manpar:
502 for key, value in manpar:
504 parsizes.append(len(key))
503 parsizes.append(len(key))
505 parsizes.append(len(value))
504 parsizes.append(len(value))
506 for key, value in advpar:
505 for key, value in advpar:
507 parsizes.append(len(key))
506 parsizes.append(len(key))
508 parsizes.append(len(value))
507 parsizes.append(len(value))
509 paramsizes = _pack(_makefpartparamsizes(len(parsizes) / 2), *parsizes)
508 paramsizes = _pack(_makefpartparamsizes(len(parsizes) / 2), *parsizes)
510 header.append(paramsizes)
509 header.append(paramsizes)
511 # key, value
510 # key, value
512 for key, value in manpar:
511 for key, value in manpar:
513 header.append(key)
512 header.append(key)
514 header.append(value)
513 header.append(value)
515 for key, value in advpar:
514 for key, value in advpar:
516 header.append(key)
515 header.append(key)
517 header.append(value)
516 header.append(value)
518 ## finalize header
517 ## finalize header
519 headerchunk = ''.join(header)
518 headerchunk = ''.join(header)
520 yield _pack(_fpartheadersize, len(headerchunk))
519 yield _pack(_fpartheadersize, len(headerchunk))
521 yield headerchunk
520 yield headerchunk
522 ## payload
521 ## payload
523 for chunk in self._payloadchunks():
522 for chunk in self._payloadchunks():
524 yield _pack(_fpayloadsize, len(chunk))
523 yield _pack(_fpayloadsize, len(chunk))
525 yield chunk
524 yield chunk
526 # end of payload
525 # end of payload
527 yield _pack(_fpayloadsize, 0)
526 yield _pack(_fpayloadsize, 0)
528
527
529 def _payloadchunks(self):
528 def _payloadchunks(self):
530 """yield chunks of a the part payload
529 """yield chunks of a the part payload
531
530
532 Exists to handle the different methods to provide data to a part."""
531 Exists to handle the different methods to provide data to a part."""
533 # we only support fixed size data now.
532 # we only support fixed size data now.
534 # This will be improved in the future.
533 # This will be improved in the future.
535 if util.safehasattr(self.data, 'next'):
534 if util.safehasattr(self.data, 'next'):
536 buff = util.chunkbuffer(self.data)
535 buff = util.chunkbuffer(self.data)
537 chunk = buff.read(preferedchunksize)
536 chunk = buff.read(preferedchunksize)
538 while chunk:
537 while chunk:
539 yield chunk
538 yield chunk
540 chunk = buff.read(preferedchunksize)
539 chunk = buff.read(preferedchunksize)
541 elif len(self.data):
540 elif len(self.data):
542 yield self.data
541 yield self.data
543
542
544 class unbundlepart(unpackermixin):
543 class unbundlepart(unpackermixin):
545 """a bundle part read from a bundle"""
544 """a bundle part read from a bundle"""
546
545
547 def __init__(self, ui, header, fp):
546 def __init__(self, ui, header, fp):
548 super(unbundlepart, self).__init__(fp)
547 super(unbundlepart, self).__init__(fp)
549 self.ui = ui
548 self.ui = ui
550 # unbundle state attr
549 # unbundle state attr
551 self._headerdata = header
550 self._headerdata = header
552 self._headeroffset = 0
551 self._headeroffset = 0
553 self._initialized = False
552 self._initialized = False
554 self.consumed = False
553 self.consumed = False
555 # part data
554 # part data
556 self.id = None
555 self.id = None
557 self.type = None
556 self.type = None
558 self.mandatoryparams = None
557 self.mandatoryparams = None
559 self.advisoryparams = None
558 self.advisoryparams = None
560 self._payloadstream = None
559 self._payloadstream = None
561 self._readheader()
560 self._readheader()
562
561
563 def _fromheader(self, size):
562 def _fromheader(self, size):
564 """return the next <size> byte from the header"""
563 """return the next <size> byte from the header"""
565 offset = self._headeroffset
564 offset = self._headeroffset
566 data = self._headerdata[offset:(offset + size)]
565 data = self._headerdata[offset:(offset + size)]
567 self._headeroffset = offset + size
566 self._headeroffset = offset + size
568 return data
567 return data
569
568
570 def _unpackheader(self, format):
569 def _unpackheader(self, format):
571 """read given format from header
570 """read given format from header
572
571
573 This automatically compute the size of the format to read."""
572 This automatically compute the size of the format to read."""
574 data = self._fromheader(struct.calcsize(format))
573 data = self._fromheader(struct.calcsize(format))
575 return _unpack(format, data)
574 return _unpack(format, data)
576
575
577 def _readheader(self):
576 def _readheader(self):
578 """read the header and setup the object"""
577 """read the header and setup the object"""
579 typesize = self._unpackheader(_fparttypesize)[0]
578 typesize = self._unpackheader(_fparttypesize)[0]
580 self.type = self._fromheader(typesize)
579 self.type = self._fromheader(typesize)
581 self.ui.debug('part type: "%s"\n' % self.type)
580 self.ui.debug('part type: "%s"\n' % self.type)
582 self.id = self._unpackheader(_fpartid)[0]
581 self.id = self._unpackheader(_fpartid)[0]
583 self.ui.debug('part id: "%s"\n' % self.id)
582 self.ui.debug('part id: "%s"\n' % self.id)
584 ## reading parameters
583 ## reading parameters
585 # param count
584 # param count
586 mancount, advcount = self._unpackheader(_fpartparamcount)
585 mancount, advcount = self._unpackheader(_fpartparamcount)
587 self.ui.debug('part parameters: %i\n' % (mancount + advcount))
586 self.ui.debug('part parameters: %i\n' % (mancount + advcount))
588 # param size
587 # param size
589 fparamsizes = _makefpartparamsizes(mancount + advcount)
588 fparamsizes = _makefpartparamsizes(mancount + advcount)
590 paramsizes = self._unpackheader(fparamsizes)
589 paramsizes = self._unpackheader(fparamsizes)
591 # make it a list of couple again
590 # make it a list of couple again
592 paramsizes = zip(paramsizes[::2], paramsizes[1::2])
591 paramsizes = zip(paramsizes[::2], paramsizes[1::2])
593 # split mandatory from advisory
592 # split mandatory from advisory
594 mansizes = paramsizes[:mancount]
593 mansizes = paramsizes[:mancount]
595 advsizes = paramsizes[mancount:]
594 advsizes = paramsizes[mancount:]
596 # retrive param value
595 # retrive param value
597 manparams = []
596 manparams = []
598 for key, value in mansizes:
597 for key, value in mansizes:
599 manparams.append((self._fromheader(key), self._fromheader(value)))
598 manparams.append((self._fromheader(key), self._fromheader(value)))
600 advparams = []
599 advparams = []
601 for key, value in advsizes:
600 for key, value in advsizes:
602 advparams.append((self._fromheader(key), self._fromheader(value)))
601 advparams.append((self._fromheader(key), self._fromheader(value)))
603 self.mandatoryparams = manparams
602 self.mandatoryparams = manparams
604 self.advisoryparams = advparams
603 self.advisoryparams = advparams
605 ## part payload
604 ## part payload
606 def payloadchunks():
605 def payloadchunks():
607 payloadsize = self._unpack(_fpayloadsize)[0]
606 payloadsize = self._unpack(_fpayloadsize)[0]
608 self.ui.debug('payload chunk size: %i\n' % payloadsize)
607 self.ui.debug('payload chunk size: %i\n' % payloadsize)
609 while payloadsize:
608 while payloadsize:
610 yield self._readexact(payloadsize)
609 yield self._readexact(payloadsize)
611 payloadsize = self._unpack(_fpayloadsize)[0]
610 payloadsize = self._unpack(_fpayloadsize)[0]
612 self.ui.debug('payload chunk size: %i\n' % payloadsize)
611 self.ui.debug('payload chunk size: %i\n' % payloadsize)
613 self._payloadstream = util.chunkbuffer(payloadchunks())
612 self._payloadstream = util.chunkbuffer(payloadchunks())
614 # we read the data, tell it
613 # we read the data, tell it
615 self._initialized = True
614 self._initialized = True
616
615
617 def read(self, size=None):
616 def read(self, size=None):
618 """read payload data"""
617 """read payload data"""
619 if not self._initialized:
618 if not self._initialized:
620 self._readheader()
619 self._readheader()
621 if size is None:
620 if size is None:
622 data = self._payloadstream.read()
621 data = self._payloadstream.read()
623 else:
622 else:
624 data = self._payloadstream.read(size)
623 data = self._payloadstream.read(size)
625 if size is None or len(data) < size:
624 if size is None or len(data) < size:
626 self.consumed = True
625 self.consumed = True
627 return data
626 return data
628
627
629
628
630 @parthandler('changegroup')
629 @parthandler('changegroup')
631 def handlechangegroup(op, inpart):
630 def handlechangegroup(op, inpart):
632 """apply a changegroup part on the repo
631 """apply a changegroup part on the repo
633
632
634 This is a very early implementation that will massive rework before being
633 This is a very early implementation that will massive rework before being
635 inflicted to any end-user.
634 inflicted to any end-user.
636 """
635 """
637 # Make sure we trigger a transaction creation
636 # Make sure we trigger a transaction creation
638 #
637 #
639 # The addchangegroup function will get a transaction object by itself, but
638 # The addchangegroup function will get a transaction object by itself, but
640 # we need to make sure we trigger the creation of a transaction object used
639 # we need to make sure we trigger the creation of a transaction object used
641 # for the whole processing scope.
640 # for the whole processing scope.
642 op.gettransaction()
641 op.gettransaction()
643 data = StringIO.StringIO(inpart.read())
642 cg = changegroup.readbundle(inpart, 'bundle2part')
644 data.seek(0)
645 cg = changegroup.readbundle(data, 'bundle2part')
646 ret = changegroup.addchangegroup(op.repo, cg, 'bundle2', 'bundle2')
643 ret = changegroup.addchangegroup(op.repo, cg, 'bundle2', 'bundle2')
647 op.records.add('changegroup', {'return': ret})
644 op.records.add('changegroup', {'return': ret})
648 if op.reply is not None:
645 if op.reply is not None:
649 # This is definitly not the final form of this
646 # This is definitly not the final form of this
650 # return. But one need to start somewhere.
647 # return. But one need to start somewhere.
651 part = bundlepart('reply:changegroup', (),
648 part = bundlepart('reply:changegroup', (),
652 [('in-reply-to', str(inpart.id)),
649 [('in-reply-to', str(inpart.id)),
653 ('return', '%i' % ret)])
650 ('return', '%i' % ret)])
654 op.reply.addpart(part)
651 op.reply.addpart(part)
655 assert not inpart.read()
652 assert not inpart.read()
656
653
657 @parthandler('reply:changegroup')
654 @parthandler('reply:changegroup')
658 def handlechangegroup(op, inpart):
655 def handlechangegroup(op, inpart):
659 p = dict(inpart.advisoryparams)
656 p = dict(inpart.advisoryparams)
660 ret = int(p['return'])
657 ret = int(p['return'])
661 op.records.add('changegroup', {'return': ret}, int(p['in-reply-to']))
658 op.records.add('changegroup', {'return': ret}, int(p['in-reply-to']))
662
659
General Comments 0
You need to be logged in to leave comments. Login now