为什么 platform.linux_distribution() returns 一个 OS 的结果不同?
Why does platform.linux_distribution() returns different results on one OS?
我在某些虚拟机 2.7.9
和 2.7.6
上安装了 python 的 2 个版本。 2.7.6
是从系统包安装的,而 2.7.9
是从源安装的。这台机器 运行 Ubuntu 14.04.
我想使用 platform
模块来获取有关 linux 分发的信息。然而事实证明,在这两个版本中,我得到了 platform.linux_distribution()
的不同结果。
Python 2.7.9 (...)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.linux_distribution()
('debian', 'jessie/sid', '')
Python 2.7.6 (...)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.linux_distribution()
('Ubuntu', '14.04', 'trusty')
知道为什么会这样吗?
或者更一般地说,平台模块如何获取有关 linux 分发的信息。它是基于 lsb_relase
或其他一些系统命令还是在某处进行了硬编码?
platform.linux_distribution()
platform.linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat', 'mandrake', ...), full_distribution_name=1)
尝试确定 Linux OS 分发名称的名称。
Supported_dists may be given to define the set of Linux distributions
to look for. It defaults to a list of currently supported Linux
distributions identified by their release file name.
If full_distribution_name is true (default), the full distribution
read from the OS is returned. Otherwise the short name taken from
supported_dists is used.
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.
在您的 2.7.9
结果中,该命令无法从 OS 中确定 full_distribution_name
(因为它显示为空白),因此它改为使用 supported_dists
。
正在查看 linux_distribution()
的源代码
- 它列出了
/etc
、 中的所有文件
- 搜索名称与
r'(\w+)[-_](release|version)'
匹配的文件。在我的 OS 上,它选择 debian_version
文件。
- 然后获取正则表达式的第一个匹配项 (
debian
) 并查看它是否在支持的列表中(静态数组:platform._supported_dists
)
- 如果是,则从文件中读取信息。
- 如果没有,它将运行
_dist_try_harder(distname,version,id)
。 return 来自 /var/adm/inst-log/info
、/etc/.installed
或 /usr/lib/setup
的版本(按此顺序,解析第一个文件并 returned)。
因此,根据 linux_distribution()
读取信息的位置,您可能会遇到 不同的输出 。
Ubuntu 14.04 包括两个发布文件:
# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@yeni2:/# cat /etc/debian_version
jessie/sid
两者都被函数 platform.linux_distribution()
使用,但是,此函数被 Ubuntu 修补为 return Ubuntu OS 名称,另请参阅在代码中注释(右边是 Ubuntu 安装的文件,左边是 Python 2.7.10 中找到的源文件):
Python-2.7.10 # diff Lib/platform.py /mnt/ubu/\@/usr/lib/python2.7/platform.py
1c1
< #!/usr/bin/env python
---
> #! /usr/bin/python2.7
262c262
< 'UnitedLinux', 'turbolinux')
---
> 'UnitedLinux', 'turbolinux', 'Ubuntu')
290a291,294
> _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
> _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
> _codename_file_re = re.compile("(?:DISTRIB_CODENAME\s*=)\s*(.*)", re.I)
>
314a319,337
> # check for the LSB /etc/lsb-release file first, needed so
> # that the distribution doesn't get identified as Debian.
> try:
> with open("/etc/lsb-release", "rU") as etclsbrel:
> for line in etclsbrel:
> m = _distributor_id_file_re.search(line)
> if m:
> _u_distname = m.group(1).strip()
> m = _release_file_re.search(line)
> if m:
> _u_version = m.group(1).strip()
> m = _codename_file_re.search(line)
> if m:
> _u_id = m.group(1).strip()
> if _u_distname and _u_version:
> return (_u_distname, _u_version, _u_id)
> except (EnvironmentError, UnboundLocalError):
> pass
>
您的 python 2.7.9 编译了源代码,不包含来自 Ubuntu 的补丁,这就是为什么它 returning /etc/debian_version
[ 的内容=14=]
我在某些虚拟机 2.7.9
和 2.7.6
上安装了 python 的 2 个版本。 2.7.6
是从系统包安装的,而 2.7.9
是从源安装的。这台机器 运行 Ubuntu 14.04.
我想使用 platform
模块来获取有关 linux 分发的信息。然而事实证明,在这两个版本中,我得到了 platform.linux_distribution()
的不同结果。
Python 2.7.9 (...)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.linux_distribution()
('debian', 'jessie/sid', '')
Python 2.7.6 (...)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.linux_distribution()
('Ubuntu', '14.04', 'trusty')
知道为什么会这样吗?
或者更一般地说,平台模块如何获取有关 linux 分发的信息。它是基于 lsb_relase
或其他一些系统命令还是在某处进行了硬编码?
platform.linux_distribution()
platform.linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat', 'mandrake', ...), full_distribution_name=1)
尝试确定 Linux OS 分发名称的名称。
Supported_dists may be given to define the set of Linux distributions to look for. It defaults to a list of currently supported Linux distributions identified by their release file name.
If full_distribution_name is true (default), the full distribution read from the OS is returned. Otherwise the short name taken from supported_dists is used.
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.
在您的 2.7.9
结果中,该命令无法从 OS 中确定 full_distribution_name
(因为它显示为空白),因此它改为使用 supported_dists
。
正在查看 linux_distribution()
- 它列出了
/etc
、 中的所有文件
- 搜索名称与
r'(\w+)[-_](release|version)'
匹配的文件。在我的 OS 上,它选择debian_version
文件。 - 然后获取正则表达式的第一个匹配项 (
debian
) 并查看它是否在支持的列表中(静态数组:platform._supported_dists
) - 如果是,则从文件中读取信息。
- 如果没有,它将运行
_dist_try_harder(distname,version,id)
。 return 来自/var/adm/inst-log/info
、/etc/.installed
或/usr/lib/setup
的版本(按此顺序,解析第一个文件并 returned)。
因此,根据 linux_distribution()
读取信息的位置,您可能会遇到 不同的输出 。
Ubuntu 14.04 包括两个发布文件:
# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@yeni2:/# cat /etc/debian_version
jessie/sid
两者都被函数 platform.linux_distribution()
使用,但是,此函数被 Ubuntu 修补为 return Ubuntu OS 名称,另请参阅在代码中注释(右边是 Ubuntu 安装的文件,左边是 Python 2.7.10 中找到的源文件):
Python-2.7.10 # diff Lib/platform.py /mnt/ubu/\@/usr/lib/python2.7/platform.py
1c1
< #!/usr/bin/env python
---
> #! /usr/bin/python2.7
262c262
< 'UnitedLinux', 'turbolinux')
---
> 'UnitedLinux', 'turbolinux', 'Ubuntu')
290a291,294
> _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
> _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
> _codename_file_re = re.compile("(?:DISTRIB_CODENAME\s*=)\s*(.*)", re.I)
>
314a319,337
> # check for the LSB /etc/lsb-release file first, needed so
> # that the distribution doesn't get identified as Debian.
> try:
> with open("/etc/lsb-release", "rU") as etclsbrel:
> for line in etclsbrel:
> m = _distributor_id_file_re.search(line)
> if m:
> _u_distname = m.group(1).strip()
> m = _release_file_re.search(line)
> if m:
> _u_version = m.group(1).strip()
> m = _codename_file_re.search(line)
> if m:
> _u_id = m.group(1).strip()
> if _u_distname and _u_version:
> return (_u_distname, _u_version, _u_id)
> except (EnvironmentError, UnboundLocalError):
> pass
>
您的 python 2.7.9 编译了源代码,不包含来自 Ubuntu 的补丁,这就是为什么它 returning /etc/debian_version
[ 的内容=14=]