util: introduce datapath for getting the location of supporting data files
authorMads Kiilerich <madski@unity3d.com>
Sun, 28 Sep 2014 16:57:06 +0200
changeset 22633 92b54547ac5d
parent 22632 db15bb2d6323
child 22634 e48a5d3996c2
util: introduce datapath for getting the location of supporting data files templates, help and locale data is normally stored as sub folders in the directory containing the source of the mercurial module. In a frozen build they live as sub folders next to 'hg.exe' and 'library.zip'. These different kind of data were handled in different ways. Unify that by introducing util.datapath. The value is computed from the environment and is always used, so we just calculate the value on module load.
mercurial/help.py
mercurial/templater.py
mercurial/util.py
--- a/mercurial/help.py	Sun Sep 28 16:57:06 2014 +0200
+++ b/mercurial/help.py	Sun Sep 28 16:57:06 2014 +0200
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import gettext, _
-import itertools, sys, os
+import itertools, os
 import error
 import extensions, revset, fileset, templatekw, templatefilters, filemerge
 import encoding, util, minirst
@@ -129,14 +129,8 @@
     """Return a delayed loader for help/topic.txt."""
 
     def loader():
-        if util.mainfrozen():
-            module = sys.executable
-        else:
-            module = __file__
-        base = os.path.dirname(module)
-
         for dir in ('.', '..'):
-            docdir = os.path.join(base, dir, 'help')
+            docdir = os.path.join(util.datapath, dir, 'help')
             if os.path.isdir(docdir):
                 break
 
--- a/mercurial/templater.py	Sun Sep 28 16:57:06 2014 +0200
+++ b/mercurial/templater.py	Sun Sep 28 16:57:06 2014 +0200
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
-import sys, os, re
+import os, re
 import util, config, templatefilters, templatekw, parser, error
 import revset as revsetmod
 import types
@@ -715,17 +715,12 @@
     returns None if not found.'''
     normpaths = []
 
-    # executable version (py2exe) doesn't support __file__
-    if util.mainfrozen():
-        module = sys.executable
-    else:
-        module = __file__
     for f in path:
         if f.startswith('/'):
             p = f
         else:
             fl = f.split('/')
-            p = os.path.join(os.path.dirname(module), *fl)
+            p = os.path.join(util.datapath, *fl)
         if name:
             p = os.path.join(p, name)
         if name and os.path.exists(p):
--- a/mercurial/util.py	Sun Sep 28 16:57:06 2014 +0200
+++ b/mercurial/util.py	Sun Sep 28 16:57:06 2014 +0200
@@ -460,6 +460,13 @@
             safehasattr(sys, "importers") or # old py2exe
             imp.is_frozen("__main__")) # tools/freeze
 
+# the location of data files matching the source code
+if mainfrozen():
+    # executable version (py2exe) doesn't support __file__
+    datapath = os.path.dirname(sys.executable)
+else:
+    datapath = os.path.dirname(__file__)
+
 _hgexecutable = None
 
 def hgexecutable():