From: Didier Raboud Date: Wed, 28 Nov 2018 18:28:31 +0000 (+0100) Subject: Remove support for /etc/lsb-release in favour of /usr/lib/os-release X-Git-Tag: archive/raspbian/10.2018112800+rpi1^2~9 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=b05d8b6b70e5af820ddb4e651868488ed9257c17;p=lsb.git Remove support for /etc/lsb-release in favour of /usr/lib/os-release This modifies `lsb-release -r` to be a single-digit alternative (10 instead of 10.5). Closes: #914287 --- diff --git a/lsb_release.py b/lsb_release.py index 221a321..c03d036 100644 --- a/lsb_release.py +++ b/lsb_release.py @@ -2,6 +2,7 @@ # LSB release detection module for Debian # (C) 2005-10 Chris Lawrence +# (C) 2018 Didier Raboud # This package is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -313,14 +314,14 @@ def guess_debian_release(): return distinfo -# Whatever is guessed above can be overridden in /etc/lsb-release -def get_lsb_information(): +# Whatever is guessed above can be overridden in /usr/lib/os-release by derivatives +def get_os_release(): distinfo = {} - etc_lsb_release = os.environ.get('LSB_ETC_LSB_RELEASE','/etc/lsb-release') - if os.path.exists(etc_lsb_release): + os_release = os.environ.get('LSB_OS_RELEASE', '/usr/lib/os-release') + if os.path.exists(os_release): try: - with open(etc_lsb_release) as lsb_release_file: - for line in lsb_release_file: + with open(os_release) as os_release_file: + for line in os_release_file: line = line.strip() if not line: continue @@ -328,19 +329,27 @@ def get_lsb_information(): if not '=' in line: continue var, arg = line.split('=', 1) - if var.startswith('DISTRIB_'): - var = var[8:] - if arg.startswith('"') and arg.endswith('"'): - arg = arg[1:-1] - if arg: # Ignore empty arguments - distinfo[var] = arg.strip() + if arg.startswith('"') and arg.endswith('"'): + arg = arg[1:-1] + if arg: # Ignore empty arguments + # Concert os-release to lsb-release-style + if var == 'VERSION_ID': + # It'll ignore point-releases + distinfo['RELEASE'] = arg.strip() + elif var == 'VERSION_CODENAME': + distinfo['CODENAME'] = arg.strip() + elif var == 'ID': + # ID=debian + distinfo['ID'] = arg.strip().title() + elif var == 'PRETTY_NAME': + distinfo['DESCRIPTION'] = arg.strip() except IOError as msg: - print('Unable to open ' + etc_lsb_release + ':', str(msg), file=sys.stderr) - + print('Unable to open ' + os_release + ':', str(msg), file=sys.stderr) + return distinfo def get_distro_information(): - lsbinfo = get_lsb_information() + lsbinfo = get_os_release() # OS is only used inside guess_debian_release anyway for key in ('ID', 'RELEASE', 'CODENAME', 'DESCRIPTION',): if key not in lsbinfo: diff --git a/test/lsb-release b/test/lsb-release deleted file mode 100644 index 06d7473..0000000 --- a/test/lsb-release +++ /dev/null @@ -1,5 +0,0 @@ -DISTRIB_ID=(Distributor ID) -DISTRIB_DESCRIPTION=(A human-readable description of the release) -DISTRIB_RELEASE=(The release number) -DISTRIB_CODENAME=(The codename for the release) -OTHER_VARIABLE=Not supposed to exist diff --git a/test/os-release b/test/os-release new file mode 100644 index 0000000..4bcb19c --- /dev/null +++ b/test/os-release @@ -0,0 +1,5 @@ +PRETTY_NAME="(A human-readable description of the release)" +NAME="(The standard name of the distribution)" +VERSION_ID="(The release number)" +VERSION="(The release number + codename)" +ID="(distributor id)" diff --git a/test/os-release-minimal b/test/os-release-minimal new file mode 100644 index 0000000..12dbd03 --- /dev/null +++ b/test/os-release-minimal @@ -0,0 +1,3 @@ +PRETTY_NAME="(A human-readable description of the release)" +NAME="(The standard name of the distribution)" +ID="(distributor id)" diff --git a/test/test_lsb_release.py b/test/test_lsb_release.py index 478380d..d0619bd 100644 --- a/test/test_lsb_release.py +++ b/test/test_lsb_release.py @@ -274,19 +274,23 @@ class TestLSBRelease(unittest.TestCase): os.environ.pop('LSB_ETC_DEBIAN_VERSION') os.environ.pop('TEST_APT_CACHE_UNSTABLE') - def test_get_lsb_information(self): - # Test that an inexistant /etc/lsb-release leads to empty output + def test_get_os_release(self): + # Test that an inexistant /usr/lib/os-release leads to empty output supposed_output = {} - os.environ['LSB_ETC_LSB_RELEASE'] = 'test/inexistant_file_' + rnd_string(2,5) - self.assertEqual(lr.get_lsb_information(),supposed_output) - # Test that a fake /etc/lsb-release leads to output with only the content we want + os.environ['LSB_OS_RELEASE'] = 'test/inexistant_file_' + rnd_string(2,5) + self.assertEqual(lr.get_os_release(),supposed_output) + # Test that a fake full /usr/lib/os-release leads to output with only the content we want supposed_output = {'RELEASE': '(The release number)', - 'CODENAME': '(The codename for the release)', - 'ID': '(Distributor ID)', + 'ID': '(Distributor Id)', 'DESCRIPTION': '(A human-readable description of the release)'} - os.environ['LSB_ETC_LSB_RELEASE'] = 'test/lsb-release' - self.assertEqual(lr.get_lsb_information(),supposed_output) - os.environ.pop('LSB_ETC_LSB_RELEASE') + os.environ['LSB_OS_RELEASE'] = 'test/os-release' + self.assertEqual(lr.get_os_release(),supposed_output) + # Test that a fake minimal /usr/lib/os-release leads to output with only the content we want + supposed_output = {'ID': '(Distributor Id)', + 'DESCRIPTION': '(A human-readable description of the release)'} + os.environ['LSB_OS_RELEASE'] = 'test/os-release-minimal' + self.assertEqual(lr.get_os_release(),supposed_output) + os.environ.pop('LSB_OS_RELEASE') def test_get_distro_information_no_distinfo_file(self): # Test that a missing /usr/share/distro-info/{distro}.csv indeed falls @@ -296,12 +300,12 @@ class TestLSBRelease(unittest.TestCase): self.assertEqual(debian_info, other_distro_info) def test_get_distro_information(self): - # Test that an inexistant /etc/lsb-release leads to empty output + # Test that an inexistant /usr/lib/os-release leads to empty output supposed_output = get_arch_distinfo() supposed_output['RELEASE'] = 'testing/unstable'; supposed_output['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s' % supposed_output - os.environ['LSB_ETC_LSB_RELEASE'] = 'test/inexistant_file_' + rnd_string(2,5) + os.environ['LSB_OS_RELEASE'] = 'test/inexistant_file_' + rnd_string(2,5) fn = 'test/debian_version_' + rnd_string(5,12) f = open(fn,'w') f.write('testing/sid')