Skip to content

Commit

Permalink
Extract detect_os function
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-el committed Feb 22, 2024
1 parent 5859d68 commit c779d6f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/subscript/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import sys
from pathlib import Path

try:
from importlib import metadata
Expand All @@ -9,6 +10,21 @@
pass


def detect_os(release_file: Path = Path("/etc/redhat-release")) -> str:
"""Detect operating system string in runtime, just use default if not found."""
default_os_version = "x86_64_RH_7"

if release_file.is_file():
with open(release_file, "r", encoding="utf-8") as buffer:
tokens = buffer.read().split()
for t in tokens:
if "." in t:
major = t.split(".")[0]
return f"x86_64_RH_{major}"
raise ValueError("Could not detect RHEL version")
return default_os_version


def getLogger(module_name="subscript"):
# pylint: disable=invalid-name
"""Provides a unified logger for subscript scripts.
Expand Down
17 changes: 2 additions & 15 deletions src/subscript/runrms/runrms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import yaml

from subscript import getLogger
from subscript import detect_os, getLogger

logger = getLogger(__name__)

Expand Down Expand Up @@ -288,7 +288,7 @@ def __init__(self):
self.warn_empty_version = False
self.aps_toolbox_path = None

self.detect_os()
self.osver = detect_os(RHEL_ID)

self.oldpythonpath = ""
if "PYTHONPATH" in os.environ:
Expand All @@ -304,19 +304,6 @@ def __init__(self):
_BColors.ENDC,
)

def detect_os(self):
"""Detect operating system string in runtime, just use default if not found."""
if RHEL_ID.is_file():
with open(RHEL_ID, "r", encoding="utf-8") as buffer:
tokens = buffer.read().split()
for t in tokens:
if "." in t:
major = t.split(".")[0]
self.osver = f"x86_64_RH_{major}"
logger.debug("RHEL version found in %s", RHEL_ID)
return
raise ValueError("Could not detect RHEL version")

def do_parse_args(self, args):
"""Parse command line args."""
if args is None:
Expand Down

0 comments on commit c779d6f

Please sign in to comment.