o
    i                     @   s   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Zd dlmZ dej	e j
 dej	e fddZeeZdZdd	 Zde jfd
dZde jfddZdd ZedkrZe  dS dS )    N)__version__argsreturnc                 C   s0   | d u rd S | j d }tj|r|S t|S )Nr   )commandospathisfileshutilwhich)r   r    r   P/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/commands/ddtrace_run.py_find_executable   s   

r   z
Execute the given Python command after configuring it to emit Datadog traces
and profiles.


Examples
ddtrace-run python app.py
ddtrace-run gunicorn myproject.wsgi
c                 C   sF   t jdd}|rd| t jjt jd f }|t jd< dS | t jd< dS )zo
    Add our bootstrap directory to the head of $PYTHONPATH to ensure
    it is loaded before program code
    
PYTHONPATH z%s%s%sN)r   environgetr   pathsep)bootstrap_dirpython_pathnew_pathr   r   r   _add_bootstrap_to_pythonpath)   s
   r   c                  C   s   t jtddt jd} | jdt jtdd | jddd	d
d | jdddd
d | jdddd
d | jddddt d | jdddd
d | S )Nzddtrace-runz'ddtrace-run <your usual python command>)descriptionprogusageformatter_classr   zCommand string to execute.)nargstypehelpz-dz--debugz'enable debug mode (disabled by default)
store_true)r   actionz-iz--infoz}print library info useful for debugging. Only reflects configurations made via environment variables, not those made in code.z-pz--profilingz&enable profiling (disabled by default)z-vz	--versionversionz	%(prog)s )r   r    z-ncz--colorlessz%print output of command without color)argparseArgumentParserUSAGERawTextHelpFormatteradd_argument	REMAINDERstrr   )parserr   r   r   _get_arg_parser7   s$   	r)   r(   c                 C   s   |   }|jrdtjd< |jstjjrtj	tj
d dtjd< |jr6ddlm} t||j d td tjtj}td| tj|d	}td
| t| tdtjd  tdtj |jsp|   td |S )NtrueDD_PROFILING_ENABLED)levelDD_TRACE_DEBUGr   )pretty_collect)colorzddtrace root: %s	bootstrapzddtrace bootstrap: %szPYTHONPATH: %sr   zsys.path: %s   )
parse_args	profilingr   r   debugddtraceconfig_debug_modeloggingbasicConfigDEBUGinfoddtrace.internal.debugr.   print	colorlesssysexitr   dirname__file__logjoinr   r   
print_help)r(   r   r.   root_dirr   r   r   r   _prepare_envO   s*   



rG   c                  C   s"  d } d }d }zt  } t| }t|}W n ty#   tjddd Y nw |d u s,|d u r?td|  | d ur:|   t	d t
d| tj|dkrQtd ztj||g|jdd  R   W n' ty|   td	|  td
|  t	d Y n ty   td|   w t	d d S )Nz#error bootstrapping Datadog tracingT)exc_infoz-ddtrace-run: failed to bootstrap: args '%s'.
r1   zprogram executable: %suwsgia+  ddtrace-run has known compatibility issues with uWSGI where the tracer is not started properly in uWSGI workers which can cause broken behavior. It is recommended you remove ddtrace-run and update your uWSGI configuration following https://ddtrace.readthedocs.io/en/stable/advanced_usage.html#uwsgi.z2ddtrace-run: permission error while launching '%s'z%Did you mean `ddtrace-run python %s`?z!ddtrace-run: error launching '%s'r   )r)   rG   r   	ExceptionrC   warningr=   print_usager?   r@   r4   r   r   basenameexeclr   PermissionError)r(   r   
executabler   r   r   mainp   s>   

"rQ   __main__)r!   r8   r   r	   r?   typingr5   ddtrace.versionr   Optional	Namespacer'   r   	getLogger__name__rC   r#   r   r"   r)   rG   rQ   r   r   r   r   <module>   s$    
!+
