【发布时间】:2019-07-05 07:40:45
【问题描述】:
我正在编写一个依赖于file-magic 的library,它适用于大多数 平台,但在Alpine Linux 中,文件魔术不起作用,所以我需要改为使用python-magic 库。
现在我知道如何编写自己的代码来处理不同的 Python 库 API,但我不知道如何编写我的 setup.cfg 或 setup.py 以根据系统有不同的要求我们正在其上进行安装。
我认为最好的选择是使用PEP 508 规则,但我不知道如何说“libmagic like Alpine”或该语法中的某些内容,更不用说是否可以在包的设置中使用。 py。确实,如果不安装file-magic 并看着它死掉,我什至无法弄清楚如何区分架构之间的区别:-(
当然,这种事情一定有最佳实践吗?
更新
在下面蒂姆的一些更广泛的理解之后,我拼凑出这个 hack 来让它工作:
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 值。
【问题讨论】: