[CalendarServer-changes] [7445] CalendarServer/branches/users/glyph/new-export/calendarserver/tools

source_changes at macosforge.org source_changes at macosforge.org
Mon May 16 07:40:01 PDT 2011


Revision: 7445
          http://trac.macosforge.org/projects/calendarserver/changeset/7445
Author:   glyph at apple.com
Date:     2011-05-16 07:40:01 -0700 (Mon, 16 May 2011)
Log Message:
-----------
Move shared_main to a more shared location (calendarserver/tools/util.py) since it is useful for more than just purge.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/new-export/calendarserver/tools/purge.py
    CalendarServer/branches/users/glyph/new-export/calendarserver/tools/util.py

Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/purge.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/purge.py	2011-05-16 14:38:11 UTC (rev 7444)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/purge.py	2011-05-16 14:40:01 UTC (rev 7445)
@@ -37,11 +37,9 @@
 from twext.web2.dav import davxml
 from twext.web2.responsecode import NO_CONTENT
 
-from calendarserver.tap.caldav import CalDAVServiceMaker, CalDAVOptions
-from calendarserver.tap.util import FakeRequest
+from calendarserver.tap.util import FakeRequest, utilityMain
 from calendarserver.tap.util import getRootResource
 from calendarserver.tools.principals import removeProxy
-from calendarserver.tools.util import loadConfig
 from pycalendar.datetime import PyCalendarDateTime
 
 log = Logger()
@@ -205,27 +203,7 @@
 
 
 
-def shared_main(configFileName, serviceClass):
 
-    try:
-        loadConfig(configFileName)
-
-        config.ProcessType = "Utility"
-        config.UtilityServiceClass = serviceClass
-
-        maker = CalDAVServiceMaker()
-        options = CalDAVOptions
-        service = maker.makeService(options)
-
-        reactor.addSystemEventTrigger("during", "startup", service.startService)
-        reactor.addSystemEventTrigger("before", "shutdown", service.stopService)
-
-    except ConfigurationError, e:
-        sys.stderr.write("Error: %s\n" % (e,))
-        return
-
-    reactor.run()
-
 def main_purge_events():
 
     try:
@@ -295,7 +273,7 @@
     PurgeOldEventsService.dryrun = dryrun
     PurgeOldEventsService.verbose = verbose
 
-    shared_main(
+    utilityMain(
         configFileName,
         PurgeOldEventsService,
     )
@@ -357,7 +335,7 @@
     PurgeOrphanedAttachmentsService.dryrun = dryrun
     PurgeOrphanedAttachmentsService.verbose = verbose
 
-    shared_main(
+    utilityMain(
         configFileName,
         PurgeOrphanedAttachmentsService,
     )
@@ -406,7 +384,7 @@
     PurgePrincipalService.verbose = verbose
 
 
-    shared_main(
+    utilityMain(
         configFileName,
         PurgePrincipalService
     )

Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/util.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/util.py	2011-05-16 14:38:11 UTC (rev 7444)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/util.py	2011-05-16 14:40:01 UTC (rev 7445)
@@ -1,5 +1,5 @@
 ##
-# Copyright (c) 2008-2010 Apple Inc. All rights reserved.
+# Copyright (c) 2008-2011 Apple Inc. All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -14,6 +14,10 @@
 # limitations under the License.
 ##
 
+"""
+Utility functionality shared between calendarserver command-line tools.
+"""
+
 __all__ = [
     "loadConfig",
     "getDirectory",
@@ -22,6 +26,7 @@
     "booleanArgument",
 ]
 
+import sys
 import os
 from time import sleep
 import socket
@@ -34,6 +39,8 @@
 
 
 from calendarserver.provision.root import RootResource
+from calendarserver.tap.caldav import CalDAVServiceMaker, CalDAVOptions
+
 from twistedcaldav import memcachepool
 from twistedcaldav.config import config, ConfigurationError
 from twistedcaldav.directory import augment, calendaruserproxy
@@ -270,3 +277,52 @@
             % (description, dirpath)
         )
 
+
+
+def utilityMain(configFileName, serviceClass, reactor=None):
+    """
+    Shared main-point for utilities.
+
+    This function will:
+
+        - Load the configuration file named by C{configFileName},
+        - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of
+          C{"Utility"}
+        - run the reactor, with start/stop events hooked up to the service's
+          C{startService}/C{stopService} methods.
+
+    It is C{serviceClass}'s responsibility to stop the reactor when it's
+    complete.
+
+    @param configFileName: the name of the configuration file to load.
+    @type configuration: C{str}
+
+    @param serviceClass: a 1-argument callable which takes an object that
+        provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an
+        L{IService}.
+
+    @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} /
+        L{IReactorTCP} (etc) provider to use.  If C{None}, the default reactor
+        will be imported and used.
+    """
+    if reactor is None:
+        from twisted.internet import reactor
+    try:
+        loadConfig(configFileName)
+
+        config.ProcessType = "Utility"
+        config.UtilityServiceClass = serviceClass
+
+        maker = CalDAVServiceMaker()
+        options = CalDAVOptions
+        service = maker.makeService(options)
+
+        reactor.addSystemEventTrigger("during", "startup", service.startService)
+        reactor.addSystemEventTrigger("before", "shutdown", service.stopService)
+
+    except ConfigurationError, e:
+        sys.stderr.write("Error: %s\n" % (e,))
+        return
+
+    reactor.run()
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110516/22d19ce3/attachment-0001.html>


More information about the calendarserver-changes mailing list