1 | n/a | import os |
---|
2 | n/a | import sys |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | # Bob Ippolito: |
---|
6 | n/a | # |
---|
7 | n/a | # Ok.. the code to find the filename for __getattr__ should look |
---|
8 | n/a | # something like: |
---|
9 | n/a | # |
---|
10 | n/a | # import os |
---|
11 | n/a | # from macholib.dyld import dyld_find |
---|
12 | n/a | # |
---|
13 | n/a | # def find_lib(name): |
---|
14 | n/a | # possible = ['lib'+name+'.dylib', name+'.dylib', |
---|
15 | n/a | # name+'.framework/'+name] |
---|
16 | n/a | # for dylib in possible: |
---|
17 | n/a | # try: |
---|
18 | n/a | # return os.path.realpath(dyld_find(dylib)) |
---|
19 | n/a | # except ValueError: |
---|
20 | n/a | # pass |
---|
21 | n/a | # raise ValueError, "%s not found" % (name,) |
---|
22 | n/a | # |
---|
23 | n/a | # It'll have output like this: |
---|
24 | n/a | # |
---|
25 | n/a | # >>> find_lib('pthread') |
---|
26 | n/a | # '/usr/lib/libSystem.B.dylib' |
---|
27 | n/a | # >>> find_lib('z') |
---|
28 | n/a | # '/usr/lib/libz.1.dylib' |
---|
29 | n/a | # >>> find_lib('IOKit') |
---|
30 | n/a | # '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit' |
---|
31 | n/a | # |
---|
32 | n/a | # -bob |
---|
33 | n/a | |
---|
34 | n/a | from ctypes.macholib.dyld import dyld_find |
---|
35 | n/a | |
---|
36 | n/a | def find_lib(name): |
---|
37 | n/a | possible = ['lib'+name+'.dylib', name+'.dylib', name+'.framework/'+name] |
---|
38 | n/a | for dylib in possible: |
---|
39 | n/a | try: |
---|
40 | n/a | return os.path.realpath(dyld_find(dylib)) |
---|
41 | n/a | except ValueError: |
---|
42 | n/a | pass |
---|
43 | n/a | raise ValueError("%s not found" % (name,)) |
---|
44 | n/a | |
---|
45 | n/a | class MachOTest(unittest.TestCase): |
---|
46 | n/a | @unittest.skipUnless(sys.platform == "darwin", 'OSX-specific test') |
---|
47 | n/a | def test_find(self): |
---|
48 | n/a | |
---|
49 | n/a | self.assertEqual(find_lib('pthread'), |
---|
50 | n/a | '/usr/lib/libSystem.B.dylib') |
---|
51 | n/a | |
---|
52 | n/a | result = find_lib('z') |
---|
53 | n/a | # Issue #21093: dyld default search path includes $HOME/lib and |
---|
54 | n/a | # /usr/local/lib before /usr/lib, which caused test failures if |
---|
55 | n/a | # a local copy of libz exists in one of them. Now ignore the head |
---|
56 | n/a | # of the path. |
---|
57 | n/a | self.assertRegex(result, r".*/lib/libz\..*.*\.dylib") |
---|
58 | n/a | |
---|
59 | n/a | self.assertEqual(find_lib('IOKit'), |
---|
60 | n/a | '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit') |
---|
61 | n/a | |
---|
62 | n/a | if __name__ == "__main__": |
---|
63 | n/a | unittest.main() |
---|