]>
code.delx.au - offlineimap/blob - offlineimap/head/offlineimap/init.py
1 # OfflineIMAP initialization code
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 from offlineimap
import imaplib
, imapserver
, repository
, folder
, mbnames
, threadutil
, version
, syncmaster
20 from offlineimap
. localeval
import LocalEval
21 from offlineimap
. threadutil
import InstanceLimitedThread
, ExitNotifyThread
22 from offlineimap
. ui
import UIBase
23 import re
, os
, os
. path
, offlineimap
, sys
, fcntl
24 from offlineimap
. CustomConfig
import CustomConfigParser
25 from threading
import *
26 from getopt
import getopt
32 lockfd
= open ( config
. getmetadatadir () + "/lock" , "w" )
34 fcntl
. flock ( lockfd
, fcntl
. LOCK_EX | fcntl
. LOCK_NB
)
39 def startup ( versionno
):
40 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
)
42 if '--help' in sys
. argv
[ 1 :]:
43 sys
. stdout
. write ( version
. cmdhelp
+ " \n " )
46 for optlist
in getopt ( sys
. argv
[ 1 :], 'P:1oa:c:d:u:h' )[ 0 ]:
47 options
[ optlist
[ 0 ]] = optlist
[ 1 ]
50 sys
. stdout
. write ( version
. cmdhelp
)
51 sys
. stdout
. write ( " \n " )
53 configfilename
= os
. path
. expanduser ( "~/.offlineimaprc" )
55 configfilename
= options
[ '-c' ]
57 if not '-1' in options
:
58 sys
. stderr
. write ( "FATAL: profile mode REQUIRES -1 \n " )
60 profiledir
= options
[ '-P' ]
62 threadutil
. setprofiledir ( profiledir
)
63 sys
. stderr
. write ( "WARNING: profile mode engaged; \n Potentially large data will be created in " + profiledir
+ " \n " )
65 config
= CustomConfigParser ()
66 if not os
. path
. exists ( configfilename
):
67 sys
. stderr
. write ( " *** Config file %s does not exist; aborting! \n " % configfilename
)
70 config
. read ( configfilename
)
72 ui
= offlineimap
. ui
. detector
. findUI ( config
, options
. get ( '-u' ))
74 UIBase
. setglobalui ( ui
)
77 for debugtype
in options
[ '-d' ]. split ( ',' ):
78 ui
. add_debug ( debugtype
. strip ())
79 if debugtype
== 'imap' :
83 for section
in config
. getaccountlist ():
84 config
. remove_option ( section
, "autorefresh" )
88 accounts
= config
. get ( "general" , "accounts" )
90 accounts
= options
[ '-a' ]
91 accounts
= accounts
. replace ( " " , "" )
92 accounts
= accounts
. split ( "," )
99 threadutil
. initInstanceLimit ( "ACCOUNTLIMIT" , 1 )
101 threadutil
. initInstanceLimit ( "ACCOUNTLIMIT" ,
102 config
. getdefaultint ( "general" , "maxsyncaccounts" , 1 ))
104 for account
in accounts
:
105 for instancename
in [ "FOLDER_" + account
, "MSGCOPY_" + account
]:
107 threadutil
. initInstanceLimit ( instancename
, 1 )
109 threadutil
. initInstanceLimit ( instancename
,
110 config
. getdefaultint ( account
, "maxconnections" , 1 ))
112 threadutil
. initexitnotify ()
113 t
= ExitNotifyThread ( target
= syncmaster
. syncitall
,
115 kwargs
= { 'accounts' : accounts
,
120 threadutil
. exitnotifymonitorloop ( threadutil
. threadexited
)
124 ui
. mainException () # Also expected to terminate.