comparison mercurial/bundle2.py @ 21013:a813caca89b3

bundle2: extract stream/unpack logic in an unpackermixin The coming `unbundlepart` will need the same kind of method than `unbundle20` for unpacking data from the stream. We extract them into a mixin class before the creation of `unbundlepart`.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Fri, 11 Apr 2014 15:19:54 -0400
parents 3d38ebb586fe
children a6246bba7b9e
comparison
equal deleted inserted replaced
21012:c827a0028e6f 21013:a813caca89b3
371 value = urllib.quote(value) 371 value = urllib.quote(value)
372 par = '%s=%s' % (par, value) 372 par = '%s=%s' % (par, value)
373 blocks.append(par) 373 blocks.append(par)
374 return ' '.join(blocks) 374 return ' '.join(blocks)
375 375
376 class unbundle20(object): 376 class unpackermixin(object):
377 """A mixin to extract bytes and struct data from a stream"""
378
379 def __init__(self, fp):
380 self._fp = fp
381
382 def _unpack(self, format):
383 """unpack this struct format from the stream"""
384 data = self._readexact(struct.calcsize(format))
385 return _unpack(format, data)
386
387 def _readexact(self, size):
388 """read exactly <size> bytes from the stream"""
389 return changegroup.readexactly(self._fp, size)
390
391
392 class unbundle20(unpackermixin):
377 """interpret a bundle2 stream 393 """interpret a bundle2 stream
378 394
379 (this will eventually yield parts)""" 395 (this will eventually yield parts)"""
380 396
381 def __init__(self, ui, fp): 397 def __init__(self, ui, fp):
382 self.ui = ui 398 self.ui = ui
383 self._fp = fp 399 super(unbundle20, self).__init__(fp)
384 header = self._readexact(4) 400 header = self._readexact(4)
385 magic, version = header[0:2], header[2:4] 401 magic, version = header[0:2], header[2:4]
386 if magic != 'HG': 402 if magic != 'HG':
387 raise util.Abort(_('not a Mercurial bundle')) 403 raise util.Abort(_('not a Mercurial bundle'))
388 if version != '20': 404 if version != '20':
389 raise util.Abort(_('unknown bundle version %s') % version) 405 raise util.Abort(_('unknown bundle version %s') % version)
390 self.ui.debug('start processing of %s stream\n' % header) 406 self.ui.debug('start processing of %s stream\n' % header)
391
392 def _unpack(self, format):
393 """unpack this struct format from the stream"""
394 data = self._readexact(struct.calcsize(format))
395 return _unpack(format, data)
396
397 def _readexact(self, size):
398 """read exactly <size> bytes from the stream"""
399 return changegroup.readexactly(self._fp, size)
400 407
401 @util.propertycache 408 @util.propertycache
402 def params(self): 409 def params(self):
403 """dictionnary of stream level parameters""" 410 """dictionnary of stream level parameters"""
404 self.ui.debug('reading bundle2 stream parameters\n') 411 self.ui.debug('reading bundle2 stream parameters\n')