def hg(stdin: BinaryIO, stdout: BinaryIO, stderr: BinaryIO, env: Env) -> int: """run hg commands in-process requires edenscm modules - run from "hg debugpython", not vanilla python """ # debugpython won't work - emulate Py_Main instead if env.args[1:3] == ["debugpython", "--"]: env.args = [env.args[0]] + env.args[3:] args = env.args[1:] return python(args, stdin, stdout, stderr, env) import bindings from edenscm.mercurial import encoding, extensions, pycompat, util # emulate ui.system via sheval rawsystem = partial(_rawsystem, env, stdin, stdout, stderr) origstdio = (pycompat.stdin, pycompat.stdout, pycompat.stderr) try: with shellenv(env, stdin=stdin, stdout=stdout, stderr=stderr), extensions.wrappedfunction( util, "rawsystem", rawsystem): encoding.setfromenviron() pycompat.stdin = stdin pycompat.stdout = stdout pycompat.stderr = stderr pycompat.sysargv = env.args util._reloadenv() exitcode = bindings.commands.run(env.args, stdin, stdout, stderr) return exitcode finally: # restore environ encoding.setfromenviron() pycompat.stdin, pycompat.stdout, pycompat.stderr = origstdio
def _hg(*args, **kwargs): stdin = kwargs.get("stdin") or "" encoding.setfromenviron() cwdbefore = os.getcwd() fout = util.stringio() fin = util.stringio(stdin) status = bindings.commands.run(["hg"] + list(args), fin, fout, fout) cwdafter = os.getcwd() if cwdafter != cwdbefore: # Revert side effect of --cwd os.chdir(cwdbefore) buf = fout.getvalue().rstrip() return (status, buf)
def _hg(*args, **kwargs): stdin = kwargs.get("stdin") or "" encoding.setfromenviron() cwdbefore = os.getcwd() fout = util.stringio() fin = util.stringio(pycompat.encodeutf8(stdin)) sysargs = ["hg"] + list(args) pycompat.sysargv = sysargs status = bindings.commands.run(sysargs, fin, fout, fout) cwdafter = os.getcwd() if cwdafter != cwdbefore: # Revert side effect of --cwd os.chdir(cwdbefore) buf = fout.getvalue().rstrip() return (status, pycompat.decodeutf8(buf, errors="surrogateescape"))