[CalendarServer-changes] [4868] CalendarServer/branches/users/cdaboo/partition-4464

source_changes at macosforge.org source_changes at macosforge.org
Wed Dec 16 09:40:11 PST 2009


Revision: 4868
          http://trac.macosforge.org/projects/calendarserver/changeset/4868
Author:   cdaboo at apple.com
Date:     2009-12-16 09:40:09 -0800 (Wed, 16 Dec 2009)
Log Message:
-----------
Sync'ing up with changes from deployment branch.

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

Added Paths:
-----------
    CalendarServer/branches/users/cdaboo/partition-4464/bin/calendarserver_manage_augments
    CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/manageaugments.py
    CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/managepostgres.py

Added: CalendarServer/branches/users/cdaboo/partition-4464/bin/calendarserver_manage_augments
===================================================================
--- CalendarServer/branches/users/cdaboo/partition-4464/bin/calendarserver_manage_augments	                        (rev 0)
+++ CalendarServer/branches/users/cdaboo/partition-4464/bin/calendarserver_manage_augments	2009-12-16 17:40:09 UTC (rev 4868)
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+##
+# Copyright (c) 2006-2007 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+import sys
+
+#PYTHONPATH
+
+if __name__ == "__main__":
+    if "PYTHONPATH" in globals():
+        sys.path.insert(0, PYTHONPATH)
+    else:
+        from os.path import dirname, abspath, join
+        from subprocess import Popen, PIPE
+
+        home = dirname(dirname(abspath(__file__)))
+        run = join(home, "run")
+
+        child = Popen((run, "-p"), stdout=PIPE)
+        path, stderr = child.communicate()
+
+        if child.wait() == 0:
+            sys.path[0:0] = path.split(":")
+
+    from calendarserver.tools.manageaugments import main
+    main()

Modified: CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/loadaugmentdb.py
===================================================================
--- CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/loadaugmentdb.py	2009-12-16 15:42:35 UTC (rev 4867)
+++ CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/loadaugmentdb.py	2009-12-16 17:40:09 UTC (rev 4868)
@@ -1,9 +1,6 @@
 #!/usr/bin/env python
-from twistedcaldav.directory import augment
-from twistedcaldav.directory.augment import AugmentXMLDB
-
 ##
-# Copyright (c) 2006-2007 Apple Inc. All rights reserved.
+# Copyright (c) 2009 Apple Inc. All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -18,7 +15,6 @@
 # limitations under the License.
 ##
 
-
 from calendarserver.tools.util import loadConfig, getDirectory,\
     autoDisableMemcached
 from getopt import getopt, GetoptError
@@ -30,6 +26,8 @@
 from twisted.python.log import addObserver, removeObserver
 from twisted.python.util import switchUID
 from twistedcaldav.config import config, ConfigurationError
+from twistedcaldav.directory import augment
+from twistedcaldav.directory.augment import AugmentXMLDB
 from twistedcaldav.log import setLogLevelForNamespace
 import os
 import sys

