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

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


Revision: 7474
          http://trac.macosforge.org/projects/calendarserver/changeset/7474
Author:   glyph at apple.com
Date:     2011-05-16 07:46:01 -0700 (Mon, 16 May 2011)
Log Message:
-----------
First test to actually call utilityMain.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py
    CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py

Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py	2011-05-16 14:45:49 UTC (rev 7473)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py	2011-05-16 14:46:01 UTC (rev 7474)
@@ -50,10 +50,11 @@
 from twistedcaldav.resource import isCalendarCollectionResource,\
     CalendarHomeResource
 from twistedcaldav.directory.directory import DirectoryService
+from twistedcaldav.stdconfig import DEFAULT_CARDDAV_CONFIG_FILE
+from calendarserver.tools.cmdline import utilityMain
+from twisted.application.service import Service
 
-from calendarserver.tools.util import UsageError
-from calendarserver.tools.util import (
-    loadConfig, getDirectory, dummyDirectoryRecord, autoDisableMemcached)
+from calendarserver.tools.util import (loadConfig, getDirectory)
 
 def usage(e=None):
     if e:
@@ -79,6 +80,9 @@
         parsing --record and --collection options.
     """
 
+    optParameters = [['config', 'c', DEFAULT_CARDDAV_CONFIG_FILE,
+                      "Specify caldavd.plist configuration path."]]
+
     def __init__(self):
         super(ExportOptions, self).__init__()
         self.exporters = []
@@ -235,7 +239,7 @@
         elif opt in ("-H", "--home"):
             path = abspath(arg)
             parent = CalDAVFile(dirname(abspath(path)))
-            calendarHome = CalendarHomeResource(arg, parent, dummyDirectoryRecord)
+            calendarHome = CalendarHomeResource(arg, parent, None)
             checkExists(calendarHome)
             calendarHomes.add(calendarHome)
 
@@ -260,7 +264,6 @@
         try:
             config = loadConfig(configFileName)
             config.directory = getDirectory()
-            autoDisableMemcached(config)
         except ConfigurationError, e:
             sys.stdout.write("%s\n" % (e,))
             sys.exit(1)
@@ -279,33 +282,59 @@
             if isCalendarCollectionResource(child):
                 collections.add(child)
 
-    try:
-        calendar = Component.newCalendar()
+    calendar = Component.newCalendar()
 
-        uids  = set()
+    uids  = set()
 
-        for collection in collections:
-            for name, uid, type in collection.index().indexedSearch(None):
-                child = collection.getChild(name)
+    for collection in collections:
+        for name, uid, type in collection.index().indexedSearch(None):
+            child = collection.getChild(name)
 
-                if uid in uids:
-                    sys.stderr.write("Skipping duplicate event UID %r from %s\n" % (uid, collection.fp.path))
-                    continue
-                else:
-                    uids.add(uid)
+            if uid in uids:
+                sys.stderr.write("Skipping duplicate event UID %r from %s\n" % (uid, collection.fp.path))
+                continue
+            else:
+                uids.add(uid)
 
-        calendarData = str(calendar)
-        output = sys.stdout
-        output.write(calendarData)
-    except UsageError, e:
-        usage(e)
+    calendarData = str(calendar)
+    output = sys.stdout
+    output.write(calendarData)
 
 
 
-def main(argv=sys.argv, stderr=sys.stderr):
+class ExporterService(Service, object):
     """
+    Service which runs, exports the appropriate records, then stops the reactor.
+    """
+
+    def __init__(self):
+        super(ExporterService, self).__init__()
+
+
+    def startService(self):
+        """
+        Start the service.
+        """
+        super(ExporterService, self).startService()
+
+
+    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).
+
+
+def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
+    """
     Do the export.
     """
+    if reactor is None:
+        from twisted.internet import reactor
     options = ExportOptions()
     options.parseOptions(argv[1:])
     try:
@@ -314,6 +343,7 @@
         stderr.write("Unable to open output file for writing: %s\n" %
                      (e))
         sys.exit(1)
+    utilityMain(options['config'], lambda store: Service(), reactor)
     output #pyflakes
 
 

Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py	2011-05-16 14:45:49 UTC (rev 7473)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py	2011-05-16 14:46:01 UTC (rev 7474)
@@ -31,10 +31,12 @@
 from twext.enterprise.ienterprise import AlreadyFinishedError
 
 from twistedcaldav.ical import Component
+from calendarserver.tools import export
 from calendarserver.tools.export import ExportOptions, main
 from calendarserver.tools.export import HomeExporter
 from twistedcaldav.datafilters.test.test_peruserdata import dataForTwoUsers
 from twistedcaldav.datafilters.test.test_peruserdata import resultForUser2
+from twisted.internet.defer import Deferred
 from txdav.common.datastore.test.util import buildStore
 from txdav.common.datastore.test.util import populateCalendarsFrom
 
@@ -162,8 +164,6 @@
     Tests for exporting data from a live store.
     """
 
-    fakeConfigFile = 'not-a-real-config-file.plist'
-
     @inlineCallbacks
     def setUp(self):
         """
@@ -174,23 +174,46 @@
         L{utilityMain}.
         """
         self.mainCalled = False
-        #self.patch(export, "utilityMain", self.fakeUtilityMain)
+        self.patch(export, "utilityMain", self.fakeUtilityMain)
         self.store = yield buildStore(self, None)
+        self.waitToStop = Deferred()
 
 
+    def stop(self):
+        """
+        Emulate reactor.stop(), which the service must call when it is done with
+        work.
+        """
+        self.waitToStop.callback(None)
+
+
     def fakeUtilityMain(self, configFileName, serviceClass, reactor=None):
         """
         Verify a few basic things.
         """
         if self.mainCalled:
             raise RuntimeError(
-                "Main called twice this test; duplicate reactor run.")
+                "Main called twice during this test; duplicate reactor run.")
         self.mainCalled = True
-        self.assertEquals(configFileName, self.fakeConfigFile)
-        theService = serviceClass(self.store)
-        theService.startService()
+        self.usedConfigFile = configFileName
+        self.usedReactor = reactor
+        self.exportService = serviceClass(self.store)
+        self.exportService.startService()
+        self.addCleanup(self.exportService.stopService)
 
 
+    def test_configFile(self):
+        """
+        export.main() invokes utilityMain with the configuration file specified
+        on the command line.
+        """
+        tempConfig = self.mktemp()
+        main(['calendarserver_export', '--config', tempConfig], reactor=self)
+        self.assertEquals(self.mainCalled, True, "Main not called.")
+        self.assertEquals(self.usedConfigFile, tempConfig)
+        self.assertEquals(self.usedReactor, self)
+
+
     @inlineCallbacks
     def test_emptyCalendar(self):
         """
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110516/e0f57a4a/attachment-0001.html>


More information about the calendarserver-changes mailing list