Mercurial > hg
comparison mercurial/bundle2.py @ 35046:241d9caca11e
bundle2: use os.SEEK_* constants
Constants make code easier to read than magic numbers.
I also threw in an explicit argument in a caller to further
increase code comprehension.
Differential Revision: https://phab.mercurial-scm.org/D1370
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 11 Nov 2017 16:48:40 -0800 |
parents | a95067b1dca6 |
children | 073eec083e25 |
comparison
equal
deleted
inserted
replaced
35045:b0262b25ab48 | 35046:241d9caca11e |
---|---|
146 """ | 146 """ |
147 | 147 |
148 from __future__ import absolute_import, division | 148 from __future__ import absolute_import, division |
149 | 149 |
150 import errno | 150 import errno |
151 import os | |
151 import re | 152 import re |
152 import string | 153 import string |
153 import struct | 154 import struct |
154 import sys | 155 import sys |
155 | 156 |
360 itr = enumerate(self.unbundler.iterparts()) | 361 itr = enumerate(self.unbundler.iterparts()) |
361 for count, p in itr: | 362 for count, p in itr: |
362 self.count = count | 363 self.count = count |
363 self.current = p | 364 self.current = p |
364 yield p | 365 yield p |
365 p.seek(0, 2) | 366 p.seek(0, os.SEEK_END) |
366 self.current = None | 367 self.current = None |
367 self.iterator = func() | 368 self.iterator = func() |
368 return self.iterator | 369 return self.iterator |
369 | 370 |
370 def __exit__(self, type, exc, tb): | 371 def __exit__(self, type, exc, tb): |
382 # re-raise the original error. | 383 # re-raise the original error. |
383 seekerror = False | 384 seekerror = False |
384 try: | 385 try: |
385 if self.current: | 386 if self.current: |
386 # consume the part content to not corrupt the stream. | 387 # consume the part content to not corrupt the stream. |
387 self.current.seek(0, 2) | 388 self.current.seek(0, os.SEEK_END) |
388 | 389 |
389 for part in self.iterator: | 390 for part in self.iterator: |
390 # consume the bundle content | 391 # consume the bundle content |
391 part.seek(0, 2) | 392 part.seek(0, os.SEEK_END) |
392 except Exception: | 393 except Exception: |
393 seekerror = True | 394 seekerror = True |
394 | 395 |
395 # Small hack to let caller code distinguish exceptions from bundle2 | 396 # Small hack to let caller code distinguish exceptions from bundle2 |
396 # processing from processing the old format. This is mostly needed | 397 # processing from processing the old format. This is mostly needed |
856 part = unbundlepart(self.ui, headerblock, self._fp) | 857 part = unbundlepart(self.ui, headerblock, self._fp) |
857 yield part | 858 yield part |
858 # Seek to the end of the part to force it's consumption so the next | 859 # Seek to the end of the part to force it's consumption so the next |
859 # part can be read. But then seek back to the beginning so the | 860 # part can be read. But then seek back to the beginning so the |
860 # code consuming this generator has a part that starts at 0. | 861 # code consuming this generator has a part that starts at 0. |
861 part.seek(0, 2) | 862 part.seek(0, os.SEEK_END) |
862 part.seek(0) | 863 part.seek(0, os.SEEK_SET) |
863 headerblock = self._readpartheader() | 864 headerblock = self._readpartheader() |
864 indebug(self.ui, 'end of bundle2 stream') | 865 indebug(self.ui, 'end of bundle2 stream') |
865 | 866 |
866 def _readpartheader(self): | 867 def _readpartheader(self): |
867 """reads a part header size and return the bytes blob | 868 """reads a part header size and return the bytes blob |
1162 except (SystemExit, KeyboardInterrupt): | 1163 except (SystemExit, KeyboardInterrupt): |
1163 hardabort = True | 1164 hardabort = True |
1164 raise | 1165 raise |
1165 finally: | 1166 finally: |
1166 if not hardabort: | 1167 if not hardabort: |
1167 part.seek(0, 2) | 1168 part.seek(0, os.SEEK_END) |
1168 self.ui.debug('bundle2-input-stream-interrupt:' | 1169 self.ui.debug('bundle2-input-stream-interrupt:' |
1169 ' closing out of band context\n') | 1170 ' closing out of band context\n') |
1170 | 1171 |
1171 class interruptoperation(object): | 1172 class interruptoperation(object): |
1172 """A limited operation to be use by part handler during interruption | 1173 """A limited operation to be use by part handler during interruption |
1328 return data | 1329 return data |
1329 | 1330 |
1330 def tell(self): | 1331 def tell(self): |
1331 return self._pos | 1332 return self._pos |
1332 | 1333 |
1333 def seek(self, offset, whence=0): | 1334 def seek(self, offset, whence=os.SEEK_SET): |
1334 if whence == 0: | 1335 if whence == os.SEEK_SET: |
1335 newpos = offset | 1336 newpos = offset |
1336 elif whence == 1: | 1337 elif whence == os.SEEK_CUR: |
1337 newpos = self._pos + offset | 1338 newpos = self._pos + offset |
1338 elif whence == 2: | 1339 elif whence == os.SEEK_END: |
1339 if not self.consumed: | 1340 if not self.consumed: |
1340 self.read() | 1341 self.read() |
1341 newpos = self._chunkindex[-1][0] - offset | 1342 newpos = self._chunkindex[-1][0] - offset |
1342 else: | 1343 else: |
1343 raise ValueError('Unknown whence value: %r' % (whence,)) | 1344 raise ValueError('Unknown whence value: %r' % (whence,)) |