<!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>[15473] CalendarServer/trunk/calendarserver/tools/calverify.py</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/15473">15473</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2016-03-11 19:52:28 -0800 (Fri, 11 Mar 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add option to upgrade calendar data by scanning/reading each resource.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolscalverifypy">CalendarServer/trunk/calendarserver/tools/calverify.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolscalverifypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/calverify.py (15472 => 15473)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/calverify.py        2016-03-12 00:49:48 UTC (rev 15472)
+++ CalendarServer/trunk/calendarserver/tools/calverify.py        2016-03-12 03:52:28 UTC (rev 15473)
</span><span class="lines">@@ -239,6 +239,7 @@
</span><span class="cx"> --double            : detect double-bookings.
</span><span class="cx"> --dark-purge        : purge room/resource events with invalid organizer
</span><span class="cx"> --split             : split recurring event
</span><ins>+--upgrade           : upgrade iCalendar data.
</ins><span class="cx"> 
</span><span class="cx"> --nuke PATH|RID     : remove specific calendar resources - can
</span><span class="cx">                       only be used by itself. PATH is the full
</span><span class="lines">@@ -251,7 +252,7 @@
</span><span class="cx"> --config   : caldavd.plist file for the server.
</span><span class="cx"> -v         : verbose logging
</span><span class="cx"> 
</span><del>-Options for --ical:
</del><ins>+Options for --ical / --upgrade:
</ins><span class="cx"> 
</span><span class="cx"> --uuid     : only scan specified calendar homes. Can be a partial GUID
</span><span class="cx">              to scan all GUIDs with that as a prefix.
</span><span class="lines">@@ -336,6 +337,8 @@
</span><span class="cx">         ['dark-purge', 'p', &quot;Purge room/resource events with invalid organizer.&quot;],
</span><span class="cx">         ['missing-location', 'g', &quot;Room events with missing location.&quot;],
</span><span class="cx">         ['split', 'l', &quot;Split an event.&quot;],
</span><ins>+        ['upgrade', 'u', &quot;Upgrade calendar data.&quot;],
+
</ins><span class="cx">         ['fix', 'x', &quot;Fix problems.&quot;],
</span><span class="cx">         ['verbose', 'v', &quot;Verbose logging.&quot;],
</span><span class="cx">         ['details', 'V', &quot;Detailed logging.&quot;],
</span><span class="lines">@@ -2994,6 +2997,153 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class UpgradeDataService(CalVerifyService):
+    &quot;&quot;&quot;
+    Service which scans calendar data.
+    &quot;&quot;&quot;
+
+    def title(self):
+        return &quot;Upgrade Data Service&quot;
+
+
+    @inlineCallbacks
+    def doAction(self):
+
+        self.output.write(&quot;\n---- Scanning calendar data ----\n&quot;)
+
+        self.now = DateTime.getNowUTC()
+        self.start = DateTime.getToday()
+        self.start.setDateOnly(False)
+        self.end = self.start.duplicate()
+        self.end.offsetYear(1)
+        self.fix = self.options[&quot;fix&quot;]
+
+        self.tzid = Timezone(tzid=self.options[&quot;tzid&quot;] if self.options[&quot;tzid&quot;] else &quot;America/Los_Angeles&quot;)
+
+        self.txn = self.store.newTransaction()
+
+        if self.options[&quot;verbose&quot;]:
+            t = time.time()
+        descriptor = None
+        if self.options[&quot;uuid&quot;]:
+            rows = yield self.getAllResourceInfoWithUUID(self.options[&quot;uuid&quot;], inbox=True)
+            descriptor = &quot;getAllResourceInfoWithUUID&quot;
+        elif self.options[&quot;uid&quot;]:
+            rows = yield self.getAllResourceInfoWithUID(self.options[&quot;uid&quot;], inbox=True)
+            descriptor = &quot;getAllResourceInfoWithUID&quot;
+        else:
+            rows = yield self.getAllResourceInfo(inbox=True)
+            descriptor = &quot;getAllResourceInfo&quot;
+
+        yield self.txn.commit()
+        self.txn = None
+
+        if self.options[&quot;verbose&quot;]:
+            self.output.write(&quot;%s time: %.1fs\n&quot; % (descriptor, time.time() - t,))
+
+        self.total = len(rows)
+        self.logResult(&quot;Number of events to process&quot;, self.total)
+        self.addSummaryBreak()
+
+        yield self.calendarDataUpgrade(rows)
+
+        self.printSummary()
+
+
+    @inlineCallbacks
+    def calendarDataUpgrade(self, rows):
+        &quot;&quot;&quot;
+        Check each calendar resource for valid iCalendar data.
+        &quot;&quot;&quot;
+
+        self.output.write(&quot;\n---- Upgrading each calendar object resource ----\n&quot;)
+        self.txn = self.store.newTransaction()
+
+        if self.options[&quot;verbose&quot;]:
+            t = time.time()
+
+        results_bad = []
+        count = 0
+        total = len(rows)
+        upgradelen = 0
+        badlen = 0
+        rjust = 10
+        for owner, resid, uid, _ignore_calname, _ignore_md5, _ignore_organizer, _ignore_created, _ignore_modified in rows:
+            try:
+                calendarObj = yield CalendarStoreFeatures(self.txn._store).calendarObjectWithID(self.txn, resid)
+                if calendarObj._dataversion &lt; calendarObj._currentDataVersion:
+                    upgradelen += 1
+                yield calendarObj.component(doUpdate=True)
+                result = True
+            except Exception, e:
+                result = False
+                message = &quot;Exception when reading resource&quot;
+                if self.options[&quot;verbose&quot;]:
+                    print(e)
+            if not result:
+                results_bad.append((owner, uid, resid, message))
+                badlen += 1
+            count += 1
+            if self.options[&quot;verbose&quot;]:
+                if count == 1:
+                    self.output.write(&quot;Upgraded&quot;.rjust(rjust) + &quot;Bad&quot;.rjust(rjust) + &quot;Current&quot;.rjust(rjust) + &quot;Total&quot;.rjust(rjust) + &quot;Complete&quot;.rjust(rjust) + &quot;\n&quot;)
+                if divmod(count, 100)[1] == 0:
+                    self.output.write((
+                        &quot;\r&quot; +
+                        (&quot;%s&quot; % upgradelen).rjust(rjust) +
+                        (&quot;%s&quot; % badlen).rjust(rjust) +
+                        (&quot;%s&quot; % count).rjust(rjust) +
+                        (&quot;%s&quot; % total).rjust(rjust) +
+                        (&quot;%d%%&quot; % safePercent(count, total)).rjust(rjust)
+                    ).ljust(80))
+                    self.output.flush()
+
+            # To avoid holding locks on all the rows scanned, commit every 100 resources
+            if divmod(count, 100)[1] == 0:
+                yield self.txn.commit()
+                self.txn = self.store.newTransaction()
+
+        yield self.txn.commit()
+        self.txn = None
+        if self.options[&quot;verbose&quot;]:
+            self.output.write((
+                &quot;\r&quot; +
+                (&quot;%s&quot; % upgradelen).rjust(rjust) +
+                (&quot;%s&quot; % badlen).rjust(rjust) +
+                (&quot;%s&quot; % count).rjust(rjust) +
+                (&quot;%s&quot; % total).rjust(rjust) +
+                (&quot;%d%%&quot; % safePercent(count, total)).rjust(rjust)
+            ).ljust(80) + &quot;\n&quot;)
+
+        # Print table of results
+        table = tables.Table()
+        table.addHeader((&quot;Owner&quot;, &quot;Event UID&quot;, &quot;RID&quot;, &quot;Problem&quot;,))
+        for item in sorted(results_bad, key=lambda x: (x[0], x[1])):
+            owner, uid, resid, message = item
+            owner_record = yield self.directoryService().recordWithUID(owner)
+            table.addRow((
+                &quot;%s/%s (%s)&quot; % (owner_record.recordType if owner_record else &quot;-&quot;, owner_record.shortNames[0] if owner_record else &quot;-&quot;, owner,),
+                uid,
+                resid,
+                message,
+            ))
+
+        self.output.write(&quot;\n&quot;)
+        self.logResult(&quot;Upgraded iCalendar data&quot;, upgradelen, total)
+        self.logResult(&quot;Bad iCalendar data&quot;, len(results_bad), total)
+        self.results[&quot;Upgraded iCalendar data&quot;] = upgradelen
+        self.results[&quot;Bad iCalendar data&quot;] = results_bad
+        table.printTable(os=self.output)
+
+        if self.options[&quot;verbose&quot;]:
+            diff_time = time.time() - t
+            self.output.write(&quot;Time: %.2f s  Average: %.1f ms/resource\n&quot; % (
+                diff_time,
+                safePercent(diff_time, total, 1000.0),
+            ))
+
+
+
</ins><span class="cx"> def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
</span><span class="cx"> 
</span><span class="cx">     if reactor is None:
</span><span class="lines">@@ -3030,6 +3180,8 @@
</span><span class="cx">             return MissingLocationService(store, options, output, reactor, config)
</span><span class="cx">         elif options[&quot;split&quot;]:
</span><span class="cx">             return EventSplitService(store, options, output, reactor, config)
</span><ins>+        elif options[&quot;upgrade&quot;]:
+            return UpgradeDataService(store, options, output, reactor, config)
</ins><span class="cx">         else:
</span><span class="cx">             printusage(&quot;Invalid operation&quot;)
</span><span class="cx">             sys.exit(1)
</span></span></pre>
</div>
</div>

</body>
</html>