Use pyalpm PacmanConfig instead of pulling repos manually

This commit is contained in:
Eric Torres 2023-01-06 21:56:50 -08:00
parent 45b80ecab4
commit a22774da5f

View File

@ -1,33 +1,13 @@
"""Module for config file helper functions.""" """Module for config file helper functions."""
import configparser
from pathlib import Path from pathlib import Path
from pycman.config import PacmanConfig
# ========== Constants ========== # ========== Constants ==========
PACMAN_CONF = Path("/etc/pacman.conf") PACMAN_CONF = Path("/etc/pacman.conf")
# ========== Functions ========== # ========== Functions ==========
def parse_configfile(filepath):
"""Parse a config file given its path and return
a ConfigParser object.
:param path: path to config file to parse
:type path: str, bytes, or path-like object
:returns: object used to parse config file
:rtype: ConfigParser object
:raises FileNotFoundError: if path does not exist
"""
if not Path(filepath).is_file():
raise FileNotFoundError(f"{filepath} does not exist")
config_reader = configparser.ConfigParser(strict=False, allow_no_value=True)
config_reader.read(filepath)
return config_reader
def list_configured_repos(): def list_configured_repos():
"""Read /etc/pacman.conf to list all configured repositories. """Read /etc/pacman.conf to list all configured repositories.
@ -35,12 +15,10 @@ def list_configured_repos():
:returns: all repos configured on the system :returns: all repos configured on the system
:rtype: list :rtype: list
""" """
parsed_config = parse_configfile(PACMAN_CONF) if not Path(PACMAN_CONF).is_file():
raise FileNotFoundError(f"{PACMAN_CONF} does not exist")
repos = parsed_config.sections() config = PacmanConfig()
# remove the 'options' entry from the list config.load_from_file(PACMAN_CONF)
del repos[repos.index("options")]
repos.sort() return list(config.repos)
return repos