o
    ȭRh4                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ eeZd	d
 Zedd Zdd Zdd Zdd Zdd Zdd Z 		dde
e! de
e! ddfddZ"dS )    N)PIPECalledProcessErrorPopenSubprocessError
check_call)Optional)Requirement)default_instrumentations)	libraries)__version__)PackageNotFoundErrorversionc                    s   d fdd	}|S )Nc              
      sn   z| r | W S   W S  t y6 } zt|dd }|r$dd| d}| r.| d|  d}t|d }~ww )NcmdzError calling system command " "z for package ")r   getattrjoinRuntimeError)packageexpr   msgfunc i/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/opentelemetry/instrumentation/bootstrap.pywrapper-   s   
z_syscall.<locals>.wrapperNr   )r   r   r   r   r   _syscall,   s   r   c              
   C   sP   zt tjdddddd| g W d S  ty' } zt| W Y d }~d S d }~ww )N-mpipinstallz-Uz--upgrade-strategyzonly-if-needed)r   sys
executabler   print)r   errorr   r   r   _sys_pip_install=   s    
r%   c                 C   s   t tjdddgtd}| d  }| }W d   n1 s"w   Y  | D ]}|D ]}| |v r<td| q-q)dS )aw  Ensures none of the instrumentations have dependency conflicts.
    Clean check reported as:
    'No broken requirements found.'
    Dependency conflicts are reported as:
    'opentelemetry-instrumentation-flask 1.0.1 has requirement opentelemetry-sdk<2.0,>=1.0, but you have opentelemetry-sdk 0.5.'
    To not be too restrictive, we'll only check for relevant packages.
    r   r   check)stdoutr   NzDependency conflict found: )r   r!   r"   r   communicatedecodelowerr   )r
   
check_pipe	pip_checkpip_check_lowerpackage_tupr   r   r   r   
_pip_checkQ   s   
r/   c                 C   sN   t | } zt| j}W n
 ty   Y dS w | j|s%td| | dS dS )NFzRinstrumentation for package %s is available but version %s is installed. Skipping.T)r   r   namer   	specifierfilterloggerwarning)reqdist_versionr   r   r   _is_installedd   s   r7   c                 c   s6    | D ]}|V  q|D ]}t |d r|d V  qd S )Nlibraryinstrumentation)r7   r	   r
   libr   r   r   _find_installed_librariesw   s   
r<   c                 C   s$   t tj tdt| | d S )N
)r3   setLevelloggingERRORr#   r   r<   )r	   r
   r   r   r   _run_requirements   s   rA   c                 C   s$   t | |D ]}t| qt| d S r   )r<   r%   r/   r:   r   r   r   _run_install   s   
rB   r	   r
   returnc                 C   s   d}d}t jdd}|jddddt d	 |jd
d||g|dd | }|d u r+t}| d u r1t} |t|ti|j	 }|| | d S )Nr    requirementsz
        opentelemetry-bootstrap detects installed libraries and automatically
        installs the relevant instrumentation packages for them.
        )descriptionz	--versionzprint version informationr   z	%(prog)s )helpactionr   z-az--actiona  
        install - uses pip to install the new requirements using to the
                  currently active site-package.
        requirements - prints out the new requirements to stdout. Action can
                       be piped and appended to a requirements.txt file.
        )choicesdefaultrF   )
argparseArgumentParseradd_argumentr   
parse_argsgen_librariesgen_default_instrumentationsrB   rA   rG   )r	   r
   action_installaction_requirementsparserargsr   r   r   r   run   s:   rT   )NN)#rJ   r?   r!   
subprocessr   r   r   r   r   typingr   packaging.requirementsr   +opentelemetry.instrumentation.bootstrap_genr	   rO   r
   rN   %opentelemetry.instrumentation.versionr   &opentelemetry.util._importlib_metadatar   r   	getLogger__name__r3   r   r%   r/   r7   r<   rA   rB   listrT   r   r   r   r   <module>   s8   

		