[CalendarServer-changes] [13405] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri May 2 09:51:33 PDT 2014


Revision: 13405
          http://trac.calendarserver.org//changeset/13405
Author:   sagen at apple.com
Date:     2014-05-02 09:51:33 -0700 (Fri, 02 May 2014)
Log Message:
-----------
Migrating locations and resources works with twext.who; adds InMemoryDirectoryService

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/resources.py
    CalendarServer/trunk/calendarserver/tools/test/test_resources.py
    CalendarServer/trunk/twistedcaldav/test/test_upgrade.py
    CalendarServer/trunk/twistedcaldav/upgrade.py
    CalendarServer/trunk/txdav/who/test/test_util.py
    CalendarServer/trunk/txdav/who/util.py

Modified: CalendarServer/trunk/calendarserver/tools/resources.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/resources.py	2014-05-02 15:44:31 UTC (rev 13404)
+++ CalendarServer/trunk/calendarserver/tools/resources.py	2014-05-02 16:51:33 UTC (rev 13405)
@@ -36,10 +36,10 @@
 from twisted.internet.defer import inlineCallbacks
 from twisted.python.util import switchUID
 from twistedcaldav.config import config, ConfigurationError
-from twistedcaldav.directory.appleopendirectory import OpenDirectoryService
-from twistedcaldav.directory.directory import DirectoryService, DirectoryError
-from twistedcaldav.directory.xmlfile import XMLDirectoryService
 from txdav.who.util import directoryFromConfig
+from twext.who.opendirectory import (
+    DirectoryService as OpenDirectoryService
+)
 
 log = Logger()
 
@@ -142,131 +142,69 @@
         # hierarchy
         setupMemcached(config)
 
-        try:
-            config.directory = directoryFromConfig(config)
-        except DirectoryError, e:
-            abort(e)
+        config.directory = directoryFromConfig(config)
 
     except ConfigurationError, e:
         abort(e)
 
-    # FIXME: this all has to change:
-    # Find the opendirectory service
-    userService = config.directory.serviceForRecordType("users")
-    resourceService = config.directory.serviceForRecordType("resources")
-    if (
-        not isinstance(userService, OpenDirectoryService) or
-        not isinstance(resourceService, XMLDirectoryService)
-    ):
-        abort(
-            "This script only migrates resources and locations from "
-            "OpenDirectory to XML; this calendar server does not have such a "
-            "configuration."
-        )
+    sourceService = OpenDirectoryService()
+    destService = config.directory
 
     #
     # Start the reactor
     #
     reactor.callLater(
-        0, migrate, userService, resourceService, verbose=verbose
+        0, migrate, sourceService, destService, verbose=verbose
     )
     reactor.run()
 
 
 
 @inlineCallbacks
-def migrate(sourceService, resourceService, verbose=False):
+def migrate(sourceService, destService, verbose=False):
     """
     Simply a wrapper around migrateResources in order to stop the reactor
     """
 
     try:
-        yield migrateResources(sourceService, resourceService, verbose=verbose)
+        yield migrateResources(sourceService, destService, verbose=verbose)
     finally:
         reactor.stop()
 
 
 
-def queryForType(sourceService, recordType, verbose=False):
-    """
-    Queries OD for all records of the specified record type
-    """
 
-    attrs = [
-        "dsAttrTypeStandard:GeneratedUID",
-        "dsAttrTypeStandard:RealName",
-    ]
 
-    if verbose:
-        print("Querying for all %s records" % (recordType,))
 
-    results = list(sourceService.odModule.listAllRecordsWithAttributes_list(
-        sourceService.directory,
-        recordType,
-        attrs,
-    ))
-
-    if verbose:
-        print("Found %d records" % (len(results),))
-
-    return results
-
-
-
 @inlineCallbacks
-def migrateResources(
-    sourceService, destService, autoSchedules=None,
-    queryMethod=queryForType, verbose=False
-):
+def migrateResources(sourceService, destService, verbose=False):
 
-    directoryRecords = []
-    augmentRecords = []
+    destRecords = []
 
