comparison mercurial/wireproto.py @ 36066:2ad145fbde54

wireprotoserver: add context manager mechanism for redirecting stdio Today, proto.redirect() sets up redirecting stdio and proto.restore() undoes that. The API is a bit wonky because restore() is only implemented on the HTTP protocol. Furthermore, not all calls to redirect() are obviously paired with calls to restore(). For example, the call to restore() for "unbundle" requests is handled by the response handler for the HTTP protocol. This commit introduces a new method on the protocol handler interface to maybe capture stdio. It emits a file object or None depending on whether stdio capture is used by the transport. To prove it works, the "pushkey" wire protocol command has been updated to use the new API. I'm not convinced this is the best mechanism to capture stdio. I may need to come up with something better once the new wire protocol emerges into existence. But it is strictly better than before because it removes variance in the wire protocol handler interface. It therefore gets us closer to a unified interface between the SSH and HTTP transports. Differential Revision: https://phab.mercurial-scm.org/D2081
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 07 Feb 2018 20:17:47 -0800
parents 5a56bf4180ad
children caca3ac2ac04
comparison
equal deleted inserted replaced
36065:bf676267f64f 36066:2ad145fbde54
976 except UnicodeDecodeError: 976 except UnicodeDecodeError:
977 pass # binary, leave unmodified 977 pass # binary, leave unmodified
978 else: 978 else:
979 new = encoding.tolocal(new) # normal path 979 new = encoding.tolocal(new) # normal path
980 980
981 if util.safehasattr(proto, 'restore'): 981 with proto.mayberedirectstdio() as output:
982
983 proto.redirect()
984
985 r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key), 982 r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key),
986 encoding.tolocal(old), new) or False 983 encoding.tolocal(old), new) or False
987 984
988 output = proto.restore() 985 output = output.getvalue() if output else ''
989 986 return '%s\n%s' % (int(r), output)
990 return '%s\n%s' % (int(r), output)
991
992 r = repo.pushkey(encoding.tolocal(namespace), encoding.tolocal(key),
993 encoding.tolocal(old), new)
994 return '%s\n' % int(r)
995 987
996 @wireprotocommand('stream_out') 988 @wireprotocommand('stream_out')
997 def stream(repo, proto): 989 def stream(repo, proto):
998 '''If the server supports streaming clone, it advertises the "stream" 990 '''If the server supports streaming clone, it advertises the "stream"
999 capability with a value representing the version and flags of the repo 991 capability with a value representing the version and flags of the repo