
    wi                       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	Z	d dl
Z
d dlmZ d dlmZ d dlmZ d dlmZ  ee	j                  ZdZdZd	Zd
ZdZdZdZdZ eeeeef          Z eeef          Z eeef          Zh eeeZ ej         ej!        "                                   ej         ej#        "                                   ej         ej$        "                                   ej         ej%        "                                   ee          Z&d+dZ'd+dZ(d,dZ)d-dZ*d.dZ+d/dZ,d0d!Z-d1d"Z.d2d#Z/ ej0        d$ej1        ej2        z            Z3 ej0        d%          Z4d3d'Z5d4d*Z6dS )5    )annotationsN)IO)
extensions)interpreters)licenses	directorysymlinksocketfile
executableznon-executabletextbinarypathstrreturnset[str]c                    	 t          j        |           }n'# t          t          f$ r t          |  d          w xY w|j        }t          j        |          rt          hS t          j        |          rt          hS t          j
        |          rt          hS t          h}t          j        | t           j                  }|r|                    t                      n|                    t"                     t%          t           j                            |                     }t+          |          dk    r|                    |           nL|rJt/          |           }t+          |          dk    r(|                    t1          |d                              t2          |z  sDt5          |           r|                    t6                     n|                    t8                     t2          |z  s
J |            t:          |z  s
J |            |S )N does not exist.r   )oslstatOSError
ValueErrorst_modestatS_ISDIR	DIRECTORYS_ISLNKSYMLINKS_ISSOCKSOCKETFILEaccessX_OKadd
EXECUTABLENON_EXECUTABLEtags_from_filenamer   basenamelenupdateparse_shebang_from_filetags_from_interpreterENCODING_TAGSfile_is_textTEXTBINARY	MODE_TAGS)r   srmodetagsr   tshebangs          a/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/identify/identify.pytags_from_pathr8   (   s   4Xd^^Z  4 4 4D2223334 :D|D {|D y}T x6D4))J !    	27++D1122A
1vvzzA 	?-d33G7||a1'!*==>>> 4  	HHTNNNNHHV4%%%%t!!T!!Ks    $;c                x   t           j                            |           \  }}t           j                            |          \  }}t	                      }|g|                    d          z   D ]7}|t
          j        v r'|                    t
          j        |                     n8t          |          dk    r|dd          	                                }|t
          j
        v r&|                    t
          j
        |                    n3|t
          j        v r%|                    t
          j        |                    |S )N.r      )r   r   splitsplitextsetr   NAMESr*   r)   lower
EXTENSIONSEXTENSIONS_NEED_BINARY_CHECK)r   _filenameextretparts         r7   r'   r'   V   s   '--%%KAxWh''FAs
%%C 
X^^C000  :###JJz'-...E $ 3xx!||!""gmmoo*'''JJz,S12222J;;;JJz>sCDDDJ    interpreterc                    |                      d          \  }}} | r;| t          j        v rt          j        |          S |                      d          \  } }}| ;t                      S )N/r:   )
rpartitionr   INTERPRETERSr>   )rI   rC   s     r7   r,   r,   l   sq    #..s33Aq+  <,333,[99 + 6 6s ; ;KA	  < 55LrH   bytesio	IO[bytes]boolc                
   t          g d          t          t          dd                    z   t          t          dd                    z   }t          |                     d                              d|                     S )zReturn whether the first KB of contents seems to be binary.

    This is roughly based on libmagic's binary/text detection:
    https://github.com/file/file/blob/df74b09b9027676088c797528edcaae5a9ce9ad0/src/encoding.c#L203-L228
    )      	   
                            i   N)	bytearrayrangerP   read	translate)rN   
text_charss     r7   is_textrc   y   s~     	///00%d##$$	%%e$$%%	& 
 GLL&&00zBBCCCCrH   c                    t           j                            |           st          |  d          t	          | d          5 }t          |          cd d d            S # 1 swxY w Y   d S )Nr   rb)r   r   lexistsr   openrc   )r   fs     r7   r.   r.      s    7??4   4D222333	dD		 Qqzz                 s   AA"%A"line	list[str]c                t    	 t          j        |           S # t          $ r |                                 cY S w xY w)N)shlexr<   r   )ri   s    r7   _shebang_splitrm      sJ     {4       zz||s    77cmdtuple[str, ...]c                   |                      d          dk    r|                                 }	 |                    d          }n# t          $ r |cY S w xY w|D ]}|t          vr|c S t          t          |                                                    }t          |d d                   D ]\  }}|dk    r||dz            f}|                      d          dk    |S )N      #!UTF-8z-ir;   )	r`   readlinedecodeUnicodeDecodeError	printabletuplerm   strip	enumerate)rN   rn   next_line_b	next_linecline_tokensitokens           r7   _parse_nix_shebangr      s    ,,q//U
