
    9Yf&B                        d Z ddlmZ ddlZddlmZ ddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZmZ ddlmZ dZdZd	Zd
Z G d de      Z G d dej.                        Zd Zd Zd Zd Zd Z G d dej<                        ZddZ d Z!e"dk(  r9ejF                  d   jI                  d      r e
jJ                  de&        e!        yy)a  

Wraps shell commands and sends the result to Datadog as events. Ex:

dogwrap -n test-job -k $API_KEY --submit_mode all "ls -lah"

Note that you need to enclose your command in quotes to prevent python
from thinking the command line arguments belong to the python command
instead of the wrapped command.

You can also have the script only send events if they fail:

dogwrap -n test-job -k $API_KEY --submit_mode errors "ls -lah"

And you can give the command a timeout too:

dogwrap -n test-job -k $API_KEY --timeout=1 "sleep 3"

    )print_functionN)copy)
initializeapi__version__)is_p3ksuccesserrorwarningi  c                       e Zd Zy)TimeoutN)__name__
__module____qualname__     U/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/datadog/dogshell/wrap.pyr   r   /   s    r   r   c                   .    e Zd ZdZddZd Zed        Zy)OutputReaderz
    Thread collecting the output of a subprocess, optionally forwarding it to
    a given file descriptor and storing it for further retrieval.
    Nc                 z    t         j                  j                  |        d| _        d| _        || _        || _        y)a   
        Instantiates an OutputReader.
        :param proc_out: the output to read
        :type proc_out: file descriptor
        :param fwd_out: the output to forward to (None to disable forwarding)
        :type fwd_out: file descriptor or None
        Tr   N)	threadingThread__init__daemon_out_content_out_fwd_out)selfproc_outfwd_outs      r   r   zOutputReader.__init__9   s5     	!!$'	r   c                     t        | j                  j                  d      D ]>  }| j                  | j                  j	                  |       | xj
                  |z  c_        @ | j                  j                          y)z
        Thread's main loop: collects the output optionnally forwarding it to
        the file descriptor passed in the constructor.
        r   N)iterr   readliner   writer   close)r   lines     r   runzOutputReader.runG   sa    
 ++S1 	&D}}(##D)%	& 			r   c                     | j                   S )zW
        The content stored in out so far. (Not threadsafe, wait with .join())
        )r   )r   s    r   contentzOutputReader.contentR   s    
    r   N)r   r   r   __doc__r   r'   propertyr)   r   r   r   r   r   3   s%    
 	 ! !r   r   c                     t        j                          }d}|L| j                         }t        j                          |z
  |kD  r
t               t        j                  |       |L|S )zP
    Polls the process until it returns or a given timeout has been reached
    N)timepollr   sleep)procsleep_intervaltimeout
start_time
returncodes        r   	poll_procr6   Z   s[     JJ

YY[
99;#g-)OJJ~& 
 r   c                    t        j                          }d}d}d}		 t        j                  | t        j                  t        j                  d      }
	 t               rt        j                  j                  nt        j                  }t               rt        j                  j                  nt        j                  }t        |
j                  |s|nd      }t        |
j                  |s|nd      }|j                          |j                          t        |
||      }j)                          j)                          |j*                  }|j*                  }	t        j                          |z
  }|||	|fS # t        $ r) t        dt        |       z  t        j                          w xY w# t        $ rO t        }t        j                          }t        dt        j                          |z
  z  t        j                         	 |
j!                          	 t        |
||       n# t        $ r t        d	t        j                          |z
  z  t        j                         t        j                          }|
j#                          	 t        |
||       nB# t        $ r6 t        d
t        j                          |z
  z  t        j                         Y nw xY wY nw xY wn&# t$        $ r}|j&                  dk7  r Y d}~nd}~ww xY wY w xY w)z7
    Launches the process and monitors its outputs
    r   T)stdoutstderrshellzFailed to execute %sfileNz3Command timed out after %.2fs, killing with SIGTERMz8SIGTERM timeout failed after %.2fs, killing with SIGKILLz+SIGKILL timeout failed after %.2fs, exiting   )r.   
subprocessPopenPIPE	Exceptionprintreprsysr:   r   r9   bufferr   startr6   r   	terminatekillOSErrorerrnojoinr)   )cmdcmd_timeoutsigterm_timeoutsigkill_timeoutproc_poll_intervalbuffer_outsr4   r5   r9   r:   r1   stdout_bufferstderr_buffer
out_reader
err_readersigterm_startsigkill_startedurations                      r   executer[   i   s`    JJFFJOOJOO[_`% .4X

))3::-3X

))3::!$++K}UYZ
!$++K}UYZ
 t%7E
: OOOOFFyy{Z'Hvvx//g  %c3#**E   
		Ctyy{U_G_`gjgqgqr	NN$ 2OD NRVR[R[R]`mRmn !%				d$6H EWdIdelolvlv  	ww!| 	+s   5E0 C	F% 02F"%AK>=KHKA K=J
K<K
K	K

KKKKK>	K7K2-K>2K77K>=K>c                     t        |       |k  r| S dj                  | d|dz   | t        |       d|z  dz  z
  d       }|S )z
    Trim input text to fit the `max_len` condition.

    If trim is needed: keep the first 1/3rd of the budget on the top,
    and the other 2 thirds on the bottom.
    z7{top_third}
