pipeline: fix detect_os() default values

The keys in `/etc/os-release` are not mandatory. Make sure we use their
default values (defined in the man-page) if missing.
This commit is contained in:
David Rheinsberg 2020-02-29 11:45:03 +01:00
parent 4b790ac284
commit cd07d588fc
2 changed files with 18 additions and 14 deletions

View file

@ -311,22 +311,25 @@ def detect_os(*paths):
Returns ID + VERSION_ID (without dots), which is the same format that
runners are named as.
"""
osrelease = {}
path = next((p for p in paths if os.path.exists(p)), None)
if not path:
raise FileNotFoundError("none of the specified os-release files exist")
if path:
with open(path) as f:
for line in f:
line = line.strip()
if not line:
continue
if line[0] == "#":
continue
key, value = line.split("=", 1)
osrelease[key] = value.strip('"')
with open(path) as f:
osrelease = {}
for line in f:
line = line.strip()
if not line:
continue
if line[0] == "#":
continue
key, value = line.split("=", 1)
osrelease[key] = value.strip('"')
# Fetch `ID` and `VERSION_ID`. Defaults are defined in `os-release(5)`.
osrelease_id = osrelease.get("ID", "linux")
osrelease_version_id = osrelease.get("VERSION_ID", "")
return osrelease["ID"] + osrelease["VERSION_ID"].replace(".", "")
return osrelease_id + osrelease_version_id.replace(".", "")
def load_build(description, sources_options):

View file

@ -5,7 +5,8 @@ import osbuild
class TestOSRelease(unittest.TestCase):
def test_non_existant(self):
self.assertRaises(FileNotFoundError, osbuild.pipeline.detect_os, "💩")
"""Verify default os-release value, if no files are given."""
self.assertEqual(osbuild.pipeline.detect_os(), "linux")
def test_detect_os(self):
"""Test host os detection. test/os-release contains the os-release files