<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14148] CalendarServer/trunk/calendarserver/tools</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14148">14148</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-11-07 17:05:42 -0800 (Fri, 07 Nov 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>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.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolsexportpy">CalendarServer/trunk/calendarserver/tools/export.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolsimporterpy">CalendarServer/trunk/calendarserver/tools/importer.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolsexportpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/export.py (14147 => 14148)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -35,20 +35,23 @@
</span><span class="cx">
</span><span class="cx"> from __future__ import print_function
</span><span class="cx">
</span><ins>+import itertools
</ins><span class="cx"> import os
</span><ins>+import shutil
</ins><span class="cx"> import sys
</span><del>-import itertools
</del><span class="cx">
</span><ins>+from calendarserver.tools.cmdline import utilityMain, WorkerService
+from twext.python.log import Logger
+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</ins><span class="cx"> from twisted.python.text import wordWrap
</span><span class="cx"> from twisted.python.usage import Options, UsageError
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</del><ins>+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
</ins><span class="cx">
</span><del>-from twext.python.log import Logger
-from twistedcaldav.ical import Component
</del><span class="cx">
</span><del>-from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
-from calendarserver.tools.cmdline import utilityMain, WorkerService
-
</del><span class="cx"> log = Logger()
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -100,6 +103,7 @@
</span><span class="cx"> super(ExportOptions, self).__init__()
</span><span class="cx"> self.exporters = []
</span><span class="cx"> self.outputName = '-'
</span><ins>+ self.outputDirectoryName = None
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> def opt_uid(self, uid):
</span><span class="lines">@@ -132,6 +136,15 @@
</span><span class="cx"> opt_c = opt_collection
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def opt_directory(self, dirname):
+ """
+ Specify output directory path.
+ """
+ self.outputDirectoryName = dirname
+
+ opt_d = opt_directory
+
+
</ins><span class="cx"> def opt_output(self, filename):
</span><span class="cx"> """
</span><span class="cx"> Specify output file path (default: '-', meaning stdout).
</span><span class="lines">@@ -278,7 +291,9 @@
</span><span class="cx"> for calendar in calendars:
</span><span class="cx"> calendar = yield calendar
</span><span class="cx"> for obj in (yield calendar.calendarObjects()):
</span><del>- evt = yield obj.filteredComponent(calendar.ownerCalendarHome().uid(), True)
</del><ins>+ evt = yield obj.filteredComponent(
+ calendar.ownerCalendarHome().uid(), True
+ )
</ins><span class="cx"> for sub in evt.subcomponents():
</span><span class="cx"> if sub.name() != 'VTIMEZONE':
</span><span class="cx"> # Omit all VTIMEZONE components here - we will include them later
</span><span class="lines">@@ -288,7 +303,54 @@
</span><span class="cx"> fileobj.write(comp.getTextWithTimezones(True))
</span><span class="cx">
</span><span class="cx">
</span><ins>+@inlineCallbacks
+def exportToDirectory(calendars, dirname):
+ """
+ Export some calendars to a file as their owner would see them.
</ins><span class="cx">
</span><ins>+ @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()
+
+
+
</ins><span class="cx"> class ExporterService(WorkerService, object):
</span><span class="cx"> """
</span><span class="cx"> Service which runs, exports the appropriate records, then stops the reactor.
</span><span class="lines">@@ -310,16 +372,26 @@
</span><span class="cx"> """
</span><span class="cx"> txn = self.store.newTransaction()
</span><span class="cx"> try:
</span><ins>+
</ins><span class="cx"> allCalendars = itertools.chain(
</span><span class="cx"> *[(yield exporter.listCalendars(txn, self)) for exporter in
</span><span class="cx"> self.options.exporters]
</span><span class="cx"> )
</span><del>- yield exportToFile(allCalendars, self.output)
</del><ins>+
+ 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()
+
</ins><span class="cx"> yield txn.commit()
</span><span class="cx"> # TODO: should be read-only, so commit/abort shouldn't make a
</span><span class="cx"> # difference. commit() for now, in case any transparent cache /
</span><span class="cx"> # update stuff needed to happen, don't want to undo it.
</span><del>- self.output.close()
</del><span class="cx"> except:
</span><span class="cx"> log.failure("doWork()")
</span><span class="cx">
</span><span class="lines">@@ -356,12 +428,16 @@
</span><span class="cx"> except UsageError, e:
</span><span class="cx"> usage(e)
</span><span class="cx">
</span><del>- try:
- output = options.openOutput()
- except IOError, e:
- stderr.write("Unable to open output file for writing: %s\n" %
- (e))
- sys.exit(1)
</del><ins>+ 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)
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> def makeService(store):
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsimporterpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/importer.py (14147 => 14148)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -97,8 +97,18 @@
</span><span class="cx"> def __init__(self):
</span><span class="cx"> super(ImportOptions, self).__init__()
</span><span class="cx"> self.inputName = '-'
</span><ins>+ self.inputDirectoryName = None
</ins><span class="cx">
</span><span class="cx">
</span><ins>+ def opt_directory(self, dirname):
+ """
+ Specify input directory path.
+ """
+ self.inputDirectoryName = dirname
+
+ opt_d = opt_directory
+
+
</ins><span class="cx"> def opt_input(self, filename):
</span><span class="cx"> """
</span><span class="cx"> Specify input file path (default: '-', meaning stdin).
</span><span class="lines">@@ -253,10 +263,9 @@
</span><span class="cx"> Service which runs, imports the data, then stops the reactor.
</span><span class="cx"> """
</span><span class="cx">
</span><del>- def __init__(self, store, options, input, reactor, config):
</del><ins>+ def __init__(self, store, options, reactor, config):
</ins><span class="cx"> super(ImporterService, self).__init__(store)
</span><span class="cx"> self.options = options
</span><del>- self.input = input
</del><span class="cx"> self.reactor = reactor
</span><span class="cx"> self.config = config
</span><span class="cx"> self._directory = self.store.directoryService()
</span><span class="lines">@@ -271,9 +280,33 @@
</span><span class="cx"> Do the export, stopping the reactor when done.
</span><span class="cx"> """
</span><span class="cx"> try:
</span><del>- component = Component.allFromStream(self.input)
- self.input.close()
- yield importCollectionComponent(self.store, component)
</del><ins>+ 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)
</ins><span class="cx"> except:
</span><span class="cx"> log.failure("doWork()")
</span><span class="cx">
</span><span class="lines">@@ -297,7 +330,7 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><del>-def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
</del><ins>+def main(argv=sys.argv, reactor=None):
</ins><span class="cx"> """
</span><span class="cx"> Do the import.
</span><span class="cx"> """
</span><span class="lines">@@ -310,16 +343,8 @@
</span><span class="cx"> except UsageError, e:
</span><span class="cx"> usage(e)
</span><span class="cx">
</span><del>- try:
- input = options.openInput()
- except IOError, e:
- stderr.write("Unable to open input file for reading: %s\n" %
- (e))
- sys.exit(1)
-
-
</del><span class="cx"> def makeService(store):
</span><span class="cx"> from twistedcaldav.config import config
</span><del>- return ImporterService(store, options, input, reactor, config)
</del><ins>+ return ImporterService(store, options, reactor, config)
</ins><span class="cx">
</span><span class="cx"> utilityMain(options["config"], makeService, reactor, verbose=options["debug"])
</span></span></pre>
</div>
</div>
</body>
</html>