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