]> code.delx.au - offlineimap/blobdiff - offlineimap/init.py
Add option '-k' for overriding config options
[offlineimap] / offlineimap / init.py
index ed0ff6760e8eea3859d72722fa0b0b543db984a1..d901864929f6c30e61f9f947c1ca2142bc7a1b55 100644 (file)
@@ -1,5 +1,5 @@
 # OfflineIMAP initialization code
-# Copyright (C) 2002, 2003 John Goerzen
+# Copyright (C) 2002-2007 John Goerzen
 # <jgoerzen@complete.org>
 #
 #    This program is free software; you can redistribute it and/or modify
 #
 #    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
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
-from offlineimap import imaplib, imapserver, repository, folder, mbnames, threadutil, version, syncmaster, accounts
+import imaplib
+from offlineimap import imapserver, repository, folder, mbnames, threadutil, version, syncmaster, accounts
 from offlineimap.localeval import LocalEval
 from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
 from offlineimap.ui import UIBase
 import re, os, os.path, offlineimap, sys
 from offlineimap.CustomConfig import CustomConfigParser
 from threading import *
-import threading
+import threading, socket
 from getopt import getopt
 
 try:
@@ -49,14 +50,14 @@ def startup(versionno):
     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)
     options = {}
     if '--help' in sys.argv[1:]:
-        sys.stdout.write(version.cmdhelp + "\n")
+        sys.stdout.write(version.getcmdhelp() + "\n")
         sys.exit(0)
 
-    for optlist in getopt(sys.argv[1:], 'P:1oa:c:d:l:u:h')[0]:
+    for optlist in getopt(sys.argv[1:], 'P:1oqa:c:d:l:u:hk:')[0]:
         options[optlist[0]] = optlist[1]
 
     if options.has_key('-h'):
-        sys.stdout.write(version.cmdhelp)
+        sys.stdout.write(version.getcmdhelp())
         sys.stdout.write("\n")
         sys.exit(0)
     configfilename = os.path.expanduser("~/.offlineimaprc")
@@ -78,6 +79,17 @@ def startup(versionno):
 
     config.read(configfilename)
 
+    # 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)
+
     ui = offlineimap.ui.detector.findUI(config, options.get('-u'))
     UIBase.setglobalui(ui)
 
@@ -99,48 +111,75 @@ def startup(versionno):
         for section in accounts.getaccountlist(config):
             config.remove_option('Account ' + section, "autorefresh")
 
+    if options.has_key('-q'):
+        for section in accounts.getaccountlist(config):
+            config.set('Account ' + section, "quick", '-1')
+
     lock(config, ui)
 
-    if options.has_key('-l'):
-        sys.stderr = ui.logfile
-
-    activeaccounts = config.get("general", "accounts")
-    if options.has_key('-a'):
-        activeaccounts = options['-a']
-    activeaccounts = activeaccounts.replace(" ", "")
-    activeaccounts = activeaccounts.split(",")
-    allaccounts = accounts.AccountHashGenerator(config)
-
-    syncaccounts = {}
-    for account in activeaccounts:
-        syncaccounts[account] = allaccounts[account]
-
-    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))
-
-    threadutil.initexitnotify()
-    t = ExitNotifyThread(target=syncmaster.syncitall,
-                         name='Sync Runner',
-                         kwargs = {'accounts': syncaccounts,
-                                   'config': config})
-    t.setDaemon(1)
-    t.start()
+    try:
+        pidfd = open(config.getmetadatadir() + "/pid", "w")
+        pidfd.write(os.getpid())
+        pidfd.close()
+    except:
+        pass
+
+    try:
+        if options.has_key('-l'):
+            sys.stderr = ui.logfile
+
+        socktimeout = config.getdefaultint("general", "socktimeout", 0)
+        if socktimeout > 0:
+            socket.setdefaulttimeout(socktimeout)
+
+        activeaccounts = config.get("general", "accounts")
+        if options.has_key('-a'):
+            activeaccounts = options['-a']
+        activeaccounts = activeaccounts.replace(" ", "")
+        activeaccounts = activeaccounts.split(",")
+        allaccounts = accounts.AccountHashGenerator(config)
+
+        syncaccounts = {}
+        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)
+            syncaccounts[account] = allaccounts[account]
+
+        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))
+
+        threadutil.initexitnotify()
+        t = ExitNotifyThread(target=syncmaster.syncitall,
+                             name='Sync Runner',
+                             kwargs = {'accounts': syncaccounts,
+                                       'config': config})
+        t.setDaemon(1)
+        t.start()
+    except:
+        ui.mainException()
+
     try:
         threadutil.exitnotifymonitorloop(threadutil.threadexited)
     except SystemExit: