diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -15,7 +15,7 @@ platform-specific details from the core. from i18n import _ import cStringIO, errno, getpass, re, shutil, sys, tempfile import os, stat, threading, time, calendar, ConfigParser, locale, glob, osutil -import urlparse +import imp, urlparse # Python compatibility @@ -563,13 +563,29 @@ def _matcher(canonroot, cwd, names, inc, _hgexecutable = None +def main_is_frozen(): + """return True if we are a frozen executable. + + The code supports py2exe (most common, Windows only) and tools/freeze + (portable, not much used). + """ + return (hasattr(sys, "frozen") or # new py2exe + hasattr(sys, "importers") or # old py2exe + imp.is_frozen("__main__")) # tools/freeze + def hgexecutable(): """return location of the 'hg' executable. Defaults to $HG or 'hg' in the search path. """ if _hgexecutable is None: - set_hgexecutable(os.environ.get('HG') or find_exe('hg', 'hg')) + hg = os.environ.get('HG') + if hg: + set_hgexecutable(hg) + elif main_is_frozen(): + set_hgexecutable(sys.executable) + else: + set_hgexecutable(find_exe('hg', 'hg')) return _hgexecutable def set_hgexecutable(path):