[CalendarServer-changes] [14148] CalendarServer/trunk/calendarserver/tools

source_changes at macosforge.org source_changes at macosforge.org
Fri Nov 7 17:05:42 PST 2014


Revision: 14148
          http://trac.calendarserver.org//changeset/14148
Author:   sagen at apple.com
Date:     2014-11-07 17:05:42 -0800 (Fri, 07 Nov 2014)
Log Message:
-----------
calendarserver_export can now use -d <dirname> to export to a directory, with a .ics per calendar.  calendarserver_import -d <dirname> will read in all the files from that directory.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/export.py
    CalendarServer/trunk/calendarserver/tools/importer.py

Modified: CalendarServer/trunk/calendarserver/tools/export.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/export.py	2014-11-07 21:27:24 UTC (rev 14147)
+++ CalendarServer/trunk/calendarserver/tools/export.py	2014-11-08 01:05:42 UTC (rev 14148)
@@ -35,20 +35,23 @@
 
 from __future__ import print_function
 
+import itertools
 import os
+import shutil
 import sys
-import itertools
 
+from calendarserver.tools.cmdline import utilityMain, WorkerService
+from twext.python.log import Logger
+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options, UsageError
-from twisted.internet.defer import inlineCallbacks, returnValue, succeed
+from twistedcaldav import customxml
+from twistedcaldav.ical import Component, Property
+from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
+from txdav.base.propertystore.base import PropertyName
+from txdav.xml import element as davxml
 
-from twext.python.log import Logger
-from twistedcaldav.ical import Component
 
-from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
-from calendarserver.tools.cmdline import utilityMain, WorkerService
-
 log = Logger()
 
 
@@ -100,6 +103,7 @@
         super(ExportOptions, self).__init__()
         self.exporters = []
         self.outputName = '-'
+        self.outputDirectoryName = None
 
 
     def opt_uid(self, uid):
@@ -132,6 +136,15 @@
     opt_c = opt_collection
 
 
+    def opt_directory(self, dirname):
+        """
+        Specify output directory path.
+        """
+        self.outputDirectoryName = dirname
+
+    opt_d = opt_directory
+
+
     def opt_output(self, filename):
         """
         Specify output file path (default: '-', meaning stdout).
@@ -278,7 +291,9 @@
     for calendar in calendars:
         calendar = yield calendar
         for obj in (yield calendar.calendarObjects()):
-            evt = yield obj.filteredComponent(calendar.ownerCalendarHome().uid(), True)
+            evt = yield obj.filteredComponent(
+                calendar.ownerCalendarHome().uid(), True
+            )
             for sub in evt.subcomponents():
                 if sub.name() != 'VTIMEZONE':
                     # Omit all VTIMEZONE components here - we will include them later
@@ -288,7 +303,54 @@
     fileobj.write(comp.getTextWithTimezones(True))
 
 
+ at inlineCallbacks
+def exportToDirectory(calendars, dirname):
+    """
+    Export some calendars to a file as their owner would see them.
 
+    @param calendars: an iterable of L{ICalendar} providers (or L{Deferred}s of
+        same).
+
+    @param dirname: the path to a directory to store calendar files in; each
+        calendar being exported will have it's own .ics file
+
+    @return: a L{Deferred} which fires when the export is complete.  (Note that
+        the file will not be closed.)
+    @rtype: L{Deferred} that fires with C{None}
+    """
+
+    for calendar in calendars:
+        homeUID = calendar.ownerCalendarHome().uid()
+
+        calendarProperties = calendar.properties()
+        comp = Component.newCalendar()
+        for element, propertyName in (
+            (davxml.DisplayName, "NAME"),
+            (customxml.CalendarColor, "COLOR"),
+        ):
+
+            value = calendarProperties.get(PropertyName.fromElement(element), None)
+            if value:
+                comp.addProperty(Property(propertyName, str(value)))
+
+        source = "/calendars/__uids__/{}/{}/".format(homeUID, calendar.name())
+        comp.addProperty(Property("SOURCE", source))
+
+        for obj in (yield calendar.calendarObjects()):
+            evt = yield obj.filteredComponent(homeUID, True)
+            for sub in evt.subcomponents():
+                if sub.name() != 'VTIMEZONE':
+                    # Omit all VTIMEZONE components here - we will include them later
+                    # when we serialize the whole calendar.
+                    comp.addComponent(sub)
+
+        filename = os.path.join(dirname, "{}_{}.ics".format(homeUID, calendar.name()))
+        fileobj = open(filename, 'wb')
+        fileobj.write(comp.getTextWithTimezones(True))
+        fileobj.close()
+
+
+
 class ExporterService(WorkerService, object):
     """
     Service which runs, exports the appropriate records, then stops the reactor.
