2002-10-07 22:59:02 +02:00
# OfflineIMAP initialization code
2003-04-18 04:18:34 +02:00
# Copyright (C) 2002, 2003 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2003-04-18 04:18:34 +02:00
from offlineimap import imaplib , 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-01-30 02:19:53 +01:00
import re , os , os . path , offlineimap , sys , fcntl
2003-01-04 05:57:46 +01:00
from offlineimap . CustomConfig import CustomConfigParser
2002-10-07 22:59:02 +02:00
from threading import *
2003-04-18 04:18:34 +02:00
import threading
2002-10-07 22:59:02 +02:00
from getopt import getopt
2003-01-30 02:19:53 +01:00
lockfd = None
def lock ( config , ui ) :
global lockfd
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 ) :
assert versionno == version . versionstr , " Revision of main program ( %d ) does not match that of library ( %d ). Please double-check your PYTHONPATH and installation locations. " % ( revno , version . revno )
2002-10-07 22:59:02 +02:00
options = { }
if ' --help ' in sys . argv [ 1 : ] :
sys . stdout . write ( version . cmdhelp + " \n " )
sys . exit ( 0 )
for optlist in getopt ( sys . argv [ 1 : ] , ' P:1oa:c:d:u:h ' ) [ 0 ] :
options [ optlist [ 0 ] ] = optlist [ 1 ]
if ' -h ' in options :
sys . stdout . write ( version . cmdhelp )
sys . stdout . write ( " \n " )
sys . exit ( 0 )
configfilename = os . path . expanduser ( " ~/.offlineimaprc " )
if ' -c ' in options :
configfilename = options [ ' -c ' ]
if ' -P ' in options :
if not ' -1 ' in options :
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 )
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
ui . init_banner ( )
UIBase . setglobalui ( ui )
if ' -d ' in options :
for debugtype in options [ ' -d ' ] . split ( ' , ' ) :
ui . add_debug ( debugtype . strip ( ) )
if debugtype == ' imap ' :
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-06-02 21:06:18 +02:00
if ' -l ' in options :
ui . setlogfd ( open ( options [ ' -l ' ] , ' wt ' ) )
2003-01-04 05:57:46 +01:00
if ' -o ' in options :
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
2003-01-30 02:19:53 +01:00
lock ( config , ui )
2003-04-18 04:18:34 +02:00
activeaccounts = config . get ( " general " , " accounts " )
2002-10-07 22:59:02 +02:00
if ' -a ' in options :
2003-04-18 04:18:34 +02:00
activeaccounts = options [ ' -a ' ]
activeaccounts = activeaccounts . replace ( " " , " " )
activeaccounts = activeaccounts . split ( " , " )
allaccounts = accounts . AccountHashGenerator ( config )
syncaccounts = { }
for account in activeaccounts :
syncaccounts [ account ] = allaccounts [ account ]
2002-10-07 22:59:02 +02:00
server = None
remoterepos = None
localrepos = None
if ' -1 ' in options :
threadutil . initInstanceLimit ( " ACCOUNTLIMIT " , 1 )
else :
threadutil . initInstanceLimit ( " ACCOUNTLIMIT " ,
2003-01-07 04:15:22 +01:00
config . getdefaultint ( " general " , " maxsyncaccounts " , 1 ) )
2002-10-07 22:59:02 +02:00
2003-04-18 04:18:34 +02:00
for reposname in config . getsectionlist ( ' Repository ' ) :
for instancename in [ " FOLDER_ " + reposname ,
" MSGCOPY_ " + reposname ] :
2002-10-07 22:59:02 +02:00
if ' -1 ' in options :
threadutil . initInstanceLimit ( instancename , 1 )
else :
threadutil . initInstanceLimit ( instancename ,
2003-04-18 04:18:34 +02:00
config . getdefaultint ( ' Repository ' + reposname , " maxconnections " , 1 ) )
2002-10-07 22:59:02 +02:00
threadutil . initexitnotify ( )
2003-01-04 05:57:46 +01:00
t = ExitNotifyThread ( target = syncmaster . syncitall ,
2002-10-07 23:11:19 +02:00
name = ' Sync Runner ' ,
2003-04-18 04:18:34 +02:00
kwargs = { ' accounts ' : syncaccounts ,
2003-01-04 05:57:46 +01:00
' config ' : config } )
2002-10-07 22:59:02 +02:00
t . setDaemon ( 1 )
t . start ( )
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.