|
4 | 4 | import glob |
5 | 5 | import os.path |
6 | 6 |
|
7 | | -def _lib_candidates(): |
8 | 7 |
|
9 | | - yield find_library('magic') |
| 8 | +def _lib_candidates_linux(): |
| 9 | + """Yield possible libmagic library names on Linux. |
| 10 | +
|
| 11 | + This is necessary because alpine is bad |
| 12 | + """ |
| 13 | + yield "libmagic.so.1" |
10 | 14 |
|
11 | | - if sys.platform == 'darwin': |
12 | 15 |
|
| 16 | +def _lib_candidates_macos(): |
| 17 | + """Yield possible libmagic library names on macOS.""" |
13 | 18 | paths = [ |
14 | | - '/opt/local/lib', |
15 | | - '/usr/local/lib', |
16 | | - '/opt/homebrew/lib', |
17 | | - ] + glob.glob('/usr/local/Cellar/libmagic/*/lib') |
| 19 | + "/opt/homebrew/lib", |
| 20 | + "/opt/local/lib", |
| 21 | + "/usr/local/lib", |
| 22 | + ] + glob.glob("/usr/local/Cellar/libmagic/*/lib") |
18 | 23 |
|
19 | | - for i in paths: |
20 | | - yield os.path.join(i, 'libmagic.dylib') |
| 24 | + for path in paths: |
| 25 | + yield os.path.join(path, "libmagic.dylib") |
21 | 26 |
|
22 | | - elif sys.platform in ('win32', 'cygwin'): |
23 | 27 |
|
24 | | - prefixes = ['libmagic', 'magic1', 'magic-1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1'] |
| 28 | +def _lib_candidates_windows(): |
| 29 | + """Yield possible libmagic library names on Windows.""" |
| 30 | + prefixes = ( |
| 31 | + "libmagic", |
| 32 | + "magic1", |
| 33 | + "magic-1", |
| 34 | + "cygmagic-1", |
| 35 | + "libmagic-1", |
| 36 | + "msys-magic-1", |
| 37 | + ) |
25 | 38 |
|
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) |
| 39 | + for prefix in prefixes: |
| 40 | + # find_library searches in %PATH% but not the current directory, |
| 41 | + # so look for both |
| 42 | + yield "./%s.dll" % (prefix,) |
| 43 | + yield find_library(prefix) |
31 | 44 |
|
32 | | - elif sys.platform == 'linux': |
33 | | - # This is necessary because alpine is bad |
34 | | - yield 'libmagic.so.1' |
35 | 45 |
|
| 46 | +def _lib_candidates(): |
| 47 | + yield find_library("magic") |
36 | 48 |
|
37 | | -def load_lib(): |
| 49 | + func = { |
| 50 | + "cygwin": _lib_candidates_windows, |
| 51 | + "darwin": _lib_candidates_macos, |
| 52 | + "linux": _lib_candidates_linux, |
| 53 | + "win32": _lib_candidates_windows, |
| 54 | + }[sys.platform] |
| 55 | + # When we drop legacy Python, we can just `yield from func()` |
| 56 | + for path in func(): |
| 57 | + yield path |
38 | 58 |
|
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 | 59 |
|
| 60 | +def load_lib(): |
| 61 | + for lib in _lib_candidates(): |
| 62 | + # find_library returns None when lib not found |
| 63 | + if lib: |
| 64 | + try: |
| 65 | + return ctypes.CDLL(lib) |
| 66 | + except OSError: |
| 67 | + pass |
| 68 | + else: |
| 69 | + # It is better to raise an ImportError since we are importing magic module |
| 70 | + raise ImportError("failed to find libmagic. Check your installation") |
0 commit comments