Show More
@@ -1,59 +1,74 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 Matt Mackall <mpm@selenic.com> |
|
4 | # Copyright 2005-2007 Matt Mackall <mpm@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") |
|
26 | pycompat.safehasattr(sys, "frozen") | |
27 | or pycompat.safehasattr(sys, "importers") # new py2exe |
|
27 | or pycompat.safehasattr(sys, "importers") # new py2exe | |
28 | or imp.is_frozen("__main__") # old py2exe |
|
28 | or imp.is_frozen("__main__") # old py2exe | |
29 | ) # tools/freeze |
|
29 | ) # tools/freeze | |
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 | else: |
|
36 | else: | |
37 | datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) |
|
37 | datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) | |
38 | _rootpath = os.path.dirname(datapath) |
|
38 | _rootpath = os.path.dirname(datapath) | |
39 |
|
39 | |||
40 | try: |
|
40 | try: | |
41 | from importlib import resources |
|
41 | from importlib import resources | |
42 |
|
42 | |||
|
43 | from .. import encoding | |||
|
44 | ||||
43 | # Force loading of the resources module |
|
45 | # Force loading of the resources module | |
44 | resources.open_binary # pytype: disable=module-attr |
|
46 | resources.open_binary # pytype: disable=module-attr | |
45 |
|
47 | |||
46 | def open_resource(package, name): |
|
48 | def open_resource(package, name): | |
47 | return resources.open_binary( # pytype: disable=module-attr |
|
49 | return resources.open_binary( # pytype: disable=module-attr | |
48 | pycompat.sysstr(package), pycompat.sysstr(name) |
|
50 | pycompat.sysstr(package), pycompat.sysstr(name) | |
49 | ) |
|
51 | ) | |
50 |
|
52 | |||
|
53 | def is_resource(package, name): | |||
|
54 | return resources.is_resource( | |||
|
55 | pycompat.sysstr(package), encoding.strfromlocal(name) | |||
|
56 | ) | |||
|
57 | ||||
51 |
|
58 | |||
52 | except (ImportError, AttributeError): |
|
59 | except (ImportError, AttributeError): | |
53 |
|
60 | |||
54 | def _package_path(package): |
|
61 | def _package_path(package): | |
55 | return os.path.join(_rootpath, *package.split(b'.')) |
|
62 | return os.path.join(_rootpath, *package.split(b'.')) | |
56 |
|
63 | |||
57 | def open_resource(package, name): |
|
64 | def open_resource(package, name): | |
58 | path = os.path.join(_package_path(package), name) |
|
65 | path = os.path.join(_package_path(package), name) | |
59 | return open(path, 'rb') |
|
66 | return open(path, 'rb') | |
|
67 | ||||
|
68 | def is_resource(package, name): | |||
|
69 | path = os.path.join(_package_path(package), name) | |||
|
70 | ||||
|
71 | try: | |||
|
72 | return os.path.isfile(pycompat.fsdecode(path)) | |||
|
73 | except (IOError, OSError): | |||
|
74 | return False |
General Comments 0
You need to be logged in to leave comments.
Login now