mirror of
https://github.com/chatmail/relay.git
synced 2026-05-20 12:58:04 +00:00
remove superflous Stats class
This commit is contained in:
@@ -17,18 +17,10 @@ from chatmaild.config import read_config
|
|||||||
FileEntry = namedtuple("FileEntry", ("relpath", "mtime", "size"))
|
FileEntry = namedtuple("FileEntry", ("relpath", "mtime", "size"))
|
||||||
|
|
||||||
|
|
||||||
class Stats:
|
def iter_mailboxes(basedir, maxnum):
|
||||||
def __init__(self, basedir, maxnum=None):
|
for name in os.listdir(basedir)[:maxnum]:
|
||||||
self.basedir = str(basedir)
|
if "@" in name:
|
||||||
self.maxnum = maxnum
|
yield MailboxStat(basedir + "/" + name)
|
||||||
|
|
||||||
def iter_mailboxes(self, callback=None):
|
|
||||||
for name in os.listdir(self.basedir)[: self.maxnum]:
|
|
||||||
if "@" in name:
|
|
||||||
basedir = self.basedir + "/" + name
|
|
||||||
mailbox = MailboxStat(basedir)
|
|
||||||
if callback is not None:
|
|
||||||
callback(mailbox)
|
|
||||||
|
|
||||||
|
|
||||||
class MailboxStat:
|
class MailboxStat:
|
||||||
@@ -69,9 +61,8 @@ def print_info(msg):
|
|||||||
|
|
||||||
|
|
||||||
class Expiry:
|
class Expiry:
|
||||||
def __init__(self, config, stats, dry, now, verbose):
|
def __init__(self, config, dry, now, verbose):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.stats = stats
|
|
||||||
self.dry = dry
|
self.dry = dry
|
||||||
self.now = now
|
self.now = now
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
@@ -173,9 +164,9 @@ def main(args):
|
|||||||
now = now - 86400 * int(args.days)
|
now = now - 86400 * int(args.days)
|
||||||
|
|
||||||
maxnum = int(args.maxnum) if args.maxnum else None
|
maxnum = int(args.maxnum) if args.maxnum else None
|
||||||
stats = Stats(os.path.abspath(args.mailboxes_dir), maxnum=maxnum)
|
exp = Expiry(config, dry=not args.remove, now=now, verbose=args.verbose)
|
||||||
exp = Expiry(config, stats, dry=not args.remove, now=now, verbose=args.verbose)
|
for mailbox in iter_mailboxes(os.path.abspath(args.mailboxes_dir), maxnum=maxnum):
|
||||||
stats.iter_mailboxes(exp.process_mailbox_stat)
|
exp.process_mailbox_stat(mailbox)
|
||||||
print(exp.get_summary())
|
print(exp.get_summary())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import os
|
|||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from chatmaild.expire import Stats
|
from chatmaild.expire import iter_mailboxes
|
||||||
|
|
||||||
DAYSECONDS = 24 * 60 * 60
|
DAYSECONDS = 24 * 60 * 60
|
||||||
MONTHSECONDS = DAYSECONDS * 30
|
MONTHSECONDS = DAYSECONDS * 30
|
||||||
@@ -35,14 +35,13 @@ def H(size):
|
|||||||
|
|
||||||
|
|
||||||
class Report:
|
class Report:
|
||||||
def __init__(self, stats, now):
|
def __init__(self, now):
|
||||||
self.sum_extra = 0
|
self.sum_extra = 0
|
||||||
self.sum_all_messages = 0
|
self.sum_all_messages = 0
|
||||||
self.messages = []
|
self.messages = []
|
||||||
self.mailboxes = []
|
self.mailboxes = []
|
||||||
self.user_logins = []
|
self.user_logins = []
|
||||||
self.ci_logins = []
|
self.ci_logins = []
|
||||||
self.stats = stats
|
|
||||||
self.now = now
|
self.now = now
|
||||||
|
|
||||||
def process_mailbox_stat(self, mailbox):
|
def process_mailbox_stat(self, mailbox):
|
||||||
@@ -154,9 +153,9 @@ def main(args=None):
|
|||||||
now = now - 86400 * int(args.days)
|
now = now - 86400 * int(args.days)
|
||||||
|
|
||||||
maxnum = int(args.maxnum) if args.maxnum else None
|
maxnum = int(args.maxnum) if args.maxnum else None
|
||||||
stats = Stats(args.mailboxes_dir, maxnum=maxnum)
|
rep = Report(now=now)
|
||||||
rep = Report(stats, now=now)
|
for mbox in iter_mailboxes(os.path.abspath(args.mailboxes_dir), maxnum=maxnum):
|
||||||
stats.iter_mailboxes(rep.process_mailbox_stat)
|
rep.process_mailbox_stat(mbox)
|
||||||
rep.dump_summary()
|
rep.dump_summary()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import pytest
|
|||||||
|
|
||||||
from chatmaild.expire import FileEntry, MailboxStat
|
from chatmaild.expire import FileEntry, MailboxStat
|
||||||
from chatmaild.expire import main as expiry_main
|
from chatmaild.expire import main as expiry_main
|
||||||
from chatmaild.fsreport import Report, Stats
|
from chatmaild.fsreport import Report, iter_mailboxes
|
||||||
|
|
||||||
# XXX basedirsize (used by dovecot quota) needs to be removed after removing files
|
# XXX basedirsize (used by dovecot quota) needs to be removed after removing files
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ def test_stats_mailbox(mbox1):
|
|||||||
assert mbox1.last_login == password.stat().st_mtime
|
assert mbox1.last_login == password.stat().st_mtime
|
||||||
assert len(mbox1.messages) == 2
|
assert len(mbox1.messages) == 2
|
||||||
|
|
||||||
msgs = list(mbox1.messages)
|
msgs = list(sorted(mbox1.messages, key=lambda x: x.size))
|
||||||
assert len(msgs) == 2
|
assert len(msgs) == 2
|
||||||
assert msgs[0].size == 500 # cur
|
assert msgs[0].size == 500 # cur
|
||||||
assert msgs[1].size == 600 # new
|
assert msgs[1].size == 600 # new
|
||||||
@@ -78,9 +78,9 @@ def test_stats_mailbox(mbox1):
|
|||||||
def test_report(mbox1):
|
def test_report(mbox1):
|
||||||
now = datetime.utcnow().timestamp()
|
now = datetime.utcnow().timestamp()
|
||||||
mailboxes_dir = Path(mbox1.basedir).parent
|
mailboxes_dir = Path(mbox1.basedir).parent
|
||||||
stats = Stats(str(mailboxes_dir), maxnum=None)
|
rep = Report(now=now)
|
||||||
rep = Report(stats, now=now)
|
for mailbox in iter_mailboxes(str(mailboxes_dir), maxnum=None):
|
||||||
stats.iter_mailboxes(rep.process_mailbox_stat)
|
rep.process_mailbox_stat(mailbox)
|
||||||
rep.dump_summary()
|
rep.dump_summary()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user