-
-
Notifications
You must be signed in to change notification settings - Fork 34
Replace expiry "find" commands with a new chatmaild.expire python module + a reporting one #637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
888e6bf
move delete_inactive_users to new implementation
hpk42 f863600
do all expunging in python
hpk42 31ac558
add summary reporting, rework expiry logic
hpk42 64a46d6
refactor and write tests for overall expiry/report runs
hpk42 8d9371e
add basic command line parsing for expire + some streamlining
hpk42 4be2beb
add argument parsing for reporting
hpk42 0dc3196
fix comment
hpk42 e77215c
strike superflous code
hpk42 a034986
more streamline
hpk42 f32c5c5
don't globally collect files anymore to avoid using growing-with-numb…
hpk42 01a2a87
no reporting by default, and adding a summary line
hpk42 a18c582
further reduce code
hpk42 c59f6c7
remove superflous Stats class
hpk42 5956c51
some renaming
hpk42 101ffca
fix lint issues
hpk42 d1941f0
during fsreport (reporting) don't store all mailbxoes but categorize …
hpk42 21efffa
remove superflous totalsize attribute
hpk42 c3ed0ee
prefix new commands
hpk42 fcea25c
address four review comments from link2xt
hpk42 6e26a62
cosmetic: refine summary and fix typo
hpk42 aeec17c
unify chatmail-fsreport and chatmail-expire to both just require a ch…
hpk42 3fd56ce
fix another invocation
hpk42 b159b60
always use "H" for printing numbers, and make "chatmail.ini" file opt…
hpk42 39adf4a
unify K output
hpk42 0e4899e
use systemd timer instead of cron-job for expiry (tested by hand on c2)
hpk42 b834c43
also run fsreport
hpk42 a5fa8cb
simplify and beautify formatting and sizes
hpk42 97ddeb3
Apply suggestions from code review
hpk42 4d69543
replace expunge mentioning in architecture
hpk42 aa0aa7c
try fix CI
hpk42 5e01aa3
make sure fsreport can run on empty mailbox dir
hpk42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| """ | ||
| Expire old messages and addresses. | ||
|
|
||
| """ | ||
|
|
||
| import os | ||
| import shutil | ||
| import sys | ||
| import time | ||
| from argparse import ArgumentParser | ||
| from collections import namedtuple | ||
| from datetime import datetime | ||
| from stat import S_ISREG | ||
|
|
||
| from chatmaild.config import read_config | ||
|
|
||
| FileEntry = namedtuple("FileEntry", ("relpath", "mtime", "size")) | ||
|
|
||
|
|
||
| def iter_mailboxes(basedir, maxnum): | ||
| if not os.path.exists(basedir): | ||
| print_info(f"no mailboxes found at: {basedir}") | ||
| return | ||
|
|
||
| for name in os.listdir(basedir)[:maxnum]: | ||
| if "@" in name: | ||
| yield MailboxStat(basedir + "/" + name) | ||
|
|
||
|
|
||
| class MailboxStat: | ||
| last_login = None | ||
|
|
||
| def __init__(self, basedir): | ||
| self.basedir = str(basedir) | ||
| # all detected messages in cur/new/tmp folders | ||
| self.messages = [] | ||
|
|
||
| # all detected files in mailbox top dir | ||
| self.extrafiles = [] | ||
|
|
||
| # scan all relevant files (without recursion) | ||
| old_cwd = os.getcwd() | ||
| os.chdir(self.basedir) | ||
| for name in os.listdir("."): | ||
| if name in ("cur", "new", "tmp"): | ||
| for msg_name in os.listdir(name): | ||
| relpath = name + "/" + msg_name | ||
| st = os.stat(relpath) | ||
| self.messages.append(FileEntry(relpath, st.st_mtime, st.st_size)) | ||
| else: | ||
| st = os.stat(name) | ||
| if S_ISREG(st.st_mode): | ||
| self.extrafiles.append(FileEntry(name, st.st_mtime, st.st_size)) | ||
| if name == "password": | ||
| self.last_login = st.st_mtime | ||
| self.extrafiles.sort(key=lambda x: -x.size) | ||
| os.chdir(old_cwd) | ||
|
|
||
|
|
||
| def print_info(msg): | ||
| print(msg, file=sys.stderr) | ||
|
|
||
|
|
||
| class Expiry: | ||
| def __init__(self, config, dry, now, verbose): | ||
| self.config = config | ||
| self.dry = dry | ||
| self.now = now | ||
| self.verbose = verbose | ||
| self.del_mboxes = 0 | ||
| self.all_mboxes = 0 | ||
| self.del_files = 0 | ||
| self.all_files = 0 | ||
| self.start = time.time() | ||
|
|
||
| def remove_mailbox(self, mboxdir): | ||
| if self.verbose: | ||
| print_info(f"removing {mboxdir}") | ||
| if not self.dry: | ||
| shutil.rmtree(mboxdir) | ||
| self.del_mboxes += 1 | ||
|
|
||
| def remove_file(self, path): | ||
| if self.verbose: | ||
| print_info(f"removing {path}") | ||
| if not self.dry: | ||
| try: | ||
| os.unlink(path) | ||
| except FileNotFoundError: | ||
| print_info(f"file not found/vanished {path}") | ||
| self.del_files += 1 | ||
|
|
||
| def process_mailbox_stat(self, mbox): | ||
| cutoff_without_login = ( | ||
| self.now - int(self.config.delete_inactive_users_after) * 86400 | ||
| ) | ||
| cutoff_mails = self.now - int(self.config.delete_mails_after) * 86400 | ||
| cutoff_large_mails = self.now - int(self.config.delete_large_after) * 86400 | ||
|
|
||
| self.all_mboxes += 1 | ||
| changed = False | ||
| if mbox.last_login and mbox.last_login < cutoff_without_login: | ||
| self.remove_mailbox(mbox.basedir) | ||
| return | ||
|
|
||
| # all to-be-removed files are relative to the mailbox basedir | ||
| os.chdir(mbox.basedir) | ||
| self.all_files += len(mbox.messages) | ||
| for message in mbox.messages: | ||
| if message.mtime < cutoff_mails: | ||
| self.remove_file(message.relpath) | ||
| elif message.size > 200000 and message.mtime < cutoff_large_mails: | ||
hpk42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.remove_file(message.relpath) | ||
| else: | ||
| continue | ||
| changed = True | ||
| if changed: | ||
| self.remove_file("maildirsize") | ||
|
|
||
| def get_summary(self): | ||
| return ( | ||
| f"Removed {self.del_mboxes} out of {self.all_mboxes} mailboxes " | ||
| f"and {self.del_files} out of {self.all_files} files in existing mailboxes " | ||
| f"in {time.time() - self.start:2.2f} seconds" | ||
| ) | ||
|
|
||
|
|
||
| def main(args=None): | ||
| """Expire mailboxes and messages according to chatmail config""" | ||
| parser = ArgumentParser(description=main.__doc__) | ||
| ini = "/usr/local/lib/chatmaild/chatmail.ini" | ||
| parser.add_argument( | ||
| "chatmail_ini", | ||
| action="store", | ||
| nargs="?", | ||
| help=f"path pointing to chatmail.ini file, default: {ini}", | ||
| default=ini, | ||
| ) | ||
| parser.add_argument( | ||
| "--days", action="store", help="assume date to be days older than now" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--maxnum", | ||
| default=None, | ||
| action="store", | ||
| help="maximum number of mailboxes to iterate on", | ||
| ) | ||
| parser.add_argument( | ||
| "-v", | ||
| dest="verbose", | ||
| action="store_true", | ||
| help="print out removed files and mailboxes", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--remove", | ||
| dest="remove", | ||
| action="store_true", | ||
| help="actually remove all expired files and dirs", | ||
| ) | ||
| 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 | ||
| exp = Expiry(config, dry=not args.remove, now=now, verbose=args.verbose) | ||
| for mailbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum): | ||
| exp.process_mailbox_stat(mailbox) | ||
| print(exp.get_summary()) | ||
hpk42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main(sys.argv[1:]) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.