【问题标题】:How to check whether the system is FreeBSD in a python script?如何在 python 脚本中检查系统是否为 FreeBSD?
【发布时间】:2015-07-12 23:38:19
【问题描述】:

我想在python 2.7.x 脚本中添加一个检查,格式为

if __check_freebsd__():
    # run actions which would only work on FreeBSD (e.g. create a jail)
elif __check_debian__():
    # run an alternative that runs on Debian-based systems
else:
    raise Error("unsupported OS")

__check_freebsd__ 函数是什么样子的?

我已经有__check_debian__ 的以下代码:

try:
    lsb_release_id_short = sp.check_output([lsb_release, "-d", "-s"]).strip().decode("utf-8")
    ret_value = "Debian" in lsb_release_id_short
    return ret_value
except Exception:
    return False

因此您不必为此烦恼(当然,欢迎提出改进建议)。

【问题讨论】:

    标签: python python-2.7 freebsd


    【解决方案1】:

    in documentation所述,

    platform.system()
    

    返回平台操作系统名称,因此您可以使用它。在this thread,您还可以看到检查底层操作系统的不同方法。

    【讨论】:

    • if platform.system() == 'FreeBSD' 应该可以工作,就我而言。 Karl 询问了有关 freeBSD 检查的问题,所以我认为我不需要检查 Linux 发行版。如果我错了,请纠正我。
    • 适用于 FreeBSD 9.3 和 FreeBSD 10.0。
    【解决方案2】:

    os.uname

    我不能 100% 确定,但它可能类似于 os.uname()[0] == "FreeBSD"

    【讨论】:

      【解决方案3】:

      试试这个:

      >>> from sys import platform
      >>> platform()
      # on my system I get
      'linux' # check string for freebsd
      

      还有:

      # below are my results
      >>> import platform
      >>> platform.system()
      'Linux' # would be 'FreeBSD' if I was using that
      >>> platform.platform()
      'Linux-3.19.0-15-generic-x86_64-with-Ubuntu-15.04-vivid'
      

      【讨论】:

      • platform.linux_platform() 在 FreeBSD 9.3 中返回 ('', '', '')(在 qemu 2.2 上运行)。
      • @KarlRichter 它仅适用于 linux,所以我想它不适用于 FreeBSD。 platform.system() 和 sys.platform 一样是跨平台的
      • 我明白了。那么这不是我问题的答案,是吗?也许这是由于qemu
      • @KarlRichter 不,是的。 sys.platform、platform.system 和 platform.platform 都为您提供所需的信息。前两个以更直接的方式。我也包含了platform.linux_platform,我不记得为什么了。但我之前编辑过。
      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      相关资源
      最近更新 更多