mirror of
https://github.com/chatmail/relay.git
synced 2026-05-11 08:24:37 +00:00
unify chatmail-fsreport and chatmail-expire to both just require a chatmail.ini file
This commit is contained in:
@@ -121,15 +121,12 @@ class Expiry:
|
||||
)
|
||||
|
||||
|
||||
def main(args):
|
||||
def main(args=None):
|
||||
"""Expire mailboxes and messages according to chatmail config"""
|
||||
parser = ArgumentParser(description=main.__doc__)
|
||||
parser.add_argument(
|
||||
"chatmail_ini", action="store", help="path pointing to chatmail.ini file"
|
||||
)
|
||||
parser.add_argument(
|
||||
"mailboxes_dir", action="store", help="path to directory of mailboxes"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--days", action="store", help="assume date to be days older than now"
|
||||
)
|
||||
@@ -153,7 +150,7 @@ def main(args):
|
||||
action="store_true",
|
||||
help="actually remove all expired files and dirs",
|
||||
)
|
||||
args = parser.parse_args([str(x) for x in args])
|
||||
args = parser.parse_args(args)
|
||||
|
||||
config = read_config(args.chatmail_ini)
|
||||
now = datetime.utcnow().timestamp()
|
||||
@@ -162,7 +159,7 @@ def main(args):
|
||||
|
||||
maxnum = int(args.maxnum) if args.maxnum else None
|
||||
exp = Expiry(config, dry=not args.remove, now=now, verbose=args.verbose)
|
||||
for mailbox in iter_mailboxes(os.path.abspath(args.mailboxes_dir), maxnum=maxnum):
|
||||
for mailbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum):
|
||||
exp.process_mailbox_stat(mailbox)
|
||||
print(exp.get_summary())
|
||||
|
||||
|
||||
@@ -3,15 +3,15 @@ command line tool to analyze mailbox message storage
|
||||
|
||||
example invocation:
|
||||
|
||||
python -m chatmaild.fsreport /home/vmail/mail/example.org
|
||||
python -m chatmaild.fsreport /path/to/chatmail.ini
|
||||
|
||||
to show storage summaries for all "cur" folders
|
||||
|
||||
python -m chatmaild.fsreport /home/vmail/mail/example.org --mdir cur
|
||||
python -m chatmaild.fsreport /path/to/chatmail.ini --mdir cur
|
||||
|
||||
to show storage summaries only for first 1000 mailboxes
|
||||
|
||||
python -m chatmaild.fsreport /home/vmail/mail/example.org --maxnum 1000
|
||||
python -m chatmaild.fsreport /path/to/chatmail.ini --maxnum 1000
|
||||
|
||||
"""
|
||||
|
||||
@@ -19,6 +19,7 @@ import os
|
||||
from argparse import ArgumentParser
|
||||
from datetime import datetime
|
||||
|
||||
from chatmaild.config import read_config
|
||||
from chatmaild.expire import iter_mailboxes
|
||||
|
||||
DAYSECONDS = 24 * 60 * 60
|
||||
@@ -125,7 +126,7 @@ def main(args=None):
|
||||
"""Report about filesystem storage usage of all mailboxes and messages"""
|
||||
parser = ArgumentParser(description=main.__doc__)
|
||||
parser.add_argument(
|
||||
"mailboxes_dir", action="store", help="path to directory of mailboxes"
|
||||
"chatmail_ini", action="store", help="path pointing to chatmail.ini file"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--days",
|
||||
@@ -155,13 +156,15 @@ def main(args=None):
|
||||
|
||||
args = parser.parse_args(args)
|
||||
|
||||
config = read_config(args.chatmail_ini)
|
||||
|
||||
now = datetime.utcnow().timestamp()
|
||||
if args.days:
|
||||
now = now - 86400 * int(args.days)
|
||||
|
||||
maxnum = int(args.maxnum) if args.maxnum else None
|
||||
rep = Report(now=now, min_login_age=int(args.min_login_age), mdir=args.mdir)
|
||||
for mbox in iter_mailboxes(os.path.abspath(args.mailboxes_dir), maxnum=maxnum):
|
||||
for mbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum):
|
||||
rep.process_mailbox_stat(mbox)
|
||||
rep.dump_summary()
|
||||
|
||||
|
||||
@@ -49,7 +49,6 @@ def test_delete_inactive_users(example_config):
|
||||
args=[
|
||||
"--remove",
|
||||
str(example_config._inipath),
|
||||
str(example_config.mailboxes_dir),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@@ -11,9 +11,8 @@ from chatmaild.expire import main as expiry_main
|
||||
from chatmaild.fsreport import main as report_main
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def basedir1(tmp_path):
|
||||
basedir1 = tmp_path.joinpath("mailbox1@example.org")
|
||||
def fill_mbox(basedir):
|
||||
basedir1 = basedir.joinpath("mailbox1@example.org")
|
||||
basedir1.mkdir()
|
||||
password = basedir1.joinpath("password")
|
||||
password.write_text("xxx")
|
||||
@@ -39,7 +38,8 @@ def create_new_messages(basedir, relpaths, size=1000, days=0):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mbox1(basedir1):
|
||||
def mbox1(example_config):
|
||||
basedir1 = fill_mbox(example_config.mailboxes_dir)
|
||||
return MailboxStat(basedir1)
|
||||
|
||||
|
||||
@@ -73,8 +73,8 @@ def test_stats_mailbox(mbox1):
|
||||
assert mbox3.last_login is None
|
||||
|
||||
|
||||
def test_report(mbox1):
|
||||
args = (str(Path(mbox1.basedir).parent),)
|
||||
def test_report(mbox1, example_config):
|
||||
args = (str(example_config._inipath),)
|
||||
report_main(args)
|
||||
args = list(args) + "--days 1".split()
|
||||
report_main(args)
|
||||
@@ -85,7 +85,7 @@ def test_report(mbox1):
|
||||
|
||||
|
||||
def test_expiry_cli_basic(example_config, mbox1):
|
||||
args = example_config._inipath, Path(mbox1.basedir).parent
|
||||
args = (example_config._inipath,)
|
||||
expiry_main(args)
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ def test_expiry_cli_old_files(capsys, example_config, mbox1):
|
||||
|
||||
create_new_messages(mbox1.basedir, ["cur/shouldstay"], size=1000 * 300, days=1)
|
||||
|
||||
args = example_config._inipath, Path(mbox1.basedir).parent, "--remove", "-v"
|
||||
args = str(example_config._inipath), "--remove", "-v"
|
||||
expiry_main(args)
|
||||
out, err = capsys.readouterr()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user