From a22774da5f4b99d1ed44858fb403302fa9cf0074 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Fri, 6 Jan 2023 21:56:50 -0800 Subject: [PATCH] Use pyalpm PacmanConfig instead of pulling repos manually --- packaging_scripts/pacman.py | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/packaging_scripts/pacman.py b/packaging_scripts/pacman.py index 7e9972c..793fdaf 100644 --- a/packaging_scripts/pacman.py +++ b/packaging_scripts/pacman.py @@ -1,33 +1,13 @@ """Module for config file helper functions.""" -import configparser - from pathlib import Path +from pycman.config import PacmanConfig # ========== Constants ========== PACMAN_CONF = Path("/etc/pacman.conf") # ========== 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(): """Read /etc/pacman.conf to list all configured repositories. @@ -35,12 +15,10 @@ def list_configured_repos(): :returns: all repos configured on the system :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() - # remove the 'options' entry from the list - del repos[repos.index("options")] + config = PacmanConfig() + config.load_from_file(PACMAN_CONF) - repos.sort() - - return repos + return list(config.repos)