summaryrefslogtreecommitdiff
path: root/tools/regression/xsl_reports/email_maintainers.py
blob: 06c7e3b7145da8c55ade4697f7f0d890269ced4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#
# Copyright (C) 2005 The Trustees of Indiana University 
# Author: Douglas Gregor
#
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
import re
import smtplib
import os
import time
import string
import datetime
import sys

report_author = "Douglas Gregor <dgregor@cs.indiana.edu>"
boost_dev_list = "Boost Developer List <boost@lists.boost.org>"

def sorted_keys( dict ):
    result = dict.keys()
    result.sort()
    return result


class Platform:
    """
    All of the failures for a particular platform.
    """
    def __init__(self, name):
        self.name = name
        self.failures = list()
        return

    def addFailure(self, failure):
        self.failures.append(failure)
        return

    def isBroken(self):
        return len(self.failures) > 300

class Failure:
    """
    A single test case failure in the report.
    """
    def __init__(self, test, platform):
        self.test = test
        self.platform = platform
        return

class Test:
    """
    All of the failures for a single test name within a library.
    """
    def __init__(self, library, name):
        self.library = library
        self.name = name
        self.failures = list()
        return

    def addFailure(self, failure):
        self.failures.append(failure)
        return

    def numFailures(self):
        return len(self.failures)

    def numReportableFailures(self):
        """
        Returns the number of failures that we will report to the
        maintainers of the library. This doesn't count failures on
        broken platforms.
        """
        count = 0
        for failure in self.failures:
            if not failure.platform.isBroken():
                count += 1
                pass
            pass
        return count

class Library:
    """
    All of the information about the failures in a single library.
    """
    def __init__(self, name):
        self.name = name
        self.maintainers = list()
        self.tests = list()
        return

    def addTest(self, test):
        """
        Add another test to the library.
        """
        self.tests.append(test)
        return

    def addMaintainer(self, maintainer):
        """
        Add a new maintainer for this library.
        """
        self.maintainers.append(maintainer)
        return

    def numFailures(self):
        count = 0
        for test in self.tests:
            count += test.numFailures()
            pass
        return count

    def numReportableFailures(self):
        count = 0
        for test in self.tests:
            count += test.numReportableFailures()
            pass
        return count

class Maintainer:
    """
    Information about the maintainer of a library
    """
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.libraries = list()
        return

    def addLibrary(self, library):
        self.libraries.append(library)
        return

    def composeEmail(self, report):
        """
        Composes an e-mail to this maintainer with information about
        the failures in his or her libraries, omitting those that come
        from "broken" platforms. Returns the e-mail text if a message
        needs to be sent, or None otherwise.
        """

        # Determine if we need to send a message to this developer.
        requires_message = False
        for library in self.libraries:
            if library.numReportableFailures() > 0:
                requires_message = True
                break

        if not requires_message:
            return None

        # Build the message header
        message = """From: Douglas Gregor <dgregor@cs.indiana.edu>
To: """
        message += self.name + ' <' + self.email + '>'
        message += """
Reply-To: boost@lists.boost.org
Subject: Regressions in your Boost libraries as of """
        message += str(datetime.date.today()) + " [" + report.branch + "]"
        message += """

You are receiving this report because one or more of the libraries you
maintain has regression test failures that are not accounted for.
A full version of the report is sent to the Boost developer's mailing
list.

Detailed report:
"""
        message += '  ' + report.url + """

There are failures in these libraries you maintain:
"""

        # List the libraries this maintainer is responsible for and
        # the number of reportable failures in that library.
        for library in self.libraries:
            num_failures = library.numReportableFailures()
            if num_failures > 0:
                message += '  ' + library.name + ' (' + str(num_failures) + ')\n'
                pass
            pass

        # Provide the details for the failures in each library.
        for library in self.libraries:
            if library.numReportableFailures() > 0:
                message += '\n|' + library.name + '|\n'
                for test in library.tests:
                    if test.numReportableFailures() > 0:
                        message += '  ' + test.name + ':'
                        for failure in test.failures:
                            if not failure.platform.isBroken():
                                message += '  ' + failure.platform.name
                                pass
                            pass
                        message += '\n'
                        pass
                    pass
                pass
            pass

        return message

