]> code.delx.au - offlineimap/blob - offlineimap/head/offlineimap/init.py
/offlineimap/head: changeset 367
[offlineimap] / offlineimap / head / offlineimap / init.py
1 # OfflineIMAP initialization code
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
4 #
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.
9 #
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.
14 #
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
18
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
27
28 lockfd = None
29
30 def lock(config, ui):
31 global lockfd
32 lockfd = open(config.getmetadatadir() + "/lock", "w")
33 try:
34 fcntl.flock(lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
35 except IOError:
36 ui.locked()
37 ui.terminate(1)
38
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)
41 options = {}
42 if '--help' in sys.argv[1:]:
43 sys.stdout.write(version.cmdhelp + "\n")
44 sys.exit(0)
45
46 for optlist in getopt(sys.argv[1:], 'P:1oa:c:d:u:h')[0]:
47 options[optlist[0]] = optlist[1]
48
49 if '-h' in options:
50 sys.stdout.write(version.cmdhelp)
51 sys.stdout.write("\n")
52 sys.exit(0)
53 configfilename = os.path.expanduser("~/.offlineimaprc")
54 if '-c' in options:
55 configfilename = options['-c']
56 if '-P' in options:
57 if not '-1' in options:
58 sys.stderr.write("FATAL: profile mode REQUIRES -1\n")
59 sys.exit(100)
60 profiledir = options['-P']
61 os.mkdir(profiledir)
62 threadutil.setprofiledir(profiledir)
63 sys.stderr.write("WARNING: profile mode engaged;\nPotentially large data will be created in " + profiledir + "\n")
64
65 config = CustomConfigParser()
66 if not os.path.exists(configfilename):
67 sys.stderr.write(" *** Config file %s does not exist; aborting!\n" % configfilename)
68 sys.exit(1)
69
70 config.read(configfilename)
71
72 ui = offlineimap.ui.detector.findUI(config, options.get('-u'))
73 ui.init_banner()
74 UIBase.setglobalui(ui)
75
76 if '-d' in options:
77 for debugtype in options['-d'].split(','):
78 ui.add_debug(debugtype.strip())
79 if debugtype == 'imap':
80 imaplib.Debug = 5
81
82 if '-o' in options:
83 for section in config.getaccountlist():
84 config.remove_option(section, "autorefresh")
85
86 lock(config, ui)
87
88 accounts = config.get("general", "accounts")
89 if '-a' in options:
90 accounts = options['-a']
91 accounts = accounts.replace(" ", "")
92 accounts = accounts.split(",")
93
94 server = None
95 remoterepos = None
96 localrepos = None
97
98 if '-1' in options:
99 threadutil.initInstanceLimit("ACCOUNTLIMIT", 1)
100 else:
101 threadutil.initInstanceLimit("ACCOUNTLIMIT",
102 config.getdefaultint("general", "maxsyncaccounts", 1))
103
104 for account in accounts:
105 for instancename in ["FOLDER_" + account, "MSGCOPY_" + account]:
106 if '-1' in options:
107 threadutil.initInstanceLimit(instancename, 1)
108 else:
109 threadutil.initInstanceLimit(instancename,
110 config.getdefaultint(account, "maxconnections", 1))
111
112 threadutil.initexitnotify()
113 t = ExitNotifyThread(target=syncmaster.syncitall,
114 name='Sync Runner',
115 kwargs = {'accounts': accounts,
116 'config': config})
117 t.setDaemon(1)
118 t.start()
119 try:
120 threadutil.exitnotifymonitorloop(threadutil.threadexited)
121 except SystemExit:
122 raise
123 except:
124 ui.mainException() # Also expected to terminate.
125
126