Mercurial > public > mercurial-scm > hg
comparison mercurial/httppeer.py @ 42853:37debb6771f5
httppeer: use context manager when reading temporary bundle to send
Differential Revision: https://phab.mercurial-scm.org/D6784
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Wed, 04 Sep 2019 14:35:39 -0700 |
parents | 58f73e9ccfff |
children | 2372284d9457 |
comparison
equal
deleted
inserted
replaced
42852:58f73e9ccfff | 42853:37debb6771f5 |
---|---|
488 finally: | 488 finally: |
489 fp.close() | 489 fp.close() |
490 os.unlink(tempname) | 490 os.unlink(tempname) |
491 | 491 |
492 def _calltwowaystream(self, cmd, fp, **args): | 492 def _calltwowaystream(self, cmd, fp, **args): |
493 fp_ = None | |
494 filename = None | 493 filename = None |
495 try: | 494 try: |
496 # dump bundle to disk | 495 # dump bundle to disk |
497 fd, filename = pycompat.mkstemp(prefix="hg-bundle-", suffix=".hg") | 496 fd, filename = pycompat.mkstemp(prefix="hg-bundle-", suffix=".hg") |
498 with os.fdopen(fd, r"wb") as fh: | 497 with os.fdopen(fd, r"wb") as fh: |
499 d = fp.read(4096) | 498 d = fp.read(4096) |
500 while d: | 499 while d: |
501 fh.write(d) | 500 fh.write(d) |
502 d = fp.read(4096) | 501 d = fp.read(4096) |
503 # start http push | 502 # start http push |
504 fp_ = httpconnection.httpsendfile(self.ui, filename, "rb") | 503 with httpconnection.httpsendfile(self.ui, filename, "rb") as fp_: |
505 headers = {r'Content-Type': r'application/mercurial-0.1'} | 504 headers = {r'Content-Type': r'application/mercurial-0.1'} |
506 return self._callstream(cmd, data=fp_, headers=headers, **args) | 505 return self._callstream(cmd, data=fp_, headers=headers, **args) |
507 finally: | 506 finally: |
508 if fp_ is not None: | |
509 fp_.close() | |
510 if filename is not None: | 507 if filename is not None: |
511 os.unlink(filename) | 508 os.unlink(filename) |
512 | 509 |
513 def _callcompressable(self, cmd, **args): | 510 def _callcompressable(self, cmd, **args): |
514 return self._callstream(cmd, _compressible=True, **args) | 511 return self._callstream(cmd, _compressible=True, **args) |