From 39337b6b5826806c96f54ce5c454b3ef65ee6ce0 Mon Sep 17 00:00:00 2001 From: larkery Date: Fri, 17 Oct 2014 13:33:08 +0100 Subject: [PATCH 1/5] Don't break mbsync when moving mail mbsync dies if you move files around but keep their names the same, after a while. This patch randomises the names when moving things. --- afew/MailMover.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/afew/MailMover.py b/afew/MailMover.py index f99b1a5..8dee7f3 100644 --- a/afew/MailMover.py +++ b/afew/MailMover.py @@ -25,6 +25,7 @@ from .Database import Database from .utils import get_message_summary from datetime import date, datetime, timedelta +import uuid class MailMover(Database): @@ -72,7 +73,14 @@ def move(self, maildir, rules): if self.dry_run: continue try: - shutil.copy2(fname, destination) + shutil.copy2(fname, + os.path.join( + destination, + # construct a new filename, composed of a made-up ID and the flags part + # of the original filename. + str(uuid.uuid1()) + ':' + os.path.basename(fname).split(':')[-1] + ) + ) to_delete_fnames.append(fname) except shutil.Error as e: # this is ugly, but shutil does not provide more From 9c662f553f35fa2a30e32184ff96524574ab4467 Mon Sep 17 00:00:00 2001 From: larkery Date: Fri, 17 Oct 2014 13:40:14 +0100 Subject: [PATCH 2/5] Add a rename option to the mail mover This corresponds to a change in MailMover which makes it rename moved mails so as not to break mbsync --- afew/Settings.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/afew/Settings.py b/afew/Settings.py index a95eef4..cbbc6ee 100644 --- a/afew/Settings.py +++ b/afew/Settings.py @@ -101,3 +101,8 @@ def get_mail_move_age(): max_age = settings.get(mail_mover_section, 'max_age') return max_age +def get_mail_move_rename(): + rename = False + if settings.has_option(mail_mover_section, 'rename'): + rename = settings.get(mail_mover_section, 'rename').lower() == 'true' + return rename From 61f5380188bfa1f95111638b624d1ee9abeeed1b Mon Sep 17 00:00:00 2001 From: larkery Date: Fri, 17 Oct 2014 13:42:28 +0100 Subject: [PATCH 3/5] Pass rename option through to mail mover --- afew/commands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/afew/commands.py b/afew/commands.py index 154b16b..2cce09f 100644 --- a/afew/commands.py +++ b/afew/commands.py @@ -29,7 +29,7 @@ from afew.utils import filter_compat from afew.FilterRegistry import all_filters from afew.Settings import user_config_dir, get_filter_chain -from afew.Settings import get_mail_move_rules, get_mail_move_age +from afew.Settings import get_mail_move_rules, get_mail_move_age, get_mail_move_rename from afew.NotmuchSettings import read_notmuch_settings, get_notmuch_new_query option_parser = optparse.OptionParser( @@ -183,6 +183,7 @@ def main(): if options.move_mails: options.mail_move_rules = get_mail_move_rules() options.mail_move_age = get_mail_move_age() + options.mail_move_rename = get_mail_move_rename() with Database() as database: configured_filter_chain = get_filter_chain(database) From f0146f768a1c97bad6b461b6b827c76a1b32f95d Mon Sep 17 00:00:00 2001 From: larkery Date: Fri, 17 Oct 2014 13:43:11 +0100 Subject: [PATCH 4/5] give MailMover rename option --- afew/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/afew/main.py b/afew/main.py index 2a61ac9..6a34ee4 100644 --- a/afew/main.py +++ b/afew/main.py @@ -79,7 +79,7 @@ def main(options, database, query_string): print('%s --> %s' % (message, category)) elif options.move_mails: for maildir, rules in options.mail_move_rules.items(): - mover = MailMover(options.mail_move_age, options.dry_run) + mover = MailMover(options.mail_move_age, options.mail_move_rename, options.dry_run) mover.move(maildir, rules) mover.close() else: From 7532c8113e391bbbc0561f0d6ed9c62763f746ae Mon Sep 17 00:00:00 2001 From: larkery Date: Fri, 17 Oct 2014 13:45:38 +0100 Subject: [PATCH 5/5] Use the rename control --- afew/MailMover.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/afew/MailMover.py b/afew/MailMover.py index 8dee7f3..faa89c7 100644 --- a/afew/MailMover.py +++ b/afew/MailMover.py @@ -34,7 +34,7 @@ class MailMover(Database): ''' - def __init__(self, max_age=0, dry_run=False): + def __init__(self, max_age=0, rename = False, dry_run=False): super(MailMover, self).__init__() self.db = notmuch.Database(self.db_path) self.query = 'folder:{folder} AND {subquery}' @@ -45,7 +45,18 @@ def __init__(self, max_age=0, dry_run=False): self.query += ' AND {start}..{now}'.format(start=start.strftime('%s'), now=now.strftime('%s')) self.dry_run = dry_run - + self.rename = rename + + def get_new_name(self, fname, destination): + if self.rename: + return os.path.join( + destination, + # construct a new filename, composed of a made-up ID and the flags part + # of the original filename. + str(uuid.uuid1()) + ':' + os.path.basename(fname).split(':')[-1] + ) + else: + return destination def move(self, maildir, rules): ''' @@ -73,14 +84,7 @@ def move(self, maildir, rules): if self.dry_run: continue try: - shutil.copy2(fname, - os.path.join( - destination, - # construct a new filename, composed of a made-up ID and the flags part - # of the original filename. - str(uuid.uuid1()) + ':' + os.path.basename(fname).split(':')[-1] - ) - ) + shutil.copy2(fname, self.get_new_name(fname, destination)) to_delete_fnames.append(fname) except shutil.Error as e: # this is ugly, but shutil does not provide more