class Report:
    """
    The complete report of all failing test cases.
    """
    def __init__(self, branch = 'HEAD'):
        self.branch = branch
        self.date = None
        self.url = None
        self.libraries = dict()
        self.platforms = dict()
        self.maintainers = dict()
        return

    def getPlatform(self, name):
        """
        Retrieve the platform with the given name.
        """
        if self.platforms.has_key(name):
            return self.platforms[name]
        else:
            self.platforms[name] = Platform(name)
            return self.platforms[name]

    def getMaintainer(self, name, email):
        """
        Retrieve the maintainer with the given name and e-mail address.
        """
        if self.maintainers.has_key(name):
            return self.maintainers[name]
        else:
            self.maintainers[name] = Maintainer(name, email)
            return self.maintainers[name]

    def parseIssuesEmail(self):
        """
        Try to parse the issues e-mail file. Returns True if everything was
        successful, false otherwise.
        """
        # See if we actually got the file
        if not os.path.isfile('issues-email.txt'):
            return False

        # Determine the set of libraries that have unresolved failures
        date_regex = re.compile('Report time: (.*)')
        url_regex = re.compile('  (http://.*)')
        library_regex = re.compile('\|(.*)\|')
        failure_regex = re.compile('  ([^:]*):  (.*)')
        current_library = None
        for line in file('issues-email.txt', 'r'):
            # Check for the report time line
            m = date_regex.match(line)
            if m:
                self.date = m.group(1)
                continue

            # Check for the detailed report URL
            m = url_regex.match(line)
            if m:
                self.url = m.group(1)
                continue
                
            # Check for a library header
            m = library_regex.match(line)
            if m:
                current_library = Library(m.group(1))
                self.libraries[m.group(1)] = current_library
                continue
                
            # Check for a library test and its failures
            m = failure_regex.match(line)
            if m:
                test = Test(current_library, m.group(1))
                for platform_name in re.split('\s*', m.group(2)):
                    if platform_name != '':
                        platform = self.getPlatform(platform_name)
                        failure = Failure(test, platform)
                        test.addFailure(failure)
                        platform.addFailure(failure)
                        pass
                current_library.addTest(test)
                continue
            pass

        return True

    def getIssuesEmail(self):
        """
        Retrieve the issues email from MetaComm, trying a few times in
        case something wonky is happening. If we can retrieve the file,
        calls parseIssuesEmail and return True; otherwise, return False.
        """
        base_url = "http://engineering.meta-comm.com/boost-regression/CVS-"
        base_url += self.branch
        base_url += "/developer/";
        got_issues = False

        # Ping the server by looking for an HTML file
        print "Pinging the server to initiate extraction..."
        ping_url = base_url + "issues.html"
        os.system('curl -O ' + ping_url)
        os.system('rm -f issues.html')
            
        for x in range(30):
            # Update issues-email.txt
            url = base_url + "issues-email.txt"
            print 'Retrieving issues email from ' + url
            os.system('rm -f issues-email.txt')
            os.system('curl -O ' + url)

            if self.parseIssuesEmail():
                return True

            print 'Failed to fetch issues email. '
            time.sleep (30)

        return False
        
    # Parses the file $BOOST_ROOT/libs/maintainers.txt to create a hash
    # mapping from the library name to the list of maintainers.
    def parseLibraryMaintainersFile(self):
        """
        Parse the maintainers file in ../../../libs/maintainers.txt to
        collect information about the maintainers of broken libraries.
        """
        lib_maintainer_regex = re.compile('(\S+)\s*(.*)')
        name_email_regex = re.compile('\s*(\w*(\s*\w+)+)\s*<\s*(\S*(\s*\S+)+)\S*>')
        at_regex = re.compile('\s*-\s*at\s*-\s*')
        for line in file('../../../libs/maintainers.txt', 'r'):
            m = lib_maintainer_regex.match (line)
            if m:
                libname = m.group(1)
                if self.libraries.has_key(m.group(1)):
                    library = self.libraries[m.group(1)]
                    for person in re.split('\s*,\s*', m.group(2)):
                        nmm = name_email_regex.match(person)
                        if nmm:
                            name = nmm.group(1)
                            email = nmm.group(3)
                            email = at_regex.sub('@', email)
                            maintainer = self.getMaintainer(name, email)
                            maintainer.addLibrary(library)
                            library.addMaintainer(maintainer)
                            pass
                        pass
                    pass
                pass
            pass
        pass

    def numFailures(self):
        count = 0
        for library in self.libraries:
            count += self.libraries[library].numFailures()
            pass
        return count

    def numReportableFailures(self):
        count = 0
        for library in self.libraries:
            count += self.libraries[library].numReportableFailures()
            pass
        return count

    def composeSummaryEmail(self):
        """
        Compose a message to send to the Boost developer's
        list. Return the message and return it.
        """
        message = """From: Douglas Gregor <dgregor@cs.indiana.edu>
To: boost@lists.boost.org
Reply-To: boost@lists.boost.org
Subject: Boost regression notification ("""

        message += str(datetime.date.today()) + " [" + branch + "]"
        message += ")"

        message += """

Boost Regression test failures
"""
        message += "Report time: " + self.date + """

This report lists all regression test failures on release platforms.

Detailed report:
"""

        message += '  ' + self.url + '\n\n'

        if self.numFailures() == 0:
            message += "No failures! Yay!\n"
            return message
            
        # List the platforms that are broken
        any_broken_platforms = self.numReportableFailures() < self.numFailures()
        if any_broken_platforms:
            message += """The following platforms have a large number of failures:
"""
            for platform in sorted_keys( self.platforms ):
                if self.platforms[platform].isBroken():
                    message += '  ' + platform + '\n'

            message += '\n'
   
        # Display the number of failures
        message += (str(self.numFailures()) + ' failures in ' + 
                    str(len(self.libraries)) + ' libraries')
        if any_broken_platforms:
            diff = self.numFailures() - self.numReportableFailures()
            message += ' (' + str(diff) + ' are from non-broken platforms)'
        message += '\n'

        # Display the number of failures per library
        for k in sorted_keys( self.libraries ):
            library = self.libraries[k]
            num_failures = library.numFailures()
            message += ('  ' + library.name + ' (' 
                        + str(library.numReportableFailures()))
            if library.numReportableFailures() < num_failures:
                message += (' of ' + str(num_failures) 
                            + ' failures are from non-broken platforms')
            message += ')\n'
            pass

        # If we have any broken platforms, tell the user how we're
        # displaying them.
        if any_broken_platforms:
            message += """
Test failures marked with a (*) represent tests that failed on
platforms that are considered broken. They are likely caused by
misconfiguration by the regression tester or a failure in a core
library such as Test or Config."""
        message += '\n'

        # Provide the details for the failures in each library.
        for k in sorted_keys( self.libraries ):
            library = self.libraries[k]
            message += '\n|' + library.name + '|\n'
            for test in library.tests:
                message += '  ' + test.name + ':'
                for failure in test.failures:
                    platform = failure.platform
                    message += '  ' + platform.name
                    if platform.isBroken():
                        message += '*'
                    pass
                message += '\n'
                pass
            pass

        return message

