Get Distributor ID from /etc/dpkg/origins/default if existant
authorDidier Raboud <odyx@debian.org>
Wed, 15 May 2013 19:27:53 +0000 (21:27 +0200)
committerDidier Raboud <odyx@debian.org>
Wed, 15 May 2013 20:09:18 +0000 (22:09 +0200)
This helps Debian derivatives to avoid forking the lsb package.

Closes: #703677
lsb_release.py
test/test_lsb_release.py

index f9cfb53d9b37229a2bfbf4988c9d122a6d6d041b..d36eeb670aa9012d5373b1e07b67436a25cdb1d5 100644 (file)
@@ -252,7 +252,25 @@ def guess_release_from_apt(origin='Debian', component='main',
     return releases[0][1]
 
 def guess_debian_release():
-    distinfo = {'ID' : 'Debian'}
+    distinfo = {}
+
+    distinfo['ID'] = 'Debian'
+    # Use /etc/dpkg/origins/default to fetch the distribution name
+    etc_dpkg_origins_default = os.environ.get('LSB_ETC_DPKG_ORIGINS_DEFAULT','/etc/dpkg/origins/default')
+    if os.path.exists(etc_dpkg_origins_default):
+        try:
+            with open(etc_dpkg_origins_default) as dpkg_origins_file:
+                for line in dpkg_origins_file:
+                    try:
+                        (header, content) = line.split(': ', 1)
+                        header = header.lower()
+                        content = content.strip()
+                        if header == 'vendor':
+                            distinfo['ID'] = content
+                    except ValueError:
+                        pass
+        except IOError as msg:
+            print('Unable to open ' + etc_dpkg_origins_default + ':', str(msg), file=sys.stderr)
 
     kern = os.uname()[0]
     if kern in ('Linux', 'Hurd', 'NetBSD'):
index 63e632c2c38dab1361e0b55f04f53d4af7f4aa93..d100c219a66e618c347242e31d7c4ad5d852d132 100644 (file)
@@ -217,6 +217,27 @@ class TestLSBRelease(unittest.TestCase):
 
        def test_guess_debian_release(self):
                distinfo = get_arch_distinfo()
+               
+               # Test different dpkg origin with an fake "unstable releases" that ends in /sid, and an invalid apt-cache policy
+               distinfo['ID'] = rnd_string(5,12)
+               fn = 'test/dpkg_origins_default_' + rnd_string(5,5)
+               f = open(fn,'w')
+               f.write('Vendor: ' + distinfo['ID'] + "\n")
+               f.close()
+               os.environ['LSB_ETC_DPKG_ORIGINS_DEFAULT'] = fn
+               
+               distinfo['RELEASE']  = 'testing/unstable'
+               distinfo['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s' % distinfo
+               fn2 = 'test/debian_version_' + rnd_string(5,12)
+               f = open(fn2,'w')
+               f.write(rnd_string(5,12) + '/sid')
+               f.close()
+               os.environ['LSB_ETC_DEBIAN_VERSION'] = fn2
+               self.assertEqual(lr.guess_debian_release(),distinfo)
+               os.remove(fn)
+               # Make sure no existing /etc/dpkg/origins/default is used
+               os.environ['LSB_ETC_DPKG_ORIGINS_DEFAULT'] = '/non-existant'
+               distinfo['ID'] = 'Debian'
 
                # Test "stable releases" with numeric debian_versions
                for rno in lr.RELEASE_CODENAME_LOOKUP: