如何使文件魔术模块在Alpine Linux上运行?

时间:2018-12-26 19:47:19

标签: python alpine libmagic

我正在尝试在Alpine Linux上使用文件魔术,并且每次导入AttributeError: Symbol not found: magic_open模块时,它都会magic爆炸。

我注意到有两个Python模块具有相同的magic名称空间,但是由于大多数Linux发行版似乎都在使用file-magic而不是python-magic,所以我决定制作我的模块取决于前者。但是在Alpine上,只有python-magic似乎可以工作:

设置Alpine并安装libmagic:

$ docker run --rm -it python:3-alpine /bin/sh
/ # apk add libmagic
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/1) Installing libmagic (5.32-r0)
OK: 21 MiB in 35 packages

安装文件魔术师:

/ # pip install file-magic
Collecting file-magic
  Downloading https://files.pythonhosted.org/packages/bb/7e/b256e53a6558afd348387c3119dc4a1e3003d36030584a427168c8d72a7a/file_magic-0.4.0-py3-none-any.whl
Installing collected packages: file-magic
Successfully installed file-magic-0.4.0

但是它不起作用:

/ # python
Python 3.7.1 (default, Dec 21 2018, 03:21:42) 
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/magic.py", line 61, in <module>
    _open = _libraries['magic'].magic_open
  File "/usr/local/lib/python3.7/ctypes/__init__.py", line 369, in __getattr__
    func = self.__getitem__(name)
  File "/usr/local/lib/python3.7/ctypes/__init__.py", line 374, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: Symbol not found: magic_open
>>> 

将文件魔术转换为python魔术:

$ pip uninstall file-magic
Uninstalling file-magic-0.4.0:
  Would remove:
    /usr/local/lib/python3.7/site-packages/file_magic-0.4.0.dist-info/*
    /usr/local/lib/python3.7/site-packages/magic.py
Proceed (y/n)? y
  Successfully uninstalled file-magic-0.4.0

/ # pip install python-magic
Collecting python-magic
  Downloading https://files.pythonhosted.org/packages/42/a1/76d30c79992e3750dac6790ce16f056f870d368ba142f83f75f694d93001/python_magic-0.4.15-py2.py3-none-any.whl
Installing collected packages: python-magic
Successfully installed python-magic-0.4.15

有效:

/ # python
Python 3.7.1 (default, Dec 21 2018, 03:21:42) 
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
>>> 

我知道每个发行版都有其怪癖,我想适应,但是我现在不知道该怎么做。有没有一种方法可以定义模块的setup.py来解决这种情况?有没有办法调整Docker容器以处理文件魔术?在这种情况下正确的做法是什么?

1 个答案:

答案 0 :(得分:3)

Python-Magic在Alpine Linux上有a hard-coded fallback,因此您可能很难使用file-magic,它仅使用ctypes.util.find_library('magic')并且显然找不到您的库。

ctypes.util.find_library的确切行为是pretty complicated,但是如果您遍历代码并查看其尝试在平台上使用的方法,则可能会找到一种方法来找到它(可能是gcc/sbin/ldconfigobjdump之一。

另一种解决方案是修补ctypes.py以正确处理Alpine之类的系统:

wget https://patch-diff.githubusercontent.com/raw/python/cpython/pull/10461.patch -O - | patch /usr/local/lib/python3.7/ctypes/util.py

现在,您只需要设置LD_LIBRARY_PATH,Python就可以找到该库:

/ # LD_LIBRARY_PATH="/usr/lib/" python -c "import ctypes.util; print(ctypes.util.find_library('magic'))"
libmagic.so.1