[CalendarServer-changes] [12357] CalendarServer/branches/release/CalendarServer-5.2-dev/ calendarserver/tools

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:17:02 PDT 2014


Revision: 12357
          http://trac.calendarserver.org//changeset/12357
Author:   cdaboo at apple.com
Date:     2014-01-16 07:36:02 -0800 (Thu, 16 Jan 2014)
Log Message:
-----------
Make sure all cli tools use a service class derived from WorkerService. Also add an assert to ensure that is the case.

Modified Paths:
--------------
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/calverify.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/cmdline.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/dbinspect.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/export.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/migrate_verify.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/obliterate.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/shell/terminal.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/test/test_calverify.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/upgrade.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/validcalendardata.py
    CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/workitems.py

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/calverify.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/calverify.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/calverify.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -54,7 +54,6 @@
 from pycalendar.period import PyCalendarPeriod
 from pycalendar.timezone import PyCalendarTimezone
 
-from twisted.application.service import Service
 from twisted.internet.defer import inlineCallbacks, returnValue
 from twisted.python import usage
 from twisted.python.usage import Options
@@ -78,7 +77,7 @@
 from txdav.common.datastore.sql_tables import schema, _BIND_MODE_OWN
 from txdav.common.icommondatastore import InternalDataStoreError
 
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 
 from calendarserver.tools import tables
 from calendarserver.tools.util import getDirectory
@@ -386,14 +385,13 @@
 
 
 
-class CalVerifyService(Service, object):
+class CalVerifyService(WorkerService, object):
     """
     Base class for common service behaviors.
     """
 
     def __init__(self, store, options, output, reactor, config):
-        super(CalVerifyService, self).__init__()
-        self.store = store
+        super(CalVerifyService, self).__init__(store)
         self.options = options
         self.output = output
         self.reactor = reactor
@@ -409,32 +407,12 @@
         self.totalExceptions = None
 
 
-    def startService(self):
-        """
-        Start the service.
-        """
-        super(CalVerifyService, self).startService()
-        self.doCalVerify()
-
-
-    def stopService(self):
-        """
-        Stop the service.  Nothing to do; everything should be finished by this
-        time.
-        """
-        # TODO: stopping this service mid-export should really stop the export
-        # loop, but this is not implemented because nothing will actually do it
-        # except hitting ^C (which also calls reactor.stop(), so that will exit
-        # anyway).
-        pass
-
-
     def title(self):
         return ""
 
 
     @inlineCallbacks
-    def doCalVerify(self):
+    def doWork(self):
         """
         Do the operation stopping the reactor when done.
         """
@@ -444,11 +422,9 @@
             yield self.doAction()
             self.output.close()
         except:
-            log.failure("doCalVerify()")
+            log.failure("doWork()")
 
-        self.reactor.stop()
 
-
     def directoryService(self):
         """
         Get an appropriate directory service for this L{CalVerifyService}'s
@@ -2792,7 +2768,8 @@
         self.output.write("\n")
         self.output.write("Actual RECURRENCE-ID: %s.\n" % (rid,))
 
-        oldUID = yield calendarObj.split(rid=rid)
+        oldObj = yield calendarObj.split(rid=rid)
+        oldUID = oldObj.uid()
 
         self.output.write("\n")
         self.output.write("Split Resource: %s at %s, old UID: %s.\n" % (resid, rid, oldUID,))

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/cmdline.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/cmdline.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/cmdline.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -68,6 +68,13 @@
         will be imported and used.
     """
 
+    # We want to validate that the actual service is always an instance of WorkerService, so wrap the
+    # service maker callback inside a function that does that check
+    def _makeValidService(store):
+        service = serviceClass(store)
+        assert isinstance(service, WorkerService)
+        return service
+
     # Install std i/o observer
     if verbose:
         observer = StandardIOObserver()
@@ -83,7 +90,7 @@
         checkDirectories(config)
 
         config.ProcessType = "Utility"
