diff --git a/osbuild/pipeline.py b/osbuild/pipeline.py index 50b3c9f1..f51f1f77 100644 --- a/osbuild/pipeline.py +++ b/osbuild/pipeline.py @@ -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): diff --git a/test/test_osrelease.py b/test/test_osrelease.py index 1c8578cd..f22d795d 100644 --- a/test/test_osrelease.py +++ b/test/test_osrelease.py @@ -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