2002-10-07 22:59:02 +02:00
|
|
|
# OfflineIMAP initialization code
|
2017-04-30 13:25:40 +02:00
|
|
|
# Copyright (C) 2002-2017 John Goerzen & contributors
|
2002-10-07 22:59:02 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-10-07 22:59:02 +02:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-10-07 22:59:02 +02:00
|
|
|
|
2011-01-10 12:12:42 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import threading
|
Patch for signal handling to start a sync by Jim Pryor
Here's the way I'd like to use offlineimap on my laptop:
1. Have a regular cron job running infrequently. The cron job
checks to see
if I'm online, plugged in, and that no other copy of offlineimap is
running. If
all of these conditions are satisfied, it runs offlineimap just once:
"offlineimap -o -u Noninteractive.Quiet"
2. When I start up mutt, I do it by calling a wrapper script that
delays
until cron-started copies of offlineimap have finished, then starts
offlineimap
on its regular, stay-alive and keep checking schedule. When I quit
mutt, the
wrapper script tells offlineimap to stop.
This way I get frequent regular checks while I have mutt running, but
I don't
waste my battery/cpu checking frequently for mail when I'm not
interested in
it.
To make this work, though, it'd be nicer if it were easier to tell
offlineimap,
from the outside, things like "terminate cleanly now" and "when you've
finished
synching, then terminate instead of sleeping and synching again."
OK, to put my money where my mouth is, I attach two patches against
offlineimap
6.0.3.
The first, "cleanup.patch", cleans up a few spots that tend to throw
exceptions
for me as offlineimap is exiting from a KeyboardInterrupt.
The second adds signaling capabilities to offlineimap.
* sending a SIGTERM tells offlineimap to terminate immediately but
cleanly,
just as if "q" had been pressed in the GUI interface
* sending a SIGUSR1 tells every account to do a full sync asap: if
it's
sleeping, then wake up and do the sync now. If it's mid-sync, then
re-synch
any folders whose syncing has already been started or completed, and
continue
to synch the other, queued but not-yet-synched folders.
* sending a SIGHUP tells every account to die as soon as it can (but
not
immediately: only after finishing any synch it's now engaged in)
* sending a SIGUSR2 tells every account to do a full sync asap (as
with
SIGUSR1), then die
It's tricky to mix signals with threads, but I think I've done this
correctly.
I've been using it now for a few weeks without any obvious
problems. But I'm passing it
on so that others can review the code and test it out on their
systems. I developed the
patch when I was running Python 2.5.2, but to my knowledge I don't use
any Python 2.5-specific
code. Now I'm using the patch with Python 2.6.
Although I said "without any obvious problems," let me confess that
I'm
seeing offlineimap regularly choke when I do things like this: start
up
my offlineimap-wrapped copy of mutt, wait a while, put the machine to
sleep (not sure if offlineimap is active in the background or idling),
move to a different spot, wake the machine up again and it acquires a
new network, sometimes a wired network instead of wifi. Offlineimap
doesn't like that so much. I don't yet have any reason to think the
problems here come from my patches. But I'm just acknowledging them,
so
that if others are able to use offlineimap without any difficulty in
situations like I described, then maybe the fault is with my patches.
2008-12-01 23:13:16 +01:00
|
|
|
import signal
|
2011-01-10 12:12:42 +01:00
|
|
|
import socket
|
2010-12-15 19:06:59 +01:00
|
|
|
import logging
|
2016-06-28 02:24:34 +02:00
|
|
|
import traceback
|
|
|
|
import collections
|
2011-01-10 12:12:42 +01:00
|
|
|
from optparse import OptionParser
|
2015-01-08 17:13:33 +01:00
|
|
|
|
2011-01-10 12:12:42 +01:00
|
|
|
import offlineimap
|
2016-06-28 02:24:34 +02:00
|
|
|
import offlineimap.virtual_imaplib2 as imaplib
|
Import ui before threadutil to resolve circular dependency
The `threadutil` module depends on the `ui` module, which cycle depends
on `threadutil` through the `Curses` module.
The `ui` module had been loaded before the `threadutil` module, breaking
the above circular dependency, but this was changed in a recent code
refactoring, preventing the `Curses` module from being loaded with the
following exception:
File "./offlineimap.py", line 23, in <module>
from offlineimap import OfflineImap
File "offlineimap/offlineimap/__init__.py", line 20, in <module>
from offlineimap.init import OfflineImap
File "offlineimap/offlineimap/init.py", line 30, in <module>
from offlineimap import globals, threadutil, accounts, folder, mbnames
File "offlineimap/offlineimap/threadutil.py", line 25, in <module>
from offlineimap.ui import getglobalui
File "offlineimap/offlineimap/ui/__init__.py", line 28, in <module>
from offlineimap.ui import Curses
File "offlineimap/offlineimap/ui/Curses.py", line 27, in <module>
from offlineimap.threadutil import ExitNotifyThread
ImportError: cannot import name ExitNotifyThread
Fix the above error by ensuring that the `ui` module gets loaded before
the `threadutil` module. Please note that the above fix is temporary,
and we should refactor the modules to not have circular dependencies.
Bug-Debian: https://bugs.debian.org/839862
Reported-by: Neil McGovern <neilm@debian.org>
Signed-off-by: Ilias Tsitsimpis <i.tsitsimpis@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2016-10-06 12:30:48 +02:00
|
|
|
|
|
|
|
# Ensure that `ui` gets loaded before `threadutil` in order to
|
|
|
|
# break the circular dependency between `threadutil` and `Curses`.
|
2011-01-10 12:12:42 +01:00
|
|
|
from offlineimap.ui import UI_LIST, setglobalui, getglobalui
|
2016-11-08 07:07:55 +01:00
|
|
|
from offlineimap import threadutil, accounts, folder, mbnames
|
|
|
|
from offlineimap import globals as glob
|
2011-01-10 12:12:42 +01:00
|
|
|
from offlineimap.CustomConfig import CustomConfigParser
|
2013-01-28 19:49:29 +01:00
|
|
|
from offlineimap.utils import stacktrace
|
2016-03-05 18:07:07 +01:00
|
|
|
from offlineimap.repository import Repository
|
2016-05-18 02:42:09 +02:00
|
|
|
from offlineimap.folder.IMAP import MSGCOPY_NAMESPACE
|
2011-01-10 12:12:42 +01:00
|
|
|
|
2016-05-17 03:53:09 +02:00
|
|
|
ACCOUNT_LIMITED_THREAD_NAME = 'MAX_ACCOUNTS'
|
2016-08-15 04:53:23 +02:00
|
|
|
PYTHON_VERSION = sys.version.split(' ')[0]
|
|
|
|
|
2002-10-07 22:59:02 +02:00
|
|
|
|
2016-05-17 02:38:35 +02:00
|
|
|
def syncitall(list_accounts, config):
|
2016-05-17 02:25:52 +02:00
|
|
|
"""The target when in multithreading mode for running accounts threads."""
|
|
|
|
|
2020-08-29 20:21:07 +02:00
|
|
|
threads = threadutil.accountThreads() # The collection of accounts threads.
|
2016-05-17 02:38:35 +02:00
|
|
|
for accountname in list_accounts:
|
2016-05-17 02:25:52 +02:00
|
|
|
# Start a new thread per account and store it in the collection.
|
2016-05-17 02:38:35 +02:00
|
|
|
account = accounts.SyncableAccount(config, accountname)
|
|
|
|
thread = threadutil.InstanceLimitedThread(
|
2016-05-18 02:42:09 +02:00
|
|
|
ACCOUNT_LIMITED_THREAD_NAME,
|
2020-08-29 20:21:07 +02:00
|
|
|
target=account.syncrunner,
|
|
|
|
name="Account sync %s" % accountname
|
|
|
|
)
|
2016-05-17 02:38:35 +02:00
|
|
|
thread.setDaemon(True)
|
|
|
|
# The add() method expects a started thread.
|
|
|
|
thread.start()
|
|
|
|
threads.add(thread)
|
2016-05-17 02:25:52 +02:00
|
|
|
# Wait for the threads to finish.
|
2020-08-29 20:21:07 +02:00
|
|
|
threads.wait() # Blocks until all accounts are processed.
|
2016-05-17 02:25:52 +02:00
|
|
|
|
|
|
|
|
2020-08-30 18:46:08 +02:00
|
|
|
class OfflineImap:
|
2010-12-15 19:06:59 +01:00
|
|
|
"""The main class that encapsulates the high level use of OfflineImap.
|
2010-12-06 13:36:54 +01:00
|
|
|
|
2011-05-02 17:11:40 +02:00
|
|
|
To invoke OfflineImap you would call it with::
|
|
|
|
|
|
|
|
oi = OfflineImap()
|
|
|
|
oi.run()
|
2010-12-15 19:06:59 +01:00
|
|
|
"""
|
2015-01-13 13:59:52 +01:00
|
|
|
|
2017-04-30 13:25:40 +02:00
|
|
|
def get_env_info(self):
|
2020-08-29 20:21:07 +02:00
|
|
|
info = "imaplib2 v%s (%s), Python v%s" % (imaplib.__version__, imaplib.DESC, PYTHON_VERSION)
|
2017-05-02 17:09:03 +02:00
|
|
|
try:
|
|
|
|
import ssl
|
2020-08-29 20:21:07 +02:00
|
|
|
info = "%s, %s" % (info, ssl.OPENSSL_VERSION)
|
2017-05-02 17:09:03 +02:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return info
|
2017-04-30 13:25:40 +02:00
|
|
|
|
2010-12-15 19:06:59 +01:00
|
|
|
def run(self):
|
|
|
|
"""Parse the commandline and invoke everything"""
|
2011-06-30 15:18:03 +02:00
|
|
|
# next line also sets self.config and self.ui
|
2014-03-16 13:27:35 +01:00
|
|
|
options, args = self.__parse_cmd_options()
|
2011-06-30 15:18:03 +02:00
|
|
|
if options.diagnostics:
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__serverdiagnostics(options)
|
2016-03-05 18:07:07 +01:00
|
|
|
elif options.migrate_fmd5:
|
|
|
|
self.__migratefmd5(options)
|
2016-07-08 16:30:50 +02:00
|
|
|
elif options.mbnames_prune:
|
|
|
|
mbnames.init(self.config, self.ui, options.dryrun)
|
|
|
|
mbnames.prune(self.config.get("general", "accounts"))
|
|
|
|
mbnames.write()
|
2016-07-08 21:23:27 +02:00
|
|
|
elif options.deletefolder:
|
|
|
|
return self.__deletefolder(options)
|
2011-06-30 15:18:03 +02:00
|
|
|
else:
|
2015-09-27 07:28:06 +02:00
|
|
|
return self.__sync(options)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __parse_cmd_options(self):
|
2016-06-07 00:36:57 +02:00
|
|
|
parser = OptionParser(
|
|
|
|
version=offlineimap.__version__,
|
2020-08-29 20:21:07 +02:00
|
|
|
description="%s.\n\n%s" % (offlineimap.__copyright__,
|
|
|
|
offlineimap.__license__)
|
|
|
|
)
|
2016-07-09 01:28:51 +02:00
|
|
|
|
|
|
|
parser.add_option("-V",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="version",
|
|
|
|
default=False,
|
|
|
|
help="show full version infos")
|
2016-07-09 01:28:51 +02:00
|
|
|
|
2011-09-15 13:24:48 +02:00
|
|
|
parser.add_option("--dry-run",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="dryrun",
|
|
|
|
default=False,
|
|
|
|
help="dry run mode")
|
2012-02-17 11:59:37 +01:00
|
|
|
|
|
|
|
parser.add_option("--info",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="diagnostics",
|
|
|
|
default=False,
|
|
|
|
help="output information on the configured email repositories")
|
2011-09-15 13:24:48 +02:00
|
|
|
|
2010-12-15 19:06:59 +01:00
|
|
|
parser.add_option("-1",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="singlethreading",
|
|
|
|
default=False,
|
|
|
|
help="(the number one) disable all multithreading operations")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-P", dest="profiledir", metavar="DIR",
|
2020-08-29 20:21:07 +02:00
|
|
|
help="sets OfflineIMAP into profile mode.")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2015-03-17 12:27:34 +01:00
|
|
|
parser.add_option("-a", dest="accounts",
|
2020-08-29 20:21:07 +02:00
|
|
|
metavar="account1[,account2[,...]]",
|
|
|
|
help="list of accounts to sync")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-c", dest="configfile", metavar="FILE",
|
2020-08-29 20:21:07 +02:00
|
|
|
default=None,
|
|
|
|
help="specifies a configuration file to use")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2015-03-17 12:27:34 +01:00
|
|
|
parser.add_option("-d", dest="debugtype",
|
2020-08-29 20:21:07 +02:00
|
|
|
metavar="type1[,type2[,...]]",
|
|
|
|
help="enables debugging for OfflineIMAP "
|
|
|
|
" (types: imap, maildir, thread)")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-l", dest="logfile", metavar="FILE",
|
2020-08-29 20:21:07 +02:00
|
|
|
help="log to FILE")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2015-09-27 02:43:09 +02:00
|
|
|
parser.add_option("-s",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="syslog",
|
|
|
|
default=False,
|
|
|
|
help="log to syslog")
|
2015-09-27 02:43:09 +02:00
|
|
|
|
2015-03-17 12:27:34 +01:00
|
|
|
parser.add_option("-f", dest="folders",
|
2020-08-29 20:21:07 +02:00
|
|
|
metavar="folder1[,folder2[,...]]",
|
|
|
|
help="only sync the specified folders")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-k", dest="configoverride",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="append",
|
|
|
|
metavar="[section:]option=value",
|
|
|
|
help="override configuration file option")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-o",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="runonce",
|
|
|
|
default=False,
|
|
|
|
help="run only once (ignore autorefresh)")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-q",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="quick",
|
|
|
|
default=False,
|
|
|
|
help="run only quick synchronizations (don't update flags)")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-u", dest="interface",
|
2020-08-29 20:21:07 +02:00
|
|
|
help="specifies an alternative user interface"
|
|
|
|
" (quiet, basic, syslog, ttyui, blinkenlights, machineui)")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2016-07-08 21:23:27 +02:00
|
|
|
parser.add_option("--delete-folder", dest="deletefolder",
|
2020-08-29 20:21:07 +02:00
|
|
|
default=None,
|
|
|
|
metavar="FOLDERNAME",
|
|
|
|
help="Delete a folder (on the remote repository)")
|
2016-07-08 21:23:27 +02:00
|
|
|
|
2016-03-05 18:07:07 +01:00
|
|
|
parser.add_option("--migrate-fmd5-using-nametrans",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="migrate_fmd5", default=False,
|
|
|
|
help="migrate FMD5 hashes from versions prior to 6.3.5")
|
2016-03-05 18:07:07 +01:00
|
|
|
|
2016-06-26 18:12:35 +02:00
|
|
|
parser.add_option("--mbnames-prune",
|
2020-08-29 20:21:07 +02:00
|
|
|
action="store_true", dest="mbnames_prune", default=False,
|
|
|
|
help="remove mbnames entries for accounts not in accounts")
|
2016-06-26 18:12:35 +02:00
|
|
|
|
2010-12-15 19:06:59 +01:00
|
|
|
(options, args) = parser.parse_args()
|
2016-11-08 07:07:55 +01:00
|
|
|
glob.set_options(options)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2016-06-07 00:36:57 +02:00
|
|
|
if options.version:
|
2020-08-29 20:21:07 +02:00
|
|
|
print(("offlineimap v%s, %s" % (
|
2017-04-30 13:25:40 +02:00
|
|
|
offlineimap.__version__, self.get_env_info())
|
2020-08-29 20:21:07 +02:00
|
|
|
))
|
2016-06-07 00:36:57 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Read in configuration file.
|
2014-05-06 22:40:59 +02:00
|
|
|
if not options.configfile:
|
|
|
|
# Try XDG location, then fall back to ~/.offlineimaprc
|
|
|
|
xdg_var = 'XDG_CONFIG_HOME'
|
2020-08-30 18:47:24 +02:00
|
|
|
if xdg_var not in os.environ or not os.environ[xdg_var]:
|
2014-05-06 22:40:59 +02:00
|
|
|
xdg_home = os.path.expanduser('~/.config')
|
|
|
|
else:
|
|
|
|
xdg_home = os.environ[xdg_var]
|
|
|
|
options.configfile = os.path.join(xdg_home, "offlineimap", "config")
|
|
|
|
if not os.path.exists(options.configfile):
|
|
|
|
options.configfile = os.path.expanduser('~/.offlineimaprc')
|
|
|
|
configfilename = options.configfile
|
|
|
|
else:
|
|
|
|
configfilename = os.path.expanduser(options.configfile)
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2010-12-06 13:36:54 +01:00
|
|
|
config = CustomConfigParser()
|
|
|
|
if not os.path.exists(configfilename):
|
2011-10-26 16:47:21 +02:00
|
|
|
# TODO, initialize and make use of chosen ui for logging
|
2020-08-29 20:21:07 +02:00
|
|
|
logging.error(" *** Config file '%s' does not exist; aborting!" %
|
2010-12-15 19:06:59 +01:00
|
|
|
configfilename)
|
2010-12-06 13:36:54 +01:00
|
|
|
sys.exit(1)
|
|
|
|
config.read(configfilename)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Profile mode chosen?
|
2010-12-15 19:06:59 +01:00
|
|
|
if options.profiledir:
|
|
|
|
if not options.singlethreading:
|
2011-10-26 16:47:21 +02:00
|
|
|
# TODO, make use of chosen ui for logging
|
2010-12-15 19:06:59 +01:00
|
|
|
logging.warn("Profile mode: Forcing to singlethreaded.")
|
2011-03-03 13:44:39 +01:00
|
|
|
options.singlethreading = True
|
2011-09-29 16:02:51 +02:00
|
|
|
if os.path.exists(options.profiledir):
|
2011-10-26 16:47:21 +02:00
|
|
|
# TODO, make use of chosen ui for logging
|
2020-08-29 20:21:07 +02:00
|
|
|
logging.warn("Profile mode: Directory '%s' already exists!" %
|
2011-09-29 16:02:51 +02:00
|
|
|
options.profiledir)
|
|
|
|
else:
|
|
|
|
os.mkdir(options.profiledir)
|
2011-10-26 16:47:21 +02:00
|
|
|
# TODO, make use of chosen ui for logging
|
2010-12-15 19:06:59 +01:00
|
|
|
logging.warn("Profile mode: Potentially large data will be "
|
2020-08-29 20:21:07 +02:00
|
|
|
"created in '%s'" % options.profiledir)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Override a config value.
|
2010-12-15 19:06:59 +01:00
|
|
|
if options.configoverride:
|
|
|
|
for option in options.configoverride:
|
|
|
|
(key, value) = option.split('=', 1)
|
|
|
|
if ':' in key:
|
|
|
|
(secname, key) = key.split(':', 1)
|
|
|
|
section = secname.replace("_", " ")
|
|
|
|
else:
|
|
|
|
section = "general"
|
|
|
|
config.set(section, key, value)
|
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Which ui to use? CLI option overrides config file.
|
2015-01-01 21:41:11 +01:00
|
|
|
ui_type = config.getdefault('general', 'ui', 'ttyui')
|
2020-08-30 11:27:03 +02:00
|
|
|
if options.interface is not None:
|
2011-01-05 17:00:54 +01:00
|
|
|
ui_type = options.interface
|
2011-03-06 11:04:46 +01:00
|
|
|
if '.' in ui_type:
|
2016-06-04 15:22:55 +02:00
|
|
|
# Transform Curses.Blinkenlights -> Blinkenlights.
|
2011-03-06 11:04:46 +01:00
|
|
|
ui_type = ui_type.split('.')[-1]
|
2011-10-26 16:47:21 +02:00
|
|
|
# TODO, make use of chosen ui for logging
|
2011-03-06 11:04:46 +01:00
|
|
|
logging.warning('Using old interface name, consider using one '
|
2020-08-29 20:21:07 +02:00
|
|
|
'of %s' % ', '.join(list(UI_LIST.keys())))
|
2016-07-22 01:40:37 +02:00
|
|
|
if options.diagnostics:
|
2020-08-29 20:21:07 +02:00
|
|
|
ui_type = 'ttyui' # Enforce this UI for --info.
|
2011-09-15 13:24:48 +02:00
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# dry-run? Set [general]dry-run=True.
|
2011-09-15 13:24:48 +02:00
|
|
|
if options.dryrun:
|
2015-01-01 21:41:11 +01:00
|
|
|
dryrun = config.set('general', 'dry-run', 'True')
|
|
|
|
config.set_if_not_exists('general', 'dry-run', 'False')
|
2011-09-15 13:24:48 +02:00
|
|
|
|
2011-01-05 17:00:54 +01:00
|
|
|
try:
|
2016-06-04 15:22:55 +02:00
|
|
|
# Create the ui class.
|
2011-06-30 14:04:18 +02:00
|
|
|
self.ui = UI_LIST[ui_type.lower()](config)
|
2011-01-05 17:00:54 +01:00
|
|
|
except KeyError:
|
2020-08-29 20:21:07 +02:00
|
|
|
logging.error("UI '%s' does not exist, choose one of: %s" %
|
|
|
|
(ui_type, ', '.join(list(UI_LIST.keys()))))
|
2011-01-05 17:00:54 +01:00
|
|
|
sys.exit(1)
|
2011-06-30 14:04:18 +02:00
|
|
|
setglobalui(self.ui)
|
2011-01-05 17:00:54 +01:00
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Set up additional log files.
|
2010-12-15 19:06:59 +01:00
|
|
|
if options.logfile:
|
2011-10-26 16:47:21 +02:00
|
|
|
self.ui.setlogfile(options.logfile)
|
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Set up syslog.
|
2015-09-27 02:43:09 +02:00
|
|
|
if options.syslog:
|
|
|
|
self.ui.setup_sysloghandler()
|
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Welcome blurb.
|
2011-06-30 14:04:18 +02:00
|
|
|
self.ui.init_banner()
|
2017-04-30 13:25:40 +02:00
|
|
|
self.ui.info(self.get_env_info())
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
if options.debugtype:
|
2011-10-26 16:47:21 +02:00
|
|
|
self.ui.logger.setLevel(logging.DEBUG)
|
2010-12-15 19:06:59 +01:00
|
|
|
if options.debugtype.lower() == 'all':
|
|
|
|
options.debugtype = 'imap,maildir,thread'
|
2016-06-04 15:22:55 +02:00
|
|
|
# Force single threading?
|
2020-08-30 18:46:08 +02:00
|
|
|
if not ('thread' in options.debugtype.split(',')
|
2011-05-12 20:59:26 +02:00
|
|
|
and not options.singlethreading):
|
2011-06-30 14:04:18 +02:00
|
|
|
self.ui._msg("Debug mode: Forcing to singlethreaded.")
|
2011-05-05 11:15:51 +02:00
|
|
|
options.singlethreading = True
|
2011-01-12 11:15:13 +01:00
|
|
|
|
2011-05-01 20:18:28 +02:00
|
|
|
debugtypes = options.debugtype.split(',') + ['']
|
2015-01-08 22:14:43 +01:00
|
|
|
for dtype in debugtypes:
|
|
|
|
dtype = dtype.strip()
|
|
|
|
self.ui.add_debug(dtype)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
if options.runonce:
|
2016-06-26 18:12:35 +02:00
|
|
|
# Must kill the possible default option.
|
2015-09-04 12:12:28 +02:00
|
|
|
if config.has_option('DEFAULT', 'autorefresh'):
|
|
|
|
config.remove_option('DEFAULT', 'autorefresh')
|
2015-01-01 21:41:11 +01:00
|
|
|
# FIXME: spaghetti code alert!
|
2010-12-06 13:36:54 +01:00
|
|
|
for section in accounts.getaccountlist(config):
|
|
|
|
config.remove_option('Account ' + section, "autorefresh")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
if options.quick:
|
2010-12-06 13:36:54 +01:00
|
|
|
for section in accounts.getaccountlist(config):
|
|
|
|
config.set('Account ' + section, "quick", '-1')
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Custom folder list specified?
|
2010-12-15 19:06:59 +01:00
|
|
|
if options.folders:
|
2011-04-28 15:26:07 +02:00
|
|
|
foldernames = options.folders.split(",")
|
2020-08-29 20:21:07 +02:00
|
|
|
folderfilter = "lambda f: f in %s" % foldernames
|
2010-12-06 13:36:54 +01:00
|
|
|
folderincludes = "[]"
|
|
|
|
for accountname in accounts.getaccountlist(config):
|
|
|
|
account_section = 'Account ' + accountname
|
|
|
|
remote_repo_section = 'Repository ' + \
|
2020-08-29 20:21:07 +02:00
|
|
|
config.get(account_section, 'remoterepository')
|
2012-02-24 14:52:30 +01:00
|
|
|
config.set(remote_repo_section, "folderfilter", folderfilter)
|
|
|
|
config.set(remote_repo_section, "folderincludes",
|
|
|
|
folderincludes)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2011-06-30 14:04:18 +02:00
|
|
|
if options.logfile:
|
|
|
|
sys.stderr = self.ui.logfile
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2011-06-30 14:04:18 +02:00
|
|
|
socktimeout = config.getdefaultint("general", "socktimeout", 0)
|
|
|
|
if socktimeout > 0:
|
|
|
|
socket.setdefaulttimeout(socktimeout)
|
|
|
|
|
2016-05-17 03:53:09 +02:00
|
|
|
threadutil.initInstanceLimit(
|
|
|
|
ACCOUNT_LIMITED_THREAD_NAME,
|
|
|
|
config.getdefaultint('general', 'maxsyncaccounts', 1)
|
2020-08-29 20:21:07 +02:00
|
|
|
)
|
2011-06-30 14:04:18 +02:00
|
|
|
|
|
|
|
for reposname in config.getsectionlist('Repository'):
|
2016-07-08 16:30:50 +02:00
|
|
|
# Limit the number of threads. Limitation on usage is handled at the
|
|
|
|
# imapserver level.
|
2016-05-19 08:35:45 +02:00
|
|
|
for namespace in [accounts.FOLDER_NAMESPACE + reposname,
|
2020-08-29 20:21:07 +02:00
|
|
|
MSGCOPY_NAMESPACE + reposname]:
|
2011-06-30 14:04:18 +02:00
|
|
|
if options.singlethreading:
|
2016-05-19 08:35:45 +02:00
|
|
|
threadutil.initInstanceLimit(namespace, 1)
|
2011-06-30 14:04:18 +02:00
|
|
|
else:
|
2016-05-17 03:53:09 +02:00
|
|
|
threadutil.initInstanceLimit(
|
2016-05-19 08:35:45 +02:00
|
|
|
namespace,
|
2016-05-17 03:53:09 +02:00
|
|
|
config.getdefaultint(
|
|
|
|
'Repository ' + reposname,
|
|
|
|
'maxconnections', 2)
|
2020-08-29 20:21:07 +02:00
|
|
|
)
|
2011-06-30 14:04:18 +02:00
|
|
|
self.config = config
|
2020-08-30 18:46:08 +02:00
|
|
|
return options, args
|
2011-06-30 14:04:18 +02:00
|
|
|
|
2015-11-09 03:35:03 +01:00
|
|
|
def __dumpstacks(self, context=1, sighandler_deep=2):
|
|
|
|
""" Signal handler: dump a stack trace for each existing thread."""
|
|
|
|
|
|
|
|
currentThreadId = threading.currentThread().ident
|
|
|
|
|
|
|
|
def unique_count(l):
|
|
|
|
d = collections.defaultdict(lambda: 0)
|
|
|
|
for v in l:
|
|
|
|
d[tuple(v)] += 1
|
2020-08-28 03:32:43 +02:00
|
|
|
return list((k, v) for k, v in list(d.items()))
|
2015-11-09 03:35:03 +01:00
|
|
|
|
|
|
|
stack_displays = []
|
2020-08-28 03:32:43 +02:00
|
|
|
for threadId, stack in list(sys._current_frames().items()):
|
2015-11-09 03:35:03 +01:00
|
|
|
stack_display = []
|
|
|
|
for filename, lineno, name, line in traceback.extract_stack(stack):
|
|
|
|
stack_display.append(' File: "%s", line %d, in %s'
|
|
|
|
% (filename, lineno, name))
|
|
|
|
if line:
|
|
|
|
stack_display.append(" %s" % (line.strip()))
|
|
|
|
if currentThreadId == threadId:
|
|
|
|
stack_display = stack_display[:- (sighandler_deep * 2)]
|
|
|
|
stack_display.append(' => Stopped to handle current signal. ')
|
|
|
|
stack_displays.append(stack_display)
|
|
|
|
stacks = unique_count(stack_displays)
|
2015-11-09 07:31:06 +01:00
|
|
|
self.ui.debug('thread', "** Thread List:\n")
|
2015-11-09 03:35:03 +01:00
|
|
|
for stack, times in stacks:
|
|
|
|
if times == 1:
|
|
|
|
msg = "%s Thread is at:\n%s\n"
|
|
|
|
else:
|
|
|
|
msg = "%s Threads are at:\n%s\n"
|
|
|
|
self.ui.debug('thread', msg % (times, '\n'.join(stack[- (context * 2):])))
|
|
|
|
|
|
|
|
self.ui.debug('thread', "Dumped a total of %d Threads." %
|
2020-08-28 03:32:43 +02:00
|
|
|
len(list(sys._current_frames().keys())))
|
2015-11-09 03:35:03 +01:00
|
|
|
|
2016-07-08 16:47:42 +02:00
|
|
|
def _get_activeaccounts(self, options):
|
|
|
|
activeaccounts = []
|
|
|
|
errormsg = None
|
|
|
|
|
|
|
|
activeaccountnames = self.config.get("general", "accounts")
|
|
|
|
if options.accounts:
|
|
|
|
activeaccountnames = options.accounts
|
2016-07-28 04:06:52 +02:00
|
|
|
activeaccountnames = [x.lstrip() for x in activeaccountnames.split(",")]
|
2016-07-08 16:47:42 +02:00
|
|
|
|
|
|
|
allaccounts = accounts.getaccountlist(self.config)
|
|
|
|
for accountname in activeaccountnames:
|
|
|
|
if accountname in allaccounts:
|
|
|
|
activeaccounts.append(accountname)
|
|
|
|
else:
|
2020-08-29 20:21:07 +02:00
|
|
|
errormsg = "Valid accounts are: %s" % (
|
2016-07-08 16:47:42 +02:00
|
|
|
", ".join(allaccounts))
|
2020-08-29 20:21:07 +02:00
|
|
|
self.ui.error("The account '%s' does not exist" % accountname)
|
2016-07-08 16:47:42 +02:00
|
|
|
|
|
|
|
if len(activeaccounts) < 1:
|
|
|
|
errormsg = "No accounts are defined!"
|
|
|
|
|
|
|
|
if errormsg is not None:
|
|
|
|
self.ui.terminate(1, errormsg=errormsg)
|
|
|
|
|
|
|
|
return activeaccounts
|
2015-11-09 03:35:03 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __sync(self, options):
|
2011-06-30 14:04:18 +02:00
|
|
|
"""Invoke the correct single/multithread syncing
|
|
|
|
|
|
|
|
self.config is supposed to have been correctly initialized
|
|
|
|
already."""
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2016-07-08 16:47:42 +02:00
|
|
|
def sig_handler(sig, frame):
|
|
|
|
if sig == signal.SIGUSR1:
|
|
|
|
# tell each account to stop sleeping
|
|
|
|
accounts.Account.set_abort_event(self.config, 1)
|
2017-01-21 11:36:59 +01:00
|
|
|
elif sig in (signal.SIGUSR2, signal.SIGABRT):
|
2016-07-08 16:47:42 +02:00
|
|
|
# tell each account to stop looping
|
|
|
|
getglobalui().warn("Terminating after this sync...")
|
|
|
|
accounts.Account.set_abort_event(self.config, 2)
|
|
|
|
elif sig in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP):
|
|
|
|
# tell each account to ABORT ASAP (ctrl-c)
|
2020-08-30 18:46:08 +02:00
|
|
|
getglobalui().warn("Preparing to shutdown after sync (this may "
|
|
|
|
"take some time), press CTRL-C three "
|
2018-05-01 15:55:26 +02:00
|
|
|
"times to shutdown immediately")
|
2016-07-08 16:47:42 +02:00
|
|
|
accounts.Account.set_abort_event(self.config, 3)
|
|
|
|
if 'thread' in self.ui.debuglist:
|
|
|
|
self.__dumpstacks(5)
|
|
|
|
|
|
|
|
# Abort after three Ctrl-C keystrokes
|
|
|
|
self.num_sigterm += 1
|
|
|
|
if self.num_sigterm >= 3:
|
|
|
|
getglobalui().warn("Signaled thrice. Aborting!")
|
|
|
|
sys.exit(1)
|
|
|
|
elif sig == signal.SIGQUIT:
|
|
|
|
stacktrace.dump(sys.stderr)
|
|
|
|
os.abort()
|
|
|
|
|
|
|
|
try:
|
2016-02-04 11:31:29 +01:00
|
|
|
self.num_sigterm = 0
|
2015-01-01 21:41:11 +01:00
|
|
|
signal.signal(signal.SIGHUP, sig_handler)
|
|
|
|
signal.signal(signal.SIGUSR1, sig_handler)
|
|
|
|
signal.signal(signal.SIGUSR2, sig_handler)
|
2017-01-21 11:36:59 +01:00
|
|
|
signal.signal(signal.SIGABRT, sig_handler)
|
2012-01-04 19:24:19 +01:00
|
|
|
signal.signal(signal.SIGTERM, sig_handler)
|
|
|
|
signal.signal(signal.SIGINT, sig_handler)
|
2013-01-28 19:49:29 +01:00
|
|
|
signal.signal(signal.SIGQUIT, sig_handler)
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2016-06-04 15:22:55 +02:00
|
|
|
# Various initializations that need to be performed:
|
2016-07-08 16:47:42 +02:00
|
|
|
activeaccounts = self._get_activeaccounts(options)
|
2016-06-23 00:20:57 +02:00
|
|
|
mbnames.init(self.config, self.ui, options.dryrun)
|
2011-01-12 11:15:12 +01:00
|
|
|
|
|
|
|
if options.singlethreading:
|
2016-06-04 15:22:55 +02:00
|
|
|
# Singlethreaded.
|
2016-11-08 07:07:55 +01:00
|
|
|
self.__sync_singlethreaded(activeaccounts, options.profiledir)
|
2011-01-12 11:15:12 +01:00
|
|
|
else:
|
2016-06-04 15:22:55 +02:00
|
|
|
# Multithreaded.
|
2016-05-17 02:38:35 +02:00
|
|
|
t = threadutil.ExitNotifyThread(
|
|
|
|
target=syncitall,
|
2016-05-12 05:29:49 +02:00
|
|
|
name='Sync Runner',
|
2016-07-08 16:47:42 +02:00
|
|
|
args=(activeaccounts, self.config,)
|
2020-08-29 20:21:07 +02:00
|
|
|
)
|
2016-05-17 02:25:52 +02:00
|
|
|
# Special exit message for the monitor to stop looping.
|
|
|
|
t.exit_message = threadutil.STOP_MONITOR
|
2011-01-12 11:15:12 +01:00
|
|
|
t.start()
|
2016-05-17 02:04:48 +02:00
|
|
|
threadutil.monitor()
|
2015-03-22 19:34:24 +01:00
|
|
|
|
2016-06-23 00:20:57 +02:00
|
|
|
# All sync are done.
|
|
|
|
mbnames.write()
|
2011-06-30 14:04:18 +02:00
|
|
|
self.ui.terminate()
|
2015-09-27 07:28:06 +02:00
|
|
|
return 0
|
2020-08-30 18:46:08 +02:00
|
|
|
except SystemExit:
|
2010-12-06 13:36:54 +01:00
|
|
|
raise
|
2012-02-05 10:14:23 +01:00
|
|
|
except Exception as e:
|
2011-10-26 16:47:21 +02:00
|
|
|
self.ui.error(e)
|
|
|
|
self.ui.terminate()
|
2015-09-27 07:28:06 +02:00
|
|
|
return 1
|
2002-10-07 22:59:02 +02:00
|
|
|
|
2016-11-08 07:07:55 +01:00
|
|
|
def __sync_singlethreaded(self, list_accounts, profiledir):
|
2016-05-17 02:25:52 +02:00
|
|
|
"""Executed in singlethreaded mode only.
|
2011-01-12 11:15:12 +01:00
|
|
|
|
|
|
|
:param accs: A list of accounts that should be synced
|
|
|
|
"""
|
2016-07-08 16:47:42 +02:00
|
|
|
for accountname in list_accounts:
|
2016-05-17 02:25:52 +02:00
|
|
|
account = accounts.SyncableAccount(self.config, accountname)
|
2016-11-08 07:07:55 +01:00
|
|
|
threading.currentThread().name = \
|
2020-08-29 20:21:07 +02:00
|
|
|
"Account sync %s" % account.getname()
|
2016-11-08 07:07:55 +01:00
|
|
|
if not profiledir:
|
|
|
|
account.syncrunner()
|
|
|
|
# Profile mode.
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
import cProfile as profile
|
|
|
|
except ImportError:
|
|
|
|
import profile
|
|
|
|
prof = profile.Profile()
|
|
|
|
try:
|
|
|
|
prof = prof.runctx("account.syncrunner()", globals(), locals())
|
|
|
|
except SystemExit:
|
|
|
|
pass
|
|
|
|
from datetime import datetime
|
|
|
|
dt = datetime.now().strftime('%Y%m%d%H%M%S')
|
|
|
|
prof.dump_stats(os.path.join(
|
2020-08-29 20:21:07 +02:00
|
|
|
profiledir, "%s_%s.prof" % (dt, account.getname())))
|
2011-06-30 15:18:03 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __serverdiagnostics(self, options):
|
2020-08-29 20:21:07 +02:00
|
|
|
self.ui.info(" imaplib2: %s (%s)" % (imaplib.__version__, imaplib.DESC))
|
2016-07-08 16:47:42 +02:00
|
|
|
for accountname in self._get_activeaccounts(options):
|
|
|
|
account = accounts.Account(self.config, accountname)
|
2011-06-30 15:18:03 +02:00
|
|
|
account.serverdiagnostics()
|
2016-03-05 18:07:07 +01:00
|
|
|
|
2016-07-08 21:23:27 +02:00
|
|
|
def __deletefolder(self, options):
|
|
|
|
list_accounts = self._get_activeaccounts(options)
|
|
|
|
if len(list_accounts) != 1:
|
|
|
|
self.ui.error("you must supply only one account with '-a'")
|
|
|
|
return 1
|
|
|
|
account = accounts.Account(self.config, list_accounts.pop())
|
|
|
|
return account.deletefolder(options.deletefolder)
|
|
|
|
|
2016-03-05 18:07:07 +01:00
|
|
|
def __migratefmd5(self, options):
|
2016-07-08 16:47:42 +02:00
|
|
|
for accountname in self._get_activeaccounts(options):
|
|
|
|
account = accounts.Account(self.config, accountname)
|
2016-03-05 18:07:07 +01:00
|
|
|
localrepo = Repository(account, 'local')
|
|
|
|
if localrepo.getfoldertype() != folder.Maildir.MaildirFolder:
|
|
|
|
continue
|
|
|
|
folders = localrepo.getfolders()
|
|
|
|
for f in folders:
|
|
|
|
f.migratefmd5(options.dryrun)
|