[PATCH 2 of 7] util: introduce datapath for getting the location of supporting data files

Mads Kiilerich mads at kiilerich.com
Sun Sep 28 10:02:59 CDT 2014


# HG changeset patch
# User Mads Kiilerich <madski at unity3d.com>
# Date 1411916226 -7200
#      Sun Sep 28 16:57:06 2014 +0200
# Node ID da31d28c71a46741a8f1d28785859ffb91f6a026
# Parent  d064d89884c6ad9039d0b73086b337c1cabade0a
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.

diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -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 @@ def loaddoc(topic):
     """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
 
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -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 @@ def templatepath(name=None):
     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):
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -460,6 +460,13 @@ def mainfrozen():
             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():


More information about the Mercurial-devel mailing list