setup.py 中的条件要求 [英] Conditional requirements in setup.py

查看:65
本文介绍了setup.py 中的条件要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 ,它依赖于 file-magic,适用于大多数平台,但在 Alpine Linux 中,file-magic不起作用,所以我需要改用 python-magic 库.

I'm writing a library that's dependent on file-magic, which works just fine for most platforms, but in Alpine Linux, file-magic won't work so I need to instead use the python-magic library.

现在我知道如何编写自己的代码来处理不同的 Python 库 API,但我不知道如何编写我的 setup.cfgsetup.py 根据我们进行安装的系统有不同的要求.

Now I know how to write my own code to handle the different Python library APIs, but what I don't know how to do is to write my setup.cfg or setup.py to have a different requirements based on the system on which we're doing the installation.

我认为最好的选择是使用 PEP 508规则,但我不知道如何说libmagic like Alpine"或类似语法的东西,更不用说这是否适用于包的 setup.py.事实上,我什至不知道如何在不安装 file-magic 并看着它消失的情况下区分架构之间的区别:-(

I figured the best option would be to use the PEP 508 rules, but I can't figure out how to say "libmagic like Alpine" or something in that syntax, let alone if that would work in a package's setup.py. Indeed, I can't even figure out how to tell the difference between the architectures without installing file-magic and watching it die :-(

当然,这种事情一定有最佳实践吗?

Surely, there must be a best practise for this sort of thing?

在从下面的 Tim 那里得到了一些更广泛的理解之后,我拼凑了这个 hack 来让它工作:

After some broader understanding from Tim below, I cobbled together this hack to get it working:

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 值.

This allows for the declarative syntax of setup.cfg, but tweaks the install_requires value if the installation target is an Alpine system.

推荐答案

您可能想要使用平台模块来尝试识别系统详情.

You probably want to use the platform module to try to identify the system details.

最好的办法是尝试使用 platform.architecture()platform.platform()platform.system() 的组合code> 正确处理错误并考虑所有可能的返回信息.

Best bet is to try and use a combo of platform.architecture(), platform.platform(), and platform.system() with proper error handling and consideration of all possible return info.

示例:

我在 Win10 上运行,以下是这些函数的输出(还有一个):

I am running on Win10, here are the outputs of those functions (and one more):

>>> import platform
>>> print(platform.architecture())
('32bit', 'WindowsPE')
>>> print(platform.platform())
Windows-10-10.0.17134-SP0
>>> print(platform.processor())
Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
>>> print(platform.system())
Windows

编辑

上述答案不一定返回您想要的信息(我没有提到平台模块中任何已弃用的功能).

Edit

The above answer does not necessarily return the info that you want (I left out mention of any deprecated functions from the platform module).

深入挖掘,产生 这个 SO 结果,这解释了内置平台不推荐使用收集发行版名称的函数.

Digging a little deeper, yields this SO result which explains that the built-in platform functions for gathering distro names are deprecated.

官方文档指向名为 distro 的 PyPi 包的方向.PyPi 上的发行版文档承认需要此类信息,并且在那里找到的示例用法如下所示:

The official docs point in the direction of a PyPi package called distro. The distro docs on PyPi acknowledge the need for this type of info and an example usage found there looks like this:

>>> import distro
>>> distro.linux_distribution(full_distribution_name=False)
('centos', '7.1.1503', 'Core')

这篇关于setup.py 中的条件要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