windows: add a method to convert Unix style command lines to Windows style
This started as a copy/paste of `os.path.expandvars()`, but limited to a given
dictionary of variables, converting `foo = foo + bar` to `foo += bar`, and
adding 'b' string prefixes. Then code was added to make sure that a value being
substituted in wouldn't itself be expanded by cmd.exe. But that left
inconsistent results between `$var1` and `%var1%` when its value was '%foo%'-
since neither were touched, `$var1` wouldn't expand but `%var1%` would. So
instead, this just converts the Unix style to Windows style (if the variable
exists, because Windows will leave `%missing%` as-is), and lets cmd.exe do its
thing.
I then dropped the %% -> % conversion (because Windows doesn't do this), and
added the ability to escape the '$' with '\'. The escape character is dropped,
for consistency with shell handling.
After everything seemed stable and working, running the whole test suite flagged
a problem near the end of test-bookmarks.t:1069. The problem is cmd.exe won't
pass empty variables to its child, so defined but empty variables are now
skipped. I can't think of anything better, and it seems like a pre-existing
violation of the documentation, which calls out that HG_OLDNODE is empty on
bookmark creation.
Future additions could potentially be replacing strong quotes with double quotes
(cmd.exe doesn't know what to do with the former), escaping a double quote, and
some tilde expansion via os.path.expanduser(). I've got some doubts about
replacing the strong quotes in case sh.exe is run, but it seems like the right
thing to do the vast majority of the time. The original form of this was
discussed about a year ago[1].
[1] https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-July/100735.html
from __future__ import absolute_import, print_function
import sys
from mercurial import (
error,
pycompat,
ui as uimod,
util,
wireprototypes,
wireprotov1peer,
wireprotov1server,
)
from mercurial.utils import (
stringutil,
)
stringio = util.stringio
class proto(object):
def __init__(self, args):
self.args = args
self.name = 'dummyproto'
def getargs(self, spec):
args = self.args
args.setdefault(b'*', {})
names = spec.split()
return [args[n] for n in names]
def checkperm(self, perm):
pass
wireprototypes.TRANSPORTS['dummyproto'] = {
'transport': 'dummy',
'version': 1,
}
class clientpeer(wireprotov1peer.wirepeer):
def __init__(self, serverrepo, ui):
self.serverrepo = serverrepo
self.ui = ui
def url(self):
return b'test'
def local(self):
return None
def peer(self):
return self
def canpush(self):
return True
def close(self):
pass
def capabilities(self):
return [b'batch']
def _call(self, cmd, **args):
args = pycompat.byteskwargs(args)
res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd)
if isinstance(res, wireprototypes.bytesresponse):
return res.data
elif isinstance(res, bytes):
return res
else:
raise error.Abort('dummy client does not support response type')
def _callstream(self, cmd, **args):
return stringio(self._call(cmd, **args))
@wireprotov1peer.batchable
def greet(self, name):
f = wireprotov1peer.future()
yield {b'name': mangle(name)}, f
yield unmangle(f.value)
class serverrepo(object):
def greet(self, name):
return b"Hello, " + name
def filtered(self, name):
return self
def mangle(s):
return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
def unmangle(s):
return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
def greet(repo, proto, name):
return mangle(repo.greet(unmangle(name)))
wireprotov1server.commands[b'greet'] = (greet, b'name')
srv = serverrepo()
clt = clientpeer(srv, uimod.ui())
def printb(data, end=b'\n'):
out = getattr(sys.stdout, 'buffer', sys.stdout)
out.write(data + end)
out.flush()
printb(clt.greet(b"Foobar"))
with clt.commandexecutor() as e:
fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'})
fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'})
printb(stringutil.pprint([f.result() for f in (fgreet1, fgreet2)],
bprefix=True))