<s> """<STR_LIT>""" <EOL> import os <EOL> import requests <EOL> import logging <EOL> import datetime <EOL> from nose . plugins import Plugin <EOL> from seleniumbase . config import settings <EOL> HIPCHAT_URL = '<STR_LIT>' <EOL> HIPCHAT_AUTH_TOKEN = settings . HIPCHAT_AUTH_TOKEN <EOL> class HipchatReporting ( Plugin ) : <EOL> '''<STR_LIT>''' <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> super ( HipchatReporting , self ) . __init__ ( ) <EOL> self . hipchat_room_id = None <EOL> self . hipchat_owner_to_mention = None <EOL> self . hipchat_notify_on_success = False <EOL> self . build_url = os . environ . get ( '<STR_LIT>' ) <EOL> self . successes = [ ] <EOL> self . failures = [ ] <EOL> self . errors = [ ] <EOL> def options ( self , parser , env ) : <EOL> super ( HipchatReporting , self ) . options ( parser , env = env ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = None ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = None ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> dest = '<STR_LIT>' , <EOL> help = '''<STR_LIT>''' ) <EOL> def configure ( self , options , conf ) : <EOL> super ( HipchatReporting , self ) . configure ( options , conf ) <EOL> if not self . enabled : <EOL> return <EOL> if not options . hipchat_room_id : <EOL> raise Exception ( '''<STR_LIT>''' ) <EOL> else : <EOL> self . hipchat_room_id = options . hipchat_room_id <EOL> self . hipchat_owner_to_mention = ( <EOL> options . hipchat_owner_to_mention or None ) <EOL> self . hipchat_notify_on_success = options . hipchat_notify_on_success <EOL> def addSuccess ( self , test , capt ) : <EOL> self . successes . append ( test . id ( ) ) <EOL> def addError ( self , test , err , capt = None ) : <EOL> self . errors . append ( "<STR_LIT>" + test . id ( ) ) <EOL> def addFailure ( self , test , err , capt = None , tbinfo = None ) : <EOL> self . failures . append ( "<STR_LIT>" + test . id ( ) ) <EOL> def finalize ( self , result ) : <EOL> message = '<STR_LIT>' <EOL> success = True <EOL> if not result . wasSuccessful ( ) : <EOL> success = False <EOL> if ( self . hipchat_owner_to_mention and <EOL> self . _is_during_business_hours ( ) ) : <EOL> message += "<STR_LIT:@>" + self . hipchat_owner_to_mention + '<STR_LIT:\n>' <EOL> if self . failures : <EOL> message += "<STR_LIT:\n>" . join ( self . failures ) <EOL> if self . errors : <EOL> message += '<STR_LIT:\n>' <EOL> if self . errors : <EOL> message += "<STR_LIT:\n>" . join ( self . errors ) <EOL> if self . build_url : <EOL> message += '<STR_LIT:\n>' + self . build_url <EOL> elif self . hipchat_notify_on_success and self . successes : <EOL> message = "<STR_LIT>" <EOL> message += "<STR_LIT>" . join ( self . successes ) <EOL> if message : <EOL> self . _send_hipchat_notification ( message , success = success ) <EOL> def _is_during_business_hours ( self ) : <EOL> now = datetime . datetime . now ( ) <EOL> return now . weekday ( ) <= <NUM_LIT:4> and now . hour >= <NUM_LIT:9> and now . hour <= <NUM_LIT> <EOL> def _send_hipchat_notification ( self , message , success = True , <EOL> sender = '<STR_LIT>' ) : <EOL> response = requests . post ( HIPCHAT_URL , params = { <EOL> '<STR_LIT>' : HIPCHAT_AUTH_TOKEN , <EOL> '<STR_LIT>' : self . hipchat_room_id , <EOL> '<STR_LIT>' : sender , <EOL> '<STR_LIT:message>' : message , <EOL> '<STR_LIT>' : '<STR_LIT:text>' , <EOL> '<STR_LIT>' : '<STR_LIT>' if success else '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> if response . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" , self . hipchat_room_id ) <EOL> return True <EOL> else : <EOL> logging . error ( "<STR_LIT>" , <EOL> self . hipchat_room_id ) <EOL> return False </s>
