【发布时间】:2010-12-30 22:37:48
【问题描述】:
是否存在可用于检测 Linux 的哪个发行版以及当前安装的发行版的哪个版本的现有 python 模块。
例如:
- 红帽企业 5
- Fedora 11
- Suse Enterprise 11
- 等等……
我可以通过解析 /etc/redhat-release 等各种文件来制作自己的模块,但我想知道模块是否已经存在?
干杯, 伊万
【问题讨论】:
是否存在可用于检测 Linux 的哪个发行版以及当前安装的发行版的哪个版本的现有 python 模块。
例如:
我可以通过解析 /etc/redhat-release 等各种文件来制作自己的模块,但我想知道模块是否已经存在?
干杯, 伊万
【问题讨论】:
查找平台模块的文档:http://docs.python.org/library/platform.html
例子:
>>> 平台.uname() ('Linux', 'localhost', '2.6.31.5-desktop-1mnb', '#1 SMP Fri Oct 23 00:05:22 EDT 2009', 'x86_64', 'AMD Athlon(tm) 64 X2 Dual Core Processor 3600+') >>> 平台.linux_distribution() (“Mandriva Linux”、“2010.0”、“官方”)【讨论】:
我编写了一个名为distro(现在由pip 使用)的包,旨在替换distro.linux_distribution。它适用于许多使用platform 时可能返回奇怪或空元组的发行版。
https://github.com/nir0s/distro(distro,在 pypi 上)
它提供了更精细的 API 来检索分发相关信息。
$ python
Python 2.7.12 (default, Nov 7 2016, 11:55:55)
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
(u'Antergos Linux', '', u'ARCHCODE')
顺便说一句,platform.linux_distribution 将在 Python 3.7 中被删除。
【讨论】:
上述答案不适用于 RHEL 5.x。在类似 redhat 的系统上,最快的方法是阅读并查看 /etc/redhat-release 文件。每次运行更新时都会更新此文件,并且系统会按次要版本号进行升级。
$ python
>>> open('/etc/redhat-release','r').read().split(' ')[6].split('.')
['5', '5']
如果你把分割的部分拿出来,它只会给你字符串。没有像你问的那样的模块,但我认为它足够简短和优雅,你可能会发现它很有用。
【讨论】:
可能不是最好的方法,但我使用子进程执行“uname -v”,然后在输出中查找发行版名称。
import subprocess
process = subprocess.Popen(['uname','-v'], stdout=subprocess.PIPE)
stdout = process.communicate()[0]
distro = format(stdout).rstrip("\n")
if 'FreeBSD' in distro:
print "It's FreeBSD"
elif 'Ubuntu' in distro:
print "It's Ubuntu"
elif 'Darwin' in distro:
print "It's a Mac"
else:
print "Unknown distro"
【讨论】: