[CalendarServer-changes] [4632] CalendarServer/branches/users/cdaboo/deployment-partition-4593

source_changes at macosforge.org source_changes at macosforge.org
Thu Oct 22 09:17:47 PDT 2009


Revision: 4632
          http://trac.macosforge.org/projects/calendarserver/changeset/4632
Author:   cdaboo at apple.com
Date:     2009-10-22 09:17:44 -0700 (Thu, 22 Oct 2009)
Log Message:
-----------
Add an option to merge changes from the XML file to the database. Also an option to remove everything
from the database.

Modified Paths:
--------------
    CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/loadaugmentdb.py
    CalendarServer/branches/users/cdaboo/deployment-partition-4593/twistedcaldav/directory/augment.py

Modified: CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/loadaugmentdb.py
===================================================================
--- CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/loadaugmentdb.py	2009-10-22 10:08:09 UTC (rev 4631)
+++ CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/loadaugmentdb.py	2009-10-22 16:17:44 UTC (rev 4632)
@@ -78,6 +78,7 @@
     print "  -h --help: print this help and exit"
     print "  -f --config: Specify caldavd.plist configuration path"
     print "  -x --xmlfile: Specify xml augments file path"
+    print "  -r --remove: Remove all entries from the database"
 
     if e:
         sys.exit(64)
@@ -87,8 +88,9 @@
 def main():
     try:
         (optargs, args) = getopt(
-            sys.argv[1:], "hf:x:", [
+            sys.argv[1:], "hf:rx:", [
                 "config=",
+                "remove",
                 "xmlfile=",
                 "help",
             ],
@@ -98,6 +100,7 @@
 
     configFileName = None
     xmlFileName = None
+    remove = False
 
     for opt, arg in optargs:
         if opt in ("-h", "--help"):
@@ -106,6 +109,11 @@
         elif opt in ("-f", "--config"):
             configFileName = arg
 
+        elif opt in ("-r", "--remove"):
+            remove = True
+            if raw_input("Do you really want to remove all records from the database? [y/n] ") != "y":
+                sys.exit(0)
+
         elif opt in ("-x", "--xmlfile"):
             xmlFileName = arg
 
@@ -136,7 +144,7 @@
         usage("Unable to start: %s" % (e,))
 
     try:
-        dbxml = AugmentXMLDB((xmlFileName,))
+        dbxml = AugmentXMLDB((xmlFileName,)) if not remove else None
     except IOError, e:
         usage("Could not read XML augment file: %s" % (e,))
 
@@ -150,9 +158,28 @@
 def run(dbxml):
     
     try:
-        yield augment.AugmentService.clean()
-        for record in dbxml.db.values():
-            yield augment.AugmentService.addAugmentRecord(record)
+        guids = set((yield augment.AugmentService.getAllGUIDs()))
+        added = 0
+        updated = 0
+        removed = 0
+        if dbxml:
+            for record in dbxml.db.values():
+                yield augment.AugmentService.addAugmentRecord(record, record.guid in guids)
+                if record.guid in guids:
+                    updated += 1
+                else:
+                    added += 1
+            for guid in guids.difference(dbxml.db.keys()):
+                yield augment.AugmentService.removeAugmentRecord(guid)
+                removed += 1
+        else:
+            yield augment.AugmentService.clean()
+            removed = len(guids)
+            
+        print "Changes:"
+        print "  Added: %d" % (added,)
+        print "  Changed: %d" % (updated,)
+        print "  Removed: %d" % (removed,)
     finally:
         #
         # Stop the reactor

Modified: CalendarServer/branches/users/cdaboo/deployment-partition-4593/twistedcaldav/directory/augment.py
===================================================================
--- CalendarServer/branches/users/cdaboo/deployment-partition-4593/twistedcaldav/directory/augment.py	2009-10-22 10:08:09 UTC (rev 4631)
+++ CalendarServer/branches/users/cdaboo/deployment-partition-4593/twistedcaldav/directory/augment.py	2009-10-22 16:17:44 UTC (rev 4632)
@@ -75,6 +75,16 @@
                 result.guid = guid
         returnValue(result)
 
+    @inlineCallbacks
+    def getAllGUIDs(self):
+        """
+        Get all AugmentRecord GUIDs.
+
+        @return: L{Deferred}
+        """
+        
+        raise NotImplementedError("Child class must define this.")
+
     def _lookupAugmentRecord(self, guid):
         """
         Get an AugmentRecord for the specified GUID.
@@ -116,6 +126,16 @@
             
         self.lastCached = time.time()
 
+    @inlineCallbacks
+    def getAllGUIDs(self):
+        """
+        Get all AugmentRecord GUIDs.
+
+        @return: L{Deferred}
+        """
+        
+        return succeed(self.db.keys())
+
     def _lookupAugmentRecord(self, guid):
         """
         Get an AugmentRecord for the specified GUID.
@@ -169,6 +189,18 @@
         AbstractADBAPIDatabase.__init__(self, dbID, dbapiName, dbapiArgs, True, **kwargs)
         
     @inlineCallbacks
+    def getAllGUIDs(self):
+        """
+        Get all AugmentRecord GUIDs.
+
+        @return: L{Deferred}
+        """
+        
+        # Query for the record information
+        results = (yield self.queryList("select GUID from AUGMENTS", ()))
+        returnValue(results)
+
+    @inlineCallbacks
     def _lookupAugmentRecord(self, guid):
         """
         Get an AugmentRecord for the specified GUID.
@@ -198,24 +230,44 @@
             returnValue(record)
 
     @inlineCallbacks
-    def addAugmentRecord(self, record):
+    def addAugmentRecord(self, record, update=False):
 
         partitionid = (yield self._getPartitionID(record.hostedAt))
         cuaddrs = "\t".join(record.calendarUserAddresses)
         
-        yield self.execute(
-            """insert into AUGMENTS
-            (GUID, ENABLED, PARTITIONID, CALENDARING, AUTOSCHEDULE, CUADDRS)
-            values (:1, :2, :3, :4, :5, :6)""",
-            (
-                record.guid,
-                "T" if record.enabled else "F",
-                partitionid,
-                "T" if record.enabledForCalendaring else "F",
-                "T" if record.autoSchedule else "F",
-                cuaddrs,)
-        )
+        if update:
+            yield self.execute(
+                """update AUGMENTS set
+                   (GUID, ENABLED, PARTITIONID, CALENDARING, AUTOSCHEDULE, CUADDRS) =
+                   (:1, :2, :3, :4, :5, :6) where GUID = :7""",
+                (
+                    record.guid,
+                    "T" if record.enabled else "F",
+                    partitionid,
+                    "T" if record.enabledForCalendaring else "F",
+                    "T" if record.autoSchedule else "F",
+                    cuaddrs,
+                    record.guid,
+                )
+            )
+        else:
+            yield self.execute(
+                """insert into AUGMENTS
+                   (GUID, ENABLED, PARTITIONID, CALENDARING, AUTOSCHEDULE, CUADDRS)
+                   values (:1, :2, :3, :4, :5, :6)""",
+                (
+                    record.guid,
+                    "T" if record.enabled else "F",
+                    partitionid,
+                    "T" if record.enabledForCalendaring else "F",
+                    "T" if record.autoSchedule else "F",
+                    cuaddrs,)
+            )
 
+    def removeAugmentRecord(self, guid):
+
+        return self.query("delete from AUGMENTS where GUID = :1", (guid,))
+
     @inlineCallbacks
     def _getPartitionID(self, hostedat, createIfMissing=True):
         
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20091022/55da018a/attachment.html>


More information about the calendarserver-changes mailing list