<s> from functools import wraps <EOL> import logging <EOL> import time <EOL> from fabric . tasks import WrappedCallableTask <EOL> from dogapi import dog_http_api <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> MAX_ARGS_LEN = <NUM_LIT> <EOL> def setup ( api_key , application_key = None ) : <EOL> global dog_http_api <EOL> dog_http_api . api_key = api_key <EOL> if application_key is not None : <EOL> dog_http_api . application_key = application_key <EOL> def _human_duration ( d ) : <EOL> def pluralize ( quantity , noun ) : <EOL> if quantity >= <NUM_LIT:2> : <EOL> return "<STR_LIT>" . format ( quantity , noun ) <EOL> else : <EOL> return "<STR_LIT>" . format ( quantity , noun ) <EOL> if d < <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> elif d < <NUM_LIT> : <EOL> return "<STR_LIT>" . format ( pluralize ( int ( d ) , "<STR_LIT>" ) ) <EOL> elif d >= <NUM_LIT> and d < <NUM_LIT> : <EOL> return "<STR_LIT>" . format ( pluralize ( d / <NUM_LIT> , "<STR_LIT>" ) ) <EOL> else : <EOL> return "<STR_LIT>" . format ( pluralize ( d / <NUM_LIT> , "<STR_LIT>" ) , pluralize ( d % <NUM_LIT> , "<STR_LIT>" ) ) <EOL> def _task_details ( t ) : <EOL> return "<STR_LIT>" % ( t . __module__ , t . __name__ ) <EOL> def _format_args ( args , kwargs ) : <EOL> serialized_args = u"<STR_LIT:U+002CU+0020>" . join ( map ( unicode , args ) + [ u"<STR_LIT>" . format ( k , kwargs [ k ] ) for k in kwargs ] ) <EOL> if len ( serialized_args ) > MAX_ARGS_LEN : <EOL> return serialized_args [ : MAX_ARGS_LEN ] + u"<STR_LIT>" <EOL> else : <EOL> return serialized_args <EOL> def _text ( t , args , kwargs , duration , output , error ) : <EOL> if error : <EOL> text = "<STR_LIT>" . format ( _task_details ( t ) , _format_args ( args , kwargs ) , _human_duration ( duration ) , error ) <EOL> else : <EOL> text = "<STR_LIT>" . format ( _task_details ( t ) , _format_args ( args , kwargs ) , _human_duration ( duration ) ) <EOL> if output : <EOL> text += ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) . format ( output ) <EOL> return text <EOL> def _title ( t , args , kwargs , error ) : <EOL> return "<STR_LIT>" . format ( _task_details ( t ) ) <EOL> def _aggregation_key ( t , args , kwargs , error ) : <EOL> return _task_details ( t ) <EOL> def _tags ( t , args , kwargs , error ) : <EOL> return [ ] <EOL> def notify ( t ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( t ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> start = time . time ( ) <EOL> error = None <EOL> output = None <EOL> try : <EOL> r = t ( * args , ** kwargs ) <EOL> if r : <EOL> if not isinstance ( r , list ) : <EOL> r = [ r ] <EOL> output = '<STR_LIT>' . join ( [ '<STR_LIT>' % <EOL> ( res . command , res . stdout , res . stderr ) for res in r ] <EOL> ) <EOL> except Exception as e : <EOL> error = e <EOL> end = time . time ( ) <EOL> duration = end - start <EOL> try : <EOL> dog_http_api . event ( _title ( t , args , kwargs , error ) , <EOL> _text ( t , args , kwargs , duration , output , error ) , <EOL> source_type_name = "<STR_LIT>" , <EOL> alert_type = "<STR_LIT:error>" if error else "<STR_LIT:success>" , <EOL> priority = "<STR_LIT>" , <EOL> aggregation_key = _aggregation_key ( t , args , kwargs , error ) , <EOL> tags = _tags ( t , args , kwargs , error ) ) <EOL> except Exception as e : <EOL> logger . warn ( "<STR_LIT>" . format ( t . __name__ , e ) ) <EOL> if error : <EOL> raise error <EOL> else : <EOL> return r <EOL> return WrappedCallableTask ( wrapper ) </s>
