diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 9463ace..c4f4ef4 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -1,4 +1,5 @@ import argparse +import ast import configparser import contextlib from datetime import datetime, timezone @@ -52,26 +53,33 @@ def sortify(func): return sorted(func(*args, **kwargs)) return functools.update_wrapper(wrapper, func) -def get_default_target(): +def read_os_release(): try: - os_release_file = open('/etc/os-release') + f = open('/etc/os-release') except FileNotFoundError: - os_release_file = open('/usr/lib/os-release') - with os_release_file: - conf = configparser.ConfigParser() - conf.read_file(itertools.chain(["[os-release]"], os_release_file)) - os_release = conf["os-release"] + f = open('/usr/lib/os-release') + + for line in f: + line = line.rstrip() + if not line or line.startswith('#'): + continue + m = re.match(r'([A-Z][A-Z_0-9]+)=(.*)', line) + if m: + name, val = m.groups() + if val and val[0] in '"\'': + val = ast.literal_eval(val) + yield name, val + +def get_default_target(): + os_release = dict(read_os_release()) os_id = os_release.get("ID") - os_like = os_release.get("ID_LIKE") - if os_like is not None: - os_like = shlex.split(os_like) - else: - os_like = [] + # ID_LIKE is a space-separated list of identifiers like ID + os_like = os_release.get("ID_LIKE", "").split() # Order matters here! - if os_id == "mageia" or ("mageia" in os_like): + if "mageia" in (os_id, *os_like): return "mageia" - elif os_id == "fedora" or ("fedora" in os_like): + elif "fedora" in (os_id, *os_like): return "fedora" elif "suse" in os_like: return "opensuse"