forked from ahupp/python-magic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
68 lines (55 loc) · 1.72 KB
/
loader.py
File metadata and controls
68 lines (55 loc) · 1.72 KB
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
66
67
68
from ctypes.util import find_library
import ctypes
import sys
import glob
import os.path
def _lib_candidates_linux():
"""Yield possible libmagic library names on Linux.
This is necessary because alpine is bad
"""
yield "libmagic.so.1"
def _lib_candidates_macos():
"""Yield possible libmagic library names on macOS."""
paths = [
"/opt/homebrew/lib",
"/opt/local/lib",
"/usr/local/lib",
] + glob.glob("/usr/local/Cellar/libmagic/*/lib")
for path in paths:
yield os.path.join(path, "libmagic.dylib")
def _lib_candidates_windows():
"""Yield possible libmagic library names on Windows."""
prefixes = (
"libmagic",
"magic1",
"magic-1",
"cygmagic-1",
"libmagic-1",
"msys-magic-1",
)
for prefix in prefixes:
# find_library searches in %PATH% but not the current directory,
# so look for both
yield "./%s.dll" % (prefix,)
yield find_library(prefix)
def _lib_candidates():
yield find_library("magic")
func = {
"cygwin": _lib_candidates_windows,
"darwin": _lib_candidates_macos,
"linux": _lib_candidates_linux,
"win32": _lib_candidates_windows,
}[sys.platform]
# When we drop legacy Python, we can just `yield from func()`
for path in func():
yield path
def load_lib():
for lib in _lib_candidates():
# find_library returns None when lib not found
if lib:
try:
return ctypes.CDLL(lib)
except OSError:
pass
# It is better to raise an ImportError since we are importing magic module
raise ImportError("failed to find libmagic. Check your installation")