Show More
@@ -1,105 +1,105 b'' | |||||
1 | # resourceutil.py - utility for looking up resources |
|
1 | # resourceutil.py - utility for looking up resources | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> | |
4 | # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> |
|
4 | # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> | |
5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | |
6 | # |
|
6 | # | |
7 | # This software may be used and distributed according to the terms of the |
|
7 | # This software may be used and distributed according to the terms of the | |
8 | # GNU General Public License version 2 or any later version. |
|
8 | # GNU General Public License version 2 or any later version. | |
9 |
|
9 | |||
10 | from __future__ import absolute_import |
|
10 | from __future__ import absolute_import | |
11 |
|
11 | |||
12 | import imp |
|
12 | import imp | |
13 | import os |
|
13 | import os | |
14 | import sys |
|
14 | import sys | |
15 |
|
15 | |||
16 | from .. import pycompat |
|
16 | from .. import pycompat | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | def mainfrozen(): |
|
19 | def mainfrozen(): | |
20 | """return True if we are a frozen executable. |
|
20 | """return True if we are a frozen executable. | |
21 |
|
21 | |||
22 | The code supports py2exe (most common, Windows only) and tools/freeze |
|
22 | The code supports py2exe (most common, Windows only) and tools/freeze | |
23 | (portable, not much used). |
|
23 | (portable, not much used). | |
24 | """ |
|
24 | """ | |
25 | return ( |
|
25 | return ( | |
26 | pycompat.safehasattr(sys, "frozen") # new py2exe |
|
26 | pycompat.safehasattr(sys, "frozen") # new py2exe | |
27 | or pycompat.safehasattr(sys, "importers") # old py2exe |
|
27 | or pycompat.safehasattr(sys, "importers") # old py2exe | |
28 | or imp.is_frozen("__main__") # tools/freeze |
|
28 | or imp.is_frozen("__main__") # tools/freeze | |
29 | ) |
|
29 | ) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | # the location of data files matching the source code |
|
32 | # the location of data files matching the source code | |
33 | if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app": |
|
33 | if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app": | |
34 | # executable version (py2exe) doesn't support __file__ |
|
34 | # executable version (py2exe) doesn't support __file__ | |
35 | datapath = os.path.dirname(pycompat.sysexecutable) |
|
35 | datapath = os.path.dirname(pycompat.sysexecutable) | |
36 | _rootpath = datapath |
|
36 | _rootpath = datapath | |
37 |
|
37 | |||
38 | # The installers store the files outside of library.zip, like |
|
38 | # The installers store the files outside of library.zip, like | |
39 | # C:\Program Files\Mercurial\defaultrc\*.rc. This strips the |
|
39 | # C:\Program Files\Mercurial\defaultrc\*.rc. This strips the | |
40 | # leading "mercurial." off of the package name, so that these |
|
40 | # leading "mercurial." off of the package name, so that these | |
41 | # pseudo resources are found in their directory next to the |
|
41 | # pseudo resources are found in their directory next to the | |
42 | # executable. |
|
42 | # executable. | |
43 | def _package_path(package): |
|
43 | def _package_path(package): | |
44 | dirs = package.split(b".") |
|
44 | dirs = package.split(b".") | |
45 | assert dirs[0] == b"mercurial" |
|
45 | assert dirs[0] == b"mercurial" | |
46 | return os.path.join(_rootpath, *dirs[1:]) |
|
46 | return os.path.join(_rootpath, *dirs[1:]) | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | else: |
|
49 | else: | |
50 | datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) |
|
50 | datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) | |
51 | _rootpath = os.path.dirname(datapath) |
|
51 | _rootpath = os.path.dirname(datapath) | |
52 |
|
52 | |||
53 | def _package_path(package): |
|
53 | def _package_path(package): | |
54 | return os.path.join(_rootpath, *package.split(b".")) |
|
54 | return os.path.join(_rootpath, *package.split(b".")) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | try: |
|
57 | try: | |
58 | # importlib.resources exists from Python 3.7; see fallback in except clause |
|
58 | # importlib.resources exists from Python 3.7; see fallback in except clause | |
59 | # further down |
|
59 | # further down | |
60 | from importlib import resources |
|
60 | from importlib import resources # pytype: disable=import-error | |
61 |
|
61 | |||
62 | # Force loading of the resources module |
|
62 | # Force loading of the resources module | |
63 | resources.open_binary # pytype: disable=module-attr |
|
63 | resources.open_binary # pytype: disable=module-attr | |
64 |
|
64 | |||
65 | except (ImportError, AttributeError): |
|
65 | except (ImportError, AttributeError): | |
66 | # importlib.resources was not found (almost definitely because we're on a |
|
66 | # importlib.resources was not found (almost definitely because we're on a | |
67 | # Python version before 3.7) |
|
67 | # Python version before 3.7) | |
68 |
|
68 | |||
69 | def open_resource(package, name): |
|
69 | def open_resource(package, name): | |
70 | path = os.path.join(_package_path(package), name) |
|
70 | path = os.path.join(_package_path(package), name) | |
71 | return open(path, "rb") |
|
71 | return open(path, "rb") | |
72 |
|
72 | |||
73 | def is_resource(package, name): |
|
73 | def is_resource(package, name): | |
74 | path = os.path.join(_package_path(package), name) |
|
74 | path = os.path.join(_package_path(package), name) | |
75 |
|
75 | |||
76 | try: |
|
76 | try: | |
77 | return os.path.isfile(pycompat.fsdecode(path)) |
|
77 | return os.path.isfile(pycompat.fsdecode(path)) | |
78 | except (IOError, OSError): |
|
78 | except (IOError, OSError): | |
79 | return False |
|
79 | return False | |
80 |
|
80 | |||
81 | def contents(package): |
|
81 | def contents(package): | |
82 | path = pycompat.fsdecode(_package_path(package)) |
|
82 | path = pycompat.fsdecode(_package_path(package)) | |
83 |
|
83 | |||
84 | for p in os.listdir(path): |
|
84 | for p in os.listdir(path): | |
85 | yield pycompat.fsencode(p) |
|
85 | yield pycompat.fsencode(p) | |
86 |
|
86 | |||
87 |
|
87 | |||
88 | else: |
|
88 | else: | |
89 | from .. import encoding |
|
89 | from .. import encoding | |
90 |
|
90 | |||
91 | def open_resource(package, name): |
|
91 | def open_resource(package, name): | |
92 | return resources.open_binary( # pytype: disable=module-attr |
|
92 | return resources.open_binary( # pytype: disable=module-attr | |
93 | pycompat.sysstr(package), pycompat.sysstr(name) |
|
93 | pycompat.sysstr(package), pycompat.sysstr(name) | |
94 | ) |
|
94 | ) | |
95 |
|
95 | |||
96 | def is_resource(package, name): |
|
96 | def is_resource(package, name): | |
97 | return resources.is_resource( # pytype: disable=module-attr |
|
97 | return resources.is_resource( # pytype: disable=module-attr | |
98 | pycompat.sysstr(package), encoding.strfromlocal(name) |
|
98 | pycompat.sysstr(package), encoding.strfromlocal(name) | |
99 | ) |
|
99 | ) | |
100 |
|
100 | |||
101 | def contents(package): |
|
101 | def contents(package): | |
102 | # pytype: disable=module-attr |
|
102 | # pytype: disable=module-attr | |
103 | for r in resources.contents(pycompat.sysstr(package)): |
|
103 | for r in resources.contents(pycompat.sysstr(package)): | |
104 | # pytype: enable=module-attr |
|
104 | # pytype: enable=module-attr | |
105 | yield encoding.strtolocal(r) |
|
105 | yield encoding.strtolocal(r) |
General Comments 0
You need to be logged in to leave comments.
Login now