```
*...trimmed...*
```
{bottom_two_third}
Nr>      )	top_thirdbottom_two_third)lenformat)textmax_lentrimmed_texts      r   	trim_textre      sd     4yG	  !'>W\*T#d)qSZ{_`N`B`Bb=c !' !
  r   c                    d}d}d}|r|r	t         dz  nt         }|r,dj                  t        |j                  dd      |            }|r,dj                  t        |j                  dd      |            }|r6t	        |t
              r|j                  dd      n|}d	j                  |
      }dj                  | ||||      S )za
    Format and return an event body.

    Note: do not exceed MAX_EVENT_BODY_LENGTH length.
     r]   z'**>>>> STDOUT <<<<**
```
{stdout} 
```
utf-8replace)r9   z'**>>>> STDERR <<<<**
```
{stderr} 
```
)r:   z.**>>>> NOTIFICATIONS <<<<**

 {notifications}
)notificationszu%%%
**>>>> CMD <<<<**
```
{command} 
```
**>>>> EXIT CODE <<<<**

 {returncode}


{stdout}{stderr}{notifications}%%%
)commandr5   r9   r:   rj   )MAX_EVENT_BODY_LENGTHra   re   decode
isinstancebytes)	rM   r5   r9   r:   rj   
fmt_stdout
fmt_stderrfmt_notifications
max_lengths	            r   build_event_bodyrt      s     JJ/5&&!+F[JCJJV]]7I>
K K 

 CJJV]]7I>
K K 

 DN}^cDd,,Wi@jwPWWfsWt	 !+  
