<!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>[14988] CalendarServer/branches/release/CalendarServer-5.4-dev/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/14988">14988</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-07-23 11:58:32 -0700 (Thu, 23 Jul 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Detect (but not yet fix) locations events that are missing a LOCATION property.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesreleaseCalendarServer54devcalendarservertoolscalverifypy">CalendarServer/branches/release/CalendarServer-5.4-dev/calendarserver/tools/calverify.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesreleaseCalendarServer54devcalendarservertoolscalverifypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/release/CalendarServer-5.4-dev/calendarserver/tools/calverify.py (14987 => 14988)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/release/CalendarServer-5.4-dev/calendarserver/tools/calverify.py        2015-07-22 20:12:33 UTC (rev 14987)
+++ CalendarServer/branches/release/CalendarServer-5.4-dev/calendarserver/tools/calverify.py        2015-07-23 18:58:32 UTC (rev 14988)
</span><span class="lines">@@ -173,8 +173,7 @@
</span><span class="cx">             brokenRID = brokenComponent.propertyValue(&quot;RECURRENCE-ID&quot;)
</span><span class="cx">             if doFix:
</span><span class="cx">                 master.addProperty(Property(&quot;RDATE&quot;, [brokenRID, ]))
</span><del>-                fixed.append(&quot;Added RDATE for invalid occurrence: %s&quot; %
-                    (brokenRID,))
</del><ins>+                fixed.append(&quot;Added RDATE for invalid occurrence: %s&quot; % (brokenRID,))
</ins><span class="cx">             else:
</span><span class="cx">                 unfixed.append(&quot;Invalid occurrence: %s&quot; % (brokenRID,))
</span><span class="cx"> 
</span><span class="lines">@@ -336,6 +335,7 @@
</span><span class="cx">         ['missing', 'm', &quot;Show 'orphaned' homes.&quot;],
</span><span class="cx">         ['double', 'd', &quot;Detect double-bookings.&quot;],
</span><span class="cx">         ['dark-purge', 'p', &quot;Purge room/resource events with invalid organizer.&quot;],
</span><ins>+        ['missing-location', 'g', &quot;Room events with missing location.&quot;],
</ins><span class="cx">         ['split', 'l', &quot;Split an event.&quot;],
</span><span class="cx">         ['fix', 'x', &quot;Fix problems.&quot;],
</span><span class="cx">         ['verbose', 'v', &quot;Verbose logging.&quot;],
</span><span class="lines">@@ -346,7 +346,7 @@
</span><span class="cx">         ['no-organizer', '', &quot;Detect dark events without an organizer&quot;],
</span><span class="cx">         ['invalid-organizer', '', &quot;Detect dark events with an organizer not in the directory&quot;],
</span><span class="cx">         ['disabled-organizer', '', &quot;Detect dark events with a disabled organizer&quot;],
</span><del>-]
</del><ins>+    ]
</ins><span class="cx"> 
</span><span class="cx">     optParameters = [
</span><span class="cx">         ['config', 'f', DEFAULT_CONFIG_FILE, &quot;Specify caldavd.plist configuration path.&quot;],
</span><span class="lines">@@ -487,11 +487,11 @@
</span><span class="cx"> 
</span><span class="cx">         if inbox:
</span><span class="cx">             cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                    cb.BIND_MODE == _BIND_MODE_OWN)
</del><ins>+                cb.BIND_MODE == _BIND_MODE_OWN)
</ins><span class="cx">         else:
</span><span class="cx">             cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                    cb.BIND_MODE == _BIND_MODE_OWN).And(
-                    cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</del><ins>+                cb.BIND_MODE == _BIND_MODE_OWN).And(
+                cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</ins><span class="cx"> 
</span><span class="cx">         kwds = {}
</span><span class="cx">         rows = (yield Select(
</span><span class="lines">@@ -512,11 +512,11 @@
</span><span class="cx"> 
</span><span class="cx">         if inbox:
</span><span class="cx">             cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                    cb.BIND_MODE == _BIND_MODE_OWN)
</del><ins>+                cb.BIND_MODE == _BIND_MODE_OWN)
</ins><span class="cx">         else:
</span><span class="cx">             cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                    cb.BIND_MODE == _BIND_MODE_OWN).And(
-                    cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</del><ins>+                cb.BIND_MODE == _BIND_MODE_OWN).And(
+                cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</ins><span class="cx"> 
</span><span class="cx">         kwds = {&quot;uuid&quot;: uuid}
</span><span class="cx">         if len(uuid) != 36:
</span><span class="lines">@@ -567,11 +567,11 @@
</span><span class="cx"> 
</span><span class="cx">         if inbox:
</span><span class="cx">             cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                    cb.BIND_MODE == _BIND_MODE_OWN)
</del><ins>+                cb.BIND_MODE == _BIND_MODE_OWN)
</ins><span class="cx">         else:
</span><span class="cx">             cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                    cb.BIND_MODE == _BIND_MODE_OWN).And(
-                    cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</del><ins>+                cb.BIND_MODE == _BIND_MODE_OWN).And(
+                cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</ins><span class="cx"> 
</span><span class="cx">         kwds = {
</span><span class="cx">             &quot;UID&quot; : uid,
</span><span class="lines">@@ -620,8 +620,8 @@
</span><span class="cx">         tr = schema.TIME_RANGE
</span><span class="cx"> 
</span><span class="cx">         cojoin = (cb.CALENDAR_RESOURCE_ID == co.CALENDAR_RESOURCE_ID).And(
</span><del>-                cb.BIND_MODE == _BIND_MODE_OWN).And(
-                cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</del><ins>+            cb.BIND_MODE == _BIND_MODE_OWN).And(
+            cb.CALENDAR_RESOURCE_NAME != &quot;inbox&quot;)
</ins><span class="cx"> 
</span><span class="cx">         kwds = {
</span><span class="cx">             &quot;Start&quot; : pyCalendarTodatetime(start),
</span><span class="lines">@@ -2669,6 +2669,240 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class MissingLocationService(CalVerifyService):
+    &quot;&quot;&quot;
+    Service which detects room/resource events that have an ATTENDEE;CUTYPE=ROOM property but no Location.
+    &quot;&quot;&quot;
+
+    def title(self):
+        return &quot;Missing Location Service&quot;
+
+
+    @inlineCallbacks
+    def doAction(self):
+
+        self.output.write(&quot;\n---- Scanning calendar data ----\n&quot;)
+
+        self.tzid = PyCalendarTimezone(tzid=self.options[&quot;tzid&quot;] if self.options[&quot;tzid&quot;] else &quot;America/Los_Angeles&quot;)
+        self.now = PyCalendarDateTime.getNowUTC()
+        self.start = self.options[&quot;start&quot;] if &quot;start&quot; in self.options else PyCalendarDateTime.getToday()
+        self.start.setDateOnly(False)
+        self.start.setTimezone(self.tzid)
+        self.fix = self.options[&quot;fix&quot;]
+
+        if self.options[&quot;verbose&quot;] and self.options[&quot;summary&quot;]:
+            ot = time.time()
+
+        # Check loop over uuid
+        UUIDDetails = collections.namedtuple(&quot;UUIDDetails&quot;, (&quot;uuid&quot;, &quot;rname&quot;, &quot;purged&quot;,))
+        self.uuid_details = []
+        if len(self.options[&quot;uuid&quot;]) != 36:
+            self.txn = self.store.newTransaction()
+            if self.options[&quot;uuid&quot;]:
+                homes = yield self.getMatchingHomeUIDs(self.options[&quot;uuid&quot;])
+            else:
+                homes = yield self.getAllHomeUIDs()
+            yield self.txn.commit()
+            self.txn = None
+            uuids = []
+            if self.options[&quot;verbose&quot;]:
+                self.output.write(&quot;%d uuids to check\n&quot; % (len(homes,)))
+            for uuid in sorted(homes):
+                record = self.directoryService().recordWithGUID(uuid)
+                if record is not None and record.recordType == DirectoryService.recordType_locations:
+                    uuids.append(uuid)
+        else:
+            uuids = [self.options[&quot;uuid&quot;], ]
+        if self.options[&quot;verbose&quot;]:
+            self.output.write(&quot;%d uuids to scan\n&quot; % (len(uuids,)))
+
+        count = 0
+        for uuid in uuids:
+            self.results = {}
+            self.summary = []
+            self.total = 0
+            count += 1
+
+            record = self.directoryService().recordWithGUID(uuid)
+            if record is None:
+                continue
+            if not record.thisServer() or not record.enabledForCalendaring:
+                continue
+
+            rname = record.fullName
+
+            if len(uuids) &gt; 1 and not self.options[&quot;summary&quot;]:
+                self.output.write(&quot;\n\n-----------------------------\n&quot;)
+
+            self.txn = self.store.newTransaction()
+
+            if self.options[&quot;verbose&quot;]:
+                t = time.time()
+            rows = yield self.getAllResourceInfoTimeRangeWithUUID(self.start, uuid)
+            descriptor = &quot;getAllResourceInfoTimeRangeWithUUID&quot;
+
+            yield self.txn.commit()
+            self.txn = None
+
+            if self.options[&quot;verbose&quot;]:
+                if not self.options[&quot;summary&quot;]:
+                    self.output.write(&quot;%s time: %.1fs\n&quot; % (descriptor, time.time() - t,))
+                else:
+                    self.output.write(&quot;%s (%d/%d)&quot; % (uuid, count, len(uuids),))
+                    self.output.flush()
+
+            self.total = len(rows)
+            if not self.options[&quot;summary&quot;]:
+                self.logResult(&quot;UUID to process&quot;, uuid)
+                self.logResult(&quot;Record name&quot;, rname)
+                self.addSummaryBreak()
+                self.logResult(&quot;Number of events to process&quot;, self.total)
+
+            if rows:
+                if not self.options[&quot;summary&quot;]:
+                    self.addSummaryBreak()
+                purged = yield self.missingLocation(rows, uuid)
+            else:
+                purged = False
+
+            self.uuid_details.append(UUIDDetails(uuid, rname, purged))
+
+            if not self.options[&quot;summary&quot;]:
+                self.printSummary()
+            else:
+                self.output.write(&quot; - %s\n&quot; % (&quot;Bad Events&quot; if purged else &quot;OK&quot;,))
+                self.output.flush()
+
+        if count == 0:
+            self.output.write(&quot;Nothing to scan\n&quot;)
+
+        if self.options[&quot;summary&quot;]:
+            table = tables.Table()
+            table.addHeader((&quot;GUID&quot;, &quot;Name&quot;, &quot;RID&quot;, &quot;UID&quot;,))
+            purged = 0
+            for item in sorted(self.uuid_details):
+                if not item.purged:
+                    continue
+                uuid = item.uuid
+                rname = item.rname
+                for detail in item.purged:
+                    table.addRow((
+                        uuid,
+                        rname,
+                        detail.resid,
+                        detail.uid,
+                    ))
+                    uuid = &quot;&quot;
+                    rname = &quot;&quot;
+                    purged += 1
+            table.addFooter((&quot;Total&quot;, &quot;%d&quot; % (purged,), &quot;&quot;, &quot;&quot;, &quot;&quot;,))
+            self.output.write(&quot;\n&quot;)
+            table.printTable(os=self.output)
+
+            if self.options[&quot;verbose&quot;]:
+                self.output.write(&quot;%s time: %.1fs\n&quot; % (&quot;Summary&quot;, time.time() - ot,))
+
+
+    @inlineCallbacks
+    def missingLocation(self, rows, uuid):
+        &quot;&quot;&quot;
+        Check each calendar resource by looking at any ORGANIER property value and verifying it is valid.
+        &quot;&quot;&quot;
+
+        if not self.options[&quot;summary&quot;]:
+            self.output.write(&quot;\n---- Checking for missing location events ----\n&quot;)
+        self.txn = self.store.newTransaction()
+
+        if self.options[&quot;verbose&quot;]:
+            t = time.time()
+
+        Details = collections.namedtuple(&quot;Details&quot;, (&quot;resid&quot;, &quot;uid&quot;,))
+
+        count = 0
+        total = len(rows)
+        details = []
+        fixed = 0
+        rjust = 10
+        for resid in rows:
+            resid = resid[1]
+            caldata = yield self.getCalendar(resid, self.fix)
+            if caldata is None:
+                if self.parseError:
+                    returnValue((False, self.parseError))
+                else:
+                    returnValue((True, &quot;Nothing to scan&quot;))
+
+            cal = Component(None, pycalendar=caldata)
+            uid = cal.resourceUID()
+
+            fail = False
+            if cal.getOrganizer() is not None:
+                for comp in cal.subcomponents():
+                    if comp.name() != &quot;VEVENT&quot;:
+                        continue
+                    location = comp.propertyValue(&quot;LOCATION&quot;)
+                    if location is None:
+                        fail = True
+                        break
+                    else:
+                        # Test the actual location value matches this location name?
+                        pass
+
+            if fail:
+                details.append(Details(resid, uid,))
+                if self.fix:
+                    # Add location value
+                    # TODO: locate organizer's copy of event
+                    #     Add LOCATION property to components
+                    #     Write it back to trigger scheduling
+                    fixed += 1
+
+            if self.options[&quot;verbose&quot;] and not self.options[&quot;summary&quot;]:
+                if count == 1:
+                    self.output.write(&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; % 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;] and not self.options[&quot;summary&quot;]:
+            self.output.write((
+                &quot;\r&quot; +
+                (&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
+        if not self.options[&quot;summary&quot;]:
+            self.logResult(&quot;Number of bad events&quot;, len(details))
+
+        self.results[&quot;Bad Events&quot;] = details
+        if self.fix:
+            self.results[&quot;Fix bad events&quot;] = fixed
+
+        if self.options[&quot;verbose&quot;] and not self.options[&quot;summary&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),
+            ))
+
+        returnValue(details)
+
+
+
</ins><span class="cx"> class EventSplitService(CalVerifyService):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Service which splits a recurring event at a specific date-time value.
</span><span class="lines">@@ -2818,6 +3052,8 @@
</span><span class="cx">             return DoubleBookingService(store, options, output, reactor, config)
</span><span class="cx">         elif options[&quot;dark-purge&quot;]:
</span><span class="cx">             return DarkPurgeService(store, options, output, reactor, config)
</span><ins>+        elif options[&quot;missing-location&quot;]:
+            return MissingLocationService(store, options, output, reactor, config)
</ins><span class="cx">         elif options[&quot;split&quot;]:
</span><span class="cx">             return EventSplitService(store, options, output, reactor, config)
</span><span class="cx">         else:
</span></span></pre>
</div>
</div>

</body>
</html>