@@ -310,16 +372,26 @@
         """
         txn = self.store.newTransaction()
         try:
+
             allCalendars = itertools.chain(
                 *[(yield exporter.listCalendars(txn, self)) for exporter in
                   self.options.exporters]
             )
-            yield exportToFile(allCalendars, self.output)
+
+            if self.options.outputDirectoryName:
+                dirname = self.options.outputDirectoryName
+                if os.path.exists(dirname):
+                    shutil.rmtree(dirname)
+                os.mkdir(dirname)
+                yield exportToDirectory(allCalendars, dirname)
+            else:
+                yield exportToFile(allCalendars, self.output)
+                self.output.close()
+
             yield txn.commit()
             # TODO: should be read-only, so commit/abort shouldn't make a
             # difference.  commit() for now, in case any transparent cache /
             # update stuff needed to happen, don't want to undo it.
-            self.output.close()
         except:
             log.failure("doWork()")
 
@@ -356,12 +428,16 @@
     except UsageError, e:
         usage(e)
 
-    try:
-        output = options.openOutput()
-    except IOError, e:
-        stderr.write("Unable to open output file for writing: %s\n" %
-                     (e))
-        sys.exit(1)
+    if options.outputDirectoryName:
+        output = None
+    else:
+        try:
+            output = options.openOutput()
+        except IOError, e:
+            stderr.write(
+                "Unable to open output file for writing: %s\n" % (e)
+            )
+            sys.exit(1)
 
 
     def makeService(store):

Modified: CalendarServer/trunk/calendarserver/tools/importer.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/importer.py	2014-11-07 21:27:24 UTC (rev 14147)
+++ CalendarServer/trunk/calendarserver/tools/importer.py	2014-11-08 01:05:42 UTC (rev 14148)
@@ -97,8 +97,18 @@
     def __init__(self):
         super(ImportOptions, self).__init__()
         self.inputName = '-'
+        self.inputDirectoryName = None
 
 
+    def opt_directory(self, dirname):
+        """
+        Specify input directory path.
+        """
+        self.inputDirectoryName = dirname
+
+    opt_d = opt_directory
+
+
     def opt_input(self, filename):
         """
         Specify input file path (default: '-', meaning stdin).
@@ -253,10 +263,9 @@
     Service which runs, imports the data, then stops the reactor.
     """
 
-    def __init__(self, store, options, input, reactor, config):
+    def __init__(self, store, options, reactor, config):
         super(ImporterService, self).__init__(store)
         self.options = options
-        self.input = input
         self.reactor = reactor
         self.config = config
         self._directory = self.store.directoryService()
@@ -271,9 +280,33 @@
         Do the export, stopping the reactor when done.
         """
         try:
-            component = Component.allFromStream(self.input)
-            self.input.close()
-            yield importCollectionComponent(self.store, component)
+            if self.options.inputDirectoryName:
+                dirname = self.options.inputDirectoryName
+                if not os.path.exists(dirname):
+                    sys.stderr.write(
+                        "Directory does not exist: {}\n".format(dirname)
+                    )
+                    sys.exit(1)
+                for filename in os.listdir(dirname):
+                    fullpath = os.path.join(dirname, filename)
+                    print("Importing {}".format(fullpath))
+                    fileobj = open(fullpath, 'r')
+                    component = Component.allFromStream(fileobj)
+                    fileobj.close()
+                    yield importCollectionComponent(self.store, component)
+
+            else:
+                try:
+                    input = self.options.openInput()
+                except IOError, e:
+                    sys.stderr.write(
+                        "Unable to open input file for reading: %s\n" % (e)
+                    )
+                    sys.exit(1)
+
+                component = Component.allFromStream(input)
+                input.close()
+                yield importCollectionComponent(self.store, component)
         except:
             log.failure("doWork()")
 
@@ -297,7 +330,7 @@
 
 
 
-def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
+def main(argv=sys.argv, reactor=None):
     """
     Do the import.
     """
@@ -310,16 +343,8 @@
     except UsageError, e:
         usage(e)
 
-    try:
-        input = options.openInput()
-    except IOError, e:
-        stderr.write("Unable to open input file for reading: %s\n" %
-                     (e))
-        sys.exit(1)
-
-
     def makeService(store):
         from twistedcaldav.config import config
-        return ImporterService(store, options, input, reactor, config)
+        return ImporterService(store, options, reactor, config)
 
     utilityMain(options["config"], makeService, reactor, verbose=options["debug"])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20141107/b2d9d03c/attachment-0001.html>


More information about the calendarserver-changes mailing list