Index: Lib/platform.py =================================================================== --- Lib/platform.py (revision 66520) +++ Lib/platform.py (working copy) @@ -109,7 +109,7 @@ __version__ = '1.0.4' -import sys,string,os,re +import subprocess,sys,string,os,re ### Platform specific APIs @@ -217,13 +217,75 @@ return distname,version,id +_distributor_id = re.compile("(?:Distributor ID)?:?\s*(.*)", re.I) +_release = re.compile("(?:Release)?:?\s*(.*)", re.I) +_codename = re.compile("(?:Codename)?:?\s*(.*)", re.I) + +def dist(distname='',version='',id=''): + + """ Tries to determine the name of the Linux OS distribution name. + + The function tries to execute "lsb_release", as standardized in 2001: + + http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html + + The current version of the standard is here: + + http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html + + If that works (that is, executing "lsb_release" raises no exception, + returns exit code 0, and emits a string which is non-empty after being + stripped of whitespace in response to "lsb_release --id" and + "lsb_release --release"), then dist() returns the information that + lsb_release emitted, else it falls back to _dist_try_a_little_harder() + (which is the code that used to be called dist() before this new dist() + that uses lsb_release was added). + + Returns a tuple (distname,version,id) which default to the + args given as parameters. Distname is what LSB calls a + "distributor id", e.g. "Ubuntu". Version is what LSB calls a "release", + e.g. "8.04". ID is what LSB calls a "codename", e.g. "hardy". + + """ + _distname = None + _version = None + _id = None + try: + p = subprocess.Popen(["lsb_release", "--id"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + rc = p.wait() + if rc == 0: + m = _distributor_id.search(p.stdout.read()) + if m: + _distname = m.group(1).strip() + + p = subprocess.Popen(["lsb_release", "--release"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + rc = p.wait() + if rc == 0: + m = _release.search(p.stdout.read()) + if m: + _version = m.group(1).strip() + + p = subprocess.Popen(["lsb_release", "--codename"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + rc = p.wait() + if rc == 0: + m = _codename.search(p.stdout.read()) + if m: + _id = m.group(1).strip() + except EnvironmentError: + pass + + if not _distname or not _version: + return _dist_try_a_little_harder(distname, version, id) + + return (_distname, _version, _id) + _release_filename = re.compile(r'(\w+)[-_](release|version)') _release_version = re.compile(r'([\d.]+)[^(]*(?:\((.+)\))?') # Note:In supported_dists below we need 'fedora' before 'redhat' as in # Fedora redhat-release is a link to fedora-release. -def dist(distname='',version='',id='', +def _dist_try_a_little_harder(distname='',version='',id='', supported_dists=('SuSE', 'debian', 'fedora', 'redhat', 'mandrake')):