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,69 @@ 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 doesn't work, then it reverse to _dist_try_a_little_harder(). + + 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 Heron". + + """ + _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) + + 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) + + 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) + 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')):