Home | History | Annotate | Download | only in deprecated
      1 #!/usr/bin/python2
      2 #
      3 # Copyright 2010 Google Inc. All Rights Reserved.
      4 """Script to rotate the weekly team sheriff.
      5 
      6 This script determines who the next sheriff is, updates the file
      7 appropriately and sends out email notifying the team.
      8 """
      9 
     10 from __future__ import print_function
     11 
     12 __author__ = 'asharif (at] google.com (Ahmad Sharif)'
     13 
     14 import argparse
     15 import datetime
     16 import os
     17 import sys
     18 
     19 from cros_utils import constants
     20 from cros_utils import email_sender
     21 
     22 
     23 class SheriffHandler(object):
     24   """Main class for handling sheriff rotations."""
     25 
     26   SHERIFF_FILE = os.path.join(constants.CROSTC_WORKSPACE, 'sheriffs.txt')
     27   SUBJECT = 'You (%s) are the sheriff for the week: %s - %s'
     28   BODY = ('Please see instructions here: '
     29           'https://sites.google.com/a/google.com/chromeos-toolchain-team-home2'
     30           '/home/sheriff-s-corner/sheriff-duties')
     31 
     32   def GetWeekInfo(self, day=datetime.datetime.today()):
     33     """Return week_start, week_end."""
     34 
     35     epoch = datetime.datetime.utcfromtimestamp(0)
     36     delta_since_epoch = day - epoch
     37 
     38     abs_days = abs(delta_since_epoch.days) - 2  # To get it to start from Sat.
     39     day_of_week = abs_days % 7
     40 
     41     week_begin = day - datetime.timedelta(days=day_of_week)
     42     week_end = day + datetime.timedelta(days=(6 - day_of_week))
     43 
     44     strftime_format = '%A, %B %d %Y'
     45 
     46     return (week_begin.strftime(strftime_format),
     47             week_end.strftime(strftime_format))
     48 
     49   def GetCurrentSheriff(self):
     50     """Return the current sheriff."""
     51     return self.ReadSheriffsAsList()[0]
     52 
     53   def ReadSheriffsAsList(self):
     54     """Return the sheriff file contents."""
     55     contents = ''
     56     with open(self.SHERIFF_FILE, 'r') as f:
     57       contents = f.read()
     58     return contents.splitlines()
     59 
     60   def WriteSheriffsAsList(self, to_write):
     61     with open(self.SHERIFF_FILE, 'w') as f:
     62       f.write('\n'.join(to_write))
     63 
     64   def GetRotatedSheriffs(self, num_rotations=1):
     65     """Return the sheriff file contents."""
     66     sheriff_list = self.ReadSheriffsAsList()
     67 
     68     new_sheriff_list = []
     69     num_rotations = num_rotations % len(sheriff_list)
     70     new_sheriff_list = (
     71         sheriff_list[num_rotations:] + sheriff_list[:num_rotations])
     72     return new_sheriff_list
     73 
     74   def Email(self):
     75     es = email_sender.EmailSender()
     76     current_sheriff = self.GetCurrentSheriff()
     77     week_start, week_end = self.GetWeekInfo()
     78     subject = self.SUBJECT % (current_sheriff, week_start, week_end)
     79     es.SendEmail([current_sheriff],
     80                  subject,
     81                  self.BODY,
     82                  email_from=os.path.basename(__file__),
     83                  email_cc=['c-compiler-chrome'])
     84 
     85 
     86 def Main(argv):
     87   parser = argparse.ArgumentParser()
     88   parser.add_argument('-e',
     89                       '--email',
     90                       dest='email',
     91                       action='store_true',
     92                       help='Email the sheriff.')
     93   parser.add_argument('-r',
     94                       '--rotate',
     95                       dest='rotate',
     96                       help='Print sheriffs after n rotations.')
     97   parser.add_argument('-w',
     98                       '--write',
     99                       dest='write',
    100                       action='store_true',
    101                       default=False,
    102                       help='Wrote rotated contents to the sheriff file.')
    103 
    104   options = parser.parse_args(argv)
    105 
    106   sheriff_handler = SheriffHandler()
    107 
    108   current_sheriff = sheriff_handler.GetCurrentSheriff()
    109   week_start, week_end = sheriff_handler.GetWeekInfo()
    110 
    111   print('Current sheriff: %s (%s - %s)' % (current_sheriff, week_start,
    112                                            week_end))
    113 
    114   if options.email:
    115     sheriff_handler.Email()
    116 
    117   if options.rotate:
    118     rotated_sheriffs = sheriff_handler.GetRotatedSheriffs(int(options.rotate))
    119     print('Rotated sheriffs (after %s rotations)' % options.rotate)
    120     print('\n'.join(rotated_sheriffs))
    121     if options.write:
    122       sheriff_handler.WriteSheriffsAsList(rotated_sheriffs)
    123       print('Rotated sheriffs written to file.')
    124 
    125   return 0
    126 
    127 
    128 if __name__ == '__main__':
    129   retval = Main(sys.argv[1:])
    130   sys.exit(retval)
    131