Skip to content

Commit

Permalink
bridge: Split out os-release parser
Browse files Browse the repository at this point in the history
We want to re-use it in beiboot.py.
  • Loading branch information
martinpitt committed Oct 21, 2024
1 parent 33a807b commit 7b14b42
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/cockpit/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import socket
import stat
import subprocess
from typing import Iterable, List, Optional, Sequence, Tuple, Type
from typing import Dict, Iterable, List, Optional, Sequence, Tuple, Type

from cockpit._vendor.ferny import interaction_client
from cockpit._vendor.systemd_ctypes import bus, run_async
Expand Down Expand Up @@ -115,19 +115,7 @@ def get_os_release():
logger.warning("Neither /etc/os-release nor /usr/lib/os-release exists")
return {}

os_release = {}
for line in file.readlines():
line = line.strip()
if not line or line.startswith('#'):
continue
try:
k, v = line.split('=')
(v_parsed, ) = shlex.split(v) # expect exactly one token
except ValueError:
logger.warning('Ignoring invalid line in os-release: %r', line)
continue
os_release[k] = v_parsed
return os_release
return parse_os_release(file.read())

def do_init(self, message: JsonObject) -> None:
# we're only interested in the case where this is a dict, but
Expand Down Expand Up @@ -242,6 +230,22 @@ def setup_logging(*, debug: bool) -> None:
logging.getLogger(module).setLevel(logging.DEBUG)


def parse_os_release(text: str) -> Dict[str, str]:
os_release = {}
for line in text.splitlines():
line = line.strip()
if not line or line.startswith('#'):
continue
try:
k, v = line.split('=')
(v_parsed, ) = shlex.split(v) # expect exactly one token
except ValueError:
logger.warning('Ignoring invalid line in os-release: %r', line)
continue
os_release[k] = v_parsed
return os_release


def start_ssh_agent() -> None:
# Launch the agent so that it goes down with us on EOF; PDEATHSIG would be more robust,
# but it gets cleared on setgid ssh-agent, which some distros still do
Expand Down

0 comments on commit 7b14b42

Please sign in to comment.