mercurial/utils/procutil.py
changeset 43671 664e24207728
parent 43657 38387f9e4d22
child 43787 be8552f25cab
equal deleted inserted replaced
43670:02fe8dedab8c 43671:664e24207728
     9 
     9 
    10 from __future__ import absolute_import
    10 from __future__ import absolute_import
    11 
    11 
    12 import contextlib
    12 import contextlib
    13 import errno
    13 import errno
    14 import imp
       
    15 import io
    14 import io
    16 import os
    15 import os
    17 import signal
    16 import signal
    18 import subprocess
    17 import subprocess
    19 import sys
    18 import sys
    29     encoding,
    28     encoding,
    30     error,
    29     error,
    31     policy,
    30     policy,
    32     pycompat,
    31     pycompat,
    33 )
    32 )
       
    33 
       
    34 # Import like this to keep import-checker happy
       
    35 from ..utils import resourceutil
    34 
    36 
    35 osutil = policy.importmod('osutil')
    37 osutil = policy.importmod('osutil')
    36 
    38 
    37 stderr = pycompat.stderr
    39 stderr = pycompat.stderr
    38 stdin = pycompat.stdin
    40 stdin = pycompat.stdin
   252         if cmd.startswith(name):
   254         if cmd.startswith(name):
   253             return fn(s, cmd[len(name) :].lstrip())
   255             return fn(s, cmd[len(name) :].lstrip())
   254     return pipefilter(s, cmd)
   256     return pipefilter(s, cmd)
   255 
   257 
   256 
   258 
   257 def mainfrozen():
       
   258     """return True if we are a frozen executable.
       
   259 
       
   260     The code supports py2exe (most common, Windows only) and tools/freeze
       
   261     (portable, not much used).
       
   262     """
       
   263     return (
       
   264         pycompat.safehasattr(sys, "frozen")
       
   265         or pycompat.safehasattr(sys, "importers")  # new py2exe
       
   266         or imp.is_frozen("__main__")  # old py2exe
       
   267     )  # tools/freeze
       
   268 
       
   269 
       
   270 _hgexecutable = None
   259 _hgexecutable = None
   271 
   260 
   272 
   261 
   273 def hgexecutable():
   262 def hgexecutable():
   274     """return location of the 'hg' executable.
   263     """return location of the 'hg' executable.
   278     if _hgexecutable is None:
   267     if _hgexecutable is None:
   279         hg = encoding.environ.get(b'HG')
   268         hg = encoding.environ.get(b'HG')
   280         mainmod = sys.modules['__main__']
   269         mainmod = sys.modules['__main__']
   281         if hg:
   270         if hg:
   282             _sethgexecutable(hg)
   271             _sethgexecutable(hg)
   283         elif mainfrozen():
   272         elif resourceutil.mainfrozen():
   284             if getattr(sys, 'frozen', None) == 'macosx_app':
   273             if getattr(sys, 'frozen', None) == 'macosx_app':
   285                 # Env variable set by py2app
   274                 # Env variable set by py2app
   286                 _sethgexecutable(encoding.environ[b'EXECUTABLEPATH'])
   275                 _sethgexecutable(encoding.environ[b'EXECUTABLEPATH'])
   287             else:
   276             else:
   288                 _sethgexecutable(pycompat.sysexecutable)
   277                 _sethgexecutable(pycompat.sysexecutable)
   454 
   443 
   455     This is different from hgexecutable() because on Windows we want
   444     This is different from hgexecutable() because on Windows we want
   456     to avoid things opening new shell windows like batch files, so we
   445     to avoid things opening new shell windows like batch files, so we
   457     get either the python call or current executable.
   446     get either the python call or current executable.
   458     """
   447     """
   459     if mainfrozen():
   448     if resourceutil.mainfrozen():
   460         if getattr(sys, 'frozen', None) == 'macosx_app':
   449         if getattr(sys, 'frozen', None) == 'macosx_app':
   461             # Env variable set by py2app
   450             # Env variable set by py2app
   462             return [encoding.environ[b'EXECUTABLEPATH']]
   451             return [encoding.environ[b'EXECUTABLEPATH']]
   463         else:
   452         else:
   464             return [pycompat.sysexecutable]
   453             return [pycompat.sysexecutable]