Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image introspect for long python metadata #1555

Merged
merged 7 commits into from
Jun 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions src/ansible_navigator/data/image_introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,51 @@ def re_partition(content, separator):
:returns: The first partition, separator, and final partition
"""
separator_match = re.search(separator, content)
if not separator_match:
return content, "", ""
matched_separator = separator_match.group(0)
parts = re.split(matched_separator, content, 1)
return parts[0], matched_separator, parts[1]
if not separator_match or content.startswith(" "):
return "", "", content
delim = separator_match.group(0)
key, content = re.split(delim, content, 1)
return key, delim, content

def splitter(self, lines, delimiter):
def splitter(self, lines, line_split, section_delim=None):
"""Split lines given a delimiter.

:param lines: The lines to split
:param delimiter: The delimiter to use for splitting
:param line_split: The delimiter use for splitting each line
:param section_delim: The separator between different packages
:returns: All lines split on the delimiter
"""
results = []
result = {}
current_key = ""
while lines:
line = lines.pop()
left, delim, right = self.re_partition(line, delimiter)
right = self._strip(right)
if not delim:
if result:
results.append(result)
result = {}
line = lines.pop(0)
key, delim, content = self.re_partition(line, line_split)
content = self._strip(content)
if section_delim and line == section_delim:
results.append(result)
result = {}
continue
key = left.lower().replace("_", "-").strip()
value = right
result[key] = value
if not delim and current_key:
result[current_key] += f" {content}"
continue
current_key = key.lower().replace("_", "-").strip()
# system_packages description field needs special handling
if current_key == "description":
description = []
while lines:
line = lines.pop(0)
if line == section_delim:
break
description.append(line)
if description:
result[current_key] = " ".join(description)
else:
result[current_key] = "No description available"
results.append(result)
return result
result[current_key] = content

if result:
results.append(result)
return results
Expand Down Expand Up @@ -293,7 +311,7 @@ def parse(self, command):

:param command: The result of running the command
"""
parsed = self.splitter(command.stdout.splitlines(), ":")
parsed = self.splitter(command.stdout.splitlines(), line_split=":", section_delim="---")
for pkg in parsed:
for entry in ["required-by", "requires"]:
if pkg[entry]:
Expand Down Expand Up @@ -362,28 +380,8 @@ def parse(self, command):

parsed = []
for package in packages:
entry = {}
while package:
line = package.pop(0)
left, _delim, right = self.re_partition(line, ":")
key = left.lower().replace("_", "-").strip()

# Description is at the end of the package section
# read until package is empty
if key == "description":
description = []
while package:
description.append(package.pop(0))
# Normalize the data, in the case description is totally empty
if description:
entry[key] = "\n".join(description)
else:
entry[key] = "No description available"
parsed.append(entry)
# other package details are 1 line each
else:
value = self._strip(right)
entry[key] = value
result = self.splitter(package, line_split=":")
parsed.append(result)

command.details = parsed

Expand Down