Fix regression in yumpkg._parse_repo_file()

Changing this function to use a configparser removed its ability to
track comments, causing a loss of idempotence when comments are being
managed. This fixes that regression by adding a check for each repo's
comments during the same pass we use to read in the header comments.
This commit is contained in:
Erik Johnson 2018-03-13 21:08:50 -05:00
parent f0c79e3da3
commit c3e36a6c94
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -2663,20 +2663,18 @@ def mod_repo(repo, basedir=None, **kwargs):
filerepos[repo].update(repo_opts)
content = header
for stanza in six.iterkeys(filerepos):
comments = ''
if 'comments' in six.iterkeys(filerepos[stanza]):
comments = salt.utils.pkg.rpm.combine_comments(
filerepos[stanza]['comments'])
del filerepos[stanza]['comments']
content += '\n[{0}]'.format(stanza)
comments = filerepos[stanza].pop('comments', [])
if comments:
comments = salt.utils.pkg.rpm.combine_comments(comments)
content += '[{0}]\n'.format(stanza)
for line in six.iterkeys(filerepos[stanza]):
content += '\n{0}={1}'.format(
content += '{0}={1}\n'.format(
line,
filerepos[stanza][line]
if not isinstance(filerepos[stanza][line], bool)
else _bool_to_str(filerepos[stanza][line])
)
content += '\n{0}\n'.format(comments)
content += comments + '\n'
with salt.utils.fopen(repofile, 'w') as fileout:
fileout.write(content)
@ -2704,14 +2702,29 @@ def _parse_repo_file(filename):
section_dict.pop('__name__', None)
config[section] = section_dict
# Try to extract leading comments
# Try to extract header comments, as well as comments for each repo. Read
# from the beginning of the file and assume any leading comments are
# header comments. Continue to read each section header and then find the
# comments for each repo.
headers = ''
with salt.utils.fopen(filename, 'r') as rawfile:
for line in rawfile:
if line.strip().startswith('#'):
headers += '{0}\n'.format(line.strip())
else:
break
section = None
with salt.utils.fopen(filename, 'r') as repofile:
for line in repofile:
line = line.strip()
if line.startswith('#'):
if section is None:
headers += line + '\n'
else:
try:
comments = config[section].setdefault('comments', [])
comments.append(line[1:].lstrip())
except KeyError:
log.debug(
'Found comment in %s which does not appear to '
'belong to any repo section: %s', filename, line
)
elif line.startswith('[') and line.endswith(']'):
section = line[1:-1]
return (headers, config)