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