[CalendarServer-changes] [13677] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Jun 23 22:18:48 PDT 2014


Revision: 13677
          http://trac.calendarserver.org//changeset/13677
Author:   sagen at apple.com
Date:     2014-06-23 22:18:48 -0700 (Mon, 23 Jun 2014)
Log Message:
-----------
Convert resources.xml to new format

Modified Paths:
--------------
    CalendarServer/trunk/requirements-stable.txt
    CalendarServer/trunk/twistedcaldav/test/test_upgrade.py
    CalendarServer/trunk/twistedcaldav/upgrade.py
    CalendarServer/trunk/txdav/who/wiki.py

Modified: CalendarServer/trunk/requirements-stable.txt
===================================================================
--- CalendarServer/trunk/requirements-stable.txt	2014-06-24 05:07:09 UTC (rev 13676)
+++ CalendarServer/trunk/requirements-stable.txt	2014-06-24 05:18:48 UTC (rev 13677)
@@ -5,7 +5,7 @@
 # For CalendarServer development, don't try to get these projects from PyPI; use svn.
 
 -e .
--e svn+http://svn.calendarserver.org/repository/calendarserver/twext/trunk@13660#egg=twextpy
+-e svn+http://svn.calendarserver.org/repository/calendarserver/twext/trunk@13676#egg=twextpy
 -e svn+http://svn.calendarserver.org/repository/calendarserver/PyKerberos/trunk@13420#egg=kerberos
 -e svn+http://svn.calendarserver.org/repository/calendarserver/PyCalendar/trunk@13621#egg=pycalendar
 

Modified: CalendarServer/trunk/twistedcaldav/test/test_upgrade.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_upgrade.py	2014-06-24 05:07:09 UTC (rev 13676)
+++ CalendarServer/trunk/twistedcaldav/test/test_upgrade.py	2014-06-24 05:18:48 UTC (rev 13677)
@@ -14,26 +14,26 @@
 # limitations under the License.
 ##
 
+import cPickle
 import hashlib
 import os
 import zlib
-import cPickle
 
 from twisted.internet.defer import inlineCallbacks, succeed
-
-from txdav.xml.parser import WebDAVDocument
-from txdav.caldav.datastore.index_file import db_basename
-
+from twisted.python.filepath import FilePath
 from twistedcaldav.config import config
-from txdav.caldav.datastore.scheduling.imip.mailgateway import MailGatewayTokensDatabase
+from twistedcaldav.directory.calendaruserproxy import ProxySqliteDB
+from twistedcaldav.test.util import StoreTestCase
 from twistedcaldav.upgrade import (
     xattrname, upgradeData, updateFreeBusySet,
     removeIllegalCharacters, normalizeCUAddrs,
-    loadDelegatesFromXML, migrateDelegatesToStore
+    loadDelegatesFromXML, migrateDelegatesToStore,
+    upgradeResourcesXML
 )
-from twistedcaldav.test.util import StoreTestCase
+from txdav.caldav.datastore.index_file import db_basename
+from txdav.caldav.datastore.scheduling.imip.mailgateway import MailGatewayTokensDatabase
 from txdav.who.delegates import delegatesOf
-from twistedcaldav.directory.calendaruserproxy import ProxySqliteDB
+from txdav.xml.parser import WebDAVDocument
 
 
 
@@ -1503,7 +1503,34 @@
         yield txn.commit()
 
 
+    def test_resourcesXML(self):
+        """
+        Verify conversion of old resources.xml format to twext.who.xml format
+        """
+        fileName = self.mktemp()
+        fp = FilePath(fileName)
+        fp.setContent(oldResourcesFormat)
+        upgradeResourcesXML(fp)
+        self.assertEquals(fp.getContent(), newResourcesFormat)
 
