resourceutil: account for the non-resource-like file hierarchy under py2exe
After
9e367157a990, config files for py2exe were expected to be in
C:\Program Files\Mercurial\mercurial\defaultrc because of the implied resource
structure of 'mercurial.defaultrc.*.rc', relative to the executable.
Accomodating this would require changes to the WIX and Inno scripts (and perhaps
the script that generates the WIX script), as well as 3rd party bundlers like
TortoiseHg. But these files aren't read as resources anyway- they fall back to
the filesystem APIs. (If we really wanted to carry on the charade, the
installer would have to also sprinkle various empty __init__.py files around.)
Instead, this simply prunes the 'mercurial.' portion of the resource name when
run with py2exe. (PyOxidizer uses the resources API, not the filesystem
fallback, so it is unaffected.) Since this hack only affects the py2 Windows
installers and is less risky, I think it's reasonable. We haven't needed to
load any 3rd party resource up to this point, and would have to make packaging
changes anyway to handle that.
Differential Revision: https://phab.mercurial-scm.org/D8058
--- a/mercurial/utils/resourceutil.py Thu Jan 30 19:37:06 2020 -0500
+++ b/mercurial/utils/resourceutil.py Fri Jan 31 22:20:39 2020 -0500
@@ -34,10 +34,24 @@
# executable version (py2exe) doesn't support __file__
datapath = os.path.dirname(pycompat.sysexecutable)
_rootpath = datapath
+
+ # The installers store the files outside of library.zip, like
+ # C:\Program Files\Mercurial\defaultrc\*.rc. This strips the
+ # leading "mercurial." off of the package name, so that these
+ # pseudo resources are found in their directory next to the
+ # executable.
+ def _package_path(package):
+ dirs = package.split(b'.')
+ assert dirs[0] == b'mercurial'
+ return os.path.join(_rootpath, *dirs[1:])
+
else:
datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__)))
_rootpath = os.path.dirname(datapath)
+ def _package_path(package):
+ return os.path.join(_rootpath, *package.split(b'.'))
+
try:
from importlib import resources
@@ -63,9 +77,6 @@
except (ImportError, AttributeError):
- def _package_path(package):
- return os.path.join(_rootpath, *package.split(b'.'))
-
def open_resource(package, name):
path = os.path.join(_package_path(package), name)
return open(path, 'rb')