# HG changeset patch # User Matt Harbison # Date 2019-12-30 01:32:56 # Node ID 42a897bf678cc124ef94251b655d6dc08f2d8654 # Parent 52f0140c2604aab3ec960dd7c0c99ce550276f8e resourceutil: implement `is_resource()` This will be needed when iterating resources. Differential Revision: https://phab.mercurial-scm.org/D7773 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 @@ else: 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 @@ try: 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 @@ except (ImportError, AttributeError): 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