o
    Á¿iJ  ã                   @   sD   d dl Z d dlZd dlmZ dedejfdd„ZG dd„ deƒZdS )	é    N)ÚOptionalÚpatternÚreturnc                 C   sÎ  d}|   d¡}|dko|t| ƒd k}||rdnd7 }| d dk}|  d¡}d}d}t|ƒ}|D ]Ÿ\}	}
|r@|t |
¡7 }d}q0|
dkrGd	}q0|
d
krŸ|	d t|ƒk rš||	d  d
krš|	dk}|	dkoj||	d  dk}|	d t|ƒk}|	d t|ƒk o‚||	d  dk}|s‡|rš|s‹|rš|d7 }t|dƒ t|dƒ q0|d7 }q0|
dkr¨|d7 }q0|
dkr³d	}||
7 }q0|
dkrÈ|rÀ||
7 }d}q0|t |
¡7 }q0|t |
¡7 }q0|rÚtdj| dƒ‚||rßdnd7 }t 	|¡S )aÁ	  
    source https://github.com/sbdchd/codeowners/blob/c95e13d384ac09cfa1c23be1a8601987f41968ea/codeowners/__init__.py

    Copyright (c) 2019-2020 Steve Dignam

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    ported from https://github.com/hmarr/codeowners/blob/d0452091447bd2a29ee508eebc5a79874fb5d4ff/match.go#L33

    MIT License

    Copyright (c) 2020 Harry Marr
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    Ú ú/éÿÿÿÿé   z\Az(?:\A|/)Fú\TÚ*r   é   z.*Nz[^/]*ú?z[^/]ú[ú]z1unterminated character class in pattern {pattern})r   z(?:\Z|/))
ÚfindÚlenÚstripÚ	enumerateÚreÚescapeÚnextÚ
ValueErrorÚformatÚcompile)r   ÚregexÚ	slash_posÚanchoredÚmatches_dirÚpattern_trimmedÚin_char_classÚescapedÚiteratorÚiÚchÚleft_anchoredÚleading_slashÚright_anchoredÚtrailing_slash© r'   úO/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/codeowners.pyÚpath_to_regex   sV   .

  





r)   c                   @   sl   e Zd ZdZdZddee dee fdd„Zddee dee fd	d
„Zddd„Z	dede
e fdd„ZdS )Ú
CodeownerszMProvide interface to parse CODEOWNERS file and match a given path against it.)Ú
CODEOWNERSz.github/CODEOWNERSzdocs/CODEOWNERSz.gitlab/CODEOWNERSNÚpathÚcwdc                 C   s<   g | _ d| _|p|  |¡}|du rtdƒ‚|| _|  ¡  dS )z‚Initialize Codeowners object.

        :param path: path to CODEOWNERS file otherwise try to use any from known locations
        NzCODEOWNERS file not found)Úpatternsr,   Úlocationr   Úparse)Úselfr,   r-   r'   r'   r(   Ú__init__w   s   zCodeowners.__init__r   c                 C   s>   |pt  ¡ }| jD ]}t j ||¡}t j |¡r|  S q	dS )z+Return the location of the CODEOWNERS file.N)ÚosÚgetcwdÚKNOWN_LOCATIONSr,   ÚjoinÚisfile)r1   r-   r/   r,   r'   r'   r(   r/   ‡   s   
ÿzCodeowners.locationc                 C   s.  | j du rdS t| j ƒ}g }| ¡ D ]j}| ¡ }d|v r'| dd¡d  ¡ }|dkr,q| d¡r7| d¡r7q| d¡rB| d¡rBq| ¡ }t|ƒd	k rMq|d }|du rVqzt|ƒ}W n t	t
fyg   Y qw d
d„ |dd… D ƒ}|svq| ||f¡ q| ¡  || _W d  ƒ dS 1 sw   Y  dS )z6Parse CODEOWNERS file and store the lines and regexes.Nú#r   r   r   r   r   z^[r   c                 S   s   g | ]}|r|‘qS r'   r'   )Ú.0Úownerr'   r'   r(   Ú
<listcomp>µ   s    z$Codeowners.parse.<locals>.<listcomp>)r,   ÚopenÚ	readlinesr   ÚsplitÚ
startswithÚendswithr   r)   r   Ú
IndexErrorÚappendÚreverser.   )r1   Úfr.   ÚlineÚelementsr,   r   Úownersr'   r'   r(   r0      s@   
ÿ"ÙzCodeowners.parsec                 C   s&   | j D ]\}}| |¡r|  S qg S )z˜Return code owners for a given path.

        :param path: path to check
        :return: list of file code owners identified by the given path
        )r.   Úsearch)r1   r,   r   rG   r'   r'   r(   Úof¾   s
   
ÿzCodeowners.of)NN)N)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   r   Ústrr2   r/   r0   ÚlistrI   r'   r'   r'   r(   r*   m   s    
	.r*   )	r3   r   Útypingr   rN   ÚPatternr)   Úobjectr*   r'   r'   r'   r(   Ú<module>   s
    g