ยปCore Development>Code coverage>Lib/ctypes/macholib/dyld.py

Python code coverage for Lib/ctypes/macholib/dyld.py

#countcontent
1n/a"""
2n/adyld emulation
3n/a"""
4n/a
5n/aimport os
6n/afrom ctypes.macholib.framework import framework_info
7n/afrom ctypes.macholib.dylib import dylib_info
8n/afrom itertools import *
9n/a
10n/a__all__ = [
11n/a 'dyld_find', 'framework_find',
12n/a 'framework_info', 'dylib_info',
13n/a]
14n/a
15n/a# These are the defaults as per man dyld(1)
16n/a#
17n/aDEFAULT_FRAMEWORK_FALLBACK = [
18n/a os.path.expanduser("~/Library/Frameworks"),
19n/a "/Library/Frameworks",
20n/a "/Network/Library/Frameworks",
21n/a "/System/Library/Frameworks",
22n/a]
23n/a
24n/aDEFAULT_LIBRARY_FALLBACK = [
25n/a os.path.expanduser("~/lib"),
26n/a "/usr/local/lib",
27n/a "/lib",
28n/a "/usr/lib",
29n/a]
30n/a
31n/adef dyld_env(env, var):
32n/a if env is None:
33n/a env = os.environ
34n/a rval = env.get(var)
35n/a if rval is None:
36n/a return []
37n/a return rval.split(':')
38n/a
39n/adef dyld_image_suffix(env=None):
40n/a if env is None:
41n/a env = os.environ
42n/a return env.get('DYLD_IMAGE_SUFFIX')
43n/a
44n/adef dyld_framework_path(env=None):
45n/a return dyld_env(env, 'DYLD_FRAMEWORK_PATH')
46n/a
47n/adef dyld_library_path(env=None):
48n/a return dyld_env(env, 'DYLD_LIBRARY_PATH')
49n/a
50n/adef dyld_fallback_framework_path(env=None):
51n/a return dyld_env(env, 'DYLD_FALLBACK_FRAMEWORK_PATH')
52n/a
53n/adef dyld_fallback_library_path(env=None):
54n/a return dyld_env(env, 'DYLD_FALLBACK_LIBRARY_PATH')
55n/a
56n/adef dyld_image_suffix_search(iterator, env=None):
57n/a """For a potential path iterator, add DYLD_IMAGE_SUFFIX semantics"""
58n/a suffix = dyld_image_suffix(env)
59n/a if suffix is None:
60n/a return iterator
61n/a def _inject(iterator=iterator, suffix=suffix):
62n/a for path in iterator:
63n/a if path.endswith('.dylib'):
64n/a yield path[:-len('.dylib')] + suffix + '.dylib'
65n/a else:
66n/a yield path + suffix
67n/a yield path
68n/a return _inject()
69n/a
70n/adef dyld_override_search(name, env=None):
71n/a # If DYLD_FRAMEWORK_PATH is set and this dylib_name is a
72n/a # framework name, use the first file that exists in the framework
73n/a # path if any. If there is none go on to search the DYLD_LIBRARY_PATH
74n/a # if any.
75n/a
76n/a framework = framework_info(name)
77n/a
78n/a if framework is not None:
79n/a for path in dyld_framework_path(env):
80n/a yield os.path.join(path, framework['name'])
81n/a
82n/a # If DYLD_LIBRARY_PATH is set then use the first file that exists
83n/a # in the path. If none use the original name.
84n/a for path in dyld_library_path(env):
85n/a yield os.path.join(path, os.path.basename(name))
86n/a
87n/adef dyld_executable_path_search(name, executable_path=None):
88n/a # If we haven't done any searching and found a library and the
89n/a # dylib_name starts with "@executable_path/" then construct the
90n/a # library name.
91n/a if name.startswith('@executable_path/') and executable_path is not None:
92n/a yield os.path.join(executable_path, name[len('@executable_path/'):])
93n/a
94n/adef dyld_default_search(name, env=None):
95n/a yield name
96n/a
97n/a framework = framework_info(name)
98n/a
99n/a if framework is not None:
100n/a fallback_framework_path = dyld_fallback_framework_path(env)
101n/a for path in fallback_framework_path:
102n/a yield os.path.join(path, framework['name'])
103n/a
104n/a fallback_library_path = dyld_fallback_library_path(env)
105n/a for path in fallback_library_path:
106n/a yield os.path.join(path, os.path.basename(name))
107n/a
108n/a if framework is not None and not fallback_framework_path:
109n/a for path in DEFAULT_FRAMEWORK_FALLBACK:
110n/a yield os.path.join(path, framework['name'])
111n/a
112n/a if not fallback_library_path:
113n/a for path in DEFAULT_LIBRARY_FALLBACK:
114n/a yield os.path.join(path, os.path.basename(name))
115n/a
116n/adef dyld_find(name, executable_path=None, env=None):
117n/a """
118n/a Find a library or framework using dyld semantics
119n/a """
120n/a for path in dyld_image_suffix_search(chain(
121n/a dyld_override_search(name, env),
122n/a dyld_executable_path_search(name, executable_path),
123n/a dyld_default_search(name, env),
124n/a ), env):
125n/a if os.path.isfile(path):
126n/a return path
127n/a raise ValueError("dylib %s could not be found" % (name,))
128n/a
129n/adef framework_find(fn, executable_path=None, env=None):
130n/a """
131n/a Find a framework using dyld semantics in a very loose manner.
132n/a
133n/a Will take input such as:
134n/a Python
135n/a Python.framework
136n/a Python.framework/Versions/Current
137n/a """
138n/a error = None
139n/a try:
140n/a return dyld_find(fn, executable_path=executable_path, env=env)
141n/a except ValueError as e:
142n/a error = e
143n/a fmwk_index = fn.rfind('.framework')
144n/a if fmwk_index == -1:
145n/a fmwk_index = len(fn)
146n/a fn += '.framework'
147n/a fn = os.path.join(fn, os.path.basename(fn[:fmwk_index]))
148n/a try:
149n/a return dyld_find(fn, executable_path=executable_path, env=env)
150n/a except ValueError:
151n/a raise error
152n/a
153n/adef test_dyld_find():
154n/a env = {}
155n/a assert dyld_find('libSystem.dylib') == '/usr/lib/libSystem.dylib'
156n/a assert dyld_find('System.framework/System') == '/System/Library/Frameworks/System.framework/System'
157n/a
158n/aif __name__ == '__main__':
159n/a test_dyld_find()