annotate mercurial/utils/resourceutil.py @ 53040:cdd7bf612c7b stable tip

bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 11 Mar 2025 02:29:42 +0100
parents 4cb75772818d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43712
664e24207728 procutil: move mainfrozen() to new resourceutil.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 43701
diff changeset
1 # resourceutil.py - utility for looking up resources
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
2 #
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Rapha?l Gom?s <rgomes@octobus.net>
parents: 46411
diff changeset
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
6 #
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
7 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9996
diff changeset
8 # GNU General Public License version 2 or any later version.
1082
ce96e316278a Update util.py docstrings, fix walk test
mpm@selenic.com
parents: 1081
diff changeset
9
51901
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 51808
diff changeset
10 from __future__ import annotations
27358
ac839ee45b6a util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27357
diff changeset
11
43713
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
12 import os
27358
ac839ee45b6a util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27357
diff changeset
13 import sys
51729
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
14 import typing
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43021
diff changeset
15
51717
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
16 from typing import Iterator
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
17
43712
664e24207728 procutil: move mainfrozen() to new resourceutil.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 43701
diff changeset
18 from .. import pycompat
1293
a6ffcebd3315 Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1292
diff changeset
19
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43021
diff changeset
20
51729
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
21 if typing.TYPE_CHECKING:
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
22 from typing import (
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
23 BinaryIO,
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
24 Iterator,
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
25 )
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
26
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
27
14228
116de1da2154 rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents: 14167
diff changeset
28 def mainfrozen():
6499
479847ccabe0 Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents: 5659
diff changeset
29 """return True if we are a frozen executable.
479847ccabe0 Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents: 5659
diff changeset
30
479847ccabe0 Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents: 5659
diff changeset
31 The code supports py2exe (most common, Windows only) and tools/freeze
479847ccabe0 Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents: 5659
diff changeset
32 (portable, not much used).
479847ccabe0 Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents: 5659
diff changeset
33 """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43021
diff changeset
34 return (
50951
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50752
diff changeset
35 hasattr(sys, "frozen") # new py2exe
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50752
diff changeset
36 or hasattr(sys, "importers") # old py2exe
51716
c5d6a66092e8 utils: avoid using internal _imp.is_frozen()
Mads Kiilerich <mads@kiilerich.com>
parents: 50951
diff changeset
37 or getattr(
c5d6a66092e8 utils: avoid using internal _imp.is_frozen()
Mads Kiilerich <mads@kiilerich.com>
parents: 50951
diff changeset
38 getattr(sys.modules.get('__main__'), '__spec__', None),
c5d6a66092e8 utils: avoid using internal _imp.is_frozen()
Mads Kiilerich <mads@kiilerich.com>
parents: 50951
diff changeset
39 'origin',
c5d6a66092e8 utils: avoid using internal _imp.is_frozen()
Mads Kiilerich <mads@kiilerich.com>
parents: 50951
diff changeset
40 None,
c5d6a66092e8 utils: avoid using internal _imp.is_frozen()
Mads Kiilerich <mads@kiilerich.com>
parents: 50951
diff changeset
41 )
c5d6a66092e8 utils: avoid using internal _imp.is_frozen()
Mads Kiilerich <mads@kiilerich.com>
parents: 50951
diff changeset
42 == 'frozen' # tools/freeze
44802
6c8384afbf77 resourceutil: fix location of line comments
Manuel Jacob <me@manueljacob.de>
parents: 44174
diff changeset
43 )
43713
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
44
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
45
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
46 # the location of data files matching the source code
44174
27787a43712f resourceutil: blacken
Augie Fackler <augie@google.com>
parents: 44148
diff changeset
47 if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app":
43713
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
48 # executable version (py2exe) doesn't support __file__
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
49 datapath = os.path.dirname(pycompat.sysexecutable)
44146
9e367157a990 resourceutil: correct the root path for file based lookup under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44136
diff changeset
50 _rootpath = datapath
44148
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
51
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
52 # The installers store the files outside of library.zip, like
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
53 # C:\Program Files\Mercurial\defaultrc\*.rc. This strips the
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
54 # leading "mercurial." off of the package name, so that these
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
55 # pseudo resources are found in their directory next to the
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
56 # executable.
51729
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
57 def _package_path(package: bytes) -> bytes:
44174
27787a43712f resourceutil: blacken
Augie Fackler <augie@google.com>
parents: 44148
diff changeset
58 dirs = package.split(b".")
27787a43712f resourceutil: blacken
Augie Fackler <augie@google.com>
parents: 44148
diff changeset
59 assert dirs[0] == b"mercurial"
44148
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
60 return os.path.join(_rootpath, *dirs[1:])
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
61
43713
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
62 else:
9fb85668ee15 util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43712
diff changeset
63 datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__)))
44029
52f0140c2604 resourceutil: don't limit resources to the `mercurial` package
Matt Harbison <matt_harbison@yahoo.com>
parents: 43960
diff changeset
64 _rootpath = os.path.dirname(datapath)
43877
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
65
51729
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
66 def _package_path(package: bytes) -> bytes:
44174
27787a43712f resourceutil: blacken
Augie Fackler <augie@google.com>
parents: 44148
diff changeset
67 return os.path.join(_rootpath, *package.split(b"."))
27787a43712f resourceutil: blacken
Augie Fackler <augie@google.com>
parents: 44148
diff changeset
68
44148
aab70b540d3d resourceutil: account for the non-resource-like file hierarchy under py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 44146
diff changeset
69
43877
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
70 try:
45463
dd9e28612468 resourceutil: document when we expect to take the importlib.resouces code path
Martin von Zweigbergk <martinvonz@google.com>
parents: 44802
diff changeset
71 # importlib.resources exists from Python 3.7; see fallback in except clause
dd9e28612468 resourceutil: document when we expect to take the importlib.resouces code path
Martin von Zweigbergk <martinvonz@google.com>
parents: 44802
diff changeset
72 # further down
48031
c0588d389c5f typing: suppress an import-error warning in `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 47911
diff changeset
73 from importlib import resources # pytype: disable=import-error
43877
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
74
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
75 # Force loading of the resources module
51717
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
76 if hasattr(resources, 'files'): # Introduced in Python 3.9
49638
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
77 resources.files # pytype: disable=module-attr
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
78 else:
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
79 resources.open_binary # pytype: disable=module-attr
43877
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
80
49129
4ba27acdea63 resourceutil: force filesystem access to resources when using py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 48966
diff changeset
81 # py2exe raises an AssertionError if uses importlib.resources
4ba27acdea63 resourceutil: force filesystem access to resources when using py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 48966
diff changeset
82 if getattr(sys, "frozen", None) in ("console_exe", "windows_exe"):
4ba27acdea63 resourceutil: force filesystem access to resources when using py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 48966
diff changeset
83 raise ImportError
4ba27acdea63 resourceutil: force filesystem access to resources when using py2exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 48966
diff changeset
84
43960
75ec46c63657 resourceutil: use `from importlib import resources`
Martin von Zweigbergk <martinvonz@google.com>
parents: 43911
diff changeset
85 except (ImportError, AttributeError):
45463
dd9e28612468 resourceutil: document when we expect to take the importlib.resouces code path
Martin von Zweigbergk <martinvonz@google.com>
parents: 44802
diff changeset
86 # importlib.resources was not found (almost definitely because we're on a
dd9e28612468 resourceutil: document when we expect to take the importlib.resouces code path
Martin von Zweigbergk <martinvonz@google.com>
parents: 44802
diff changeset
87 # Python version before 3.7)
43877
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
88
52670
4cb75772818d pyupgrade: drop the quoting around type annotations
Matt Harbison <matt_harbison@yahoo.com>
parents: 52665
diff changeset
89 def open_resource(package: bytes, name: bytes) -> BinaryIO:
43877
1390bb81163e help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents: 43873
diff changeset
90 path = os.path.join(_package_path(package), name)
44174
27787a43712f resourceutil: blacken
Augie Fackler <augie@google.com>
parents: 44148
diff changeset
91 return open(path, "rb")
44030
42a897bf678c resourceutil: implement `is_resource()`
Matt Harbison <matt_harbison@yahoo.com>
parents: 44029
diff changeset
92
51729
32a1c9226dd9 typing: add type hints to `mercurial/utils/resourceutil.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 50951
diff changeset
93 def is_resource(package: bytes, name: bytes) -> bool:
44030
42a897bf678c resourceutil: implement `is_resource()`
Matt Harbison <matt_harbison@yahoo.com>
parents: 44029
diff changeset
94 path = os.path.join(_package_path(package), name)
42a897bf678c resourceutil: implement `is_resource()`
Matt Harbison <matt_harbison@yahoo.com>
parents: 44029
diff changeset
95
42a897bf678c resourceutil: implement `is_resource()`
Matt Harbison <matt_harbison@yahoo.com>
parents: 44029
diff changeset
96 try:
42a897bf678c resourceutil: implement `is_resource()`
Matt Harbison <matt_harbison@yahoo.com>
parents: 44029
diff changeset
97 return os.path.isfile(pycompat.fsdecode(path))
52665
24ee91ba9aa8 pyupgrade: drop usage of py3 aliases for `OSError`
Matt Harbison <matt_harbison@yahoo.com>
parents: 52118
diff changeset
98 except OSError:
44030
42a897bf678c resourceutil: implement `is_resource()`
Matt Harbison <matt_harbison@yahoo.com>
parents: 44029
diff changeset
99 return False
44031
bba9149adc14 resourceutil: implement `contents()` to iterate over resources in a package
Matt Harbison <matt_harbison@yahoo.com>
parents: 44030
diff changeset
100
52670
4cb75772818d pyupgrade: drop the quoting around type annotations
Matt Harbison <matt_harbison@yahoo.com>
parents: 52665
diff changeset
101 def contents(package: bytes) -> Iterator[bytes]:
44031
bba9149adc14 resourceutil: implement `contents()` to iterate over resources in a package
Matt Harbison <matt_harbison@yahoo.com>
parents: 44030
diff changeset
102 path = pycompat.fsdecode(_package_path(package))
bba9149adc14 resourceutil: implement `contents()` to iterate over resources in a package
Matt Harbison <matt_harbison@yahoo.com>
parents: 44030
diff changeset
103
bba9149adc14 resourceutil: implement `contents()` to iterate over resources in a package
Matt Harbison <matt_harbison@yahoo.com>
parents: 44030
diff changeset
104 for p in os.listdir(path):
bba9149adc14 resourceutil: implement `contents()` to iterate over resources in a package
Matt Harbison <matt_harbison@yahoo.com>
parents: 44030
diff changeset
105 yield pycompat.fsencode(p)
47911
08f16b3331df resources: narrow the try:except clause to minimum
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47853
diff changeset
106
08f16b3331df resources: narrow the try:except clause to minimum
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47853
diff changeset
107 else:
08f16b3331df resources: narrow the try:except clause to minimum
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47853
diff changeset
108 from .. import encoding
08f16b3331df resources: narrow the try:except clause to minimum
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47853
diff changeset
109
52670
4cb75772818d pyupgrade: drop the quoting around type annotations
Matt Harbison <matt_harbison@yahoo.com>
parents: 52665
diff changeset
110 def open_resource(package: bytes, name: bytes) -> BinaryIO:
50951
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50752
diff changeset
111 if hasattr(resources, 'files'):
49638
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
112 return (
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
113 resources.files( # pytype: disable=module-attr
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
114 pycompat.sysstr(package)
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
115 )
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
116 .joinpath(pycompat.sysstr(name))
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
117 .open('rb')
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
118 )
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
119 else:
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
120 return resources.open_binary( # pytype: disable=module-attr
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
121 pycompat.sysstr(package), pycompat.sysstr(name)
330d88217b83 resourceutil: start using importlib.resources.files() when possible
Anton Shestakov <av6@dwimlabs.net>
parents: 49129
diff changeset
122 )
47911
08f16b3331df resources: narrow the try:except clause to minimum
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47853
diff changeset
123
51717
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
124 def is_resource(package: bytes, name: bytes) -> bool:
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
125 if hasattr(resources, 'files'): # Introduced in Python 3.9
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
126 return (
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
127 resources.files(pycompat.sysstr(package))
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
128 .joinpath(encoding.strfromlocal(name))
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
129 .is_file()
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
130 )
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
131 else:
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
132 return resources.is_resource( # pytype: disable=module-attr
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
133 pycompat.sysstr(package), encoding.strfromlocal(name)
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
134 )
47911
08f16b3331df resources: narrow the try:except clause to minimum
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47853
diff changeset
135
52670
4cb75772818d pyupgrade: drop the quoting around type annotations
Matt Harbison <matt_harbison@yahoo.com>
parents: 52665
diff changeset
136 def contents(package: bytes) -> Iterator[bytes]:
51717
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
137 if hasattr(resources, 'files'): # Introduced in Python 3.9
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
138 for path in resources.files(pycompat.sysstr(package)).iterdir():
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
139 if path.is_file():
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
140 yield encoding.strtolocal(path.name)
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
141 else:
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
142 # pytype: disable=module-attr
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
143 for r in resources.contents(pycompat.sysstr(package)):
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
144 # pytype: enable=module-attr
747a1370c598 utils: fix resourceutil use of deprecated importlib.resources
Mads Kiilerich <mads@kiilerich.com>
parents: 51716
diff changeset
145 yield encoding.strtolocal(r)