diff -r 12bf7fc1ba76 Lib/platform.py --- a/Lib/platform.py Tue Oct 22 20:03:47 2013 +0100 +++ b/Lib/platform.py Thu Oct 24 17:25:45 2013 +0300 @@ -243,6 +243,7 @@ return distname,version,id +_os_release_info = re.compile(r'^(?!#)(.+)=([\'\"]?)(.+)\2$') _release_filename = re.compile(r'(\w+)[-_](release|version)', re.ASCII) _lsb_release_version = re.compile(r'(.+)' ' release ' @@ -290,6 +291,22 @@ id = l[1] return '', version, id +def _parse_os_release(): + with open('/etc/os-release') as f: + info = {} + + for line in f: + m = _os_release_info.match(line) + if m is not None: + key = m.group(1) + _value = m.group(3) + value = bytes(_value, 'utf-8').decode("unicode_escape") + info[key] = value + + return info + + return None + def linux_distribution(distname='', version='', id='', supported_dists=_supported_dists, @@ -297,9 +314,9 @@ """ Tries to determine the name of the Linux OS distribution name. - The function first looks for a distribution release file in - /etc and then reverts to _dist_try_harder() in case no - suitable files are found. + The function first checks for an /etc/os-release file, then + looks for a distribution release file in /etc and then reverts + to _dist_try_harder() in case no suitable files are found. supported_dists may be given to define the set of Linux distributions to look for. It defaults to a list of currently @@ -318,7 +335,18 @@ etc = os.listdir('/etc') except OSError: # Probably not a Unix system - return distname,version,id + return distname, version, id + + os_release_info = _parse_os_release() + if os_release_info is not None: + if 'NAME' in os_release_info: + distname = os_release_info['NAME'] + if 'VERSION_ID' in os_release_info: + version = os_release_info['VERSION_ID'] + if 'ID' in os_release_info: + id = os_release_info['ID'] + return distname, version, id + etc.sort() for file in etc: m = _release_filename.match(file)