2002-10-07 22:59:02 +02:00
# OfflineIMAP initialization code
2007-07-04 19:53:48 +02:00
# Copyright (C) 2002-2007 John Goerzen
2002-10-07 22:59:02 +02:00
# <jgoerzen@complete.org>
#
# 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
2009-08-12 21:49:58 +02:00
import imaplib
from offlineimap import imapserver , repository , folder , mbnames , threadutil , version , syncmaster , accounts
2002-10-07 22:59:02 +02:00
from offlineimap . localeval import LocalEval
from offlineimap . threadutil import InstanceLimitedThread , ExitNotifyThread
from offlineimap . ui import UIBase
2003-07-25 04:15:27 +02:00
import re , os , os . path , offlineimap , sys
2003-01-04 05:57:46 +01:00
from offlineimap . CustomConfig import CustomConfigParser
2002-10-07 22:59:02 +02:00
from threading import *
2007-07-06 18:37:58 +02:00
import threading , socket
2002-10-07 22:59:02 +02:00
from getopt import getopt
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
2002-10-07 22:59:02 +02:00
2003-07-25 04:15:27 +02:00
try :
import fcntl
hasfcntl = 1
except :
hasfcntl = 0
2003-01-30 02:19:53 +01:00
lockfd = None
def lock ( config , ui ) :
2003-07-25 04:15:27 +02:00
global lockfd , hasfcntl
if not hasfcntl :
return
2003-01-30 02:19:53 +01:00
lockfd = open ( config . getmetadatadir ( ) + " /lock " , " w " )
try :
fcntl . flock ( lockfd , fcntl . LOCK_EX | fcntl . LOCK_NB )
except IOError :
ui . locked ( )
ui . terminate ( 1 )
2002-10-07 23:30:35 +02:00
def startup ( versionno ) :
2003-07-25 04:15:27 +02:00
assert versionno == version . versionstr , " Revision of main program ( %s ) does not match that of library ( %s ). Please double-check your PYTHONPATH and installation locations. " % ( versionno , version . versionstr )
2002-10-07 22:59:02 +02:00
options = { }
if ' --help ' in sys . argv [ 1 : ] :
2007-07-10 13:57:03 +02:00
sys . stdout . write ( version . getcmdhelp ( ) + " \n " )
2002-10-07 22:59:02 +02:00
sys . exit ( 0 )
2008-01-03 04:15:11 +01:00
for optlist in getopt ( sys . argv [ 1 : ] , ' P:1oqa:c:d:l:u:hk:f: ' ) [ 0 ] :
2002-10-07 22:59:02 +02:00
options [ optlist [ 0 ] ] = optlist [ 1 ]
2003-07-26 04:01:25 +02:00
if options . has_key ( ' -h ' ) :
2007-07-10 13:57:03 +02:00
sys . stdout . write ( version . getcmdhelp ( ) )
2002-10-07 22:59:02 +02:00
sys . stdout . write ( " \n " )
sys . exit ( 0 )
configfilename = os . path . expanduser ( " ~/.offlineimaprc " )
2003-07-26 04:01:25 +02:00
if options . has_key ( ' -c ' ) :
2002-10-07 22:59:02 +02:00
configfilename = options [ ' -c ' ]
2003-07-26 04:01:25 +02:00
if options . has_key ( ' -P ' ) :
if not options . has_key ( ' -1 ' ) :
2002-10-07 22:59:02 +02:00
sys . stderr . write ( " FATAL: profile mode REQUIRES -1 \n " )
sys . exit ( 100 )
profiledir = options [ ' -P ' ]
os . mkdir ( profiledir )
threadutil . setprofiledir ( profiledir )
sys . stderr . write ( " WARNING: profile mode engaged; \n Potentially large data will be created in " + profiledir + " \n " )
2003-01-04 05:57:46 +01:00
config = CustomConfigParser ( )
2002-10-07 22:59:02 +02:00
if not os . path . exists ( configfilename ) :
sys . stderr . write ( " *** Config file %s does not exist; aborting! \n " % configfilename )
sys . exit ( 1 )
config . read ( configfilename )
2008-01-03 04:13:04 +01:00
# override config values with option '-k'
for option in options . keys ( ) :
if option == ' -k ' :
( key , value ) = options [ ' -k ' ] . split ( ' = ' , 1 )
if ' : ' in key :
( secname , key ) = key . split ( ' : ' , 1 )
section = secname . replace ( " _ " , " " )
else :
section = " general "
config . set ( section , key , value )
2003-01-04 05:57:46 +01:00
ui = offlineimap . ui . detector . findUI ( config , options . get ( ' -u ' ) )
2002-10-07 22:59:02 +02:00
UIBase . setglobalui ( ui )
2003-07-26 04:01:25 +02:00
if options . has_key ( ' -l ' ) :
2003-06-02 21:52:33 +02:00
ui . setlogfd ( open ( options [ ' -l ' ] , ' wt ' ) )
ui . init_banner ( )
2003-07-26 04:01:25 +02:00
if options . has_key ( ' -d ' ) :
2002-10-07 22:59:02 +02:00
for debugtype in options [ ' -d ' ] . split ( ' , ' ) :
ui . add_debug ( debugtype . strip ( ) )
if debugtype == ' imap ' :
2009-08-12 21:49:58 +02:00
imaplib . Debug = 5
2003-04-18 04:18:34 +02:00
if debugtype == ' thread ' :
threading . _VERBOSE = 1
2002-10-07 22:59:02 +02:00
2003-07-26 04:01:25 +02:00
if options . has_key ( ' -o ' ) :
2003-04-18 04:18:34 +02:00
# FIXME: maybe need a better
for section in accounts . getaccountlist ( config ) :
config . remove_option ( ' Account ' + section , " autorefresh " )
2002-10-07 22:59:02 +02:00
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
if options . has_key ( ' -q ' ) :
for section in accounts . getaccountlist ( config ) :
config . set ( ' Account ' + section , " quick " , ' -1 ' )
2008-01-03 04:15:11 +01:00
if options . has_key ( ' -f ' ) :
foldernames = options [ ' -f ' ] . replace ( " " , " " ) . split ( " , " )
folderfilter = " lambda f: f in %s " % foldernames
folderincludes = " [] "
for accountname in accounts . getaccountlist ( config ) :
account_section = ' Account ' + accountname
remote_repo_section = ' Repository ' + \
config . get ( account_section , ' remoterepository ' )
local_repo_section = ' Repository ' + \
config . get ( account_section , ' localrepository ' )
for section in [ remote_repo_section , local_repo_section ] :
config . set ( section , " folderfilter " , folderfilter )
config . set ( section , " folderincludes " , folderincludes )
2003-01-30 02:19:53 +01:00
lock ( config , ui )
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
def sigterm_handler ( signum , frame ) :
# die immediately
ui . terminate ( errormsg = " terminating... " )
signal . signal ( signal . SIGTERM , sigterm_handler )
2007-07-07 02:50:43 +02:00
try :
pidfd = open ( config . getmetadatadir ( ) + " /pid " , " w " )
2008-08-08 03:16:36 +02:00
pidfd . write ( str ( os . getpid ( ) ) + " \n " )
2007-07-07 02:50:43 +02:00
pidfd . close ( )
except :
pass
2006-12-01 11:59:22 +01:00
try :
if options . has_key ( ' -l ' ) :
sys . stderr = ui . logfile
2007-07-06 18:37:58 +02:00
socktimeout = config . getdefaultint ( " general " , " socktimeout " , 0 )
if socktimeout > 0 :
socket . setdefaulttimeout ( socktimeout )
2006-12-01 11:59:22 +01:00
activeaccounts = config . get ( " general " , " accounts " )
if options . has_key ( ' -a ' ) :
activeaccounts = options [ ' -a ' ]
activeaccounts = activeaccounts . replace ( " " , " " )
activeaccounts = activeaccounts . split ( " , " )
allaccounts = accounts . AccountHashGenerator ( config )
2009-05-05 21:47:11 +02:00
syncaccounts = [ ]
2006-12-01 11:59:22 +01:00
for account in activeaccounts :
if account not in allaccounts :
if len ( allaccounts ) == 0 :
errormsg = ' The account " %s " does not exist because no accounts are defined! ' % account
else :
errormsg = ' The account " %s " does not exist. Valid accounts are: ' % account
for name in allaccounts . keys ( ) :
errormsg + = ' \n %s ' % name
ui . terminate ( 1 , errortitle = ' Unknown Account " %s " ' % account , errormsg = errormsg )
2009-05-05 21:47:11 +02:00
if account not in syncaccounts :
syncaccounts . append ( account )
2006-12-01 11:59:22 +01:00
server = None
remoterepos = None
localrepos = None
if options . has_key ( ' -1 ' ) :
threadutil . initInstanceLimit ( " ACCOUNTLIMIT " , 1 )
else :
threadutil . initInstanceLimit ( " ACCOUNTLIMIT " ,
config . getdefaultint ( " general " , " maxsyncaccounts " , 1 ) )
for reposname in config . getsectionlist ( ' Repository ' ) :
for instancename in [ " FOLDER_ " + reposname ,
" MSGCOPY_ " + reposname ] :
if options . has_key ( ' -1 ' ) :
threadutil . initInstanceLimit ( instancename , 1 )
else :
threadutil . initInstanceLimit ( instancename ,
config . getdefaultint ( ' Repository ' + reposname , " maxconnections " , 1 ) )
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
siglisteners = [ ]
def sig_handler ( signum , frame ) :
if signum == signal . SIGUSR1 :
# tell each account to do a full sync asap
signum = ( 1 , )
elif signum == signal . SIGHUP :
# tell each account to die asap
signum = ( 2 , )
elif signum == signal . SIGUSR2 :
# tell each account to do a full sync asap, then die
signum = ( 1 , 2 )
# one listener per account thread (up to maxsyncaccounts)
for listener in siglisteners :
for sig in signum :
listener . put_nowait ( sig )
signal . signal ( signal . SIGHUP , sig_handler )
signal . signal ( signal . SIGUSR1 , sig_handler )
signal . signal ( signal . SIGUSR2 , sig_handler )
2006-12-01 11:59:22 +01:00
threadutil . initexitnotify ( )
t = ExitNotifyThread ( target = syncmaster . syncitall ,
name = ' Sync Runner ' ,
kwargs = { ' accounts ' : syncaccounts ,
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
' config ' : config ,
' siglisteners ' : siglisteners } )
2006-12-01 11:59:22 +01:00
t . setDaemon ( 1 )
t . start ( )
except :
ui . mainException ( )
2002-10-07 22:59:02 +02:00
try :
2002-10-07 23:17:13 +02:00
threadutil . exitnotifymonitorloop ( threadutil . threadexited )
2002-10-07 22:59:02 +02:00
except SystemExit :
raise
except :
ui . mainException ( ) # Also expected to terminate.