+
+oldResourcesFormat = """<accounts realm="/Search">
+  <location>
+    <uid>location1</uid>
+    <guid>C4F46062-9094-4D34-8591-61A42D993FAA</guid>
+    <name>location name</name>
+  </location>
+  <resource>
+    <uid>resource1</uid>
+    <guid>60B771CC-D727-4453-ACE0-0FE13CD7445A</guid>
+    <name>resource name</name>
+  </resource>
+</accounts>
+"""
+
+newResourcesFormat = """<directory realm="/Search"><record type="location"><short-name>location1</short-name><uid>C4F46062-9094-4D34-8591-61A42D993FAA</uid><full-name>location name</full-name></record><record type="resource"><short-name>resource1</short-name><uid>60B771CC-D727-4453-ACE0-0FE13CD7445A</uid><full-name>resource name</full-name></record></directory>"""
+
+
 normalizeEvent = """BEGIN:VCALENDAR
 VERSION:2.0
 BEGIN:VEVENT

Modified: CalendarServer/trunk/twistedcaldav/upgrade.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/upgrade.py	2014-06-24 05:07:09 UTC (rev 13676)
+++ CalendarServer/trunk/twistedcaldav/upgrade.py	2014-06-24 05:18:48 UTC (rev 13677)
@@ -30,6 +30,10 @@
 from zlib import compress
 from cPickle import loads as unpickle, UnpicklingError
 
+from xml.etree.ElementTree import (
+    parse as parseXML, ParseError as XMLParseError,
+    tostring as etreeToString, Element as XMLElement
+)
 
 from twext.python.log import Logger
 from txdav.xml import element
@@ -48,6 +52,7 @@
 from twisted.internet.defer import (
     inlineCallbacks, succeed, returnValue
 )
+from twisted.python.filepath import FilePath
 from twisted.python.reflect import namedAny
 from twisted.python.reflect import namedClass
 
@@ -646,7 +651,53 @@
         raise UpgradeError("Data upgrade failed, see error.log for details")
 
 
+def upgradeResourcesXML(resourcesFilePath):
+    """
+    Convert the old XML format to the twext.who.xml format
 
+    @param resourcesFilePath: the file to convert
+    @type resourcesFilePath: L{FilePath}
+    """
+    try:
+        with resourcesFilePath.open() as fh:
+            try:
+                etree = parseXML(fh)
+            except XMLParseError:
+                log.error("Cannot parse {path}", path=resourcesFilePath.path)
+                return
+    except (OSError, IOError):
+        # Can't read the file
+        log.error("Cannot read {path}", path=resourcesFilePath.path)
+        return
+
+    accountsNode = etree.getroot()
+    if accountsNode.tag != "accounts":
+        return
+
+    tagMap = {
+        "uid": "short-name",
+        "guid": "uid",
+        "name": "full-name",
+    }
+    log.info("Converting resources.xml")
+    directoryNode = XMLElement("directory")
+    directoryNode.set("realm", accountsNode.get("realm"))
+    for sourceNode in accountsNode:
+        recordType = sourceNode.tag
+        destNode = XMLElement("record")
+        destNode.set("type", recordType)
+        for sourceFieldNode in sourceNode:
+            tag = tagMap.get(sourceFieldNode.tag, None)
+            if tag:
+                destFieldNode = XMLElement(tag)
+                destFieldNode.text = sourceFieldNode.text
+                destNode.append(destFieldNode)
+
+        directoryNode.append(destNode)
+
+    resourcesFilePath.setContent(etreeToString(directoryNode, "utf-8"))
+
+
 # The on-disk version number (which defaults to zero if .calendarserver_version
 # doesn't exist), is compared with each of the numbers in the upgradeMethods
 # array.  If it is less than the number, the associated method is called.
@@ -660,6 +711,14 @@
 @inlineCallbacks
 def upgradeData(config, directory):
 
+    if config.ResourceService.Enabled:
+        resourcesFileName = config.ResourceService.params.xmlFile
+        if resourcesFileName[0] not in ("/", "."):
+            resourcesFileName = os.path.join(config.DataRoot, resourcesFileName)
+        resourcesFilePath = FilePath(resourcesFileName)
+        if resourcesFilePath.exists():
+            upgradeResourcesXML(resourcesFilePath)
+
     triggerPath = os.path.join(config.ServerRoot, TRIGGER_FILE)
     if os.path.exists(triggerPath):
         try:

Modified: CalendarServer/trunk/txdav/who/wiki.py
===================================================================
--- CalendarServer/trunk/txdav/who/wiki.py	2014-06-24 05:07:09 UTC (rev 13676)
+++ CalendarServer/trunk/txdav/who/wiki.py	2014-06-24 05:18:48 UTC (rev 13677)
@@ -194,6 +194,7 @@
                 "in wiki {log_source}: {error}",
                 record=record, error=e
             )
+            returnValue(WikiAccessLevel.none)
 
         except WebError as e:
             status = int(e.status)
@@ -216,6 +217,7 @@
                 "Unable to look up wiki access: {error}",
                 record=record, error=e
             )
+            returnValue(WikiAccessLevel.none)
 
         try:
             returnValue({
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140623/7b35e224/attachment-0001.html>


More information about the calendarserver-changes mailing list