[PATCH 2 of 3] util: adjust hgexecutable() to handle frozen Mercurial on OS X

Matt Harbison matt_harbison at yahoo.com
Mon Jan 11 11:32:07 CST 2016


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1452466568 18000
#      Sun Jan 10 17:56:08 2016 -0500
# Node ID 82944be0a4a6158bc9b2e0ce04fd108eea078805
# Parent  69ab5e228bd0fcb23636e1569afb4710d00c5f93
util: adjust hgexecutable() to handle frozen Mercurial on OS X

sys.executable is "$appbundle/Contents/MacOS/python" when Mercurial is bundled
in a frozen app bundle on OS X, so that isn't appropriate.  It appears that this
was only visible for things launched via util.system(), like external hooks,
where $HG was set wrong.

It appears that Mercurial also uses 'sys.modules['__main__'].__file__' (here)
and 'sys.argv[0]' (in platform.gethgcmd()) to figure out the command to spawn.
In both cases, this points to "$appbundle/Contents/Resources/hg", which invokes
the system python since "/usr/bin/env python" is on the shebang line.  On my
system with a screwed up python install, I get an error importing the os module
if this script is invoked.

We could take the dirname of sys.executable and join 'hg' instead of this if we
want to be paranoid, but py2app boostrap is setting the environment variable
since 0.1.6 (current version is 0.9), so it seems safe and we might as well use
it.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -907,7 +907,11 @@
         if hg:
             _sethgexecutable(hg)
         elif mainfrozen():
-            _sethgexecutable(sys.executable)
+            if getattr(sys, 'frozen', None) == 'macosx_app':
+                # Env variable set by py2app
+                _sethgexecutable(os.environ['EXECUTABLEPATH'])
+            else:
+                _sethgexecutable(sys.executable)
         elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
             _sethgexecutable(mainmod.__file__)
         else:


More information about the Mercurial-devel mailing list