D7773: resourceutil: implement `is_resource()`

mharbison72 (Matt Harbison) phabricator at mercurial-scm.org
Mon Dec 30 05:43:14 UTC 2019


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

REVISION SUMMARY
  This will be needed when iterating resources.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/utils/resourceutil.py

CHANGE DETAILS

diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py
--- a/mercurial/utils/resourceutil.py
+++ b/mercurial/utils/resourceutil.py
@@ -40,6 +40,8 @@
 try:
     from importlib import resources
 
+    from .. import encoding
+
     # Force loading of the resources module
     resources.open_binary  # pytype: disable=module-attr
 
@@ -48,6 +50,11 @@
             pycompat.sysstr(package), pycompat.sysstr(name)
         )
 
+    def is_resource(package, name):
+        return resources.is_resource(
+            pycompat.sysstr(package), encoding.strfromlocal(name)
+        )
+
 
 except (ImportError, AttributeError):
 
@@ -57,3 +64,11 @@
     def open_resource(package, name):
         path = os.path.join(_package_path(package), name)
         return open(path, 'rb')
+
+    def is_resource(package, name):
+        path = os.path.join(_package_path(package), name)
+
+        try:
+            return os.path.isfile(pycompat.fsdecode(path))
+        except (IOError, OSError):
+            return False



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


More information about the Mercurial-devel mailing list