|
1 | 1 | from ctypes.util import find_library
|
2 | 2 | import ctypes
|
3 |
| -import sys |
4 | 3 | import glob
|
5 | 4 | import os.path
|
| 5 | +import subprocess |
| 6 | +import sys |
6 | 7 |
|
7 | 8 | def _lib_candidates():
|
| 9 | + here = os.path.dirname(__file__) |
8 | 10 |
|
9 |
| - yield find_library('magic') |
| 11 | + if sys.platform == 'darwin': |
10 | 12 |
|
11 |
| - if sys.platform == 'darwin': |
| 13 | + paths = [ |
| 14 | + here, |
| 15 | + os.path.abspath("."), |
| 16 | + '/opt/local/lib', |
| 17 | + '/usr/local/lib', |
| 18 | + '/opt/homebrew/lib', |
| 19 | + ] + glob.glob('/usr/local/Cellar/libmagic/*/lib') |
12 | 20 |
|
13 |
| - paths = [ |
14 |
| - '/opt/local/lib', |
15 |
| - '/usr/local/lib', |
16 |
| - '/opt/homebrew/lib', |
17 |
| - ] + glob.glob('/usr/local/Cellar/libmagic/*/lib') |
| 21 | + for i in paths: |
| 22 | + yield os.path.join(i, 'libmagic.dylib') |
18 | 23 |
|
19 |
| - for i in paths: |
20 |
| - yield os.path.join(i, 'libmagic.dylib') |
| 24 | + elif sys.platform in ('win32', 'cygwin'): |
21 | 25 |
|
22 |
| - elif sys.platform in ('win32', 'cygwin'): |
| 26 | + prefixes = ['libmagic', 'magic1', 'magic-1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1'] |
23 | 27 |
|
24 |
| - prefixes = ['libmagic', 'magic1', 'magic-1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1'] |
| 28 | + for i in prefixes: |
| 29 | + # find_library searches in %PATH% but not the current directory, |
| 30 | + # so look for both |
| 31 | + yield os.path.join(here, '%s.dll' % i) |
| 32 | + yield os.path.join(os.path.abspath("."), '%s.dll' % i) |
| 33 | + yield find_library(i) |
25 | 34 |
|
26 |
| - for i in prefixes: |
27 |
| - # find_library searches in %PATH% but not the current directory, |
28 |
| - # so look for both |
29 |
| - yield './%s.dll' % (i,) |
30 |
| - yield find_library(i) |
| 35 | + elif sys.platform == 'linux': |
| 36 | + # on some linux systems (musl/alpine), find_library('magic') returns None |
| 37 | + yield subprocess.check_output( |
| 38 | + "( ldconfig -p | grep 'libmagic.so.1' | grep -o '/.*' ) || echo '/usr/lib/libmagic.so.1'", |
| 39 | + shell=True, |
| 40 | + universal_newlines=True, |
| 41 | + ).strip() |
| 42 | + yield os.path.join(here, 'libmagic.so.1') |
| 43 | + yield os.path.join(os.path.abspath("."), 'libmagic.so.1') |
31 | 44 |
|
32 |
| - elif sys.platform == 'linux': |
33 |
| - # This is necessary because alpine is bad |
34 |
| - yield 'libmagic.so.1' |
| 45 | + yield find_library('magic') |
35 | 46 |
|
36 | 47 |
|
37 | 48 | def load_lib():
|
38 | 49 |
|
39 |
| - for lib in _lib_candidates(): |
40 |
| - # find_library returns None when lib not found |
41 |
| - if lib is None: |
42 |
| - continue |
43 |
| - try: |
44 |
| - return ctypes.CDLL(lib) |
45 |
| - except OSError: |
46 |
| - pass |
47 |
| - else: |
48 |
| - # It is better to raise an ImportError since we are importing magic module |
49 |
| - raise ImportError('failed to find libmagic. Check your installation') |
| 50 | + for lib in _lib_candidates(): |
| 51 | + # find_library returns None when lib not found |
| 52 | + if lib is None: |
| 53 | + continue |
| 54 | + try: |
| 55 | + return ctypes.CDLL(lib) |
| 56 | + except OSError as exc: |
| 57 | + pass |
| 58 | + else: |
| 59 | + # It is better to raise an ImportError since we are importing magic module |
| 60 | + raise ImportError('failed to find libmagic. Check your installation') |
50 | 61 |
|
0 commit comments