From 4c0a6af8cce9f5d1eb91206b44fd01f17e699459 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Wed, 26 Oct 2022 07:59:02 -0300 Subject: [PATCH] [gems.arch] enhancement: pacman.list_ignored_pkgs now use Python calls instead of system calls --- CHANGELOG.md | 4 +++ bauh/gems/arch/pacman.py | 32 +++++++++++-------- .../gems/arch/resources/pacman_ign_pkgs.conf | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cb96018..fe11b10c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [STAGING] +### Improvements +- Arch + - replaced some system calls by Python calls + ### Fixes - AppImage - some desktop entries not being displayed on the desktop environment menu (requires the AppImage to be reinstalled) [#287](https://github.com/vinifmor/bauh/issues/287) diff --git a/bauh/gems/arch/pacman.py b/bauh/gems/arch/pacman.py index ffe83c18..9b33b9a7 100644 --- a/bauh/gems/arch/pacman.py +++ b/bauh/gems/arch/pacman.py @@ -5,7 +5,7 @@ import shutil import traceback from io import StringIO from threading import Thread -from typing import List, Set, Tuple, Dict, Iterable, Optional, Any +from typing import List, Set, Tuple, Dict, Iterable, Optional, Any, Pattern from colorama import Fore @@ -26,6 +26,7 @@ RE_REMOVE_TRANSITIVE_DEPS = re.compile(r'removing\s([\w\-_]+)\s.+required\sby\s( RE_AVAILABLE_MIRRORS = re.compile(r'.+\s+OK\s+.+\s+(\d+:\d+)\s+.+(http.+)') RE_PACMAN_SYNC_FIRST = re.compile(r'SyncFirst\s*=\s*(.+)') RE_DESKTOP_FILES = re.compile(r'\n?([\w\-_]+)\s+(/usr/share/.+\.desktop)') +RE_IGNORED_PACKAGES: Optional[Pattern] = None def is_available() -> bool: @@ -234,22 +235,27 @@ def sign_key(key: str, root_password: Optional[str]) -> SystemProcess: def list_ignored_packages(config_path: str = '/etc/pacman.conf') -> Set[str]: ignored = set() + try: - pacman_conf = new_subprocess(['cat', config_path]) - grep = new_subprocess(['grep', '-Eo', r'\s*#*\s*ignorepkg\s*=\s*.+'], stdin=pacman_conf.stdout) - for o in grep.stdout: - if o: - line = o.decode().strip() + with open(config_path, 'r') as f: + file_content = f.read() - if not line.startswith('#'): - ignored.add(line.split('=')[1].strip()) + global RE_IGNORED_PACKAGES - pacman_conf.terminate() - grep.terminate() - return ignored - except: + if not RE_IGNORED_PACKAGES: + RE_IGNORED_PACKAGES = re.compile(r'[\s#]*ignorepkg\s*=\s*.+', re.IGNORECASE) + + for raw_line in RE_IGNORED_PACKAGES.findall(file_content): + line = raw_line.strip() + + if line and not line.startswith("#"): + ignored.add(line.split("=")[1].strip()) + except (FileNotFoundError, OSError): + pass + except Exception: traceback.print_exc() - return ignored + + return ignored def check_missing(names: Set[str]) -> Set[str]: diff --git a/tests/gems/arch/resources/pacman_ign_pkgs.conf b/tests/gems/arch/resources/pacman_ign_pkgs.conf index 6e502ab0..7ea5ebdf 100644 --- a/tests/gems/arch/resources/pacman_ign_pkgs.conf +++ b/tests/gems/arch/resources/pacman_ign_pkgs.conf @@ -98,4 +98,4 @@ Include = /etc/pacman.d/mirrorlist #Server = file:///home/custompkgs ignorepkg=google-chrome # ignorepkg=abc -ignorepkg = firefox \ No newline at end of file +IgnorePkg = firefox \ No newline at end of file