-    for recordTypeOD, recordType in (
-        ("dsRecTypeStandard:Resources", DirectoryService.recordType_resources),
-        ("dsRecTypeStandard:Places", DirectoryService.recordType_locations),
+    for recordType in (
+        sourceService.recordType.resource,
+        sourceService.recordType.location,
     ):
-        data = queryMethod(sourceService, recordTypeOD, verbose=verbose)
-        for recordName, val in data:
-            guid = val.get("dsAttrTypeStandard:GeneratedUID", None)
-            fullName = val.get("dsAttrTypeStandard:RealName", None)
-            if guid and fullName:
-                if not recordName:
-                    recordName = guid
-                record = yield destService.recordWithGUID(guid)
-                if record is None:
-                    if verbose:
-                        print("Migrating %s (%s)" % (fullName, recordType))
-
-                    if autoSchedules is not None:
-                        autoSchedule = autoSchedules.get(guid, 1)
-                    else:
-                        autoSchedule = True
-                    augmentRecord = (
-                        yield destService.augmentService.getAugmentRecord(
-                            guid, recordType
+        records = yield sourceService.recordsWithRecordType(recordType)
+        for sourceRecord in records:
+            destRecord = yield destService.recordWithUID(sourceRecord.uid)
+            if destRecord is None:
+                if verbose:
+                    print(
+                        "Migrating {name} {recordType} {uid}".format(
+                            name=sourceRecord.displayName,
+                            recordType=recordType.name,
+                            uid=sourceRecord.uid
                         )
                     )
-                    if autoSchedule:
-                        augmentRecord.autoScheduleMode = "automatic"
-                    else:
-                        augmentRecord.autoScheduleMode = "none"
-                    augmentRecords.append(augmentRecord)
+                destRecord = type(sourceRecord)(destService, sourceRecord.fields.copy())
+                destRecords.append(destRecord)
 
-                    directoryRecords.append((
-                        recordType,
-                        {
-                            "guid": guid,
-                            "shortNames": [recordName],
-                            "fullName": fullName,
-                        }
-                    ))
+    if destRecords:
+        yield destService.updateRecords(destRecords, create=True)
 
-    destService.createRecords(directoryRecords)
 
-    yield destService.augmentService.addAugmentRecords(augmentRecords)
 
 
-
 if __name__ == "__main__":
     main()

Modified: CalendarServer/trunk/calendarserver/tools/test/test_resources.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/test/test_resources.py	2014-05-02 15:44:31 UTC (rev 13404)
+++ CalendarServer/trunk/calendarserver/tools/test/test_resources.py	2014-05-02 16:51:33 UTC (rev 13405)
@@ -15,176 +15,110 @@
 ##
 
 
-try:
-    from calendarserver.tools.resources import migrateResources
-    from twisted.internet.defer import inlineCallbacks, succeed
-    from twistedcaldav.directory.directory import DirectoryService
-    from twistedcaldav.test.util import TestCase
-    strGUID = "dsAttrTypeStandard:GeneratedUID"
-    strName = "dsAttrTypeStandard:RealName"
 
-except ImportError:
-    pass
+from twisted.internet.defer import inlineCallbacks
+from calendarserver.tools.resources import migrateResources
+from twistedcaldav.test.util import StoreTestCase
+from txdav.who.util import InMemoryDirectoryService
+from twext.who.directory import DirectoryRecord
+from txdav.who.idirectory import RecordType as CalRecordType
+from txdav.who.directory import CalendarDirectoryRecordMixin
 
-else:
-    class StubDirectoryRecord(object):
 
-        def __init__(
-            self, recordType, guid=None, shortNames=None, fullName=None
-        ):
-            self.recordType = recordType
-            self.guid = guid
-            self.shortNames = shortNames
-            self.fullName = fullName
+class TestRecord(DirectoryRecord, CalendarDirectoryRecordMixin):
+    pass
 
 
-    class StubDirectoryService(object):
+class MigrateResourcesTest(StoreTestCase):
 
-        def __init__(self, augmentService):
-            self.records = {}
-            self.augmentService = augmentService
+    @inlineCallbacks
+    def setUp(self):
+        yield super(MigrateResourcesTest, self).setUp()
+        self.store = self.storeUnderTest()
 
-        def recordWithGUID(self, guid):
-            return None
+        self.sourceService = InMemoryDirectoryService(None)
+        fieldName = self.sourceService.fieldName
+        records = (
+            TestRecord(
+                self.sourceService,
+                {
+                    fieldName.uid: u"location1",
+                    fieldName.shortNames: (u"loc1",),
+                    fieldName.recordType: CalRecordType.location,
+                }
+            ),
+            TestRecord(
+                self.sourceService,
+                {
+                    fieldName.uid: u"location2",
+                    fieldName.shortNames: (u"loc2",),
+                    fieldName.recordType: CalRecordType.location,
+                }
+            ),
+            TestRecord(
+                self.sourceService,
+                {
+                    fieldName.uid: u"resource1",
+                    fieldName.shortNames: (u"res1",),
+                    fieldName.recordType: CalRecordType.resource,
+                }
+            ),
+        )
+        yield self.sourceService.updateRecords(records, create=True)
 
-        def createRecords(self, data):
-            for recordType, recordData in data:
-                guid = recordData["guid"]
-                record = StubDirectoryRecord(
-                    recordType, guid=guid,
-                    shortNames=recordData["shortNames"],
-                    fullName=recordData["fullName"]
-                )
-                self.records[guid] = record
 
-        def updateRecord(
-            self, recordType, guid=None, shortNames=None, fullName=None
-        ):
-            pass
 
+    @inlineCallbacks
+    def test_migrateResources(self):
 
-    class StubAugmentRecord(object):
+        # Record location1 has not been migrated
+        record = yield self.directory.recordWithUID(u"location1")
+        self.assertEquals(record, None)
 
-        def __init__(self, guid=None):
-            self.guid = guid
-            self.autoSchedule = True
+        # Migrate location1, location2, and resource1
+        yield migrateResources(self.sourceService, self.directory)
+        record = yield self.directory.recordWithUID(u"location1")
+        self.assertEquals(record.uid, u"location1")
+        self.assertEquals(record.shortNames[0], u"loc1")
+        record = yield self.directory.recordWithUID(u"location2")
+        self.assertEquals(record.uid, u"location2")
+        self.assertEquals(record.shortNames[0], u"loc2")
+        record = yield self.directory.recordWithUID(u"resource1")
+        self.assertEquals(record.uid, u"resource1")
+        self.assertEquals(record.shortNames[0], u"res1")
 
+        # Add a new location to the sourceService, and modify an existing
+        # location
+        fieldName = self.sourceService.fieldName
+        newRecords = (
+            TestRecord(
+                self.sourceService,
+                {
+                    fieldName.uid: u"location1",
+                    fieldName.shortNames: (u"newloc1",),
+                    fieldName.recordType: CalRecordType.location,
+                }
+            ),
+            TestRecord(
+                self.sourceService,
+                {
+                    fieldName.uid: u"location3",
+                    fieldName.shortNames: (u"loc3",),
+                    fieldName.recordType: CalRecordType.location,
+                }
+            ),
+        )
+        yield self.sourceService.updateRecords(newRecords, create=True)
 
-    class StubAugmentService(object):
+        yield migrateResources(self.sourceService, self.directory)
 
-        records = {}
+        # Ensure an existing record does not get migrated again; verified by
+        # seeing if shortNames changed, which they should not:
+        record = yield self.directory.recordWithUID(u"location1")
+        self.assertEquals(record.uid, u"location1")
+        self.assertEquals(record.shortNames[0], u"loc1")
 
-        @classmethod
-        def getAugmentRecord(cls, guid, recordType):
-            if guid not in cls.records:
-                record = StubAugmentRecord(guid=guid)
-                cls.records[guid] = record
-            return succeed(cls.records[guid])
-
-        @classmethod
-        def addAugmentRecords(cls, records):
-            for record in records:
-                cls.records[record.guid] = record
-            return succeed(True)
-
-
-    class MigrateResourcesTestCase(TestCase):
-
-        @inlineCallbacks
-        def test_migrateResources(self):
-
-            data = {
-                "dsRecTypeStandard:Resources":
-                [
-                    ["projector1", {
-                        strGUID: "6C99E240-E915-4012-82FA-99E0F638D7EF",
-                        strName: "Projector 1"
-                    }],
-                    ["projector2", {
-                        strGUID: "7C99E240-E915-4012-82FA-99E0F638D7EF",
-                        strName: "Projector 2"
-                    }],
-                ],
-                "dsRecTypeStandard:Places":
-                [
-                    ["office1", {
-                        strGUID: "8C99E240-E915-4012-82FA-99E0F638D7EF",
-                        strName: "Office 1"
-                    }],
-                ],
-            }
-
-            def queryMethod(sourceService, recordType, verbose=False):
-                return data[recordType]
-
-            directoryService = StubDirectoryService(StubAugmentService())
-            yield migrateResources(
-                None, directoryService, queryMethod=queryMethod
-            )
-            for guid, recordType in (
-                (
-                    "6C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_resources
-                ),
-                (
-                    "7C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_resources
-                ),
-                (
-                    "8C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_locations
-                ),
-            ):
-                self.assertTrue(guid in directoryService.records)
-                record = directoryService.records[guid]
-                self.assertEquals(record.recordType, recordType)
-
-                self.assertTrue(guid in StubAugmentService.records)
-
-            #
-            # Add more to OD and re-migrate
-            #
-            data["dsRecTypeStandard:Resources"].append(
-                ["projector3", {
-                    strGUID: "9C99E240-E915-4012-82FA-99E0F638D7EF",
-                    strName: "Projector 3"
-                }]
-            )
-            data["dsRecTypeStandard:Places"].append(
-                ["office2", {
-                    strGUID: "AC99E240-E915-4012-82FA-99E0F638D7EF",
-                    strName: "Office 2"
-                }]
-            )
-
-            yield migrateResources(
-                None, directoryService, queryMethod=queryMethod
-            )
-
-            for guid, recordType in (
-                (
-                    "6C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_resources
-                ),
-                (
-                    "7C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_resources
-                ),
-                (
-                    "9C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_resources
-                ),
-                (
-                    "8C99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_locations
-                ),
-                (
-                    "AC99E240-E915-4012-82FA-99E0F638D7EF",
-                    DirectoryService.recordType_locations
-                ),
-            ):
-                self.assertTrue(guid in directoryService.records)
-                record = directoryService.records[guid]
-                self.assertEquals(record.recordType, recordType)
-
-                self.assertTrue(guid in StubAugmentService.records)
+        # Ensure new record does get migrated
+        record = yield self.directory.recordWithUID(u"location3")
+        self.assertEquals(record.uid, u"location3")
+        self.assertEquals(record.shortNames[0], u"loc3")

Modified: CalendarServer/trunk/twistedcaldav/test/test_upgrade.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_upgrade.py	2014-05-02 15:44:31 UTC (rev 13404)
+++ CalendarServer/trunk/twistedcaldav/test/test_upgrade.py	2014-05-02 16:51:33 UTC (rev 13405)
@@ -19,14 +19,12 @@
 import zlib
 import cPickle
 
-from twisted.python.reflect import namedClass
 from twisted.internet.defer import inlineCallbacks, succeed
 
 from txdav.xml.parser import WebDAVDocument
 from txdav.caldav.datastore.index_file import db_basename
 
 from twistedcaldav.config import config
-from twistedcaldav.directory.resourceinfo import ResourceInfoDatabase
 from txdav.caldav.datastore.scheduling.imip.mailgateway import MailGatewayTokensDatabase
 from twistedcaldav.upgrade import (
     xattrname, upgradeData, updateFreeBusySet,
@@ -1399,95 +1397,6 @@
         self.assertTrue(self.verifyHierarchy(root, after))
 
 
-    @inlineCallbacks
-    def test_migrateResourceInfo(self):
-        # Fake getResourceInfo( )
-
-        assignments = {
-            'guid1' : (False, None, None),
-            'guid2' : (True, 'guid1', None),
-            'guid3' : (False, 'guid1', 'guid2'),
-            'guid4' : (True, None, 'guid3'),
-        }
-
-        def _getResourceInfo(ignored):
-            results = []
-            for guid, info in assignments.iteritems():
-                results.append((guid, info[0], info[1], info[2]))
-            return results
-
-        self.setUpInitialStates()
-        # Override the normal getResourceInfo method with our own:
-        # XMLDirectoryService.getResourceInfo = _getResourceInfo
-        # self.patch(XMLDirectoryService, "getResourceInfo", _getResourceInfo)
-
-        before = {
-            "trigger_resource_migration" : {
-                "@contents" : "x",
-            }
-        }
-        after = {
-            ".calendarserver_version" :
-            {
-                "@contents" : "2",
-            },
-            NEWPROXYFILE :
-            {
-                "@contents" : None,
-            },
-            MailGatewayTokensDatabase.dbFilename :
-            {
-                "@contents" : None,
-            },
-            "%s-journal" % (MailGatewayTokensDatabase.dbFilename,) :
-            {
-                "@contents" : None,
-                "@optional" : True,
-            },
-            ResourceInfoDatabase.dbFilename :
-            {
-                "@contents" : None,
-            },
-            "%s-journal" % (ResourceInfoDatabase.dbFilename,) :
-            {
-                "@contents" : None,
-                "@optional" : True,
-            }
-        }
-        root = self.createHierarchy(before)
-        config.DocumentRoot = root
-        config.DataRoot = root
-        config.ServerRoot = root
-
-        (yield self.doUpgrade(config))
-        self.assertTrue(self.verifyHierarchy(root, after))
-
-        proxydbClass = namedClass(config.ProxyDBService.type)
-        calendarUserProxyDatabase = proxydbClass(**config.ProxyDBService.params)
-        resourceInfoDatabase = ResourceInfoDatabase(root)
-
-        for guid, info in assignments.iteritems():
-            proxyGroup = "%s#calendar-proxy-write" % (guid,)
-            result = (yield calendarUserProxyDatabase.getMembers(proxyGroup))
-            if info[1]:
-                self.assertTrue(info[1] in result)
-            else:
-                self.assertTrue(not result)
-
-            readOnlyProxyGroup = "%s#calendar-proxy-read" % (guid,)
-            result = (yield calendarUserProxyDatabase.getMembers(readOnlyProxyGroup))
-            if info[2]:
-                self.assertTrue(info[2] in result)
-            else:
-                self.assertTrue(not result)
-
-            autoSchedule = resourceInfoDatabase._db_value_for_sql("select AUTOSCHEDULE from RESOURCEINFO where GUID = :1", guid)
-            autoSchedule = autoSchedule == 1
-            self.assertEquals(info[0], autoSchedule)
-
-    test_migrateResourceInfo.todo = "Need to port to twext.who"
-
-
     def test_removeIllegalCharacters(self):
         """
         Control characters aside from NL and CR are removed.

Modified: CalendarServer/trunk/twistedcaldav/upgrade.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/upgrade.py	2014-05-02 15:44:31 UTC (rev 13404)
+++ CalendarServer/trunk/twistedcaldav/upgrade.py	2014-05-02 16:51:33 UTC (rev 13405)
@@ -53,8 +53,6 @@
 
 from txdav.caldav.datastore.index_file import db_basename
 
-# from twisted.protocols.amp import AMP, Command, String, Boolean
-
 from calendarserver.tap.util import getRootResource, FakeRequest
 
 from txdav.caldav.datastore.scheduling.imip.mailgateway import migrateTokensToStore
@@ -63,6 +61,7 @@
 from txdav.who.idirectory import RecordType as CalRecordType
 from txdav.who.delegates import addDelegate
 from twistedcaldav.directory.calendaruserproxy import ProxySqliteDB
+from calendarserver.tools.resources import migrateResources
 
 
 deadPropertyXattrPrefix = namedAny(
@@ -267,43 +266,6 @@
 
 
 
-# class UpgradeOneHome(Command):
-#     arguments = [('path', String())]
-#     response = [('succeeded', Boolean())]
-
-
-
-# class To1Driver(AMP):
-#     """
-#     Upgrade driver which runs in the parent process.
-#     """
-
-#     def upgradeHomeInHelper(self, path):
-#         return self.callRemote(UpgradeOneHome, path=path).addCallback(
-#             operator.itemgetter("succeeded")
-#         )
-
-
-
-# class To1Home(AMP):
-#     """
-#     Upgrade worker which runs in dedicated subprocesses.
-#     """
-
-#     def __init__(self, config):
-#         super(To1Home, self).__init__()
-#         self.directory = getDirectory(config)
-#         self.cuaCache = {}
-
-
-#     @UpgradeOneHome.responder
-#     @inlineCallbacks
-#     def upgradeOne(self, path):
-#         result = yield upgradeCalendarHome(path, self.directory, self.cuaCache)
-#         returnValue(dict(succeeded=result))
-
-
-
 @inlineCallbacks
 def upgrade_to_1(config, directory):
     """
@@ -371,51 +333,8 @@
         os.rename(oldHome, newHome)
 
 
-    @inlineCallbacks
-    def migrateResourceInfo(config, directory, uid, gid):
-        """
-        Retrieve delegate assignments and auto-schedule flag from the directory
-        service, because in "v1" that's where this info lived.
-        """
 
-        print("FIXME, need to port migrateResourceInfo to twext.who")
-        returnValue(None)
 
-        log.warn("Fetching delegate assignments and auto-schedule settings from directory")
-        resourceInfo = directory.getResourceInfo()
-        if len(resourceInfo) == 0:
-            # Nothing to migrate, or else not appleopendirectory
-            log.warn("No resource info found in directory")
-            returnValue(None)
-
-        log.warn("Found info for %d resources and locations in directory; applying settings" % (len(resourceInfo),))
-
-        resourceInfoDatabase = ResourceInfoDatabase(config.DataRoot)
-        proxydbClass = namedClass(config.ProxyDBService.type)
-        calendarUserProxyDatabase = proxydbClass(**config.ProxyDBService.params)
-
-        for guid, autoSchedule, proxy, readOnlyProxy in resourceInfo:
-            resourceInfoDatabase.setAutoScheduleInDatabase(guid, autoSchedule)
-            if proxy:
-                yield calendarUserProxyDatabase.setGroupMembersInDatabase(
-                    "%s#calendar-proxy-write" % (guid,),
-                    [proxy]
-                )
-            if readOnlyProxy:
-                yield calendarUserProxyDatabase.setGroupMembersInDatabase(
-                    "%s#calendar-proxy-read" % (guid,),
-                    [readOnlyProxy]
-                )
-
-        dbPath = os.path.join(config.DataRoot, ResourceInfoDatabase.dbFilename)
-        if os.path.exists(dbPath):
-            os.chown(dbPath, uid, gid)
-
-        dbPath = os.path.join(config.DataRoot, "proxies.sqlite")
-        if os.path.exists(dbPath):
-            os.chown(dbPath, uid, gid)
-
-
     def createMailTokensDatabase(config, uid, gid):
         # Cause the tokens db to be created on disk so we can set the
         # permissions on it now
@@ -574,9 +493,6 @@
                                         )
                 log.warn("Done processing calendar homes")
 
-    triggerPath = os.path.join(config.ServerRoot, TRIGGER_FILE)
-    if os.path.exists(triggerPath):
-        yield migrateResourceInfo(config, directory, uid, gid)
     createMailTokensDatabase(config, uid, gid)
 
     if errorOccurred:
@@ -752,7 +668,7 @@
         try:
             # Migrate locations/resources now because upgrade_to_1 depends
             # on them being in resources.xml
-            yield migrateFromOD(config, directory)
+            yield migrateFromOD(directory)
         except Exception, e:
             raise UpgradeError("Unable to migrate locations and resources from OD: %s" % (e,))
 
@@ -937,37 +853,22 @@
 
 
 
-# # Deferred
-def migrateFromOD(config, directory):
-    # FIXME:
-    print("STILL NEED TO IMPLEMENT migrateFromOD")
-    return succeed(None)
-#     #
-#     # Migrates locations and resources from OD
-#     #
-#     try:
-#         from twistedcaldav.directory.appleopendirectory import OpenDirectoryService
-#         from calendarserver.tools.resources import migrateResources
-#     except ImportError:
-#         return succeed(None)
+# Deferred
+def migrateFromOD(directory):
+    #
+    # Migrates locations and resources from OD
+    #
+    log.warn("Migrating locations and resources")
 
-#     log.warn("Migrating locations and resources")
+    # Create internal copies of resources and locations based on what is
+    # found in OD
+    from twext.who.opendirectory import (
+        DirectoryService as OpenDirectoryService
+    )
+    return migrateResources(OpenDirectoryService(), directory)
 
-#     userService = directory.serviceForRecordType("users")
-#     resourceService = directory.serviceForRecordType("resources")
-#     if (
-#         not isinstance(userService, OpenDirectoryService) or
-#         not isinstance(resourceService, XMLDirectoryService)
-#     ):
-#         # Configuration requires no migration
-#         return succeed(None)
 
-#     # Create internal copies of resources and locations based on what is
-#     # found in OD
-#     return migrateResources(userService, resourceService)
 
-
-
 @inlineCallbacks
 def migrateAutoSchedule(config, directory):
     # Fetch the autoSchedule assignments from resourceinfo.sqlite and store

Modified: CalendarServer/trunk/txdav/who/test/test_util.py
===================================================================
--- CalendarServer/trunk/txdav/who/test/test_util.py	2014-05-02 15:44:31 UTC (rev 13404)
+++ CalendarServer/trunk/txdav/who/test/test_util.py	2014-05-02 16:51:33 UTC (rev 13405)
@@ -20,7 +20,7 @@
 
 import os
 
-from txdav.who.util import directoryFromConfig
+from txdav.who.util import directoryFromConfig, InMemoryDirectoryService
 from twisted.internet.defer import inlineCallbacks
 from twisted.trial.unittest import TestCase
 from twistedcaldav.config import ConfigDict
@@ -32,7 +32,8 @@
     DirectoryService as DelegateDirectoryService,
     RecordType as DelegateRecordType
 )
-from twext.who.idirectory import RecordType
+from twext.who.directory import DirectoryRecord
+from twext.who.idirectory import RecordType, NoSuchRecordError
 from txdav.who.idirectory import RecordType as CalRecordType
 from txdav.who.wiki import (
     DirectoryService as WikiDirectoryService,
@@ -161,3 +162,95 @@
         # And make sure it's functional:
         record = yield service.recordWithUID("group07")
         self.assertEquals(record.fullNames, [u'Group 07'])
+
+
+
+class IndexDirectoryServiceTest(TestCase):
+
+    @inlineCallbacks
+    def test_updateRecords(self):
+        service = InMemoryDirectoryService(u"xyzzy")
+
+        # Record does not exist
+        record = yield service.recordWithUID(u"foo")
+        self.assertEquals(None, record)
+
+        records = (
+            DirectoryRecord(
+                service,
+                {
+                    service.fieldName.uid: u"foo",
+                    service.fieldName.shortNames: (u"foo1", u"foo2"),
+                    service.fieldName.recordType: RecordType.user,
+                }
+            ),
+        )
+        try:
+            # Trying to update a record when it does not exist should fail
+            yield service.updateRecords(records, create=False)
+        except NoSuchRecordError:
+            pass
+        except:
+            self.fail("Did not raise NoSuchRecordError when create=False and record does not exist")
+
+        record = yield service.recordWithUID(u"foo")
+        self.assertEquals(None, record)
+
+        # Create the record
+        yield service.updateRecords(records, create=True)
+
+        record = yield service.recordWithUID(u"foo")
+        self.assertEquals(record.uid, u"foo")
+
+        records = yield service.recordsWithRecordType(RecordType.user)
+        self.assertEquals(len(records), 1)
+        self.assertEquals(list(records)[0].uid, u"foo")
+
+        record = yield service.recordWithShortName(RecordType.user, u"foo1")
+        self.assertEquals(record.uid, u"foo")
+        record = yield service.recordWithShortName(RecordType.user, u"foo2")
+        self.assertEquals(record.uid, u"foo")
+
+        records = (
+            DirectoryRecord(
+                service,
+                {
+                    service.fieldName.uid: u"foo",
+                    service.fieldName.shortNames: (u"foo3", u"foo4"),
+                    service.fieldName.recordType: RecordType.group,
+                }
+            ),
+            DirectoryRecord(
+                service,
+                {
+                    service.fieldName.uid: u"bar",
+                    service.fieldName.shortNames: (u"bar1", u"bar2"),
+                    service.fieldName.recordType: RecordType.user,
+                }
+            ),
+        )
+
+        # Update the existing record and create a new one
+        yield service.updateRecords(records, create=True)
+
+        record = yield service.recordWithUID(u"foo")
+        self.assertEquals(record.uid, u"foo")
+        self.assertEquals(set(record.shortNames), set((u'foo3', u'foo4')))
+
+        records = yield service.recordsWithRecordType(RecordType.group)
+        self.assertEquals(len(records), 1)
+        self.assertEquals(list(records)[0].uid, u"foo")
+
+        records = yield service.recordsWithRecordType(RecordType.user)
+        self.assertEquals(len(records), 1)
+        self.assertEquals(list(records)[0].uid, u"bar")
+
+        record = yield service.recordWithShortName(RecordType.group, u"foo3")
+        self.assertEquals(record.uid, u"foo")
+        record = yield service.recordWithShortName(RecordType.group, u"foo4")
+        self.assertEquals(record.uid, u"foo")
+
+        # Remove a record
+        yield service.removeRecords((u"foo",))
+        record = yield service.recordWithUID(u"foo")
+        self.assertEquals(None, record)

Modified: CalendarServer/trunk/txdav/who/util.py
===================================================================
--- CalendarServer/trunk/txdav/who/util.py	2014-05-02 15:44:31 UTC (rev 13404)
+++ CalendarServer/trunk/txdav/who/util.py	2014-05-02 16:51:33 UTC (rev 13405)
@@ -22,8 +22,10 @@
 from twext.python.types import MappingProxyType
 from twext.who.aggregate import DirectoryService as AggregateDirectoryService
 from twext.who.idirectory import (
-    FieldName as BaseFieldName, RecordType, DirectoryConfigurationError
+    FieldName as BaseFieldName, RecordType, DirectoryConfigurationError,
+    NoSuchRecordError
 )
+from twext.who.index import DirectoryService as IndexDirectoryService
 from twext.who.ldap import (
     DirectoryService as LDAPDirectoryService, LDAPAttribute,
     FieldName as LDAPFieldName,
@@ -44,6 +46,7 @@
 from txdav.who.wiki import DirectoryService as WikiDirectoryService
 from txdav.who.xml import DirectoryService as XMLDirectoryService
 from uuid import UUID
+from twisted.internet.defer import succeed, inlineCallbacks
 
 
 log = Logger()
@@ -273,3 +276,49 @@
             return parts[3]
 
     return None
+
+
+
+
+class InMemoryDirectoryService(IndexDirectoryService):
+    """
+    An in-memory IDirectoryService.  You must call updateRecords( ) if you want
+    to populate this service.
+    """
+
+    recordType = ConstantsContainer(
+        (
+            RecordType.user,
+            RecordType.group,
+            CalRecordType.location,
+            CalRecordType.resource,
+            CalRecordType.address
+        )
+    )
+
+
+    def loadRecords(self):
+        pass
+
+
+    @inlineCallbacks
+    def updateRecords(self, records, create=False):
+        recordsByUID = dict(((record.uid, record) for record in records))
+        if not create:
+            # Make sure all the records already exist
+            for uid, record in recordsByUID.items():
+                if uid not in self._index[self.fieldName.uid]:
+                    raise NoSuchRecordError(uid)
+
+        yield self.removeRecords(recordsByUID.keys())
+        self.indexRecords(records)
+
+
+    def removeRecords(self, uids):
+        index = self._index
+        for fieldName in self.indexedFields:
+            for recordSet in index[fieldName].itervalues():
+                for record in list(recordSet):
+                    if record.uid in uids:
+                        recordSet.remove(record)
+        return succeed(None)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140502/9d671dda/attachment-0001.html>


More information about the calendarserver-changes mailing list