[CalendarServer-changes] [7101] CalendarServer/trunk/calendarserver/tools/purge.py

source_changes at macosforge.org source_changes at macosforge.org
Fri Mar 4 14:43:27 PST 2011


Revision: 7101
          http://trac.macosforge.org/projects/calendarserver/changeset/7101
Author:   glyph at apple.com
Date:     2011-03-04 14:43:25 -0800 (Fri, 04 Mar 2011)
Log Message:
-----------
Emit a coherent message suggesting a useful course of action, rather than a traceback, when a purge tool is run with certain known configuration errors.

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

Modified: CalendarServer/trunk/calendarserver/tools/purge.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/purge.py	2011-03-04 18:30:50 UTC (rev 7100)
+++ CalendarServer/trunk/calendarserver/tools/purge.py	2011-03-04 22:43:25 UTC (rev 7101)
@@ -18,6 +18,7 @@
 
 import os
 import sys
+from errno import ENOENT, EACCES
 
 from datetime import date, timedelta, datetime
 from getopt import getopt, GetoptError
@@ -111,83 +112,98 @@
         sys.exit(0)
 
 
-class PurgeOldEventsService(Service):
+class WorkerService(Service):
 
-    cutoff = None
-    batchSize = None
-    dryrun = False
-    verbose = False
-
     def __init__(self, store):
         self._store = store
 
+    def rootResource(self):
+        try:
+            rootResource = getRootResource(config, self._store)
+        except OSError, e:
+            if e.errno == ENOENT:
+                # Trying to re-write resources.xml but its parent directory does
+                # not exist.  The server's never been started, so we're missing
+                # state required to do any work.  (Plus, what would be the point
+                # of purging stuff from a server that's completely empty?)
+                raise ConfigurationError(
+                    "It appears that the server has never been started.\n"
+                    "Please start it at least once before purging anything.")
+            elif e.errno == EACCES:
+                # Trying to re-write resources.xml but it is not writable by the
+                # current user.  This most likely means we're in a system
+                # configuration and the user doesn't have sufficient privileges
+                # to do the other things the tool might need to do either.
+                raise ConfigurationError("You must run this tool as root.")
+            else:
+                raise
+        return rootResource
+
+
     @inlineCallbacks
     def startService(self):
         try:
-            rootResource = getRootResource(config, self._store)
-            directory = rootResource.getDirectory()
-            (yield purgeOldEvents(self._store, directory, rootResource,
-                self.cutoff, self.batchSize, verbose=self.verbose,
-                dryrun=self.dryrun))
+            yield self.doWork()
+        except ConfigurationError, ce:
+            sys.stderr.write("Error: %s\n" % (str(ce),))
         except Exception, e:
             sys.stderr.write("Error: %s\n" % (e,))
             raise
-
         finally:
             reactor.stop()
 
 
-class PurgeOrphanedAttachmentsService(Service):
 
+class PurgeOldEventsService(WorkerService):
+
+    cutoff = None
     batchSize = None
     dryrun = False
     verbose = False
 
-    def __init__(self, store):
-        self._store = store
+    def doWork(self):
+        rootResource = self.rootResource()
+        directory = rootResource.getDirectory()
+        return purgeOldEvents(self._store, directory, rootResource,
+            self.cutoff, self.batchSize, verbose=self.verbose,
+            dryrun=self.dryrun)
 
-    @inlineCallbacks
-    def startService(self):
-        try:
-            (yield purgeOrphanedAttachments(self._store, self.batchSize,
-                verbose=self.verbose, dryrun=self.dryrun))
-        except Exception, e:
-            sys.stderr.write("Error: %s\n" % (e,))
-            raise
 
-        finally:
-            reactor.stop()
 
+class PurgeOrphanedAttachmentsService(WorkerService):
 
-class PurgePrincipalService(Service):
+    batchSize = None
+    dryrun = False
+    verbose = False
 
+    def doWork(self):
+        return purgeOrphanedAttachments(
+            self._store, self.batchSize,
+            verbose=self.verbose, dryrun=self.dryrun)
+
+
+
+class PurgePrincipalService(WorkerService):
+
     guids = None
     dryrun = False
     verbose = False
 
-    def __init__(self, store):
-        self._store = store
-
     @inlineCallbacks
-    def startService(self):
-        try:
-            rootResource = getRootResource(config, self._store)
-            directory = rootResource.getDirectory()
-            total = (yield purgeGUIDs(directory, rootResource, self.guids,
-                verbose=self.verbose, dryrun=self.dryrun))
-            if self.verbose:
-                amount = "%d event%s" % (total, "s" if total > 1 else "")
-                if self.dryrun:
-                    print "Would have modified or deleted %s" % (amount,)
-                else:
-                    print "Modified or deleted %s" % (amount,)
-        except Exception, e:
-            sys.stderr.write("Error: %s\n" % (e,))
-            raise
-        finally:
-            reactor.stop()
+    def doWork(self):
+        rootResource = self.rootResource()
+        directory = rootResource.getDirectory()
+        total = (yield purgeGUIDs(directory, rootResource, self.guids,
+            verbose=self.verbose, dryrun=self.dryrun))
+        if self.verbose:
+            amount = "%d event%s" % (total, "s" if total > 1 else "")
+            if self.dryrun:
+                print "Would have modified or deleted %s" % (amount,)
+            else:
+                print "Modified or deleted %s" % (amount,)
 
 
+
 def shared_main(configFileName, serviceClass):
 
     try:
@@ -394,20 +410,6 @@
 
 
 @inlineCallbacks
-def callThenStop(method, *args, **kwds):
-    try:
-        count = (yield method(*args, **kwds))
-        if kwds.get("dryrun", False):
-            print "Would have purged %d events" % (count,)
-        else:
-            print "Purged %d events" % (count,)
-    except Exception, e:
-        sys.stderr.write("Error: %s\n" % (e,))
-    finally:
-        reactor.stop()
-
-
- at inlineCallbacks
 def purgeOldEvents(store, directory, root, date, batchSize, verbose=False,
     dryrun=False):
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110304/27fe8c29/attachment.html>


More information about the calendarserver-changes mailing list