-        config.UtilityServiceClass = serviceClass
+        config.UtilityServiceClass = _makeValidService
 
         autoDisableMemcached(config)
 

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/dbinspect.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/dbinspect.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/dbinspect.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -23,12 +23,11 @@
 """
 
 from calendarserver.tools import tables
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 from pycalendar.datetime import PyCalendarDateTime
 from twext.enterprise.dal.syntax import Select, Parameter, Count, Delete, \
     Constant
-from twisted.application.service import Service
-from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
 from twisted.python.filepath import FilePath
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options
@@ -851,14 +850,13 @@
 
 
 
-class DBInspectService(Service, object):
+class DBInspectService(WorkerService, object):
     """
     Service which runs, exports the appropriate records, then stops the reactor.
     """
 
     def __init__(self, store, options, reactor, config):
-        super(DBInspectService, self).__init__()
-        self.store = store
+        super(DBInspectService, self).__init__(store)
         self.options = options
         self.reactor = reactor
         self.config = config
@@ -866,11 +864,10 @@
         self.commandMap = {}
 
 
-    def startService(self):
+    def doWork(self):
         """
         Start the service.
         """
-        super(DBInspectService, self).startService()
 
         # Register commands
         self.registerCommand(TableSizes)
@@ -890,7 +887,16 @@
         self.registerCommand(EventsInTimerange)
         self.doDBInspect()
 
+        return succeed(None)
 
+
+    def postStartService(self):
+        """
+        Don't quit right away
+        """
+        pass
+
+
     def registerCommand(self, cmd):
         self.commands.append(cmd.name())
         self.commandMap[cmd.name()] = cmd

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/export.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/export.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/export.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -42,13 +42,12 @@
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options, UsageError
 from twisted.internet.defer import inlineCallbacks, returnValue
-from twisted.application.service import Service
 
 from twext.python.log import Logger
 from twistedcaldav.ical import Component
 
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 
 log = Logger()
 
@@ -285,14 +284,13 @@
 
 
 
-class ExporterService(Service, object):
+class ExporterService(WorkerService, object):
     """
     Service which runs, exports the appropriate records, then stops the reactor.
     """
 
     def __init__(self, store, options, output, reactor, config):
-        super(ExporterService, self).__init__()
-        self.store = store
+        super(ExporterService, self).__init__(store)
         self.options = options
         self.output = output
         self.reactor = reactor
@@ -300,16 +298,15 @@
         self._directory = self.store.directoryService()
 
 
-    def startService(self):
+    def postStartService(self):
         """
-        Start the service.
+        Don't quit right away
         """
-        super(ExporterService, self).startService()
-        self.doExport()
+        pass
 
 
     @inlineCallbacks
-    def doExport(self):
+    def doWork(self):
         """
         Do the export, stopping the reactor when done.
         """
@@ -326,7 +323,7 @@
             # update stuff needed to happen, don't want to undo it.
             self.output.close()
         except:
-            log.failure("doExport()")
+            log.failure("doWork()")
 
         self.reactor.stop()
 

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/migrate_verify.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/migrate_verify.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/migrate_verify.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -29,7 +29,6 @@
 import os
 import sys
 
-from twisted.application.service import Service
 from twisted.internet.defer import inlineCallbacks, returnValue
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options
@@ -37,7 +36,7 @@
 from twext.python.log import Logger
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
 
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 
 log = Logger()
 
@@ -118,14 +117,13 @@
 
 
 
-class MigrateVerifyService(Service, object):
+class MigrateVerifyService(WorkerService, object):
     """
     Service which runs, does its stuff, then stops the reactor.
     """
 
     def __init__(self, store, options, output, reactor, config):
-        super(MigrateVerifyService, self).__init__()
-        self.store = store
+        super(MigrateVerifyService, self).__init__(store)
         self.options = options
         self.output = output
         self.reactor = reactor
@@ -141,16 +139,15 @@
         self.missingResources = []
 
 
-    def startService(self):
+    def postStartService(self):
         """
