mercurial/utils/procutil.py
changeset 45786 37c65704869d
parent 45148 a37f290a7124
child 45852 b56feaa9b520
equal deleted inserted replaced
45785:80f32ec8653a 45786:37c65704869d
   633         shell=False,
   633         shell=False,
   634         stdout=None,
   634         stdout=None,
   635         stderr=None,
   635         stderr=None,
   636         ensurestart=True,
   636         ensurestart=True,
   637         record_wait=None,
   637         record_wait=None,
       
   638         stdin_bytes=None,
   638     ):
   639     ):
   639         '''Spawn a command without waiting for it to finish.'''
   640         '''Spawn a command without waiting for it to finish.'''
   640         # we can't use close_fds *and* redirect stdin. I'm not sure that we
   641         # we can't use close_fds *and* redirect stdin. I'm not sure that we
   641         # need to because the detached process has no console connection.
   642         # need to because the detached process has no console connection.
   642         p = subprocess.Popen(
   643 
   643             tonativestr(script),
   644         try:
   644             shell=shell,
   645             stdin = None
   645             env=tonativeenv(env),
   646             if stdin_bytes is not None:
   646             close_fds=True,
   647                 stdin = pycompat.unnamedtempfile()
   647             creationflags=_creationflags,
   648                 stdin.write(stdin_bytes)
   648             stdout=stdout,
   649                 stdin.flush()
   649             stderr=stderr,
   650                 stdin.seek(0)
   650         )
   651 
   651         if record_wait is not None:
   652             p = subprocess.Popen(
   652             record_wait(p.wait)
   653                 tonativestr(script),
       
   654                 shell=shell,
       
   655                 env=tonativeenv(env),
       
   656                 close_fds=True,
       
   657                 creationflags=_creationflags,
       
   658                 stdin=stdin,
       
   659                 stdout=stdout,
       
   660                 stderr=stderr,
       
   661             )
       
   662             if record_wait is not None:
       
   663                 record_wait(p.wait)
       
   664         finally:
       
   665             if stdin is not None:
       
   666                 stdin.close()
   653 
   667 
   654 
   668 
   655 else:
   669 else:
   656 
   670 
   657     def runbgcommand(
   671     def runbgcommand(
   660         shell=False,
   674         shell=False,
   661         stdout=None,
   675         stdout=None,
   662         stderr=None,
   676         stderr=None,
   663         ensurestart=True,
   677         ensurestart=True,
   664         record_wait=None,
   678         record_wait=None,
       
   679         stdin_bytes=None,
   665     ):
   680     ):
   666         '''Spawn a command without waiting for it to finish.
   681         '''Spawn a command without waiting for it to finish.
   667 
   682 
   668 
   683 
   669         When `record_wait` is not None, the spawned process will not be fully
   684         When `record_wait` is not None, the spawned process will not be fully
   720         returncode = 255
   735         returncode = 255
   721         try:
   736         try:
   722             if record_wait is None:
   737             if record_wait is None:
   723                 # Start a new session
   738                 # Start a new session
   724                 os.setsid()
   739                 os.setsid()
   725 
   740             # connect stdin to devnull to make sure the subprocess can't
   726             stdin = open(os.devnull, b'r')
   741             # muck up that stream for mercurial.
       
   742             if stdin_bytes is None:
       
   743                 stdin = open(os.devnull, b'r')
       
   744             else:
       
   745                 stdin = pycompat.unnamedtempfile()
       
   746                 stdin.write(stdin_bytes)
       
   747                 stdin.flush()
       
   748                 stdin.seek(0)
       
   749 
   727             if stdout is None:
   750             if stdout is None:
   728                 stdout = open(os.devnull, b'w')
   751                 stdout = open(os.devnull, b'w')
   729             if stderr is None:
   752             if stderr is None:
   730                 stderr = open(os.devnull, b'w')
   753                 stderr = open(os.devnull, b'w')
   731 
   754 
   732             # connect stdin to devnull to make sure the subprocess can't
       
   733             # muck up that stream for mercurial.
       
   734             p = subprocess.Popen(
   755             p = subprocess.Popen(
   735                 cmd,
   756                 cmd,
   736                 shell=shell,
   757                 shell=shell,
   737                 env=env,
   758                 env=env,
   738                 close_fds=True,
   759                 close_fds=True,
   752         except Exception:
   773         except Exception:
   753             returncode = 255
   774             returncode = 255
   754         finally:
   775         finally:
   755             # mission accomplished, this child needs to exit and not
   776             # mission accomplished, this child needs to exit and not
   756             # continue the hg process here.
   777             # continue the hg process here.
       
   778             stdin.close()
   757             if record_wait is None:
   779             if record_wait is None:
   758                 os._exit(returncode)
   780                 os._exit(returncode)