Skip to content
Snippets Groups Projects
Commit 37c65704 authored by Pierre-Yves David's avatar Pierre-Yves David :octopus:
Browse files

procutil: allow to specify arbitrary stdin bytes to runbgcommand

For automatic clonebundles generation I need to pass arbitrary large amount of
data to the process (eg: common nodes, target nodes).

I am updating the `runbgcommand` to allow for this. Previously not stdin input
was possible, now, one can provide raw bytes and they will be feed to the
command through an unnamed temporary files.

Differential Revision: https://phab.mercurial-scm.org/D9212
parent 80f32ec8
No related branches found
No related tags found
No related merge requests found
......@@ -635,7 +635,8 @@
stderr=None,
ensurestart=True,
record_wait=None,
stdin_bytes=None,
):
'''Spawn a command without waiting for it to finish.'''
# we can't use close_fds *and* redirect stdin. I'm not sure that we
# need to because the detached process has no console connection.
......@@ -638,18 +639,31 @@
):
'''Spawn a command without waiting for it to finish.'''
# we can't use close_fds *and* redirect stdin. I'm not sure that we
# need to because the detached process has no console connection.
p = subprocess.Popen(
tonativestr(script),
shell=shell,
env=tonativeenv(env),
close_fds=True,
creationflags=_creationflags,
stdout=stdout,
stderr=stderr,
)
if record_wait is not None:
record_wait(p.wait)
try:
stdin = None
if stdin_bytes is not None:
stdin = pycompat.unnamedtempfile()
stdin.write(stdin_bytes)
stdin.flush()
stdin.seek(0)
p = subprocess.Popen(
tonativestr(script),
shell=shell,
env=tonativeenv(env),
close_fds=True,
creationflags=_creationflags,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
if record_wait is not None:
record_wait(p.wait)
finally:
if stdin is not None:
stdin.close()
else:
......@@ -662,6 +676,7 @@
stderr=None,
ensurestart=True,
record_wait=None,
stdin_bytes=None,
):
'''Spawn a command without waiting for it to finish.
......@@ -722,4 +737,13 @@
if record_wait is None:
# Start a new session
os.setsid()
# connect stdin to devnull to make sure the subprocess can't
# muck up that stream for mercurial.
if stdin_bytes is None:
stdin = open(os.devnull, b'r')
else:
stdin = pycompat.unnamedtempfile()
stdin.write(stdin_bytes)
stdin.flush()
stdin.seek(0)
......@@ -725,7 +749,6 @@
stdin = open(os.devnull, b'r')
if stdout is None:
stdout = open(os.devnull, b'w')
if stderr is None:
stderr = open(os.devnull, b'w')
......@@ -727,10 +750,8 @@
if stdout is None:
stdout = open(os.devnull, b'w')
if stderr is None:
stderr = open(os.devnull, b'w')
# connect stdin to devnull to make sure the subprocess can't
# muck up that stream for mercurial.
p = subprocess.Popen(
cmd,
shell=shell,
......@@ -754,5 +775,6 @@
finally:
# mission accomplished, this child needs to exit and not
# continue the hg process here.
stdin.close()
if record_wait is None:
os._exit(returncode)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment