2002-10-07 22:59:02 +02:00
|
|
|
# OfflineIMAP initialization code
|
2015-01-01 21:41:11 +01:00
|
|
|
# Copyright (C) 2002-2015 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
|
2011-03-08 16:05:16 +01:00
|
|
|
import offlineimap.imaplib2 as imaplib
|
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
|
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
|
|
|
|
from offlineimap import accounts, threadutil, syncmaster
|
2013-02-05 04:49:56 +01:00
|
|
|
from offlineimap import globals
|
2011-01-10 12:12:42 +01:00
|
|
|
from offlineimap.ui import UI_LIST, setglobalui, getglobalui
|
|
|
|
from offlineimap.CustomConfig import CustomConfigParser
|
2013-01-28 19:49:29 +01:00
|
|
|
from offlineimap.utils import stacktrace
|
2011-01-10 12:12:42 +01:00
|
|
|
|
2002-10-07 22:59:02 +02:00
|
|
|
|
2010-12-06 13:36:54 +01: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
|
|
|
"""
|
|
|
|
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)
|
2011-06-30 15:18:03 +02:00
|
|
|
else:
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__sync(options)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __parse_cmd_options(self):
|
2014-06-01 20:32:57 +02:00
|
|
|
parser = OptionParser(version=offlineimap.__bigversion__,
|
2011-06-30 14:04:18 +02:00
|
|
|
description="%s.\n\n%s" %
|
2010-12-22 12:35:41 +01:00
|
|
|
(offlineimap.__copyright__,
|
|
|
|
offlineimap.__license__))
|
2011-09-15 13:24:48 +02:00
|
|
|
parser.add_option("--dry-run",
|
|
|
|
action="store_true", dest="dryrun",
|
|
|
|
default=False,
|
|
|
|
help="Do not actually modify any store but check and print "
|
|
|
|
"what synchronization actions would be taken if a sync would be"
|
2012-02-17 11:59:37 +01:00
|
|
|
" performed. It will not precisely give the exact information w"
|
|
|
|
"hat will happen. If e.g. we need to create a folder, it merely"
|
|
|
|
" outputs 'Would create folder X', but not how many and which m"
|
|
|
|
"ails it would transfer.")
|
|
|
|
|
|
|
|
parser.add_option("--info",
|
|
|
|
action="store_true", dest="diagnostics",
|
|
|
|
default=False,
|
|
|
|
help="Output information on the configured email repositories"
|
|
|
|
". Useful for debugging and bug reporting. Use in conjunction wit"
|
|
|
|
"h the -a option to limit the output to a single account. This mo"
|
|
|
|
"de will prevent any actual sync to occur and exits after it outp"
|
|
|
|
"ut the debug information.")
|
2011-09-15 13:24:48 +02:00
|
|
|
|
2010-12-15 19:06:59 +01:00
|
|
|
parser.add_option("-1",
|
|
|
|
action="store_true", dest="singlethreading",
|
|
|
|
default=False,
|
|
|
|
help="Disable all multithreading operations and use "
|
|
|
|
"solely a single-thread sync. This effectively sets the "
|
|
|
|
"maxsyncaccounts and all maxconnections configuration file "
|
|
|
|
"variables to 1.")
|
|
|
|
|
|
|
|
parser.add_option("-P", dest="profiledir", metavar="DIR",
|
|
|
|
help="Sets OfflineIMAP into profile mode. The program "
|
|
|
|
"will create DIR (it must not already exist). "
|
|
|
|
"As it runs, Python profiling information about each "
|
|
|
|
"thread is logged into profiledir. Please note: "
|
|
|
|
"This option is present for debugging and optimization "
|
|
|
|
"only, and should NOT be used unless you have a "
|
|
|
|
"specific reason to do so. It will significantly "
|
|
|
|
"decrease program performance, may reduce reliability, "
|
|
|
|
"and can generate huge amounts of data. This option "
|
|
|
|
"implies the -1 option.")
|
|
|
|
|
|
|
|
parser.add_option("-a", dest="accounts", metavar="ACCOUNTS",
|
2012-02-17 11:59:37 +01:00
|
|
|
help="Overrides the accounts section in the config file. "
|
|
|
|
"Lets you specify a particular account or set of "
|
|
|
|
"accounts to sync without having to edit the config "
|
|
|
|
"file. You might use this to exclude certain accounts, "
|
|
|
|
"or to sync some accounts that you normally prefer not to.")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-c", dest="configfile", metavar="FILE",
|
2014-05-06 22:40:59 +02:00
|
|
|
default=None,
|
|
|
|
help="Specifies a configuration file to use")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-d", dest="debugtype", metavar="type1,[type2...]",
|
2012-02-17 11:59:37 +01:00
|
|
|
help="Enables debugging for OfflineIMAP. This is useful "
|
|
|
|
"if you are to track down a malfunction or figure out what is "
|
|
|
|
"going on under the hood. This option requires one or more "
|
|
|
|
"debugtypes, separated by commas. These define what exactly "
|
|
|
|
"will be debugged, and so far include two options: imap, thread, "
|
|
|
|
"maildir or ALL. The imap option will enable IMAP protocol "
|
|
|
|
"stream and parsing debugging. Note that the output may contain "
|
|
|
|
"passwords, so take care to remove that from the debugging "
|
|
|
|
"output before sending it to anyone else. The maildir option "
|
|
|
|
"will enable debugging for certain Maildir operations. "
|
|
|
|
"The use of any debug option (unless 'thread' is included), "
|
|
|
|
"implies the single-thread option -1.")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-l", dest="logfile", metavar="FILE",
|
|
|
|
help="Log to FILE")
|
|
|
|
|
|
|
|
parser.add_option("-f", dest="folders", metavar="folder1,[folder2...]",
|
2012-02-24 14:52:30 +01:00
|
|
|
help="Only sync the specified folders. The folder names "
|
|
|
|
"are the *untranslated* foldernames of the remote repository. "
|
|
|
|
"This command-line option overrides any 'folderfilter' "
|
|
|
|
"and 'folderincludes' options in the configuration file.")
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
parser.add_option("-k", dest="configoverride",
|
|
|
|
action="append",
|
|
|
|
metavar="[section:]option=value",
|
|
|
|
help=
|
|
|
|
"""Override configuration file option. If"section" is
|
|
|
|
omitted, it defaults to "general". Any underscores
|
|
|
|
in the section name are replaced with spaces:
|
|
|
|
for instance, to override option "autorefresh" in
|
|
|
|
the "[Account Personal]" section in the config file
|
|
|
|
one would use "-k Account_Personal:autorefresh=30".""")
|
|
|
|
|
|
|
|
parser.add_option("-o",
|
|
|
|
action="store_true", dest="runonce",
|
|
|
|
default=False,
|
|
|
|
help="Run only once, ignoring any autorefresh setting "
|
|
|
|
"in the configuration file.")
|
|
|
|
|
|
|
|
parser.add_option("-q",
|
|
|
|
action="store_true", dest="quick",
|
|
|
|
default=False,
|
|
|
|
help="Run only quick synchronizations. Ignore any "
|
|
|
|
"flag updates on IMAP servers (if a flag on the remote IMAP "
|
|
|
|
"changes, and we have the message locally, it will be left "
|
|
|
|
"untouched in a quick run.")
|
|
|
|
|
|
|
|
parser.add_option("-u", dest="interface",
|
|
|
|
help="Specifies an alternative user interface to "
|
|
|
|
"use. This overrides the default specified in the "
|
|
|
|
"configuration file. The UI specified with -u will "
|
|
|
|
"be forced to be used, even if checks determine that it is "
|
|
|
|
"not usable. Possible interface choices are: %s " %
|
2011-01-10 12:12:42 +01:00
|
|
|
", ".join(UI_LIST.keys()))
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
2013-02-05 04:49:56 +01:00
|
|
|
globals.set_options (options)
|
2010-12-15 19:06:59 +01: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'
|
|
|
|
if not xdg_var in os.environ or not os.environ[xdg_var]:
|
|
|
|
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
|
2015-01-08 17:13:33 +01: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
|
|
|
|
|
|
|
#profile mode chosen?
|
|
|
|
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
|
2015-01-08 17:13:33 +01:00
|
|
|
logging.warn("Profile mode: Directory '%s' already exists!"%
|
2011-09-29 16:02:51 +02:00
|
|
|
options.profiledir)
|
|
|
|
else:
|
|
|
|
os.mkdir(options.profiledir)
|
|
|
|
threadutil.ExitNotifyThread.set_profiledir(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 "
|
2015-01-08 17:13:33 +01:00
|
|
|
"created in '%s'"% options.profiledir)
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
#override a config value
|
|
|
|
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)
|
|
|
|
|
2011-03-06 11:04:46 +01:00
|
|
|
#which ui to use? cmd line option overrides config file
|
2015-01-01 21:41:11 +01:00
|
|
|
ui_type = config.getdefault('general', 'ui', 'ttyui')
|
2011-01-05 17:00:54 +01:00
|
|
|
if options.interface != None:
|
|
|
|
ui_type = options.interface
|
2011-03-06 11:04:46 +01:00
|
|
|
if '.' in ui_type:
|
|
|
|
#transform Curses.Blinkenlights -> Blinkenlights
|
|
|
|
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 '
|
2015-01-01 21:41:11 +01:00
|
|
|
'of %s'% ', '.join(UI_LIST.keys()))
|
2012-01-20 14:53:18 +01:00
|
|
|
if options.diagnostics: ui_type = 'basic' # enforce basic UI for --info
|
2011-09-15 13:24:48 +02:00
|
|
|
|
|
|
|
#dry-run? Set [general]dry-run=True
|
|
|
|
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:
|
2011-03-06 11:04:46 +01: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:
|
2015-01-08 17:13:33 +01:00
|
|
|
logging.error("UI '%s' does not exist, choose one of: %s"% \
|
|
|
|
(ui_type, ', '.join(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
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2010-12-15 19:06:59 +01:00
|
|
|
#welcome blurb
|
2011-06-30 14:04:18 +02:00
|
|
|
self.ui.init_banner()
|
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'
|
2011-01-12 11:15:13 +01:00
|
|
|
#force single threading?
|
|
|
|
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)
|
|
|
|
if dtype.lower() == u'imap':
|
2010-12-06 13:36:54 +01:00
|
|
|
imaplib.Debug = 5
|
2010-12-15 19:06:59 +01:00
|
|
|
|
|
|
|
if options.runonce:
|
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
|
|
|
|
2011-04-05 12:16:43 +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(",")
|
2015-01-01 21:41:11 +01: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 ' + \
|
2012-02-24 14:52:30 +01:00
|
|
|
config.get(account_section, 'remoterepository')
|
|
|
|
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)
|
|
|
|
|
|
|
|
threadutil.initInstanceLimit('ACCOUNTLIMIT',
|
|
|
|
config.getdefaultint('general', 'maxsyncaccounts', 1))
|
|
|
|
|
|
|
|
for reposname in config.getsectionlist('Repository'):
|
|
|
|
for instancename in ["FOLDER_" + reposname,
|
|
|
|
"MSGCOPY_" + reposname]:
|
|
|
|
if options.singlethreading:
|
|
|
|
threadutil.initInstanceLimit(instancename, 1)
|
|
|
|
else:
|
|
|
|
threadutil.initInstanceLimit(instancename,
|
|
|
|
config.getdefaultint('Repository ' + reposname,
|
|
|
|
'maxconnections', 2))
|
|
|
|
self.config = config
|
2011-06-30 15:18:03 +02:00
|
|
|
return (options, args)
|
2011-06-30 14:04:18 +02: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."""
|
2010-12-06 13:36:54 +01:00
|
|
|
try:
|
2011-06-30 14:04:18 +02:00
|
|
|
pidfd = open(self.config.getmetadatadir() + "/pid", "w")
|
2010-12-06 13:36:54 +01:00
|
|
|
pidfd.write(str(os.getpid()) + "\n")
|
|
|
|
pidfd.close()
|
|
|
|
except:
|
|
|
|
pass
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
try:
|
2015-01-11 13:54:53 +01:00
|
|
|
# Honor CLI --account option, only.
|
|
|
|
# Accounts to sync are put into syncaccounts variable.
|
2011-06-30 14:04:18 +02:00
|
|
|
activeaccounts = self.config.get("general", "accounts")
|
2010-12-15 19:06:59 +01:00
|
|
|
if options.accounts:
|
|
|
|
activeaccounts = options.accounts
|
2010-12-06 13:36:54 +01:00
|
|
|
activeaccounts = activeaccounts.replace(" ", "")
|
|
|
|
activeaccounts = activeaccounts.split(",")
|
2011-06-30 14:04:18 +02:00
|
|
|
allaccounts = accounts.AccountHashGenerator(self.config)
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2010-12-06 13:36:54 +01:00
|
|
|
syncaccounts = []
|
|
|
|
for account in activeaccounts:
|
|
|
|
if account not in allaccounts:
|
|
|
|
if len(allaccounts) == 0:
|
2015-01-08 17:13:33 +01:00
|
|
|
errormsg = "The account '%s' does not exist because no" \
|
|
|
|
" accounts are defined!"% account
|
2010-12-06 13:36:54 +01:00
|
|
|
else:
|
2015-01-08 17:13:33 +01:00
|
|
|
errormsg = "The account '%s' does not exist. Valid ac" \
|
|
|
|
"counts are: %s"% \
|
|
|
|
(account, ", ".join(allaccounts.keys()))
|
|
|
|
self.ui.terminate(1, errormsg=errormsg)
|
2010-12-06 13:36:54 +01:00
|
|
|
if account not in syncaccounts:
|
|
|
|
syncaccounts.append(account)
|
2011-06-30 15:18:03 +02:00
|
|
|
|
2011-05-07 17:40:32 +02:00
|
|
|
def sig_handler(sig, frame):
|
2012-06-05 04:47:15 +02:00
|
|
|
if sig == signal.SIGUSR1:
|
2011-05-07 17:40:32 +02:00
|
|
|
# tell each account to stop sleeping
|
|
|
|
accounts.Account.set_abort_event(self.config, 1)
|
|
|
|
elif sig == signal.SIGUSR2:
|
|
|
|
# tell each account to stop looping
|
2012-01-04 19:24:19 +01:00
|
|
|
getglobalui().warn("Terminating after this sync...")
|
2011-05-07 17:40:32 +02:00
|
|
|
accounts.Account.set_abort_event(self.config, 2)
|
2012-06-05 04:47:15 +02:00
|
|
|
elif sig in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP):
|
2012-01-04 19:24:19 +01:00
|
|
|
# tell each account to ABORT ASAP (ctrl-c)
|
|
|
|
getglobalui().warn("Terminating NOW (this may "\
|
|
|
|
"take a few seconds)...")
|
|
|
|
accounts.Account.set_abort_event(self.config, 3)
|
2013-01-28 19:49:29 +01:00
|
|
|
elif sig == signal.SIGQUIT:
|
2015-01-01 21:41:11 +01:00
|
|
|
stacktrace.dump(sys.stderr)
|
2013-01-28 19:49:29 +01:00
|
|
|
os.abort()
|
2011-10-26 16:47:21 +02:00
|
|
|
|
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)
|
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
|
|
|
|
2011-01-12 11:15:12 +01:00
|
|
|
#various initializations that need to be performed:
|
2011-06-30 14:04:18 +02:00
|
|
|
offlineimap.mbnames.init(self.config, syncaccounts)
|
2011-01-12 11:15:12 +01:00
|
|
|
|
|
|
|
if options.singlethreading:
|
|
|
|
#singlethreaded
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__sync_singlethreaded(syncaccounts)
|
2011-01-12 11:15:12 +01:00
|
|
|
else:
|
|
|
|
# multithreaded
|
|
|
|
t = threadutil.ExitNotifyThread(target=syncmaster.syncitall,
|
2010-12-06 13:36:54 +01:00
|
|
|
name='Sync Runner',
|
|
|
|
kwargs = {'accounts': syncaccounts,
|
2011-06-30 14:04:18 +02:00
|
|
|
'config': self.config})
|
2011-01-12 11:15:12 +01:00
|
|
|
t.start()
|
|
|
|
threadutil.exitnotifymonitorloop(threadutil.threadexited)
|
2011-06-30 14:04:18 +02:00
|
|
|
self.ui.terminate()
|
2011-01-12 11:15:08 +01: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()
|
2002-10-07 22:59:02 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __sync_singlethreaded(self, accs):
|
2011-01-12 11:15:12 +01:00
|
|
|
"""Executed if we do not want a separate syncmaster thread
|
|
|
|
|
|
|
|
:param accs: A list of accounts that should be synced
|
|
|
|
"""
|
|
|
|
for accountname in accs:
|
2011-06-30 14:04:18 +02:00
|
|
|
account = offlineimap.accounts.SyncableAccount(self.config,
|
|
|
|
accountname)
|
2015-01-01 21:41:11 +01:00
|
|
|
threading.currentThread().name = "Account sync %s"% accountname
|
2011-05-07 17:40:32 +02:00
|
|
|
account.syncrunner()
|
2011-06-30 15:18:03 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __serverdiagnostics(self, options):
|
2011-06-30 15:18:03 +02:00
|
|
|
activeaccounts = self.config.get("general", "accounts")
|
|
|
|
if options.accounts:
|
|
|
|
activeaccounts = options.accounts
|
|
|
|
activeaccounts = activeaccounts.split(",")
|
|
|
|
allaccounts = accounts.AccountListGenerator(self.config)
|
|
|
|
for account in allaccounts:
|
|
|
|
if account.name not in activeaccounts: continue
|
|
|
|
account.serverdiagnostics()
|