Remove support for /etc/lsb-release in favour of /usr/lib/os-release
authorDidier Raboud <odyx@debian.org>
Wed, 28 Nov 2018 18:28:31 +0000 (19:28 +0100)
committerDidier Raboud <odyx@debian.org>
Wed, 28 Nov 2018 18:59:36 +0000 (19:59 +0100)
This modifies `lsb-release -r` to be a single-digit alternative (10 instead of 10.5).

Closes: #914287
lsb_release.py
test/lsb-release [deleted file]
test/os-release [new file with mode: 0644]
test/os-release-minimal [new file with mode: 0644]
test/test_lsb_release.py

index 221a3214b1143d03ae819432ef7d072a6fc6b742..c03d03653483428274e2ad4cf0fd768f36af6989 100644 (file)
@@ -2,6 +2,7 @@
 
 # LSB release detection module for Debian
 # (C) 2005-10 Chris Lawrence <lawrencc@debian.org>
+# (C) 2018 Didier Raboud <odyx@debian.org>
 
 #    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 (file)
index 06d7473..0000000
+++ /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 (file)
index 0000000..4bcb19c
--- /dev/null
@@ -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 (file)
index 0000000..12dbd03
--- /dev/null
@@ -0,0 +1,3 @@
+PRETTY_NAME="(A human-readable description of the release)"
+NAME="(The standard name of the distribution)"
+ID="(distributor id)"
index 478380d8e8b184ae2c031d6d9fb1e5fc1968e7a7..d0619bd705b709dcca6ca01f8af63a5c55ee4d3d 100644 (file)
@@ -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')