changeset 43671:664e24207728

procutil: move mainfrozen() to new resourceutil.py The i18n module practically depends on procutil for mainfrozen() but since procutil depends on i18n, it would be a circular dependency if i18n depended directly on procutil. The cycle is currently resolved by having the higher-level util module calculate the "datapath" and inject it into i18n. Extracting mainfrozen() to a new module lets us clean up the dependencies. Differential Revision: https://phab.mercurial-scm.org/D7433
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 14 Nov 2019 11:52:22 -0800
parents 02fe8dedab8c
children 9fb85668ee15
files mercurial/hook.py mercurial/sslutil.py mercurial/util.py mercurial/utils/procutil.py mercurial/utils/resourceutil.py
diffstat 5 files changed, 39 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hook.py	Thu Nov 14 17:36:01 2019 -0800
+++ b/mercurial/hook.py	Thu Nov 14 11:52:22 2019 -0800
@@ -22,6 +22,7 @@
 )
 from .utils import (
     procutil,
+    resourceutil,
     stringutil,
 )
 
@@ -48,7 +49,7 @@
             )
         modname = funcname[:d]
         oldpaths = sys.path
-        if procutil.mainfrozen():
+        if resourceutil.mainfrozen():
             # binary installs require sys.path manipulation
             modpath, modfile = os.path.split(modname)
             if modpath and modfile:
--- a/mercurial/sslutil.py	Thu Nov 14 17:36:01 2019 -0800
+++ b/mercurial/sslutil.py	Thu Nov 14 11:52:22 2019 -0800
@@ -24,7 +24,7 @@
     util,
 )
 from .utils import (
-    procutil,
+    resourceutil,
     stringutil,
 )
 
@@ -786,7 +786,7 @@
     """
     if (
         not pycompat.isdarwin
-        or procutil.mainfrozen()
+        or resourceutil.mainfrozen()
         or not pycompat.sysexecutable
     ):
         return False
--- a/mercurial/util.py	Thu Nov 14 17:36:01 2019 -0800
+++ b/mercurial/util.py	Thu Nov 14 11:52:22 2019 -0800
@@ -54,6 +54,7 @@
 from .utils import (
     compression,
     procutil,
+    resourceutil,
     stringutil,
 )
 
@@ -1823,7 +1824,7 @@
 
 
 # the location of data files matching the source code
-if procutil.mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
+if resourceutil.mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
     # executable version (py2exe) doesn't support __file__
     datapath = os.path.dirname(pycompat.sysexecutable)
 else:
--- a/mercurial/utils/procutil.py	Thu Nov 14 17:36:01 2019 -0800
+++ b/mercurial/utils/procutil.py	Thu Nov 14 11:52:22 2019 -0800
@@ -11,7 +11,6 @@
 
 import contextlib
 import errno
-import imp
 import io
 import os
 import signal
@@ -32,6 +31,9 @@
     pycompat,
 )
 
+# Import like this to keep import-checker happy
+from ..utils import resourceutil
+
 osutil = policy.importmod('osutil')
 
 stderr = pycompat.stderr
@@ -254,19 +256,6 @@
     return pipefilter(s, cmd)
 
 
-def mainfrozen():
-    """return True if we are a frozen executable.
-
-    The code supports py2exe (most common, Windows only) and tools/freeze
-    (portable, not much used).
-    """
-    return (
-        pycompat.safehasattr(sys, "frozen")
-        or pycompat.safehasattr(sys, "importers")  # new py2exe
-        or imp.is_frozen("__main__")  # old py2exe
-    )  # tools/freeze
-
-
 _hgexecutable = None
 
 
@@ -280,7 +269,7 @@
         mainmod = sys.modules['__main__']
         if hg:
             _sethgexecutable(hg)
-        elif mainfrozen():
+        elif resourceutil.mainfrozen():
             if getattr(sys, 'frozen', None) == 'macosx_app':
                 # Env variable set by py2app
                 _sethgexecutable(encoding.environ[b'EXECUTABLEPATH'])
@@ -456,7 +445,7 @@
     to avoid things opening new shell windows like batch files, so we
     get either the python call or current executable.
     """
-    if mainfrozen():
+    if resourceutil.mainfrozen():
         if getattr(sys, 'frozen', None) == 'macosx_app':
             # Env variable set by py2app
             return [encoding.environ[b'EXECUTABLEPATH']]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/utils/resourceutil.py	Thu Nov 14 11:52:22 2019 -0800
@@ -0,0 +1,28 @@
+# resourceutil.py - utility for looking up resources
+#
+#  Copyright 2005 K. Thananchayan <thananck@yahoo.com>
+#  Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
+#  Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+import imp
+import sys
+
+from .. import pycompat
+
+
+def mainfrozen():
+    """return True if we are a frozen executable.
+
+    The code supports py2exe (most common, Windows only) and tools/freeze
+    (portable, not much used).
+    """
+    return (
+        pycompat.safehasattr(sys, "frozen")
+        or pycompat.safehasattr(sys, "importers")  # new py2exe
+        or imp.is_frozen("__main__")  # old py2exe
+    )  # tools/freeze