我正在编写一个依赖于file-magic的library,它对大多数平台都很好,但是在Alpine Linux中,文件魔法不起作用,所以我需要使用python-magic库。在
现在我知道了如何编写自己的代码来处理不同的Python库api,但是我不知道如何编写setup.cfg或{}以根据我们正在进行安装的系统有不同的需求。在
我想最好的选择是使用PEP 508规则,但我不知道如何用那种语法说“libmagic like Alpine”或其他什么,更不用说在包的设置.py. 实际上,如果不安装file-magic并看着它死掉,我甚至不知道如何区分两种架构之间的区别:-(
当然,这类事情一定有最佳实践吗?在
更新
从下面的Tim那里得到了一些更广泛的理解之后,我拼凑出了这个方法,让它发挥作用:def get_requirements():
"""
Alpine is problematic in how it doesn't play nice with file-magic -- a
module that appears to be the standard for most other Linux distros. As a
work-around for this, we swap out file-magic for python-magic in the Alpine
case.
"""
config = configparser.ConfigParser()
config.read("setup.cfg")
requirements = config["options"]["install_requires"].split()
os_id = None
try:
with open("/etc/os-release") as f:
os_id = [_ for _ in f.readlines() if _.startswith("ID=")][0] \
.strip() \
.replace("ID=", "")
except (FileNotFoundError, OSError, IndexError):
pass
if os_id == "alpine":
requirements[1] = "python-magic>=0.4.15"
return requirements
setuptools.setup(install_requires=get_requirements())
这允许使用setup.cfg的声明性语法,但是如果安装目标是一个Alpine系统,则调整install_requires值。在