[CalendarServer-changes] [5098] CalendarServer/trunk/calendarserver/tools/gateway.py

source_changes at macosforge.org source_changes at macosforge.org
Fri Feb 12 09:47:52 PST 2010


Revision: 5098
          http://trac.macosforge.org/projects/calendarserver/changeset/5098
Author:   sagen at apple.com
Date:     2010-02-12 09:47:49 -0800 (Fri, 12 Feb 2010)
Log Message:
-----------
calendarserver_command_gateway now doesn't hang when it encounters an error

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/gateway.py

Modified: CalendarServer/trunk/calendarserver/tools/gateway.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/gateway.py	2010-02-12 17:27:56 UTC (rev 5097)
+++ CalendarServer/trunk/calendarserver/tools/gateway.py	2010-02-12 17:47:49 UTC (rev 5098)
@@ -93,11 +93,13 @@
         try:
             config.directory = getDirectory()
         except DirectoryError, e:
-            abort(e)
+            respondWithError(str(e))
+            return
         setupMemcached(config)
         setupNotifications(config)
     except ConfigurationError, e:
-        abort(e)
+        respondWithError(e)
+        return
 
     #
     # Read commands from stdin
@@ -106,7 +108,8 @@
     try:
         plist = plistlib.readPlistFromString(rawInput)
     except xml.parsers.expat.ExpatError, e:
-        abort(str(e))
+        respondWithError(str(e))
+        return
 
     # If the plist is an array, each element of the array is a separate
     # command dictionary.
@@ -116,7 +119,8 @@
         commands = [plist]
 
     runner = Runner(config.directory, commands)
-    runner.validate()
+    if not runner.validate():
+        return
 
     #
     # Start the reactor
@@ -153,24 +157,32 @@
         # Make sure commands are valid
         for command in self.commands:
             if not command.has_key('command'):
-                abort("'command' missing from plist")
+                respondWithError("'command' missing from plist")
+                return False
             commandName = command['command']
             methodName = "command_%s" % (commandName,)
             if not hasattr(self, methodName):
-                abort("Unknown command '%s'" % (commandName,))
+                respondWithError("Unknown command '%s'" % (commandName,))
+                return False
+        return True
 
     @inlineCallbacks
     def run(self):
-        for command in self.commands:
-            commandName = command['command']
-            methodName = "command_%s" % (commandName,)
-            if hasattr(self, methodName):
-                (yield getattr(self, methodName)(command))
-            else:
-                abort("Unknown command '%s'" % (commandName,))
+        try:
+            for command in self.commands:
+                commandName = command['command']
+                methodName = "command_%s" % (commandName,)
+                if hasattr(self, methodName):
+                    (yield getattr(self, methodName)(command))
+                else:
+                    respondWithError("Unknown command '%s'" % (commandName,))
 
-        reactor.stop()
+        except Exception, e:
+            respondWithError("Command failed: '%s'" % (str(e),))
 
+        finally:
+            reactor.stop()
+
     # Locations
 
     def command_getLocationList(self, command):
@@ -185,7 +197,8 @@
         try:
             self.dir.createRecord("locations", **kwargs)
         except DirectoryError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         respondWithRecordsOfType(self.dir, command, "locations")
 
     def command_getLocationAttributes(self, command):
@@ -205,7 +218,8 @@
         try:
             self.dir.updateRecord("locations", **kwargs)
         except DirectoryError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
 
         # principal = principalForPrincipalID(command['GeneratedUID'],
         #     directory=self.dir)
@@ -221,7 +235,8 @@
         try:
             self.dir.destroyRecord("locations", **kwargs)
         except DirectoryError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         respondWithRecordsOfType(self.dir, command, "locations")
 
     # Resources
@@ -237,7 +252,8 @@
         try:
             self.dir.createRecord("resources", **kwargs)
         except DirectoryError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         respondWithRecordsOfType(self.dir, command, "resources")
 
     def command_getResourceAttributes(self, command):
@@ -257,7 +273,8 @@
         try:
             self.dir.updateRecord("resources", **kwargs)
         except DirectoryError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
 
         # principal = principalForPrincipalID(command['GeneratedUID'],
         #     directory=self.dir)
@@ -273,7 +290,8 @@
         try:
             self.dir.destroyRecord("resources", **kwargs)
         except DirectoryError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         respondWithRecordsOfType(self.dir, command, "resources")
 
     # Proxies
@@ -290,7 +308,8 @@
         try:
             (yield addProxy(principal, "write", proxy))
         except ProxyError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         except ProxyWarning, e:
             pass
         (yield respondWithProxies(self.dir, command, principal, "write"))
@@ -302,7 +321,8 @@
         try:
             (yield removeProxy(principal, proxy, proxyTypes=("write",)))
         except ProxyError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         except ProxyWarning, e:
             pass
         (yield respondWithProxies(self.dir, command, principal, "write"))
@@ -319,7 +339,8 @@
         try:
             (yield addProxy(principal, "read", proxy))
         except ProxyError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         except ProxyWarning, e:
             pass
         (yield respondWithProxies(self.dir, command, principal, "read"))
@@ -331,7 +352,8 @@
         try:
             (yield removeProxy(principal, proxy, proxyTypes=("read",)))
         except ProxyError, e:
-            abort(str(e))
+            respondWithError(str(e))
+            return
         except ProxyWarning, e:
             pass
         (yield respondWithProxies(self.dir, command, principal, "read"))
@@ -376,13 +398,15 @@
 def respond(command, result):
     sys.stdout.write(plistlib.writePlistToString( { 'command' : command['command'], 'result' : result } ) )
 
-def abort(msg, status=1):
+def respondWithError(msg, status=1):
     sys.stdout.write(plistlib.writePlistToString( { 'error' : msg, } ) )
+    """
     try:
         reactor.stop()
     except RuntimeError:
         pass
     sys.exit(status)
+    """
 
 if __name__ == "__main__":
     main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100212/7398a76e/attachment-0001.html>


More information about the calendarserver-changes mailing list