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