feat(web-app-stalwart): import Mailu only until one import has finished

The import script writes a done marker into the instance directory when it ends with rc 0, and the deploy skips the unit and the import block while the marker exists; deleting it imports again. The IMAP layout helpers move to imap_layout.py so the script stays under the 500-line limit, with mirrored tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-23 19:51:10 +02:00
1 parent 43ce74236e
commit 5b6db5dc24
9 files changed
+242 -128

No files matched your search

@@ -0,0 +1,94 @@
import importlib.util
import sys
import unittest
from . import PROJECT_ROOT
SCRIPTS = PROJECT_ROOT / "roles/web-app-stalwart/files/python"
LIST_LINES = [
b'(\\HasNoChildren) "/" "INBOX"',
b'(\\HasNoChildren \\Sent) "/" "Sent Items"',
b'(\\HasNoChildren \\Junk) "/" "Junk Mail"',
b'(\\HasNoChildren) "/" Archive',
]
def _load(name: str):
spec = importlib.util.spec_from_file_location(name, str(SCRIPTS / f"{name}.py"))
mod = importlib.util.module_from_spec(spec)
assert spec and spec.loader
sys.modules[name] = mod
spec.loader.exec_module(mod)
return mod
def _load_module():
_load("mailu_maildir")
return _load("imap_layout")
class _FakeImap:
def __init__(self, existing_headers: list[bytes] | None = None, list_lines=None):
self._existing_headers = existing_headers or []
self._list_lines = LIST_LINES if list_lines is None else list_lines
def list(self):
return "OK", list(self._list_lines)
def search(self, charset, *criteria):
nums = b" ".join(
str(i + 1).encode() for i in range(len(self._existing_headers))
)
return "OK", [nums]
def fetch(self, message_set, message_parts):
data = [
((f"{i + 1} ()").encode(), header)
for i, header in enumerate(self._existing_headers)
]
return "OK", data
class TestServerLayout(unittest.TestCase):
def setUp(self):
self.m = _load_module()
self.layout = self.m.server_layout(_FakeImap())
def test_reads_separator_and_special_use(self):
self.assertEqual(self.layout.separator, "/")
self.assertEqual(
self.layout.special_use, {"\\Sent": "Sent Items", "\\Junk": "Junk Mail"}
)
def test_nested_folder_uses_the_server_separator(self):
self.assertEqual(
self.m.destination_mailbox(".Archive.2024", self.layout), "Archive/2024"
)
def test_special_folder_lands_in_the_special_use_folder(self):
self.assertEqual(self.m.destination_mailbox(".Sent", self.layout), "Sent Items")
self.assertEqual(self.m.destination_mailbox(".Spam", self.layout), "Junk Mail")
def test_special_name_without_server_counterpart_keeps_its_name(self):
self.assertEqual(self.m.destination_mailbox(".Drafts", self.layout), "Drafts")
def test_ambiguous_separator_fails(self):
imap = _FakeImap(list_lines=[b'(\\X) "/" A', b'(\\X) "." B'])
with self.assertRaises(self.m.MigrationError):
self.m.server_layout(imap)
def test_imap_quote_escapes(self):
self.assertEqual(self.m.imap_quote('My "x"\\y'), '"My \\"x\\"\\\\y"')
class TestExistingKeys(unittest.TestCase):
def test_collects_the_server_side_keys(self):
m = _load_module()
imap = _FakeImap(
existing_headers=[b"Message-ID: <a@x>\r\n\r\n", b"X: 1\r\n\r\n"]
)
self.assertEqual(m.existing_keys(imap), {"<a@x>"})
if __name__ == "__main__":
unittest.main()
@@ -28,6 +28,7 @@ def _load(name: str):
def _load_module():
_load("mailu_maildir")
_load("imap_layout")
return _load("migrate_from_mailu")
@@ -78,38 +79,6 @@ class _SlowImap(_FakeImap):
return super().append(mailbox, flags, date_time, message)
class TestServerLayout(unittest.TestCase):
def setUp(self):
self.m = _load_module()
self.layout = self.m.server_layout(_FakeImap())
def test_reads_separator_and_special_use(self):
self.assertEqual(self.layout.separator, "/")
self.assertEqual(
self.layout.special_use, {"\\Sent": "Sent Items", "\\Junk": "Junk Mail"}
)
def test_nested_folder_uses_the_server_separator(self):
self.assertEqual(
self.m.destination_mailbox(".Archive.2024", self.layout), "Archive/2024"
)
def test_special_folder_lands_in_the_special_use_folder(self):
self.assertEqual(self.m.destination_mailbox(".Sent", self.layout), "Sent Items")
self.assertEqual(self.m.destination_mailbox(".Spam", self.layout), "Junk Mail")
def test_special_name_without_server_counterpart_keeps_its_name(self):
self.assertEqual(self.m.destination_mailbox(".Drafts", self.layout), "Drafts")
def test_ambiguous_separator_fails(self):
imap = _FakeImap(list_lines=[b'(\\X) "/" A', b'(\\X) "." B'])
with self.assertRaises(self.m.MigrationError):
self.m.server_layout(imap)
def test_imap_quote_escapes(self):
self.assertEqual(self.m.imap_quote('My "x"\\y'), '"My \\"x\\"\\\\y"')
class TestMigrateAccount(unittest.TestCase):
def setUp(self):
self.m = _load_module()
@@ -164,12 +133,6 @@ class TestMigrateAccount(unittest.TestCase):
self.assertEqual(stats.folder("Archive").skipped, 1)
self.assertEqual(stats.total("found"), 4)
def test_existing_keys_collects_the_server_side_keys(self):
imap = _FakeImap(
existing_headers=[b"Message-ID: <a@x>\r\n\r\n", b"X: 1\r\n\r\n"]
)
self.assertEqual(self.m.existing_keys(imap), {"<a@x>"})
def test_parallel_sessions_append_every_message_once(self):
for index in range(5, 45):
(self.account / "cur" / f"{index}.host:2,").write_bytes(
@@ -205,9 +168,17 @@ class TestOpenSessions(unittest.TestCase):
"--admin-file=/x/admin.json",
"--mailbox-map=[]",
f"--workers={workers}",
"--done-file=/x/import.done",
]
)
def test_done_marker_records_the_start_of_the_run(self):
with tempfile.TemporaryDirectory() as base:
marker = Path(base) / "import.done"
self.m.mark_done(marker, 1700000000.4)
self.assertTrue(marker.is_file())
self.assertEqual(marker.stat().st_size, len("1700000000\n"))
def test_opens_one_session_per_worker(self):
self.m.open_session = lambda args, stalwart, target: "s"
self.assertEqual(self.m.open_sessions(self._args(3), None, "a@x"), ["s"] * 3)