From 9c1394e8f69f00267aa59f4e1e083eda80dbabfd Mon Sep 17 00:00:00 2001 From: James Garner Date: Thu, 21 Nov 2024 13:10:06 +1300 Subject: [PATCH] feat: support inline comments in deb822 source files --- lib/charms/operator_libs_linux/v0/apt.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/charms/operator_libs_linux/v0/apt.py b/lib/charms/operator_libs_linux/v0/apt.py index 9e57f3b9..2da629fc 100644 --- a/lib/charms/operator_libs_linux/v0/apt.py +++ b/lib/charms/operator_libs_linux/v0/apt.py @@ -1378,16 +1378,21 @@ def _parse_deb822_lines( @staticmethod def _iter_deb822_paragraphs(lines: Iterable[str]) -> Iterator[List[Tuple[int, str]]]: + """Given lines from a deb822 format file, yield paragraphs. + + A paragraph is a list of numbered lines that make up a source entry, + with comments stripped out (but accounted for in line numbering). + """ current_paragraph: List[Tuple[int, str]] = [] for n, line in enumerate(lines): # 0 indexed line numbers, following `load` - if line.startswith("#"): - continue if not line: # blank lines separate paragraphs if current_paragraph: yield current_paragraph current_paragraph = [] continue - current_paragraph.append((n, line)) + content, _delim, _comment = line.partition("#") + if content.strip(): # skip (potentially indented) comment line + current_paragraph.append((n, content.rstrip())) # preserve indent if current_paragraph: yield current_paragraph