changeset 45332:3b27ed8e324e

templater: make open_template() read from resources if in frozen binary This takes the number of failing tests with PyOxidizer from 87 to 72. Differential Revision: https://phab.mercurial-scm.org/D8894
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 04 Aug 2020 10:51:25 -0700
parents f3481e4fcc3a
children d9a502a0a9ca
files mercurial/templater.py
diffstat 1 files changed, 15 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Tue Aug 04 13:22:00 2020 -0700
+++ b/mercurial/templater.py	Tue Aug 04 10:51:25 2020 -0700
@@ -1074,7 +1074,12 @@
 
 
 def open_template(name):
-    '''returns a file-like object for the given template, and its full path'''
+    '''returns a file-like object for the given template, and its full path
+
+    If the name is a relative path and we're in a frozen binary, the template
+    will be read from the mercurial.templates package instead. The returned path
+    will then be the relative path.
+    '''
     templatepath = templatedir()
     if templatepath is not None or os.path.isabs(name):
         f = os.path.join(templatepath, name)
@@ -1083,5 +1088,12 @@
         except EnvironmentError:
             return None, None
     else:
-        # TODO: read from resources here
-        return None, None
+        name_parts = pycompat.sysstr(name).split('/')
+        package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1])
+        try:
+            return (
+                name,
+                resourceutil.open_resource(package_name, name_parts[-1]),
+            )
+        except (ModuleNotFoundError, FileNotFoundError):
+            return None, None