Mercurial > hg
annotate mercurial/utils/resourceutil.py @ 43905:4d59cc8bda65
pytype: suppress warnings about no 'open_binary' on importlib.resources
Fixes these pytype warnings:
line 43, in <module>: No attribute 'open_binary' on module 'importlib.resources' [module-attr]
line 47, in open_resource: No attribute 'open_binary' on module 'importlib.resources' [module-attr]
For some reason, I can't upgrade from 3.6.8 in my WSL environment.
Differential Revision: https://phab.mercurial-scm.org/D7681
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sun, 15 Dec 2019 23:46:10 -0500 |
parents | 1390bb81163e |
children | 75ec46c63657 |
rev | line source |
---|---|
43671
664e24207728
procutil: move mainfrozen() to new resourceutil.py
Martin von Zweigbergk <martinvonz@google.com>
parents:
43657
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> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
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 | 8 # GNU General Public License version 2 or any later version. |
1082 | 9 |
37118
5be286db5fb5
procutil: move process/executable management functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37117
diff
changeset
|
10 from __future__ import absolute_import |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
11 |
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
12 import imp |
43672
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
13 import os |
27358
ac839ee45b6a
util: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27357
diff
changeset
|
14 import sys |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
15 |
43671
664e24207728
procutil: move mainfrozen() to new resourceutil.py
Martin von Zweigbergk <martinvonz@google.com>
parents:
43657
diff
changeset
|
16 from .. import pycompat |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
17 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
18 |
14228
116de1da2154
rename util.main_is_frozen to mainfrozen
Adrian Buehlmann <adrian@cadifra.com>
parents:
14167
diff
changeset
|
19 def mainfrozen(): |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
20 """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
|
21 |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
22 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
|
23 (portable, not much used). |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
24 """ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
25 return ( |
43115
4aa72cdf616f
py3: delete b'' prefix from safehasattr arguments
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
26 pycompat.safehasattr(sys, "frozen") |
4aa72cdf616f
py3: delete b'' prefix from safehasattr arguments
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
27 or pycompat.safehasattr(sys, "importers") # new py2exe |
43503
313e3a279828
cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents:
43478
diff
changeset
|
28 or imp.is_frozen("__main__") # old py2exe |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
43019
diff
changeset
|
29 ) # tools/freeze |
43672
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
30 |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
31 |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
32 # the location of data files matching the source code |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
33 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app': |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
34 # executable version (py2exe) doesn't support __file__ |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
35 datapath = os.path.dirname(pycompat.sysexecutable) |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
36 else: |
9fb85668ee15
util: move definition of datapath to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43671
diff
changeset
|
37 datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) |
43871
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
38 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
39 try: |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
40 import importlib |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
41 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
42 # Force loading of the resources module |
43905
4d59cc8bda65
pytype: suppress warnings about no 'open_binary' on importlib.resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43871
diff
changeset
|
43 importlib.resources.open_binary # pytype: disable=module-attr |
43871
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
44 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
45 def open_resource(package, name): |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
46 package = b'mercurial.' + package |
43905
4d59cc8bda65
pytype: suppress warnings about no 'open_binary' on importlib.resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43871
diff
changeset
|
47 return importlib.resources.open_binary( # pytype: disable=module-attr |
43871
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
48 pycompat.sysstr(package), pycompat.sysstr(name) |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
49 ) |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
50 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
51 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
52 except AttributeError: |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
53 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
54 def _package_path(package): |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
55 return os.path.join(datapath, *package.split(b'.')) |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
56 |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
57 def open_resource(package, name): |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
58 path = os.path.join(_package_path(package), name) |
1390bb81163e
help: get helptext/ data from `resources` module if available
Martin von Zweigbergk <martinvonz@google.com>
parents:
43862
diff
changeset
|
59 return open(path, 'rb') |