r   c                 z    	 |j                  d      }|S # t        $ r t        j                  d|d|      w xY w)N,zoption z": invalid warning codes value(s): )split
ValueErroroptparseOptionValueError)optionoptoptions_warningwarning_codess       r   generate_warning_codesr      sI    r (--c2 r''[^`o(pqqrs    %:c                       e Zd Zej                  j
                  dz   Z eej                  j                        Zeed<   y)DogwrapOption)r~   r~   N)	r   r   r   ry   OptionTYPESr   TYPE_CHECKERr   r   r   r   r   r      s5    OO!!$66E445L$:L!r   r   c           
      "   t        j                  ddj                  t              t              }|j                  ddddd	       |j                  d
ddddt        j                  j                  d             |j                  dddddd       |j                  dddddg dd       |j                  ddddd       |j                  dd ddd!d"gd#$       |j                  d%d&dd'd(d)       |j                  d*dd'd+d,       |j                  d-dd'd.d/       |j                  d0dd1d2d3       |j                  d4ddd5d6       |j                  d7ddd5d8       |j                  d9ddd5d:       |j                  d;d<d=d>d?d@A       |j                  dBd=dCd?dDA       |j                  dEdddFd5dGH       |j                  | I      \  }}t               rdJj                  |      }||fS dKj                  |      j                  dL      }||fS )Mzd
    Parse the raw command line options into an options object and the remaining command string
    a  %prog -n [event_name] -k [api_key] --submit_mode [ all | errors | warnings] [options] "command". 

Note that you need to enclose your command in quotes to prevent python executing as soon as there is a space in your command. 
 
NOTICE: In normal mode, the whole stderr is printed before stdout, in flush_live mode they will be mixed but there is not guarantee that messages sent by the command on both stderr and stdout are printed in the order they were sent.z	%prog {0})usageversionoption_classz-nz--namestorestringz@the name of the event as it should appear on your Datadog stream)actiontypehelpz-kz	--api_keyzyour DataDog API Key
DD_API_KEY)r   r   r   defaultz-sz--sitedatadoghq.comzThe site to send data. Accepts us (datadoghq.com), eu (datadoghq.eu), us3 (us3.datadoghq.com), us5 (us5.datadoghq.com), or ap1 (ap1.datadoghq.com), gov (ddog-gov.com), or custom url. default: us)r   r   r   r   z-mz--submit_modechoiceerrors)r   warningsallz[ all | errors | warnings ] if set to error, an event will be sent only of the command exits with a non zero exit status or if it times out. If set to warning, a list of exit codes need to be provided)r   r   r   choicesr   z--warning_codesr~   z3comma separated list of warning codes, e.g: 127,255)r   r   destr   z-pz
--prioritynormallowz-the priority of the event (default: 'normal'))r   r   r   r   z-tz	--timeoutintiQ z(in seconds)  a timeout after which your command must be aborted. An event will be sent to your DataDog stream (default: 24hours)z--sigterm_timeoutx   z(in seconds)  When your command times out, the process it triggers is sent a SIGTERM. If this sigterm_timeout is reached, it will be sent a SIGKILL signal. (default: 2m)z--sigkill_timeout<   zn(in seconds) how long to wait at most after SIGKILL                               has been sent (default: 60s)z--proc_poll_intervalfloatg      ?zL(in seconds). interval at which your command will be polled (default: 500ms)z--notify_successrg   zQa message string and @people directives to send notifications in case of success.z--notify_errorzOa message string and @people directives to send notifications in case of error.z--notify_warningzUa message string and @people directives to send notifications in     case of warning.z-bz--buffer_outs
store_truerR   Fzdisplays the stderr and stdout of the command only once it has returned (the command outputs remains buffered in dogwrap meanwhile))r   r   r   r   z--send_metricsend_metricz!sends a metric for event durationz--tagstagszcomma separated list of tags)r   r   r   r   r   )args     rh   )ry   OptionParserra   r   r   
add_optionosenvironget
parse_argsr   rL   rm   )raw_argsparseroptionsr   rM   s        r   parse_optionsr      s    "" "";/"	F ,   #

|,   0  	 -H  
 B   5!<   >      <               F   0   xfbOm   %%8%4MGTxhhtn C< iio$$W-C<r   c                     t               \  } }t        || j                  | j                  | j                  | j
                  | j                        \  }}}}| j                  dv rd}na| j                  dv rd}nP| j                  dv rd}n?| j                  dv rd}n.| j                  d	v rd
}n| j                  dv rd}n| j                  }t        | j                  |       t        j                  }d }| j                  r#t        t        t        | j                              }|dk(  rt         }	d}
d|| j"                  |fz  }n|dk7  rp| j$                  dk(  ra|s t'        d       t)        j*                          nv||v rt,        }	d}
d|| j"                  |fz  }nWt'        d       t)        j*                          n7t.        }	d}
|t0        u rd|| j"                  |fz  }d}nd|| j"                  |fz  }d}	t         k(  r| j2                  r| j2                  }nC|	t.        k(  r| j4                  r| j4                  }n!|	t,        k(  r| j6                  r| j6                  }| j8                  r7| j8                  j;                  d      D cg c]  }|j=                          }}nd }t?        |||||      }|	| j"                  || j@                  xs 
|d}| j                  r~tC               r"|jE                  d      }|jE                  d      }t'        |j=                         t(        jF                         t'        |j=                         t(        jH                         | j$                  dk(  s|dk7  rx| jJ                  rJdjM                  | j"                        }|r||gz   }n|g}t        jN                  jQ                  d||d !       t        jR                  jT                  d#|d"| t)        j*                  |       y c c}w )$N)r   uszhttps://api.datadoghq.com)zdatadoghq.eueuzhttps://api.datadoghq.eu)zus3.datadoghq.comus3zhttps://api.us3.datadoghq.com)zus5.datadoghq.comus5zhttps://api.us5.datadoghq.com)zap1.datadoghq.comap1zhttps://api.ap1.datadoghq.com)zddog-gov.comgovzhttps://api.ddog-gov.com)api_keyapi_hostr   r   z[%s] %s succeeded in %.2fsr   z8A comma separated list of exit codes need to be providedr   z[%s] %s failed in %.2fszBCommand exited with a different exit code that the one(s) providedz[%s] %s timed out after %.2fsr8   rg   rv   )
alert_typeaggregation_keyhostpriorityr   rh   r<   r   zevent_name:{}zdogwrap.durationgauge)metricpointsr   r   )titlerb   r   )+r   r[   r3   rO   rP   rQ   rR   siter   r   r   
_host_namer~   listmapr   SUCCESSnamesubmit_moderC   rE   exitWARNINGERRORr   notify_successnotify_errornotify_warningr   rw   striprt   r   r   rm   r:   r9   r   ra   MetricsendEventcreate)r   rM   r5   r9   r:   rZ   r   r   r~   r   event_priorityevent_titlerj   tr   
event_bodyeventevent_name_tagduration_tagss                      r   mainr     su    ?LGS
 ,3"",(J ||...	/	/-	5	52	5	52	5	52	0	0-<<w:>>DMSg&;&;<=Q
3tW\\86TT	qW00J>LMHHJ=( J%N4gllH7UUKVWHHJ
! :dGLLRZ=[[KJ4gllH7UUKMW!7!7..	u	!5!5,,	w	7#9#9..||#*<<#5#5c#:;a	;;!#z66=QJ !"<<$$6E 8]]7+F]]7+Fflln3::.flln3::.e#zQ,33GLLAN $'7 7!/ 0JJOO#5h]ahOi		E{EuEHHZA <s   ;O2__main__dogwrapz@dogwrap is pending deprecation. Please use dogshellwrap instead.r*   )'r+   
__future__r   r   r   ry   r?   rE   r   r.   r   datadogr   r   r   datadog.util.compatr   r   r   r   rl   rB   r   r   r   r6   r[   re   rt   r   r   r   r   r   r   argvendswithwarnPendingDeprecationWarningr   r   r   <module>r      s   ( & 	    
    1 0 & 
 	i 	$!9## $!N=0@.(Vr;HOO ;Xvk\ z
xx{I&XZstF r   