我正在尝试创建一个 python 脚本来在 linux 机器上自动安装和配置某些程序。
我的想法是使用平台和多处理库来询问系统信息(platform.system、platform.linux_distribution、multiprocessing.cpu_count 等),然后安装软件并根据需要为软件配置创建一个文本文件我从这些电话中得到了什么。
我在确定操作系统和发行版时遇到问题:我需要知道脚本在哪个 linux 发行版中运行,以便我可以启动适当的命令,而我要查找的信息不在 python 文档中。
根据该文档 ( http://docs.python.org/2/library/platform.html ), 平台.系统
Returns the system/OS name, e.g. 'Linux', 'Windows', or 'Java'. An empty string is returned if the value cannot be determined.
和 platform.linux_distribution
Tries to determine the name of the Linux OS distribution name.
(...)
Returns a tuple (distname,version,id) which defaults to the args given as parameters. id is the item in parentheses after the version number. It is usually the version codename.
但不够具体,没有提到任何具体案例。
我已经在我的机器上尝试了这些变量(platform.system 返回 'Linux' 和 platform.linux_distribution 'debian'),但我没有办法在其他系统上尝试它(而且我认为创建它不可行每个 Linux 发行版的虚拟机并在每个发行版中尝试)
我需要确切地知道 distname 字符串 platform.linux_distribution 在不同的 linux 发行版中的输出,以便我可以在我的脚本中使用这些值。
我还需要确认 platform.system 是否在每个 linux 发行版中返回相同的东西。这有什么异常(exception)吗?
最佳答案
首先,consulting the code , platform.system
覆盖了 os.uname
。后者用于后来的 *nix,是 uname
命令调用。 wiki 中提供了一些可能的值.
其次,linux_distribution
roughly speaking ,执行 head/etc/file
,其中 file
是以 release
或 version
结尾的第一个文件。即,对于 debian/ubuntu,例如 head/etc/debian_version
。它拆分结果行并在找到元组时返回。
我有权访问的基于 Linux 的平台的 linux_distribution
输出示例:
$ python -c 'import platform; print platform.linux_distribution()'
('debian', 'squeeze/sid', '')
('debian', 'wheezy/sid', '')
('SUSE Linux Enterprise Server ', '11', 'x86_64')
('Red Hat Enterprise Linux Server', '5.9', 'Tikanga')
('Red Hat Enterprise Linux Server', '6.4', 'Santiago')
我想你可以在这里找到模式。我个人不会依赖 linux_distribution
,有一些已知问题,例如,/etc/debian_version
内容在升级后可能会被破坏。
关于python - platform.system 和 platform.linux_distribution 究竟输出什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20145774/