setup: when detecting platform, ask the Python Standard Library's platform.dist() before executing lsb_release, and cache the result in global (module) variables

This should make it sufficiently fast, while still giving a better answer on Ubuntu than platform.dist() currently does, and also falling back to lsb_release if platform.dist() says that it doesn't know.
This commit is contained in:
Zooko O'Whielacronx 2008-09-24 11:09:22 -07:00
parent dd9171eb72
commit 7339e09c72

View File

@ -28,21 +28,21 @@ _release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I)
_distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
_release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
global _distname,_version
_distname = None
_version = None
def get_linux_distro():
""" Tries to determine the name of the Linux OS distribution name.
First, try to parse a file named "/etc/lsb-release". If it exists, and
contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return
the strings parsed from that file. The reason we try this first is because
it is faster than the official method of invoking "lsb_release" (which takes
half a second on my high-performance Athlon64 Ubuntu workstation). Also
because some distributions (at least Debian/Ubuntu) have /etc/lsb-release in
the "base-files" package (Priority: required) but /usr/bin/lsb_release in
the "lsb-release" package (Priority: important), so it is possible that
/etc/lsb-release is there even if /usr/bin/lsb_release isn't.
the strings parsed from that file.
If parsing /etc/lsb-release doesn't work, then try to execute "lsb_release",
as standardized in 2001:
If that doesn't work, then invoke platform.dist().
If that doesn't work, then try to execute "lsb_release", as standardized in
2001:
http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
@ -50,14 +50,8 @@ def get_linux_distro():
http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
If executing "lsb_release" raises no exception, and returns exit code 0, and
both the "distributor id" and "release" results are non-empty after being
stripped of whitespace, then return a two-tuple containing the information
that lsb_release emitted, as strings.
If that doesn't work, then invoke platform.dist() and return the first two
elements of the tuple returned by that function.
Returns a tuple (distname,version). Distname is what LSB calls a
"distributor id", e.g. "Ubuntu". Version is what LSB calls a "release",
e.g. "8.04".
@ -67,8 +61,9 @@ def get_linux_distro():
http://bugs.python.org/issue3937
"""
_distname = None
_version = None
global _distname,_version
if _distname and _version:
return (_distname, _version)
try:
etclsbrel = open("/etc/lsb-release", "rU")
@ -86,6 +81,10 @@ def get_linux_distro():
except EnvironmentError:
pass
(_distname, _version) = platform.dist()[:2]
if _distname and _version:
return (_distname, _version)
try:
p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rc = p.wait()
@ -105,7 +104,7 @@ def get_linux_distro():
except EnvironmentError:
pass
return platform.dist()[:2]
return (_distname,_version)
def get_platform():
# Our version of platform.platform(), telling us both less and more than the