From a601d31d38a972224f8f24ba2adbf75794af8c30 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Fri, 24 Apr 2026 17:17:03 +0200 Subject: [PATCH 1/2] problem_report: introduce _create_text_attachment Refactor `problem_report.py` by introducing the helper function `_create_text_attachment` in preparation for following commits. Bug: https://launchpad.net/bugs/2149892 Signed-off-by: Benjamin Drung --- problem_report.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/problem_report.py b/problem_report.py index 876796027..ae96be642 100644 --- a/problem_report.py +++ b/problem_report.py @@ -111,6 +111,13 @@ def _create_compressed_attachment(name: str, value: bytes) -> email.mime.base.MI return attachment +def _create_text_attachment(name: str, value: str) -> email.mime.base.MIMEBase: + filename = f"{name}.txt" + attachment = email.mime.text.MIMEText(value, _charset="UTF-8") + attachment.add_header("Content-Disposition", "attachment", filename=filename) + return attachment + + def _derive_compression(name: str, value: bytes) -> tuple[str, str]: if value.startswith(GZIP_HEADER_START): return ("gzip", ".gz") @@ -903,11 +910,7 @@ def write_mime( text += v.strip().replace("\n", "\n ") + "\n" else: # too large, separate attachment - att = email.mime.text.MIMEText(v, _charset="UTF-8") - att.add_header( - "Content-Disposition", "attachment", filename=k + ".txt" - ) - attachments.append(att) + attachments.append(_create_text_attachment(k, v)) # create initial text attachment att = email.mime.text.MIMEText(text, _charset="UTF-8") From 05d7d77921fc2fd38dcbb13e339e6ae890099b06 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Fri, 24 Apr 2026 17:17:08 +0200 Subject: [PATCH 2/2] problem_report: avoid double .txt file extension Some bug reports like bug #2147517 have an attachment with double file extension: `CurrentDmesg.txt.txt` Do not add `.txt` in case the key name already contains the file extension. Bug: https://launchpad.net/bugs/2149892 Signed-off-by: Benjamin Drung --- problem_report.py | 2 +- tests/unit/test_problem_report.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/problem_report.py b/problem_report.py index ae96be642..4d185e477 100644 --- a/problem_report.py +++ b/problem_report.py @@ -112,7 +112,7 @@ def _create_compressed_attachment(name: str, value: bytes) -> email.mime.base.MI def _create_text_attachment(name: str, value: str) -> email.mime.base.MIMEBase: - filename = f"{name}.txt" + filename = _add_extension_if_missing(name, ".txt") attachment = email.mime.text.MIMEText(value, _charset="UTF-8") attachment.add_header("Content-Disposition", "attachment", filename=filename) return attachment diff --git a/tests/unit/test_problem_report.py b/tests/unit/test_problem_report.py index a3996924f..9ca730672 100644 --- a/tests/unit/test_problem_report.py +++ b/tests/unit/test_problem_report.py @@ -668,7 +668,7 @@ def test_write_mime_text(self) -> None: pr["SimpleLineEnd"] = "bar\n" pr["TwoLine"] = "first\nsecond\n" pr["InlineMargin"] = "first\nsecond\nthird\nfourth\nfifth\n" - pr["Multiline"] = " foo bar\nbaz\n blip \nline4\nline♥5!!\nłıµ€ ⅝\n" + pr["Multiline.txt"] = " foo bar\nbaz\n blip \nline4\nline♥5!!\nłıµ€ ⅝\n" # still small enough for inline text pr["Largeline"] = "A" * 999