# Send a message to "person" (a maintainer of a library that is
# failing).
# maintainers is the result of get_library_maintainers()
def send_individualized_message (branch, person, maintainers):
  # There are several states we could be in:
  #   0 Initial state. Eat everything up to the "NNN failures in MMM
  #     libraries" line
  #   1 Suppress output within this library
  #   2 Forward output within this library
  state = 0
 
  failures_in_lib_regex = re.compile('\d+ failur.*\d+ librar')
  lib_failures_regex = re.compile('  (\S+) \((\d+)\)')
  lib_start_regex = re.compile('\|(\S+)\|')
  general_pass_regex = re.compile('  http://')
  for line in file('issues-email.txt', 'r'):
    if state == 0:
        lfm = lib_failures_regex.match(line)
        if lfm:
            # Pass the line through if the current person is a
            # maintainer of this library
            if lfm.group(1) in maintainers and person in maintainers[lfm.group(1)]:
                message += line
                print line,
                
        elif failures_in_lib_regex.match(line):
            message += "\nThere are failures in these libraries you maintain:\n"
        elif general_pass_regex.match(line):
            message += line
            
    lib_start = lib_start_regex.match(line)
    if lib_start:
        if state == 0:
            message += '\n'
            
        if lib_start.group(1) in maintainers and person in maintainers[lib_start.group(1)]:
            message += line
            state = 2
        else:
            state = 1
    else:
        if state == 1:
            pass
        elif state == 2:
            message += line

  if '--debug' in sys.argv:
      print '-----------------Message text----------------'
      print message
  else:
      print
      
  if '--send' in sys.argv:
      print "Sending..."
      smtp = smtplib.SMTP('milliways.osl.iu.edu')
      smtp.sendmail(from_addr = 'Douglas Gregor <dgregor@cs.indiana.edu>',
                    to_addrs = person[1],
                    msg = message)
      print "Done."


