o
    iI                      @   s   U 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 dl	m
Z
 e
eZdZdZi Zeeedf ee f ed< G dd	 d	ZG d
d dZdee dee fddZdedefddZdedefddZdS )    N)Optional)
get_loggerz__init__.pyz*.py.CACHEc                   @   s   e Zd ZdefddZdS )ServiceMetadatanamec                 C   s   |dkr	|| _ d S d | _ d S )N )r   )selfr    r	   d/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/settings/_inferred_base_service.py__init__   s   zServiceMetadata.__init__N)__name__
__module____qualname__strr   r	   r	   r	   r
   r      s    r   c                   @   s   e Zd Zdeeef fddZddee dee fddZ	d	e
jdeeef fd
dZd	e
jdefddZdedefddZdS )PythonDetectorenvironc                 C   s*   || _ d| _tdtd d | _d S )Npythonz(^|/)(?!.*\.py$)(z(\d+(\.\d+)?)?$))r   r   recompileescapepattern)r   r   r	   r	   r
   r      s   zPythonDetector.__init__Targsreturnc                 C   s
  d}d}|D ]|}| do| d }d|v }|r|p|p|}|r+t|r+t|  S |sxz0t| }	|	 s;W q|	}
|
 sD|
j}
| 	|
\}}|rTt|W   S t| 
|
W   S  tyw } ztd|d| W Y d}~nd}~ww |r|dkrd	}|}qdS )
a  
        Detects and returns service metadata based on the provided list of arguments.

        This function iterates through the provided arguments, skipping any that are
        flags (starting with '-') or environment variable assignments (containing '=').
        If a valid module flag ('-m') is encountered, it will switch to module detection mode.
        It checks for existing directories and deduces package names from provided paths
        to generate service metadata.

        Args:
            args (list[str]): A list of command-line arguments.

        Returns:
            Optional[ServiceMetadata]:
                - ServiceMetadata: The detected service metadata if found.
        F-z	--ddtrace=z,Unexpected error while processing argument: zException: Nz-mT)
startswith_module_existsr   pathlibPathresolveexistsis_dirparentdeduce_package_namefind_nearest_top_level	Exceptionlogdebug)r   r   skip_args_preceded_by_flagsprev_arg_is_flagmodule_flagarghas_flag_prefixis_env_variableshould_skip_argabs_pathstrippedvalueokexr	   r	   r
   detect'   s:   zPythonDetector.detectfpc                 C   s`   t |j}|}g }||kr%|t  sn|d|j |}|j}||ksd|t|dkfS )Nr   .)	r   r   r"   INIT_PYr    insertr   joinlen)r   r5   upcurrent	traversedr	   r	   r
   r#   ^   s   z"PythonDetector.deduce_package_namec                 C   sL   |j }|}|}||kr#tt|ts	 |jS |}|}|j }||ks|jS N)r"   fnmatchfilteroslistdirALL_PY_FILESr   )r   r5   r;   r<   lastr	   r	   r
   r$   n   s   z%PythonDetector.find_nearest_top_levelcommandc                 C   s   t | j|S r>   )boolr   search)r   rE   r	   r	   r
   matches}   s   zPythonDetector.matchesN)T)r   r   r   dictr   r   listr   r   r4   r   r   tuplerF   r#   r$   rH   r	   r	   r	   r
   r      s    7r   r   r   c              
   C   s  t g}| sdS tt| }|tv rt|S zg | tj}t }i }|D ]0}|tt	j
}t|D ]"\}}	|j}
||	rK||
|i || q2t|	rT|| q2q%g }t| D ]\}}||vri|| q\| D ]}||}|r|jr|jt|< |j  W S qn| D ]}|j|dd}|r|jr|jt|< |j  W S qW n ty } ztd| W Y d}~nd}~ww dt|< dS )a  
    Detects and returns the name of a service based on the provided list of command-line arguments.

    This function checks the provided arguments against a list of detector classes to identify
    the service type. If any of the arguments represent executables, they are ignored. The
    function iterates through the detector instances, applying their detection logic to the qualifying
    arguments in order to determine a service name.

    Args:
        args (list[str]): A list of command-line arguments.
        detector_classes (list[Type[Detector]]): A list of detector classes to use for service detection.
            Defaults to [PythonDetector].

    Returns:
        Optional[str]: The name of the detected service, or None if no service was detected.
    NF)r(   z9Unexpected error during inferred base service detection: )r   rK   sortedr   getsys
executablesetrI   rA   r   	enumerater   rH   updateadd_is_executableappendvaluesr4   r%   r&   warning)r   detector_classes	cache_keypossible_commandsexecutable_args	detectorsdetector_classdetector_instanceirE   detector_nameargs_to_searchr+   detectormetadatar3   r	   r	   r
   detect_service   s\   









rd   	file_pathc                 C   sv   t j| }t j|sdS t j|}|r9t j||kr9t j|dr)dS t j|}|r9t j||ksdS )NFbinT)rA   pathnormpathisfiledirnamebasenameendswith)re   normalized_path	directoryr	   r	   r
   rT      s   rT   module_namec                 C   s(   z	t j| duW S  ty   Y dS w )z"Check if a module can be imported.NF)	importlibutil	find_specModuleNotFoundError)ro   r	   r	   r
   r      s
   r   )r?   importlib.utilrp   rA   r   r   rN   typingr   ddtrace.internal.loggerr   r   r&   r7   rC   r   rI   rK   r   __annotations__r   r   rJ   rd   rF   rT   r   r	   r	   r	   r
   <module>   s"   
  gK