From 4f0eb6483ef867a3e39c3d5f8878ce3285f54659 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Thu, 15 Jun 2023 18:20:14 +0530 Subject: [PATCH 1/5] Fix image introspect for long python metadata --- .../data/image_introspect.py | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/ansible_navigator/data/image_introspect.py b/src/ansible_navigator/data/image_introspect.py index b42d4d12c..2c2af4c0b 100644 --- a/src/ansible_navigator/data/image_introspect.py +++ b/src/ansible_navigator/data/image_introspect.py @@ -163,11 +163,11 @@ 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): """Split lines given a delimiter. @@ -178,18 +178,23 @@ def splitter(self, lines, delimiter): """ results = [] result = {} - while lines: - line = lines.pop() - left, delim, right = self.re_partition(line, delimiter) - right = self._strip(right) + value = "" + current_key = "" + for line in lines: + left, delim, content = self.re_partition(line, delimiter) + content = self._strip(content) if not delim: - if result: - results.append(result) - result = {} + if value and current_key: + value = value + " " + content + result[current_key] = value + else: + if result: + results.append(result) + result = {} continue - key = left.lower().replace("_", "-").strip() - value = right - result[key] = value + current_key = left.lower().replace("_", "-").strip() + value = content + result[current_key] = value if result: results.append(result) return results From 036e7472be5237b5743853eabc109989f4585aca Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Wed, 21 Jun 2023 12:17:43 +0530 Subject: [PATCH 2/5] fix the splitter logic --- .../data/image_introspect.py | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/ansible_navigator/data/image_introspect.py b/src/ansible_navigator/data/image_introspect.py index 2c2af4c0b..43f4140e9 100644 --- a/src/ansible_navigator/data/image_introspect.py +++ b/src/ansible_navigator/data/image_introspect.py @@ -163,38 +163,43 @@ def re_partition(content, separator): :returns: The first partition, separator, and final partition """ separator_match = re.search(separator, content) - if not separator_match or content.startswith(" "): + if not separator_match: return "", "", content delim = separator_match.group(0) key, content = re.split(delim, content, 1) + if key.startswith(" "): + return "", "", content 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 = {} - value = "" + cached_lines = [] current_key = "" - for line in lines: - left, delim, content = self.re_partition(line, delimiter) - content = self._strip(content) - if not delim: - if value and current_key: - value = value + " " + content - result[current_key] = value - else: - if result: - results.append(result) - result = {} + + while lines: + line = lines.pop() + key, delim, content = self.re_partition(line, line_split) + if section_delim and line == section_delim: + results.append(result) + result = {} continue - current_key = left.lower().replace("_", "-").strip() - value = content - result[current_key] = value + if not delim and not current_key: + cached_lines.insert(0, self._strip(content)) + continue + current_key = key.lower().replace("_", "-").strip() + cached_lines.insert(0, self._strip(content)) + result[current_key] = " ".join(cached_lines) + cached_lines = [] + current_key = "" + if result: results.append(result) return results @@ -298,7 +303,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]: From 50bebe23878b875f55fdd1f83484a78f142336d1 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Thu, 22 Jun 2023 13:27:11 +0530 Subject: [PATCH 3/5] towards common splitter --- .../data/image_introspect.py | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ansible_navigator/data/image_introspect.py b/src/ansible_navigator/data/image_introspect.py index 43f4140e9..bd458a03a 100644 --- a/src/ansible_navigator/data/image_introspect.py +++ b/src/ansible_navigator/data/image_introspect.py @@ -163,12 +163,10 @@ def re_partition(content, separator): :returns: The first partition, separator, and final partition """ separator_match = re.search(separator, content) - if not separator_match: + if not separator_match or content.startswith(" "): return "", "", content delim = separator_match.group(0) key, content = re.split(delim, content, 1) - if key.startswith(" "): - return "", "", content return key, delim, content def splitter(self, lines, line_split, section_delim=None): @@ -181,24 +179,24 @@ def splitter(self, lines, line_split, section_delim=None): """ results = [] result = {} - cached_lines = [] + value = "" current_key = "" - while lines: - line = lines.pop() + 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 - if not delim and not current_key: - cached_lines.insert(0, self._strip(content)) + if not delim: + if value and current_key: + value = value + " " + content + result[current_key] = value continue current_key = key.lower().replace("_", "-").strip() - cached_lines.insert(0, self._strip(content)) - result[current_key] = " ".join(cached_lines) - cached_lines = [] - current_key = "" + value = content + result[current_key] = value if result: results.append(result) From 57cc9639ddb4052285847c307da86e4a832c3706 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Thu, 22 Jun 2023 16:31:51 +0530 Subject: [PATCH 4/5] system packages to use common splitter --- .../data/image_introspect.py | 48 ++++++++----------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/ansible_navigator/data/image_introspect.py b/src/ansible_navigator/data/image_introspect.py index bd458a03a..7ab91447b 100644 --- a/src/ansible_navigator/data/image_introspect.py +++ b/src/ansible_navigator/data/image_introspect.py @@ -179,7 +179,6 @@ def splitter(self, lines, line_split, section_delim=None): """ results = [] result = {} - value = "" current_key = "" while lines: line = lines.pop(0) @@ -189,14 +188,25 @@ def splitter(self, lines, line_split, section_delim=None): results.append(result) result = {} continue - if not delim: - if value and current_key: - value = value + " " + content - result[current_key] = value + if not delim and current_key: + result[current_key] += f" {content}" continue current_key = key.lower().replace("_", "-").strip() - value = content - result[current_key] = value + # 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) @@ -370,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=":", section_delim="---") + parsed.append(result) command.details = parsed From 2b8fd917b32bbe255562ffcfd58951b56ac26d31 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Thu, 22 Jun 2023 20:15:21 +0530 Subject: [PATCH 5/5] system_package does not need section_delim for parsing --- src/ansible_navigator/data/image_introspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansible_navigator/data/image_introspect.py b/src/ansible_navigator/data/image_introspect.py index 7ab91447b..84277c12b 100644 --- a/src/ansible_navigator/data/image_introspect.py +++ b/src/ansible_navigator/data/image_introspect.py @@ -380,7 +380,7 @@ def parse(self, command): parsed = [] for package in packages: - result = self.splitter(package, line_split=":", section_delim="---") + result = self.splitter(package, line_split=":") parsed.append(result) command.details = parsed