# HG changeset patch # User Mads Kiilerich # Date 2014-09-28 14:57:06 # Node ID 92b54547ac5d79c4a30c19b71bd674e4675e7cda # Parent db15bb2d632392558988c3ae0250e359f0380691 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():