File: test_macholib.py

package info (click to toggle)
python2.7 2.7.9-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 73,812 kB
  • sloc: python: 440,005; ansic: 438,254; sh: 17,372; asm: 14,304; makefile: 4,744; objc: 761; exp: 499; cpp: 128; xml: 73
file content (65 lines) | stat: -rw-r--r-- 1,798 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
import unittest

# Bob Ippolito:
"""
Ok.. the code to find the filename for __getattr__ should look
something like:

import os
from macholib.dyld import dyld_find

def find_lib(name):
     possible = ['lib'+name+'.dylib', name+'.dylib',
     name+'.framework/'+name]
     for dylib in possible:
         try:
             return os.path.realpath(dyld_find(dylib))
         except ValueError:
             pass
     raise ValueError, "%s not found" % (name,)

It'll have output like this:

 >>> find_lib('pthread')
'/usr/lib/libSystem.B.dylib'
 >>> find_lib('z')
'/usr/lib/libz.1.dylib'
 >>> find_lib('IOKit')
'/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit'

-bob

"""

from ctypes.macholib.dyld import dyld_find

def find_lib(name):
    possible = ['lib'+name+'.dylib', name+'.dylib', name+'.framework/'+name]
    for dylib in possible:
        try:
            return os.path.realpath(dyld_find(dylib))
        except ValueError:
            pass
    raise ValueError("%s not found" % (name,))

class MachOTest(unittest.TestCase):
    @unittest.skipUnless(sys.platform == "darwin", 'OSX-specific test')
    def test_find(self):

        self.assertEqual(find_lib('pthread'),
                             '/usr/lib/libSystem.B.dylib')

        result = find_lib('z')
        # Issue #21093: dyld default search path includes $HOME/lib and
        # /usr/local/lib before /usr/lib, which caused test failures if
        # a local copy of libz exists in one of them. Now ignore the head
        # of the path.
        self.assertRegexpMatches(result, r".*/lib/libz\..*.*\.dylib")

        self.assertEqual(find_lib('IOKit'),
                             '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit')

if __name__ == "__main__":
    unittest.main()