-        Start the service.
+        Don't quit right away
         """
-        super(MigrateVerifyService, self).startService()
-        self.doMigrateVerify()
+        pass
 
 
     @inlineCallbacks
-    def doMigrateVerify(self):
+    def doWork(self):
         """
         Do the work, stopping the reactor when done.
         """
@@ -163,7 +160,7 @@
         except ConfigError:
             pass
         except:
-            log.failure("doMigrateVerify()")
+            log.failure("doWork()")
 
         self.reactor.stop()
 

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/obliterate.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/obliterate.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/obliterate.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -30,7 +30,6 @@
 import sys
 import time
 
-from twisted.application.service import Service
 from twisted.internet.defer import inlineCallbacks, returnValue
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options
@@ -42,7 +41,7 @@
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
 from txdav.common.datastore.sql_tables import schema, _BIND_MODE_OWN
 
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 
 log = Logger()
 
@@ -138,14 +137,13 @@
 
 
 
-class ObliterateService(Service, object):
+class ObliterateService(WorkerService, object):
     """
     Service which runs, does its stuff, then stops the reactor.
     """
 
     def __init__(self, store, options, output, reactor, config):
-        super(ObliterateService, self).__init__()
-        self.store = store
+        super(ObliterateService, self).__init__(store)
         self.options = options
         self.output = output
         self.reactor = reactor
@@ -159,16 +157,15 @@
         self.attachments = set()
 
 
-    def startService(self):
+    def postStartService(self):
         """
-        Start the service.
+        Don't quit right away
         """
-        super(ObliterateService, self).startService()
-        self.doObliterate()
+        pass
 
 
     @inlineCallbacks
-    def doObliterate(self):
+    def doWork(self):
         """
         Do the work, stopping the reactor when done.
         """
@@ -186,7 +183,7 @@
         except ConfigError:
             pass
         except:
-            log.failure("doObliterate()")
+            log.failure("doWork()")
 
         self.reactor.stop()
 

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/shell/terminal.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/shell/terminal.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/shell/terminal.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -38,12 +38,11 @@
 from twisted.python.failure import Failure
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options, UsageError
-from twisted.internet.defer import Deferred
+from twisted.internet.defer import Deferred, succeed
 from twisted.internet.defer import inlineCallbacks
 from twisted.internet.stdio import StandardIO
 from twisted.conch.recvline import HistoricRecvLine as ReceiveLineProtocol
 from twisted.conch.insults.insults import ServerProtocol
-from twisted.application.service import Service
 
 from twext.python.log import Logger
 
@@ -51,7 +50,7 @@
 
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
 
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 from calendarserver.tools.util import getDirectory
 from calendarserver.tools.shell.cmd import Commands, UsageError as CommandUsageError
 
@@ -95,7 +94,7 @@
 
 
 
-class ShellService(Service, object):
+class ShellService(WorkerService, object):
     """
     A L{ShellService} collects all the information that a shell needs to run;
     when run, it invokes the shell on stdin/stdout.
@@ -117,21 +116,19 @@
     """
 
     def __init__(self, store, directory, options, reactor, config):
-        super(ShellService, self).__init__()
-        self.store      = store
-        self.directory  = directory
-        self.options    = options
-        self.reactor    = reactor
-        self.config     = config
+        super(ShellService, self).__init__(store)
+        self.directory = directory
+        self.options = options
+        self.reactor = reactor
+        self.config = config
         self.terminalFD = None
-        self.protocol   = None
+        self.protocol = None
 
 
-    def startService(self):
+    def doWork(self):
         """
-        Start the service.
+        Service startup.
         """
-        super(ShellService, self).startService()
 
         # Set up the terminal for interactive action
         self.terminalFD = sys.__stdin__.fileno()
@@ -140,8 +137,16 @@
 
         self.protocol = ServerProtocol(lambda: ShellProtocol(self))
         StandardIO(self.protocol)
+        return succeed(None)
 
 
+    def postStartService(self):
+        """
+        Don't quit right away
+        """
+        pass
+
+
     def stopService(self):
         """
         Stop the service.
@@ -280,7 +285,7 @@
                 word = ""
             else:
                 word = tokens[-1]
-            cmd  = tokens.pop(0)
+            cmd = tokens.pop(0)
         else:
             word = cmd = ""
 

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/test/test_calverify.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/test/test_calverify.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/test/test_calverify.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -3066,8 +3066,7 @@
         }
         output = StringIO()
         calverify = EventSplitService(self._sqlCalendarStore, options, output, reactor, config)
