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