"
"&&((	#**733II! 	 	 	JJJ	  	 	A	!!


 " N9??+<+<==>>!+crc"233 	( 	(HAu}}q1u%'CC! ,,q//U
"
"" Js   A AAc                   |                      d          dk    rdS |                                 }	 |                    d          }n# t          $ r Y dS w xY w|D ]}|t          vr dS t          t          |                                                    }|dd         dk    r|dd         }n|dd         dk    r
|dd         }|d	k    rt          | |          S |S )
z8Parse the shebang from a file opened for reading binary.rq   rr    rs   N)/usr/bin/envz-Sr;   )r   )z	nix-shell)	r`   ru   rv   rw   rx   ry   rm   rz   r   )rN   first_line_b
first_liner~   rn   s        r7   parse_shebangr      s   ||A%r##%%L!((11

   rr   I22  z//1122
3
3C
2A2w(((!""g	RaR%	%	%!""g
n!'3///Js   A 
AAc                v   t           j                            |           st          |  d          t          j        | t           j                  sdS 	 t          | d          5 }t          |          cddd           S # 1 swxY w Y   dS # t          $ r!}|j	        t          j
        k    rY d}~dS  d}~ww xY w)z$Parse the shebang given a file path.r   r   re   N)r   r   rf   r   r"   r#   rg   r   r   errnoEINVAL)r   rh   es      r7   r+   r+      s	   7??4   4D2223339T27## r$ 	$ ##	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$   7el""22222	sB   B $B 3B  BB BB 
B8B32B33B8z^\s*(Copyright|\(C\)) .*$z\s+sc                    t                               d|           } t                              d|           } |                                 S )N  )COPYRIGHT_REsubWS_RErz   )r   s    r7   _norm_licenser      s8    QA		#qA7799rH   rD   
str | Nonec                H   ddl }t          | d          5 }|                                }ddd           n# 1 swxY w Y   t          |          }t          j        }d}t          j        dt          |          z            }t          j
        D ]\  }}	t          |	          }
||
k    r|c S |rAt          t          |          t          |
          z
            t          |          z  dk    ra|                    ||
|          }||k     r
||k     r|}|}|r||k     r|S dS )a  Return the spdx id for the license contained in `filename`.  If no
    license is detected, returns `None`.

    spdx: https://spdx.org/licenses/
    licenses from choosealicense.com: https://github.com/choosealicense.com

    Approximate algorithm:

    1. strip copyright line
    2. normalize whitespace (replace all whitespace with a single space)
    3. check exact text match with existing licenses
    4. failing that use edit distance
    r   Nrs   )encodingr   g?)ukkonenrg   r`   r   sysmaxsizemathceilr)   r   LICENSESabsdistance)rD   r   rh   contentsnormmin_edit_distmin_edit_dist_spdxcutoffspdxr   norm_license	edit_dists               r7   
license_idr      s    NNN	h	)	)	) Q6688               ""DKMYsSYY''F ' & &
d$T**<KKK  	CD		C$5$5566TBSHH$$T<@@	v)m";";%M!%  &&!! ts   7;;)r   r   r   r   )rI   r   r   r   )rN   rO   r   rP   )r   r   r   rP   )ri   r   r   rj   )rN   rO   rn   ro   r   ro   )rN   rO   r   ro   )r   r   r   ro   )r   r   r   r   )rD   r   r   r   )7
__future__r   r   r   os.pathr   rerl   r   stringr   typingr   identifyr   r   identify.vendorr   	frozensetrx   r   r   r    r!   r%   r&   r/   r0   	TYPE_TAGSr1   r-   	_ALL_TAGSr*   rA   valuesrB   r?   rM   ALL_TAGSr8   r'   r,   rc   r.   rm   r   r   r+   compileI	MULTILINEr   r   r   r   r   rH   r7   <module>r      s   " " " " " "    				    



             ! ! ! ! ! ! $ $ $ $ $ $ If&''		
	
!	Iy$899	Iz>233		64.))4i4)4m4	 	 *'..00 1 1 	 *9@@BB C C 	 *"))++ , , 	 ,+2244 5 59Y+ + + +\   ,
 
 
 
D D D D   	 	 	 	   0   6   " rz6r|8KLL
6   . . . . . .rH   