##// END OF EJS Templates
utils: avoid using internal _imp.is_frozen()...
Mads Kiilerich -
r53003:c5d6a660 stable
parent child Browse files
Show More
@@ -1,120 +1,122
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 Olivia Mackall <olivia@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
11 import _imp
12 11 import os
13 12 import sys
14 13
15 14 from .. import pycompat
16 15
17 16
18 17 def mainfrozen():
19 18 """return True if we are a frozen executable.
20 19
21 20 The code supports py2exe (most common, Windows only) and tools/freeze
22 21 (portable, not much used).
23 22 """
24 23 return (
25 24 hasattr(sys, "frozen") # new py2exe
26 25 or hasattr(sys, "importers") # old py2exe
27 or _imp.is_frozen("__main__") # tools/freeze
26 or getattr(
27 getattr(sys.modules.get('__main__'), '__spec__', None),
28 'origin',
29 None,
30 )
31 == 'frozen' # tools/freeze
28 32 )
29 33
30 34
31 35 # the location of data files matching the source code
32 36 if mainfrozen() and getattr(sys, "frozen", None) != "macosx_app":
33 37 # executable version (py2exe) doesn't support __file__
34 38 datapath = os.path.dirname(pycompat.sysexecutable)
35 39 _rootpath = datapath
36 40
37 41 # The installers store the files outside of library.zip, like
38 42 # C:\Program Files\Mercurial\defaultrc\*.rc. This strips the
39 43 # leading "mercurial." off of the package name, so that these
40 44 # pseudo resources are found in their directory next to the
41 45 # executable.
42 46 def _package_path(package):
43 47 dirs = package.split(b".")
44 48 assert dirs[0] == b"mercurial"
45 49 return os.path.join(_rootpath, *dirs[1:])
46 50
47
48 51 else:
49 52 datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__)))
50 53 _rootpath = os.path.dirname(datapath)
51 54
52 55 def _package_path(package):
53 56 return os.path.join(_rootpath, *package.split(b"."))
54 57
55 58
56 59 try:
57 60 # importlib.resources exists from Python 3.7; see fallback in except clause
58 61 # further down
59 62 from importlib import resources # pytype: disable=import-error
60 63
61 64 # Force loading of the resources module
62 65 if hasattr(resources, 'files'):
63 66 resources.files # pytype: disable=module-attr
64 67 else:
65 68 resources.open_binary # pytype: disable=module-attr
66 69
67 70 # py2exe raises an AssertionError if uses importlib.resources
68 71 if getattr(sys, "frozen", None) in ("console_exe", "windows_exe"):
69 72 raise ImportError
70 73
71 74 except (ImportError, AttributeError):
72 75 # importlib.resources was not found (almost definitely because we're on a
73 76 # Python version before 3.7)
74 77
75 78 def open_resource(package, name):
76 79 path = os.path.join(_package_path(package), name)
77 80 return open(path, "rb")
78 81
79 82 def is_resource(package, name):
80 83 path = os.path.join(_package_path(package), name)
81 84
82 85 try:
83 86 return os.path.isfile(pycompat.fsdecode(path))
84 87 except (IOError, OSError):
85 88 return False
86 89
87 90 def contents(package):
88 91 path = pycompat.fsdecode(_package_path(package))
89 92
90 93 for p in os.listdir(path):
91 94 yield pycompat.fsencode(p)
92 95
93
94 96 else:
95 97 from .. import encoding
96 98
97 99 def open_resource(package, name):
98 100 if hasattr(resources, 'files'):
99 101 return (
100 102 resources.files( # pytype: disable=module-attr
101 103 pycompat.sysstr(package)
102 104 )
103 105 .joinpath(pycompat.sysstr(name))
104 106 .open('rb')
105 107 )
106 108 else:
107 109 return resources.open_binary( # pytype: disable=module-attr
108 110 pycompat.sysstr(package), pycompat.sysstr(name)
109 111 )
110 112
111 113 def is_resource(package, name):
112 114 return resources.is_resource( # pytype: disable=module-attr
113 115 pycompat.sysstr(package), encoding.strfromlocal(name)
114 116 )
115 117
116 118 def contents(package):
117 119 # pytype: disable=module-attr
118 120 for r in resources.contents(pycompat.sysstr(package)):
119 121 # pytype: enable=module-attr
120 122 yield encoding.strtolocal(r)
General Comments 0
You need to be logged in to leave comments. Login now