From 6070dc80d407c8cab4572c70f81de935d742d83a Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 15 Jul 2023 16:27:12 +0200 Subject: [PATCH] [PR #6837/e0324cdc backport][stable-7] rhsm_repository: refactor parsing of "subscription-manager repos" output (#6946) rhsm_repository: refactor parsing of "subscription-manager repos" output (#6837) Simplify a bit (and possibly speed it up a little) the parsing of the output of `subscription-manager repos --list`: - simplify skipping the lines that are not interesting: check the first character only, as it is enough to determine whether it contains repository data or not - check the start of each line manually, rather than with regexp: a simple slice + lstrip() gives the same result (cherry picked from commit e0324cdc90419d39f3b83977def82ea919a2b6d5) Co-authored-by: Pino Toscano --- ...6837-rhsm_repository-internal-refactor.yml | 8 +++++ ...6783-rhsm_repository-internal-refactor.yml | 6 ---- plugins/modules/rhsm_repository.py | 36 +++++++------------ 3 files changed, 21 insertions(+), 29 deletions(-) create mode 100644 changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml delete mode 100644 changelogs/fragments/6783-rhsm_repository-internal-refactor.yml diff --git a/changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml b/changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml new file mode 100644 index 0000000000..fe7b9510c6 --- /dev/null +++ b/changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml @@ -0,0 +1,8 @@ +minor_changes: + - | + rhsm_repository - the interaction with ``subscription-manager`` was + refactored by grouping things together, removing unused bits, and hardening + the way it is run; also, the parsing of ``subscription-manager repos --list`` + was improved and made slightly faster; no behaviour change is expected + (https://github.com/ansible-collections/community.general/pull/6783, + https://github.com/ansible-collections/community.general/pull/6837). diff --git a/changelogs/fragments/6783-rhsm_repository-internal-refactor.yml b/changelogs/fragments/6783-rhsm_repository-internal-refactor.yml deleted file mode 100644 index 7b76118d68..0000000000 --- a/changelogs/fragments/6783-rhsm_repository-internal-refactor.yml +++ /dev/null @@ -1,6 +0,0 @@ -minor_changes: - - | - rhsm_repository - the interaction with ``subscription-manager`` was - refactored by grouping things together, removing unused bits, and hardening - the way it is run; no behaviour change is expected - (https://github.com/ansible-collections/community.general/pull/6783). diff --git a/plugins/modules/rhsm_repository.py b/plugins/modules/rhsm_repository.py index 2a7d8acf2a..e58389102e 100644 --- a/plugins/modules/rhsm_repository.py +++ b/plugins/modules/rhsm_repository.py @@ -90,7 +90,6 @@ repositories: type: list ''' -import re import os from fnmatch import fnmatch from copy import deepcopy @@ -129,15 +128,6 @@ class Rhsm(object): """ rc, out, err = self.run_repos(['--list']) - skip_lines = [ - '+----------------------------------------------------------+', - ' Available Repositories in /etc/yum.repos.d/redhat.repo' - ] - repo_id_re = re.compile(r'Repo ID:\s+(.*)') - repo_name_re = re.compile(r'Repo Name:\s+(.*)') - repo_url_re = re.compile(r'Repo URL:\s+(.*)') - repo_enabled_re = re.compile(r'Enabled:\s+(.*)') - repo_id = '' repo_name = '' repo_url = '' @@ -145,27 +135,27 @@ class Rhsm(object): repo_result = [] for line in out.splitlines(): - if line == '' or line in skip_lines: + # ignore lines that are: + # - empty + # - "+---------[...]" -- i.e. header + # - " Available Repositories [...]" -- i.e. header + if line == '' or line[0] == '+' or line[0] == ' ': continue - repo_id_match = repo_id_re.match(line) - if repo_id_match: - repo_id = repo_id_match.group(1) + if line.startswith('Repo ID: '): + repo_id = line[9:].lstrip() continue - repo_name_match = repo_name_re.match(line) - if repo_name_match: - repo_name = repo_name_match.group(1) + if line.startswith('Repo Name: '): + repo_name = line[11:].lstrip() continue - repo_url_match = repo_url_re.match(line) - if repo_url_match: - repo_url = repo_url_match.group(1) + if line.startswith('Repo URL: '): + repo_url = line[10:].lstrip() continue - repo_enabled_match = repo_enabled_re.match(line) - if repo_enabled_match: - repo_enabled = repo_enabled_match.group(1) + if line.startswith('Enabled: '): + repo_enabled = line[9:].lstrip() repo = { "id": repo_id,