[CalendarServer-changes] [4605] CalendarServer/branches/users/cdaboo/deployment-partition-4593
source_changes at macosforge.org
source_changes at macosforge.org
Tue Oct 20 08:45:40 PDT 2009
Revision: 4605
http://trac.macosforge.org/projects/calendarserver/changeset/4605
Author: cdaboo at apple.com
Date: 2009-10-20 08:45:38 -0700 (Tue, 20 Oct 2009)
Log Message:
-----------
Tool to allow loading of an sqlite or PostgreSQL augment DB with records from an XML file.
Added Paths:
-----------
CalendarServer/branches/users/cdaboo/deployment-partition-4593/bin/caldav_load_augmentdb
CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/
CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/__init__.py
CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/
CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/__init__.py
CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/util.py
Added: CalendarServer/branches/users/cdaboo/deployment-partition-4593/bin/caldav_load_augmentdb
===================================================================
--- CalendarServer/branches/users/cdaboo/deployment-partition-4593/bin/caldav_load_augmentdb (rev 0)
+++ CalendarServer/branches/users/cdaboo/deployment-partition-4593/bin/caldav_load_augmentdb 2009-10-20 15:45:38 UTC (rev 4605)
@@ -0,0 +1,163 @@
+#!/usr/bin/env python
+from twistedcaldav.directory import augment
+from twistedcaldav.directory.augment import AugmentXMLDB
+
+##
+# 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.
+##
+
+
+from getopt import getopt, GetoptError
+from grp import getgrnam
+from pwd import getpwnam
+from sys import stdout, stderr
+from twisted.internet import reactor
+from twisted.python.log import addObserver, removeObserver
+from twisted.python.util import switchUID
+from twistedcaldav.config import config, ConfigurationError
+from twistedcaldav.log import setLogLevelForNamespace
+from twisted.internet.defer import inlineCallbacks
+from calendarserver.tools.util import loadConfig, getDirectory,\
+ autoDisableMemcached
+import os
+import sys
+
+class UsageError (StandardError):
+ pass
+
+class StandardIOObserver (object):
+ """
+ Log observer that writes to standard I/O.
+ """
+ def emit(self, eventDict):
+ text = None
+
+ if eventDict["isError"]:
+ output = stderr
+ if "failure" in eventDict:
+ text = eventDict["failure"].getTraceback()
+ else:
+ output = stdout
+
+ if not text:
+ text = " ".join([str(m) for m in eventDict["message"]]) + "\n"
+
+ output.write(text)
+ output.flush()
+
+ def start(self):
+ addObserver(self.emit)
+
+ def stop(self):
+ removeObserver(self.emit)
+
+def usage(e=None):
+ if e:
+ print e
+ print ""
+
+ name = os.path.basename(sys.argv[0])
+ print "usage: %s [options]" % (name,)
+ print ""
+ print "Populate an sqlite or PostgreSQL augments database with values"
+ print "from an XML augments file."
+ print ""
+ print "options:"
+ print " -h --help: print this help and exit"
+ print " -f --config: Specify caldavd.plist configuration path"
+ print " -x --xmlfile: Specify xml augments file path"
+
+ if e:
+ sys.exit(64)
+ else:
+ sys.exit(0)
+
+def main():
+ try:
+ (optargs, args) = getopt(
+ sys.argv[1:], "hf:x:", [
+ "config=",
+ "xmlfile=",
+ "help",
+ ],
+ )
+ except GetoptError, e:
+ usage(e)
+
+ configFileName = None
+ xmlFileName = None
+
+ for opt, arg in optargs:
+ if opt in ("-h", "--help"):
+ usage()
+
+ elif opt in ("-f", "--config"):
+ configFileName = arg
+
+ elif opt in ("-x", "--xmlfile"):
+ xmlFileName = arg
+
+ if args:
+ usage("Too many arguments: %s" % (" ".join(args),))
+
+ observer = StandardIOObserver()
+ observer.start()
+
+ #
+ # Get configuration
+ #
+ try:
+ loadConfig(configFileName)
+ setLogLevelForNamespace(None, "warn")
+
+ # Shed privileges
+ if config.UserName and config.GroupName and os.getuid() == 0:
+ uid = getpwnam(config.UserName).pw_uid
+ gid = getgrnam(config.GroupName).gr_gid
+ switchUID(uid, uid, gid)
+
+ os.umask(config.umask)
+
+ config.directory = getDirectory()
+ autoDisableMemcached(config)
+ except ConfigurationError, e:
+ usage("Unable to start: %s" % (e,))
+
+ try:
+ dbxml = AugmentXMLDB((xmlFileName,))
+ except IOError, e:
+ usage("Could not read XML augment file: %s" % (e,))
+
+ #
+ # Start the reactor
+ #
+ reactor.callLater(0, run, dbxml)
+ reactor.run()
+
+ at inlineCallbacks
+def run(dbxml):
+
+ try:
+ yield augment.AugmentService.clean()
+ for record in dbxml.db.values():
+ yield augment.AugmentService.addAugmentRecord(record)
+ finally:
+ #
+ # Stop the reactor
+ #
+ reactor.stop()
+
+if __name__ == "__main__":
+ main()
Added: CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/util.py
===================================================================
--- CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/util.py (rev 0)
+++ CalendarServer/branches/users/cdaboo/deployment-partition-4593/calendarserver/tools/util.py 2009-10-20 15:45:38 UTC (rev 4605)
@@ -0,0 +1,134 @@
+##
+# Copyright (c) 2008-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.
+##
+
+__all__ = [
+ "loadConfig",
+ "getDirectory",
+ "UsageError",
+ "booleanArgument",
+]
+
+import os
+
+from twisted.python.reflect import namedClass
+
+import socket
+from twistedcaldav.config import config, ConfigurationError
+from twistedcaldav.directory import augment
+from twistedcaldav.directory.directory import DirectoryService
+
+def loadConfig(configFileName):
+ if configFileName is None:
+ configFileName = "/etc/caldav/caldavd.plist"
+
+ if not os.path.isfile(configFileName):
+ raise ConfigurationError("No config file: %s" % (configFileName,))
+
+ config.loadConfig(configFileName)
+
+ return config
+
+def getDirectory():
+ BaseDirectoryService = namedClass(config.DirectoryService.type)
+
+ class MyDirectoryService (BaseDirectoryService):
+ def getPrincipalCollection(self):
+ if not hasattr(self, "_principalCollection"):
+ #
+ # Instantiating a CalendarHomeProvisioningResource with a directory
+ # will register it with the directory (still smells like a hack).
+ #
+ # We need that in order to locate calendar homes via the directory.
+ #
+ from twistedcaldav.static import CalendarHomeProvisioningFile
+ CalendarHomeProvisioningFile(os.path.join(config.DocumentRoot, "calendars"), self, "/calendars/")
+
+ from twistedcaldav.directory.principal import DirectoryPrincipalProvisioningResource
+ self._principalCollection = DirectoryPrincipalProvisioningResource("/principals/", self)
+
+ return self._principalCollection
+
+ def setPrincipalCollection(self, coll):
+ # See principal.py line 237: self.directory.principalCollection = self
+ pass
+
+ principalCollection = property(getPrincipalCollection, setPrincipalCollection)
+
+ def calendarHomeForRecord(self, record):
+ principal = self.principalCollection.principalForRecord(record)
+ if principal:
+ try:
+ return principal.calendarHome()
+ except AttributeError:
+ pass
+ return None
+
+ def calendarHomeForShortName(self, recordType, shortName):
+ principal = self.principalCollection.principalForShortName(recordType, shortName)
+ if principal:
+ return principal.calendarHome()
+ return None
+
+ def principalForCalendarUserAddress(self, cua):
+ return self.principalCollection.principalForCalendarUserAddress(cua)
+
+
+ # Wait for directory service to become available
+ directory = MyDirectoryService(**config.DirectoryService["params"])
+
+ augmentClass = namedClass(config.AugmentService.type)
+ augment.AugmentService = augmentClass(**config.AugmentService.params)
+
+ return directory
+
+class DummyDirectoryService (DirectoryService):
+ realmName = ""
+ baseGUID = "51856FD4-5023-4890-94FE-4356C4AAC3E4"
+ def recordTypes(self): return ()
+ def listRecords(self): return ()
+ def recordWithShortName(self): return None
+
+class UsageError (StandardError):
+ pass
+
+def booleanArgument(arg):
+ if arg in ("true", "yes", "yup", "uh-huh", "1", "t", "y"):
+ return True
+ elif arg in ("false", "no", "nope", "nuh-uh", "0", "f", "n"):
+ return False
+ else:
+ raise ValueError("Not a boolean: %s" % (arg,))
+
+
+
+
+
+def autoDisableMemcached(config):
+ """
+ If memcached is not running, set config.Memcached.ClientEnabled to False
+ """
+
+ if not config.Memcached.ClientEnabled:
+ return
+
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+ try:
+ s.connect((config.Memcached.Pools.Default.BindAddress, config.Memcached.Pools.Default.Port))
+ s.close()
+
+ except socket.error:
+ config.Memcached.ClientEnabled = False
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20091020/7c4329a4/attachment.html>
More information about the calendarserver-changes
mailing list