mercurial/utils/procutil.py
changeset 43506 9f70512ae2cf
parent 43503 313e3a279828
child 43655 ef43765c921d
equal deleted inserted replaced
43505:47fac1692ede 43506:9f70512ae2cf
    30     error,
    30     error,
    31     policy,
    31     policy,
    32     pycompat,
    32     pycompat,
    33 )
    33 )
    34 
    34 
    35 osutil = policy.importmod(r'osutil')
    35 osutil = policy.importmod('osutil')
    36 
    36 
    37 stderr = pycompat.stderr
    37 stderr = pycompat.stderr
    38 stdin = pycompat.stdin
    38 stdin = pycompat.stdin
    39 stdout = pycompat.stdout
    39 stdout = pycompat.stdout
    40 
    40 
    50 # destined stdout with a pipe destined stdout (e.g. pager), we want line
    50 # destined stdout with a pipe destined stdout (e.g. pager), we want line
    51 # buffering (or unbuffered, on Windows)
    51 # buffering (or unbuffered, on Windows)
    52 if isatty(stdout):
    52 if isatty(stdout):
    53     if pycompat.iswindows:
    53     if pycompat.iswindows:
    54         # Windows doesn't support line buffering
    54         # Windows doesn't support line buffering
    55         stdout = os.fdopen(stdout.fileno(), r'wb', 0)
    55         stdout = os.fdopen(stdout.fileno(), 'wb', 0)
    56     elif not pycompat.ispy3:
    56     elif not pycompat.ispy3:
    57         # on Python 3, stdout (sys.stdout.buffer) is already line buffered and
    57         # on Python 3, stdout (sys.stdout.buffer) is already line buffered and
    58         # buffering=1 is not handled in binary mode
    58         # buffering=1 is not handled in binary mode
    59         stdout = os.fdopen(stdout.fileno(), r'wb', 1)
    59         stdout = os.fdopen(stdout.fileno(), 'wb', 1)
    60 
    60 
    61 if pycompat.iswindows:
    61 if pycompat.iswindows:
    62     from .. import windows as platform
    62     from .. import windows as platform
    63 
    63 
    64     stdout = platform.winstdout(stdout)
    64     stdout = platform.winstdout(stdout)
   209     with the strings INFILE and OUTFILE replaced by the real names of
   209     with the strings INFILE and OUTFILE replaced by the real names of
   210     the temporary files generated.'''
   210     the temporary files generated.'''
   211     inname, outname = None, None
   211     inname, outname = None, None
   212     try:
   212     try:
   213         infd, inname = pycompat.mkstemp(prefix=b'hg-filter-in-')
   213         infd, inname = pycompat.mkstemp(prefix=b'hg-filter-in-')
   214         fp = os.fdopen(infd, r'wb')
   214         fp = os.fdopen(infd, 'wb')
   215         fp.write(s)
   215         fp.write(s)
   216         fp.close()
   216         fp.close()
   217         outfd, outname = pycompat.mkstemp(prefix=b'hg-filter-out-')
   217         outfd, outname = pycompat.mkstemp(prefix=b'hg-filter-out-')
   218         os.close(outfd)
   218         os.close(outfd)
   219         cmd = cmd.replace(b'INFILE', inname)
   219         cmd = cmd.replace(b'INFILE', inname)
   275 
   275 
   276     Defaults to $HG or 'hg' in the search path.
   276     Defaults to $HG or 'hg' in the search path.
   277     """
   277     """
   278     if _hgexecutable is None:
   278     if _hgexecutable is None:
   279         hg = encoding.environ.get(b'HG')
   279         hg = encoding.environ.get(b'HG')
   280         mainmod = sys.modules[r'__main__']
   280         mainmod = sys.modules['__main__']
   281         if hg:
   281         if hg:
   282             _sethgexecutable(hg)
   282             _sethgexecutable(hg)
   283         elif mainfrozen():
   283         elif mainfrozen():
   284             if getattr(sys, 'frozen', None) == b'macosx_app':
   284             if getattr(sys, 'frozen', None) == b'macosx_app':
   285                 # Env variable set by py2app
   285                 # Env variable set by py2app
   338     if _testfileno(uin, stdin):
   338     if _testfileno(uin, stdin):
   339         newfd = os.dup(uin.fileno())
   339         newfd = os.dup(uin.fileno())
   340         nullfd = os.open(os.devnull, os.O_RDONLY)
   340         nullfd = os.open(os.devnull, os.O_RDONLY)
   341         os.dup2(nullfd, uin.fileno())
   341         os.dup2(nullfd, uin.fileno())
   342         os.close(nullfd)
   342         os.close(nullfd)
   343         fin = os.fdopen(newfd, r'rb')
   343         fin = os.fdopen(newfd, 'rb')
   344     if _testfileno(uout, stdout):
   344     if _testfileno(uout, stdout):
   345         newfd = os.dup(uout.fileno())
   345         newfd = os.dup(uout.fileno())
   346         os.dup2(stderr.fileno(), uout.fileno())
   346         os.dup2(stderr.fileno(), uout.fileno())
   347         fout = os.fdopen(newfd, r'wb')
   347         fout = os.fdopen(newfd, 'wb')
   348     return fin, fout
   348     return fin, fout
   349 
   349 
   350 
   350 
   351 def restorestdio(uin, uout, fin, fout):
   351 def restorestdio(uin, uout, fin, fout):
   352     """Restore (uin, uout) streams from possibly duplicated (fin, fout)"""
   352     """Restore (uin, uout) streams from possibly duplicated (fin, fout)"""