# Send a message to the developer's list
def send_boost_developers_message(branch, maintainers, failing_libraries):
  to_line = 'boost@lists.boost.org'
  from_line = 'Douglas Gregor <dgregor@cs.indiana.edu>'

  message = """From: Douglas Gregor <dgregor@cs.indiana.edu>
To: boost@lists.boost.org
Reply-To: boost@lists.boost.org
Subject: Boost regression notification ("""

  message += str(datetime.date.today()) + " [" + branch + "]"
  message += ")"

  message += """

"""

  for line in file('issues-email.txt', 'r'):
      # Right before the detailed report, put out a warning message if
      # any libraries with failures to not have maintainers listed.
      if line.startswith('Detailed report:'):
          missing_maintainers = False
          for lib in failing_libraries:
              if not(lib in maintainers) or maintainers[lib] == list():
                  missing_maintainers = True

          if missing_maintainers:
              message += """WARNING: The following libraries have failing regression tests but do
not have a maintainer on file. Once a maintainer is found, add an
entry to libs/maintainers.txt to eliminate this message.
"""

              for lib in failing_libraries:
                  if not(lib in maintainers) or maintainers[lib] == list():
                      message += '  ' + lib + '\n'
              message += '\n'
              
      message += line
      
  if '--send' in sys.argv:
      print 'Sending notification email...'
      smtp = smtplib.SMTP('milliways.osl.iu.edu')
      smtp.sendmail(from_addr = from_line, to_addrs = to_line, msg = message)
      print 'Done.'

  if '--debug' in sys.argv:
      print "----------Boost developer's message text----------"
      print message

###############################################################################
# Main program                                                                #
###############################################################################

# Parse command-line options
branch = "HEAD"
for arg in sys.argv:
    if arg.startswith("--branch="):
        branch = arg[len("--branch="):]

report = Report(branch)

# Try to parse the issues e-mail
if '--no-get' in sys.argv:
    okay = report.parseIssuesEmail()
else:
    okay = report.getIssuesEmail()

if not okay:
    print 'Aborting.'
    if '--send' in sys.argv:
        message = """From: Douglas Gregor <dgregor@cs.indiana.edu>
        To: Douglas Gregor <dgregor@cs.indiana.edu>
        Reply-To: boost@lists.boost.org
        Subject: Regression status script failed on """
        message += str(datetime.date.today()) + " [" + branch + "]"
        smtp = smtplib.SMTP('milliways.osl.iu.edu')
        smtp.sendmail(from_addr = 'Douglas Gregor <dgregor@cs.indiana.edu>',
                      to_addrs = 'dgregor@cs.indiana.edu',
                      msg = message)
    sys.exit(1)

# Try to parse maintainers information
report.parseLibraryMaintainersFile()

for maintainer_name in report.maintainers:
    maintainer = report.maintainers[maintainer_name]

    email = maintainer.composeEmail(report)
    if email:
        if '--send' in sys.argv:
            print ('Sending notification email to ' + maintainer.name + '...')
            smtp = smtplib.SMTP('milliways.osl.iu.edu')
            smtp.sendmail(from_addr = report_author, 
                          to_addrs = maintainer.email,
                          msg = email)
            print 'done.\n'
        else:
            print 'Would send a notification e-mail to',maintainer.name

        if '--debug' in sys.argv:
            print ('Message text for ' + maintainer.name + ':\n')
            print email
            
email = report.composeSummaryEmail()
if '--send' in sys.argv:
    print 'Sending summary email to Boost developer list...'
    smtp = smtplib.SMTP('milliways.osl.iu.edu')
    smtp.sendmail(from_addr = report_author, 
                  to_addrs = boost_dev_list,
                  msg = email)
    print 'done.\n'
if '--debug' in sys.argv:
    print 'Message text for summary:\n'
    print email

if not ('--send' in sys.argv):
    print 'Chickening out and not sending any e-mail.'
    print 'Use --send to actually send e-mail, --debug to see e-mails.'