osbuild: move osrelease parser into ./util
Reduce the clutter in our pipeline implementation and move the os-release parser into ./osbuild/util/.
This commit is contained in:
parent
7176f1e85a
commit
4493d057a2
3 changed files with 56 additions and 36 deletions
|
|
@ -10,6 +10,7 @@ from . import buildroot
|
|||
from . import objectstore
|
||||
from . import remoteloop
|
||||
from . import sources
|
||||
from .util import osrelease
|
||||
|
||||
|
||||
RESET = "\033[0m"
|
||||
|
|
@ -367,38 +368,6 @@ class Pipeline:
|
|||
return results
|
||||
|
||||
|
||||
def describe_os(*paths):
|
||||
"""Read the Operating System Description from `os-release`
|
||||
|
||||
This creates a string describing the running operating-system name and
|
||||
version. It reads the information from the path array provided as `paths`.
|
||||
The first available file takes precedence. It must be formatted according
|
||||
to the rules in `os-release(5)`.
|
||||
|
||||
The returned string uses the format `${ID}${VERSION_ID}` with all dots
|
||||
stripped.
|
||||
"""
|
||||
osrelease = {}
|
||||
|
||||
path = next((p for p in paths if os.path.exists(p)), None)
|
||||
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('"')
|
||||
|
||||
# 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(".", "")
|
||||
|
||||
|
||||
def load_build(description, sources_options):
|
||||
pipeline = description.get("pipeline")
|
||||
if pipeline:
|
||||
|
|
@ -414,7 +383,7 @@ def load(description, sources_options):
|
|||
if build:
|
||||
build_pipeline, runner = load_build(build, sources_options)
|
||||
else:
|
||||
build_pipeline, runner = None, "org.osbuild." + describe_os("/etc/os-release", "/usr/lib/os-release")
|
||||
build_pipeline, runner = None, "org.osbuild." + osrelease.describe_os("/etc/os-release", "/usr/lib/os-release")
|
||||
|
||||
pipeline = Pipeline(runner, build_pipeline)
|
||||
|
||||
|
|
|
|||
51
osbuild/util/osrelease.py
Normal file
51
osbuild/util/osrelease.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
"""OS-Release Information
|
||||
|
||||
This module implements handlers for the `/etc/os-release` type of files. The
|
||||
related documentation can be found in `os-release(5)`.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
def parse_files(*paths):
|
||||
"""Read Operating System Information from `os-release`
|
||||
|
||||
This creates a dictionary with information describing the running operating
|
||||
system. It reads the information from the path array provided as `paths`.
|
||||
The first available file takes precedence. It must be formatted according
|
||||
to the rules in `os-release(5)`.
|
||||
"""
|
||||
osrelease = {}
|
||||
|
||||
path = next((p for p in paths if os.path.exists(p)), None)
|
||||
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('"')
|
||||
|
||||
return osrelease
|
||||
|
||||
|
||||
def describe_os(*paths):
|
||||
"""Read the Operating System Description from `os-release`
|
||||
|
||||
This creates a string describing the running operating-system name and
|
||||
version. It uses `parse_files()` underneath to acquire the requested
|
||||
information.
|
||||
|
||||
The returned string uses the format `${ID}${VERSION_ID}` with all dots
|
||||
stripped.
|
||||
"""
|
||||
osrelease = parse_files(*paths)
|
||||
|
||||
# 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(".", "")
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
import os
|
||||
import unittest
|
||||
|
||||
import osbuild
|
||||
from osbuild.util import osrelease
|
||||
|
||||
class TestOSRelease(unittest.TestCase):
|
||||
def test_non_existant(self):
|
||||
"""Verify default os-release value, if no files are given."""
|
||||
self.assertEqual(osbuild.pipeline.describe_os(), "linux")
|
||||
self.assertEqual(osrelease.describe_os(), "linux")
|
||||
|
||||
def test_describe_os(self):
|
||||
"""Test host os detection. test/os-release contains the os-release files
|
||||
|
|
@ -14,4 +14,4 @@ class TestOSRelease(unittest.TestCase):
|
|||
"""
|
||||
for entry in os.scandir("test/os-release"):
|
||||
with self.subTest(entry.name):
|
||||
self.assertEqual(osbuild.pipeline.describe_os(entry.path), entry.name)
|
||||
self.assertEqual(osrelease.describe_os(entry.path), entry.name)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue