]>
code.delx.au - offlineimap/blob - offlineimap/init.py
1 # OfflineIMAP initialization code
2 # Copyright (C) 2002, 2003 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 from offlineimap
import imaplib
, imapserver
, repository
, folder
, mbnames
, threadutil
, version
, syncmaster
, accounts
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
24 from offlineimap
. CustomConfig
import CustomConfigParser
25 from threading
import *
27 from getopt
import getopt
38 global lockfd
, hasfcntl
41 lockfd
= open ( config
. getmetadatadir () + "/lock" , "w" )
43 fcntl
. flock ( lockfd
, fcntl
. LOCK_EX | fcntl
. LOCK_NB
)
48 def startup ( versionno
):
49 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
)
51 if '--help' in sys
. argv
[ 1 :]:
52 sys
. stdout
. write ( version
. cmdhelp
+ " \n " )
55 for optlist
in getopt ( sys
. argv
[ 1 :], 'P:1oa:c:d:l:u:h' )[ 0 ]:
56 options
[ optlist
[ 0 ]] = optlist
[ 1 ]
58 if options
. has_key ( '-h' ):
59 sys
. stdout
. write ( version
. cmdhelp
)
60 sys
. stdout
. write ( " \n " )
62 configfilename
= os
. path
. expanduser ( "~/.offlineimaprc" )
63 if options
. has_key ( '-c' ):
64 configfilename
= options
[ '-c' ]
65 if options
. has_key ( '-P' ):
66 if not options
. has_key ( '-1' ):
67 sys
. stderr
. write ( "FATAL: profile mode REQUIRES -1 \n " )
69 profiledir
= options
[ '-P' ]
71 threadutil
. setprofiledir ( profiledir
)
72 sys
. stderr
. write ( "WARNING: profile mode engaged; \n Potentially large data will be created in " + profiledir
+ " \n " )
74 config
= CustomConfigParser ()
75 if not os
. path
. exists ( configfilename
):
76 sys
. stderr
. write ( " *** Config file %s does not exist; aborting! \n " % configfilename
)
79 config
. read ( configfilename
)
81 ui
= offlineimap
. ui
. detector
. findUI ( config
, options
. get ( '-u' ))
82 UIBase
. setglobalui ( ui
)
84 if options
. has_key ( '-l' ):
85 ui
. setlogfd ( open ( options
[ '-l' ], 'wt' ))
89 if options
. has_key ( '-d' ):
90 for debugtype
in options
[ '-d' ]. split ( ',' ):
91 ui
. add_debug ( debugtype
. strip ())
92 if debugtype
== 'imap' :
94 if debugtype
== 'thread' :
95 threading
._ VERBOSE
= 1
97 if options
. has_key ( '-o' ):
98 # FIXME: maybe need a better
99 for section
in accounts
. getaccountlist ( config
):
100 config
. remove_option ( 'Account ' + section
, "autorefresh" )
104 if options
. has_key ( '-l' ):
105 sys
. stderr
= ui
. logfile
107 activeaccounts
= config
. get ( "general" , "accounts" )
108 if options
. has_key ( '-a' ):
109 activeaccounts
= options
[ '-a' ]
110 activeaccounts
= activeaccounts
. replace ( " " , "" )
111 activeaccounts
= activeaccounts
. split ( "," )
112 allaccounts
= accounts
. AccountHashGenerator ( config
)
115 for account
in activeaccounts
:
116 syncaccounts
[ account
] = allaccounts
[ account
]
122 if options
. has_key ( '-1' ):
123 threadutil
. initInstanceLimit ( "ACCOUNTLIMIT" , 1 )
125 threadutil
. initInstanceLimit ( "ACCOUNTLIMIT" ,
126 config
. getdefaultint ( "general" , "maxsyncaccounts" , 1 ))
128 for reposname
in config
. getsectionlist ( 'Repository' ):
129 for instancename
in [ "FOLDER_" + reposname
,
130 "MSGCOPY_" + reposname
]:
131 if options
. has_key ( '-1' ):
132 threadutil
. initInstanceLimit ( instancename
, 1 )
134 threadutil
. initInstanceLimit ( instancename
,
135 config
. getdefaultint ( 'Repository ' + reposname
, "maxconnections" , 1 ))
137 threadutil
. initexitnotify ()
138 t
= ExitNotifyThread ( target
= syncmaster
. syncitall
,
140 kwargs
= { 'accounts' : syncaccounts
,
145 threadutil
. exitnotifymonitorloop ( threadutil
. threadexited
)
149 ui
. mainException () # Also expected to terminate.