D7386: help: access text files as resources under PyOxidizer

mharbison72 (Matt Harbison) phabricator at mercurial-scm.org
Thu Nov 14 04:01:04 UTC 2019


mharbison72 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This allows `hg help revsets` to run on an oxidized Mac executable.  I left the
  old path in place for py2 support, as well as the other packaging apps.
  
  On Windows, that same command simply prints:
  
    abort: Incorrect function
  
  ... and doesn't put out a stacktrace even with `--traceback`.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7386

AFFECTED FILES
  mercurial/help.py

CHANGE DETAILS

diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -10,6 +10,7 @@
 import itertools
 import os
 import re
+import sys
 import textwrap
 
 from .i18n import (
@@ -310,12 +311,26 @@
 def loaddoc(topic, subdir=None):
     """Return a delayed loader for help/topic.txt."""
 
+    if getattr(sys, 'oxidized', None):
+        from importlib import resources
+
+        def _docdata():
+            docpkg = b'mercurial.helptext'
+            if subdir:
+                docpkg += b'.' + subdir
+            with resources.open_binary(pycompat.sysstr(docpkg),
+                                       pycompat.sysstr(topic + b".txt")) as dh:
+                return dh.read()
+    else:
+        def _docdata():
+            docdir = os.path.join(util.datapath, b'helptext')
+            if subdir:
+                docdir = os.path.join(docdir, subdir)
+            path = os.path.join(docdir, topic + b".txt")
+            return util.readfile(path)
+
     def loader(ui):
-        docdir = os.path.join(util.datapath, b'helptext')
-        if subdir:
-            docdir = os.path.join(docdir, subdir)
-        path = os.path.join(docdir, topic + b".txt")
-        doc = gettext(util.readfile(path))
+        doc = gettext(_docdata())
         for rewriter in helphooks.get(topic, []):
             doc = rewriter(ui, topic, doc)
         return doc



To: mharbison72, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list