Added: CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/manageaugments.py
===================================================================
--- CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/manageaugments.py	                        (rev 0)
+++ CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/manageaugments.py	2009-12-16 17:40:09 UTC (rev 4868)
@@ -0,0 +1,200 @@
+#!/usr/bin/env python
+##
+# Copyright (c) 2009 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from optparse import OptionParser
+from twistedcaldav.directory import xmlaugmentsparser
+from xml.etree.ElementTree import ElementTree, tostring, SubElement
+from xml.parsers.expat import ExpatError
+import sys
+
+def error(s):
+    print s
+    sys.exit(1)
+
+def readXML(xmlfile):
+
+    # Read in XML
+    try:
+        tree = ElementTree(file=xmlfile)
+    except ExpatError, e:
+        error("Unable to parse file '%s' because: %s" % (xmlfile, e,))
+
+    # Verify that top-level element is correct
+    augments_node = tree.getroot()
+    if augments_node.tag != xmlaugmentsparser.ELEMENT_AUGMENTS:
+        error("Ignoring file '%s' because it is not a augments file" % (xmlfile,))
+
+    return augments_node
+
+def writeXML(xmlfile, root):
+    
+    data = """<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE augments SYSTEM "augments.dtd">
+
+""" + tostring(root)
+
+    with open(xmlfile, "w") as f:
+        f.write(data)
+
+def addSubElement(parent, tag, text=None, indent=0):
+    
+    child = SubElement(parent, tag)
+    child.text = text
+    child.tail = "\n" + " " * indent
+    return child
+
+def changeSubElementText(parent, tag, text):
+    
+    child = parent.find(tag)
+    child.text = text
+
+def doAdd(xmlfile, guid, host, enable_calendar, auto_schedule):
+
+    augments_node = readXML(xmlfile)
+
+    # Make sure GUID is not already present
+    for child in augments_node.getchildren():
+        
+        if child.tag != xmlaugmentsparser.ELEMENT_RECORD:
+            error("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, xmlfile,))
+
+        for node in child.getchildren():
+            
+            if node.tag == xmlaugmentsparser.ELEMENT_GUID and node.text == guid:
+                error("Cannot add guid '%s' because it already exists in augment file: '%s'" % (guid, xmlfile,))
+    
+    # Create new record
+    augments_node.getchildren()[-1].tail = "\n  "
+    record = addSubElement(augments_node, xmlaugmentsparser.ELEMENT_RECORD, "\n    ")
+    addSubElement(record, xmlaugmentsparser.ELEMENT_GUID, guid, 4)
+    addSubElement(record, xmlaugmentsparser.ELEMENT_ENABLE, "true", 4)
+    addSubElement(record, xmlaugmentsparser.ELEMENT_HOSTEDAT, host, 4)
+    addSubElement(record, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if enable_calendar else "false", 4)
+    addSubElement(record, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if auto_schedule else "false", 2)
+    
+    # Modify xmlfile
+    writeXML(xmlfile, augments_node)
+    print "Added guid '%s' in augment file: '%s'" % (guid, xmlfile,)
+    
+def doModify(xmlfile, guid, host, enable_calendar, auto_schedule):
+
+    augments_node = readXML(xmlfile)
+
+    # Make sure GUID is present
+    for child in augments_node.getchildren():
+        
+        if child.tag != xmlaugmentsparser.ELEMENT_RECORD:
+            error("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, xmlfile,))
+
+        for node in child.getchildren():
+            
+            if node.tag == xmlaugmentsparser.ELEMENT_GUID and node.text == guid:
+                break
+        else:
+            continue
+        break
+    else:
+        error("Cannot modify guid '%s' because it does not exist in augment file: '%s'" % (guid, xmlfile,))
+    
+    # Modify record
+    if host is not None:
+        child.find(xmlaugmentsparser.ELEMENT_HOSTEDAT).text = host
+    child.find(xmlaugmentsparser.ELEMENT_ENABLECALENDAR).text = "true" if enable_calendar else "false"
+    child.find(xmlaugmentsparser.ELEMENT_AUTOSCHEDULE).text = "true" if auto_schedule else "false"
+    
+    # Modify xmlfile
+    writeXML(xmlfile, augments_node)
+    print "Modified guid '%s' in augment file: '%s'" % (guid, xmlfile,)
+
+def doRemove(xmlfile, guid):
+
+    augments_node = readXML(xmlfile)
+
+    # Make sure GUID is present
+    for child in augments_node.getchildren():
+        
+        if child.tag != xmlaugmentsparser.ELEMENT_RECORD:
+            error("Unknown augment type: '%s' in augment file: '%s'" % (child.tag, xmlfile,))
+
+        for node in child.getchildren():
+            
+            if node.tag == xmlaugmentsparser.ELEMENT_GUID and node.text == guid:
+                break
+        else:
+            continue
+        augments_node.remove(child)
+        break
+    else:
+        error("Cannot remove guid '%s' because it does not exist in augment file: '%s'" % (guid, xmlfile,))
+    
+    # Modify xmlfile
+    writeXML(xmlfile, augments_node)
+    print "Removed guid '%s' from augment file: '%s'" % (guid, xmlfile,)
+    
+def doPrint(xmlfile):
+
+    # Read in XML
+    augments_node = readXML(xmlfile)
+
+    print tostring(augments_node)
+
+def main():
+
+    usage = "%prog [options] ACTION"
+    epilog = """
+ACTION is one of add|modify|remove|print
+
+  add:    add a user record
+  modify: modify a user record
+  remove: remove a user record
+  print:  print all user records
+"""
+    description = "Tool to manipulate CalendarServer augments XML file"
+    version = "%prog v1.0"
+    parser = OptionParser(usage=usage, description=description, version=version)
+    parser.epilog = epilog
+    parser.format_epilog = lambda _:epilog
+
+    parser.add_option("-f", "--file", dest="xmlfilename",
+                      help="XML augment file to manipulate", metavar="FILE")
+    parser.add_option("-g", "--guid", dest="guid",
+                      help="OD GUID to manipulate", metavar="GUID")
+    parser.add_option("-n", "--node", dest="node",
+                      help="Partition node to assign to GUID", metavar="NODE")
+    parser.add_option("-c", "--enable-calendar", action="store_true", dest="enable_calendar",
+                      default=True, help="Enable calendaring for this GUID: %default")
+    parser.add_option("-a", "--auto-schedule", action="store_true", dest="auto_schedule",
+                      default=False, help="Enable auto-schedule for this GUID: %default")
+
+    (options, args) = parser.parse_args()
+
+    if len(args) != 1:
+        parser.error("incorrect number of arguments")
+
+    if args[0] == "add":
+        if not options.node:
+            parser.error("Partition node must be specified when adding")   
+        doAdd(options.xmlfilename, options.guid, options.node, options.enable_calendar, options.auto_schedule)
+    elif args[0] == "modify":
+        doModify(options.xmlfilename, options.guid, options.node, options.enable_calendar, options.auto_schedule)
+    elif args[0] == "remove":
+        doRemove(options.xmlfilename, options.guid)
+    elif args[0] == "print":
+        doPrint(options.xmlfilename)
+
+if __name__ == '__main__':
+    main()

Added: CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/managepostgres.py
===================================================================
--- CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/managepostgres.py	                        (rev 0)
+++ CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/managepostgres.py	2009-12-16 17:40:09 UTC (rev 4868)
@@ -0,0 +1,114 @@
+#!/usr/bin/env python
+##
+# Copyright (c) 2009 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from optparse import OptionParser
+import os
+import subprocess
+import sys
+import time
+
+def error(s):
+    print s
+    sys.exit(1)
+
+def cmd(s):
+    print s
+    subprocess.call(s, shell=True)
+
+def doInit(basedir):
+    
+    cmd("mkdir %s/data" % (basedir,))
+    cmd("%s/bin/initdb -D %s/data" % (basedir, basedir,))
+    
+    # Have the DB listen on all interfaces
+    with open("%s/data/postgresql.conf" % (basedir,)) as f:
+        conf = f.read()
+    conf = conf.replace("#listen_addresses = 'localhost'", "listen_addresses = '*'\t")
+    conf = conf.replace("max_connections = 20 ", "max_connections = 500")
+    with open("%s/data/postgresql.conf" % (basedir,), "w") as f:
+        f.write(conf)
+        
+    # Allow current user to auth to the DBs
+    with open("%s/data/pg_hba.conf" % (basedir,)) as f:
+        conf = f.read()
+    conf = conf.replace("127.0.0.1/32", "0.0.0.0/0   ")
+    with open("%s/data/pg_hba.conf" % (basedir,), "w") as f:
+        f.write(conf)
+
+    cmd("%s/bin/pg_ctl -D %s/data -l logfile start" % (basedir, basedir,))
+    time.sleep(5)
+    cmd("%s/bin/createdb proxies" % (basedir,))
+    cmd("%s/bin/createdb augments" % (basedir,))
+    cmd("%s/bin/pg_ctl -D %s/data -l logfile stop" % (basedir, basedir,))
+
+def doStart(basedir):
+    
+    cmd("%s/bin/pg_ctl -D %s/data -l logfile start" % (basedir, basedir,))
+
+def doStop(basedir):
+    
+    cmd("%s/bin/pg_ctl -D %s/data -l logfile stop" % (basedir, basedir,))
+
+def doRun(basedir, verbose):
+    
+    cmd("%s/bin/postgres %s -D %s/data" % (basedir, "-d 3" if verbose else "",  basedir,))
+
+def doClean(basedir):
+    
+    cmd("rm -rf %s/data" % (basedir, basedir,))
+
+if __name__ == '__main__':
+
+    usage = "%prog [options] ACTION"
+    epilog = """
+ACTION is one of init|start|stop|run
+
+  init:   initialize databases
+  start:  start postgres daemon
+  stop:   stop postgres daemon
+  run:    run postgres (non-daemon)
+  clean:  remove databases
+  
+"""
+    description = "Tool to manage PostgreSQL"
+    version = "%prog v1.0"
+    parser = OptionParser(usage=usage, description=description, version=version)
+    parser.epilog = epilog
+    parser.format_epilog = lambda _:epilog
+
+    parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
+                      default=True, help="Use debug logging for PostgreSQL")
+    parser.add_option("-d", "--base-dir", dest="basedir",
+                      default="%s/../postgresql-8.4.1/_root" % (os.getcwd(),), help="Base directory for PostgreSQL install")
+
+    (options, args) = parser.parse_args()
+
+    if len(args) != 1:
+        parser.error("incorrect number of arguments")
+
+    if args[0] == "init":
+        doInit(options.basedir)
+    elif args[0] == "start":
+        doStart(options.basedir)
+    elif args[0] == "stop":
+        doStop(options.basedir)
+    elif args[0] == "run":
+        doRun(options.basedir, options.verbose)
+    elif args[0] == "clean":
+        doClean(options.basedir)
+    else:
+        parser.error("incorrect argument '%s'" % (args[0],))


Property changes on: CalendarServer/branches/users/cdaboo/partition-4464/calendarserver/tools/managepostgres.py
___________________________________________________________________
Added: svn:executable
   + *

Modified: CalendarServer/branches/users/cdaboo/partition-4464/twistedcaldav/directory/augment.py
===================================================================
--- CalendarServer/branches/users/cdaboo/partition-4464/twistedcaldav/directory/augment.py	2009-12-16 15:42:35 UTC (rev 4867)
+++ CalendarServer/branches/users/cdaboo/partition-4464/twistedcaldav/directory/augment.py	2009-12-16 17:40:09 UTC (rev 4868)
@@ -73,6 +73,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.
@@ -114,6 +124,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.
@@ -167,6 +187,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.
@@ -195,23 +227,42 @@
             returnValue(record)
 
     @inlineCallbacks
-    def addAugmentRecord(self, record):
+    def addAugmentRecord(self, record, update=False):
 
         partitionid = (yield self._getPartitionID(record.hostedAt))
         
-        yield self.execute(
-            """insert into AUGMENTS
-            (GUID, ENABLED, PARTITIONID, CALENDARING, AUTOSCHEDULE)
-            values (:1, :2, :3, :4, :5)""",
-            (
-                record.guid,
-                "T" if record.enabled else "F",
-                partitionid,
-                "T" if record.enabledForCalendaring else "F",
-                "T" if record.autoSchedule else "F",
+        if update:
+            yield self.execute(
+                """update AUGMENTS set
+                (GUID, ENABLED, PARTITIONID, CALENDARING, AUTOSCHEDULE) =
+                (:1, :2, :3, :4, :5) where GUID = :6""",
+                (
+                    record.guid,
+                    "T" if record.enabled else "F",
+                    partitionid,
+                    "T" if record.enabledForCalendaring else "F",
+                    "T" if record.autoSchedule else "F",
+                    record.guid,
+                )
             )
-        )
+        else:
+            yield self.execute(
+                """insert into AUGMENTS
+                (GUID, ENABLED, PARTITIONID, CALENDARING, AUTOSCHEDULE)
+                values (:1, :2, :3, :4, :5)""",
+                (
+                    record.guid,
+                    "T" if record.enabled else "F",
+                    partitionid,
+                    "T" if record.enabledForCalendaring else "F",
+                    "T" if record.autoSchedule else "F",
+                )
+            )
 
+    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/20091216/e875f9cf/attachment-0001.html>


More information about the calendarserver-changes mailing list