# HG changeset patch # User Martin von Zweigbergk # Date 1596572489 25200 # Node ID 65a812ed9e9fd0d93bc592ca147b143bc6a18358 # Parent dc10bcd5c08d142c8f4c1ac6fb87f367a4e2f3b7 templater: replace templatepath() with function that also opens the file For frozen binaries, such as those created by PyOxidizer, I plan to make it so the templatespec can keep an opened file/resource to read from instead of needing a file path. Having `templatepath()` return an opened file should help with that. At this point, it's just a wasteful extra opening of mapfiles that we'll open again later. I'll update the read-side next so it reads from the file-like object without opening the file again. Differential Revision: https://phab.mercurial-scm.org/D8892 diff -r dc10bcd5c08d -r 65a812ed9e9f mercurial/debugcommands.py --- a/mercurial/debugcommands.py Thu Jul 30 13:44:06 2020 -0700 +++ b/mercurial/debugcommands.py Tue Aug 04 13:21:29 2020 -0700 @@ -1672,7 +1672,7 @@ fm.write(b'templatedirs', b'checking templates (%s)...\n', p or b'') fm.condwrite(not p, b'', _(b" no template directories found\n")) if p: - m = templater.templatepath(b"map-cmdline.default") + (m, fp) = templater.open_template(b"map-cmdline.default") if m: # template found, check if it is working err = None diff -r dc10bcd5c08d -r 65a812ed9e9f mercurial/formatter.py --- a/mercurial/formatter.py Thu Jul 30 13:44:06 2020 -0700 +++ b/mercurial/formatter.py Tue Aug 04 13:21:29 2020 -0700 @@ -599,9 +599,9 @@ # perhaps a stock style? if not os.path.split(tmpl)[0]: - mapname = templater.templatepath( + (mapname, fp) = templater.open_template( b'map-cmdline.' + tmpl - ) or templater.templatepath(tmpl) + ) or templater.open_template(tmpl) if mapname: return mapfile_templatespec(topic, mapname) diff -r dc10bcd5c08d -r 65a812ed9e9f mercurial/logcmdutil.py --- a/mercurial/logcmdutil.py Thu Jul 30 13:44:06 2020 -0700 +++ b/mercurial/logcmdutil.py Tue Aug 04 13:21:29 2020 -0700 @@ -627,9 +627,9 @@ if not tmpl and style: mapfile = style if not os.path.split(mapfile)[0]: - mapname = templater.templatepath( + (mapname, fp) = templater.open_template( b'map-cmdline.' + mapfile - ) or templater.templatepath(mapfile) + ) or templater.open_template(mapfile) if mapname: mapfile = mapname return formatter.mapfile_templatespec(b'changeset', mapfile) diff -r dc10bcd5c08d -r 65a812ed9e9f mercurial/templater.py --- a/mercurial/templater.py Thu Jul 30 13:44:06 2020 -0700 +++ b/mercurial/templater.py Tue Aug 04 13:21:29 2020 -0700 @@ -1071,12 +1071,15 @@ return path if os.path.isdir(path) else None -def templatepath(name): - '''return location of template file. returns None if not found.''' - dir = templatedir() - if dir is None: - return None - f = os.path.join(templatedir(), name) - if f and os.path.isfile(f): - return f - return None +def open_template(name): + '''returns a file-like object for the given template, and its full path''' + templatepath = templatedir() + if templatepath is not None or os.path.isabs(name): + f = os.path.join(templatepath, name) + try: + return f, open(f, mode='rb') + except EnvironmentError: + return None, None + else: + # TODO: read from resources here + return None, None