[CalendarServer-changes] [7473] CalendarServer/branches/users/glyph/new-export/calendarserver/tools
source_changes at macosforge.org
source_changes at macosforge.org
Mon May 16 07:45:49 PDT 2011
Revision: 7473
http://trac.macosforge.org/projects/calendarserver/changeset/7473
Author: glyph at apple.com
Date: 2011-05-16 07:45:49 -0700 (Mon, 16 May 2011)
Log Message:
-----------
Start adding coverage for main(); test errors in opening the output file, error message. Start removing re-implemented logic from the old main function.
Modified Paths:
--------------
CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py
CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py
Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py 2011-05-16 14:45:37 UTC (rev 7472)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py 2011-05-16 14:45:49 UTC (rev 7473)
@@ -190,7 +190,7 @@
-def main():
+def oldmain():
# quiet pyflakes while I'm working on this.
from stopbotheringme import CalDAVFile
try:
@@ -206,7 +206,6 @@
usage(e)
configFileName = None
- outputFileName = None
collections = set()
calendarHomes = set()
@@ -224,12 +223,6 @@
elif opt in ("-f", "--config"):
configFileName = arg
- elif opt in ("-o", "--output"):
- if arg == "-":
- outputFileName = None
- else:
- outputFileName = arg
-
elif opt in ("-c", "--collection"):
path = abspath(arg)
collection = CalDAVFile(path)
@@ -290,50 +283,37 @@
calendar = Component.newCalendar()
uids = set()
- tzids = set()
for collection in collections:
for name, uid, type in collection.index().indexedSearch(None):
child = collection.getChild(name)
- childData = child.iCalendarText()
- try:
- childCalendar = Component.fromString(childData)
- except ValueError:
- continue
-
if uid in uids:
sys.stderr.write("Skipping duplicate event UID %r from %s\n" % (uid, collection.fp.path))
continue
else:
uids.add(uid)
- for component in childCalendar.subcomponents():
- # Only insert VTIMEZONEs once
- if component.name() == "VTIMEZONE":
- tzid = component.propertyValue("TZID")
- if tzid in tzids:
- continue
- else:
- tzids.add(tzid)
-
- calendar.addComponent(component)
-
calendarData = str(calendar)
-
- if outputFileName:
- try:
- output = open(outputFileName, "w")
- except IOError, e:
- sys.stderr.write("Unable to open output file for writing %s: %s\n" % (outputFileName, e))
- sys.exit(1)
- else:
- output = sys.stdout
-
+ output = sys.stdout
output.write(calendarData)
-
except UsageError, e:
usage(e)
-if __name__ == "__main__":
- main()
+
+
+def main(argv=sys.argv, stderr=sys.stderr):
+ """
+ Do the export.
+ """
+ options = ExportOptions()
+ options.parseOptions(argv[1:])
+ try:
+ output = options.openOutput()
+ except IOError, e:
+ stderr.write("Unable to open output file for writing: %s\n" %
+ (e))
+ sys.exit(1)
+ output #pyflakes
+
+
Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py 2011-05-16 14:45:37 UTC (rev 7472)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py 2011-05-16 14:45:49 UTC (rev 7473)
@@ -31,7 +31,7 @@
from twext.enterprise.ienterprise import AlreadyFinishedError
from twistedcaldav.ical import Component
-from calendarserver.tools.export import ExportOptions
+from calendarserver.tools.export import ExportOptions, main
from calendarserver.tools.export import HomeExporter
from twistedcaldav.datafilters.test.test_peruserdata import dataForTwoUsers
from twistedcaldav.datafilters.test.test_peruserdata import resultForUser2
@@ -139,7 +139,24 @@
self.assertEquals(eo.openOutput().name, tmpnam)
+ def test_outputFileError(self):
+ """
+ If the output file cannot be opened for writing, an error will be
+ displayed to the user on stderr.
+ """
+ io = StringIO()
+ systemExit = self.assertRaises(
+ SystemExit, main, ['calendarserver_export',
+ '--output', '/not/a/file'], io
+ )
+ self.assertEquals(systemExit.code, 1)
+ self.assertEquals(
+ io.getvalue(),
+ "Unable to open output file for writing: "
+ "[Errno 2] No such file or directory: '/not/a/file'\n")
+
+
class IntegrationTests(TestCase):
"""
Tests for exporting data from a live store.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110516/9853e1d1/attachment.html>
More information about the calendarserver-changes
mailing list