-        oldObj = yield calverify.doAction()
-        oldUID = oldObj.uid()
+        oldUID = yield calverify.doAction()
 
         relsubs = dict(self.subs)
         relsubs["relID"] = oldUID

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/upgrade.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/upgrade.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/upgrade.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 ##
+from twisted.internet.defer import succeed
 
 """
 This tool allows any necessary upgrade to complete, then exits.
@@ -31,9 +32,8 @@
 from twext.python.log import Logger, LogLevel, formatEvent, addObserver
 
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
-from twisted.application.service import Service
 
-from calendarserver.tools.cmdline import utilityMain
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 from calendarserver.tap.caldav import CalDAVServiceMaker
 
 log = Logger()
@@ -122,7 +122,7 @@
 
 
 
-class UpgraderService(Service, object):
+class UpgraderService(WorkerService, object):
     """
     Service which runs, exports the appropriate records, then stops the reactor.
     """
@@ -130,8 +130,7 @@
     started = False
 
     def __init__(self, store, options, output, reactor, config):
-        super(UpgraderService, self).__init__()
-        self.store = store
+        super(UpgraderService, self).__init__(store)
         self.options = options
         self.output = output
         self.reactor = reactor
@@ -139,7 +138,7 @@
         self._directory = None
 
 
-    def startService(self):
+    def doWork(self):
         """
         Immediately stop.  The upgrade will have been run before this.
         """
@@ -155,7 +154,13 @@
             else:
                 self.output.write("Upgrade complete, shutting down.\n")
         UpgraderService.started = True
+        return succeed(None)
 
+
+    def postStartService(self):
+        """
+        Quit right away
+        """
         from twisted.internet import reactor
         from twisted.internet.error import ReactorNotRunning
         try:

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/validcalendardata.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/validcalendardata.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/validcalendardata.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -15,14 +15,14 @@
 # limitations under the License.
 ##
 from __future__ import print_function
+from twisted.internet.defer import succeed
 
 """
 This tool takes data from stdin and validates it as iCalendar data suitable
 for the server.
 """
 
-from calendarserver.tools.cmdline import utilityMain
-from twisted.application.service import Service
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options
 from twistedcaldav.config import config
@@ -119,14 +119,13 @@
 
 errorPrefix = "Calendar data had unfixable problems:\n  "
 
-class ValidService(Service, object):
+class ValidService(WorkerService, object):
     """
     Service which runs, exports the appropriate records, then stops the reactor.
     """
 
     def __init__(self, store, options, output, input, reactor, config):
-        super(ValidService, self).__init__()
-        self.store = store
+        super(ValidService, self).__init__(store)
         self.options = options
         self.output = output
         self.input = input
@@ -135,7 +134,7 @@
         self._directory = None
 
 
-    def startService(self):
+    def doWork(self):
         """
         Start the service.
         """
@@ -149,7 +148,7 @@
             print("Calendar data OK")
         else:
             print(message)
-        self.reactor.stop()
+        return succeed(None)
 
 
     def parseCalendarData(self):

Modified: CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/workitems.py
===================================================================
--- CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/workitems.py	2014-01-16 02:21:30 UTC (rev 12356)
+++ CalendarServer/branches/release/CalendarServer-5.2-dev/calendarserver/tools/workitems.py	2014-01-16 15:36:02 UTC (rev 12357)
@@ -23,9 +23,8 @@
 import curses
 import datetime
 
-from twisted.internet.defer import inlineCallbacks
-from calendarserver.tools.cmdline import utilityMain
-from twisted.application.service import Service
+from twisted.internet.defer import inlineCallbacks, succeed
+from calendarserver.tools.cmdline import utilityMain, WorkerService
 from calendarserver.push.notifier import PushNotificationWork
 from twistedcaldav.directory.directory import GroupCacherPollingWork
 from txdav.caldav.datastore.scheduling.imip.inbound import IMIPPollingWork, IMIPReplyWork
@@ -86,21 +85,29 @@
 
 
 
-class WorkItemMonitorService(Service):
+class WorkItemMonitorService(WorkerService, object):
 
     def __init__(self, store):
-        self.store = store
+        super(WorkItemMonitorService, self).__init__(store)
         from twisted.internet import reactor
         self.reactor = reactor
 
 
-    def startService(self):
+    def doWork(self):
         self.screen = curses.initscr()
         self.windows = []
         self.updateScreenGeometry()
         self.reactor.callLater(0, self.updateDisplay)
+        return succeed(None)
 
 
+    def postStartService(self):
+        """
+        Don't quit right away
+        """
+        pass
+
+
     def updateScreenGeometry(self):
         for win in self.windows:
             del win
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/afe5806b/attachment.html>


More information about the calendarserver-changes mailing list