1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Pino Toscano 2023-07-15 12:57:54 +02:00 committed by GitHub
parent 3a6955cbd7
commit e0324cdc90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 29 deletions

View file

@ -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).

View file

@ -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).

View file

@ -90,7 +90,6 @@ repositories:
type: list type: list
''' '''
import re
import os import os
from fnmatch import fnmatch from fnmatch import fnmatch
from copy import deepcopy from copy import deepcopy
@ -129,15 +128,6 @@ class Rhsm(object):
""" """
rc, out, err = self.run_repos(['--list']) 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_id = ''
repo_name = '' repo_name = ''
repo_url = '' repo_url = ''
@ -145,27 +135,27 @@ class Rhsm(object):
repo_result = [] repo_result = []
for line in out.splitlines(): 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 continue
repo_id_match = repo_id_re.match(line) if line.startswith('Repo ID: '):
if repo_id_match: repo_id = line[9:].lstrip()
repo_id = repo_id_match.group(1)
continue continue
repo_name_match = repo_name_re.match(line) if line.startswith('Repo Name: '):
if repo_name_match: repo_name = line[11:].lstrip()
repo_name = repo_name_match.group(1)
continue continue
repo_url_match = repo_url_re.match(line) if line.startswith('Repo URL: '):
if repo_url_match: repo_url = line[10:].lstrip()
repo_url = repo_url_match.group(1)
continue continue
repo_enabled_match = repo_enabled_re.match(line) if line.startswith('Enabled: '):
if repo_enabled_match: repo_enabled = line[9:].lstrip()
repo_enabled = repo_enabled_match.group(1)
repo = { repo = {
"id": repo_id, "id": repo_id,