From patchwork Wed Jan 8 19:51:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D7773: resourceutil: implement `is_resource()` From: phabricator X-Patchwork-Id: 44205 Message-Id: <4d6c80b9e6267e19a288cc9efa1d10a1@localhost.localdomain> To: Phabricator Cc: mercurial-devel@mercurial-scm.org Date: Wed, 8 Jan 2020 19:51:35 +0000 Closed by commit rHG42a897bf678c: resourceutil: implement `is_resource()` (authored by mharbison72). This revision was automatically updated to reflect the committed changes. This revision was not accepted when it landed; it landed in state "Needs Review". REPOSITORY rHG Mercurial CHANGES SINCE LAST UPDATE https://phab.mercurial-scm.org/D7773?vs=19004&id=19102 CHANGES SINCE LAST ACTION https://phab.mercurial-scm.org/D7773/new/ REVISION DETAIL https://phab.mercurial-scm.org/D7773 AFFECTED FILES mercurial/utils/resourceutil.py CHANGE DETAILS To: mharbison72, #hg-reviewers Cc: mercurial-devel 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