o
    Vi%                     @  s   U d Z ddlm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mZmZ edejZed	ejZd
Zded< dZded< eZeZeZeZd#ddZd$ddZd%ddZd%ddZG d d! d!Zg d"ZdS )&zOA Python specification is an abstract requirement definition of an interpreter.    )annotationsN)Final   )normalize_isa)SimpleSpecifierSimpleSpecifierSetSimpleVersionaR  
    ^
    (?P<impl>[a-zA-Z]+)?            # implementation (e.g. cpython, pypy)
    (?P<version>[0-9.]+)?           # version (e.g. 3.12, 3.12.1)
    (?P<threaded>t)?                # free-threaded flag
    (?:-(?P<arch>32|64))?           # architecture bitness
    (?:-(?P<machine>[a-zA-Z0-9_]+))?  # ISA (e.g. arm64, x86_64)
    $
    z
    ^
    (?:(?P<impl>[A-Za-z]+)\s*)?     # optional implementation prefix
    (?P<spec>(?:===|==|~=|!=|<=|>=|<|>).+)  # PEP 440 version specifier
    $
       z
Final[int]_MAX_VERSION_PARTS	   _SINGLE_DIGIT_MAXval
str | Nonereturn
int | Nonec                 C  s   | d u rd S t | S Nint)r    r   M/home/ubuntu/.local/lib/python3.10/site-packages/python_discovery/_py_spec.py_int_or_none,   s   r   versionstr)tuple[int | None, int | None, int | None]c                 C  s   t dd | dD }t|tkrd}t|t|tkr)|d |d |d fS t|dkr8|d |d d fS |d }tt|d }|tkrRtt|dd  nd }||d fS )Nc                 s  s    | ]	}|rt |V  qd S r   r   ).0ir   r   r   	<genexpr>1   s    z'_parse_version_parts.<locals>.<genexpr>.ztoo many version partsr   r      )tuplesplitlenr
   
ValueErrorr   r   r   )r   versionsmsgversion_datamajorminorr   r   r   _parse_version_parts0   s    
r(   string_specPythonSpec | Nonec                 C  s   t t|  }s
d S | }|d }d\}}}}|d ur6z	t|\}}}W n
 ty/   Y d S w t|d }|d }|dv r@d }t|d }	|d}
|
d urSt	|
}
t
| |||||	d ||
d	S )	Nr   )NNNNthreadedimpl>   pypythonarchmachine)free_threadedr0   )rematchPATTERN	groupdictr(   r"   boolr   getr   
PythonSpec)r)   r3   groupsr   r&   r'   micror+   r,   r/   r0   r   r   r   _parse_spec_pattern?   s(   
r;   c              
   C  s   t |   }sd S td u rd S |d}|d }zt|}W n
 ty.   Y d S w |dv r5d }t| |d d d d d |dS )Nr,   spec>   r-   r.   )version_specifier)SPECIFIER_PATTERNr3   stripSpecifierSetgroupfrom_stringInvalidSpecifierr8   )r)   specifier_matchr,   	spec_textr=   r   r   r   _parse_specifierU   s   
rF   c                   @  sv   e Zd ZdZddddd,ddZed-ddZd.ddZed/dd Z	d0d"d#Z
ed1d&d'Zd0d(d)Zd2d*d+ZdS )3r8   aV  
    Contains specification about a Python Interpreter.

    :param str_spec: the raw specification string as provided by the caller.
    :param implementation: interpreter implementation name (e.g. ``"cpython"``, ``"pypy"``), or ``None`` for any.
    :param major: required major version, or ``None`` for any.
    :param minor: required minor version, or ``None`` for any.
    :param micro: required micro (patch) version, or ``None`` for any.
    :param architecture: required pointer-size bitness (``32`` or ``64``), or ``None`` for any.
    :param path: filesystem path to a specific interpreter, or ``None``.
    :param free_threaded: whether a free-threaded build is required, or ``None`` for any.
    :param machine: required ISA (e.g. ``"arm64"``), or ``None`` for any.
    :param version_specifier: PEP 440 version constraints, or ``None``.
    N)r1   r0   r=   str_specr   implementationr   r&   r   r'   r:   architecturepathr1   bool | Noner0   r=   SpecifierSet | Noner   Nonec                C  s@   || _ || _|| _|| _|| _|| _|| _|	| _|| _|
| _	d S r   )
rG   rH   r&   r'   r:   r1   rI   r0   rJ   r=   )selfrG   rH   r&   r'   r:   rI   rJ   r1   r0   r=   r   r   r   __init__u   s   
zPythonSpec.__init__r)   c                 C  sV   t | r| |ddddd|S t| }r|S t| }r!|S | |ddddd|S )u   
        Parse a string specification into a :class:`PythonSpec`.

        :param string_spec: an interpreter spec — an absolute path, a version string, an implementation prefix,
            or a PEP 440 specifier.
        N)pathlibPathis_absoluter;   rF   )clsr)   resultr   r   r   from_string_spec   s   zPythonSpec.from_string_specwindowsr6   
re.Patternc             
   C  s   dj dd | j| j| jfD  }| jdu rdndt| j }| jr%dnd}|r+d	nd}|s4| jdu r6d
nd}tjd| d| | d| | d	tj	dS )z
        Generate a regular expression for matching interpreter filenames.

        :param windows: if ``True``, require a ``.exe`` suffix.
        z{}(\.{}(\.{})?)?c                 s  s     | ]}|d u r
dn|V  qd S )Nz\d+r   )r   vr   r   r   r      s    z)PythonSpec.generate_re.<locals>.<genexpr>Nr.   zpython|zt? z\.exe?z	(?P<impl>z)(?P<v>)$)flags)
formatr&   r'   r:   rH   r2   escaper1   compile
IGNORECASE)rN   rV   r   r,   modsuffixversion_conditionalr   r   r   generate_re   s    zPythonSpec.generate_rec                 C  s   | j duot| j  S )z;``True`` if the spec refers to an absolute filesystem path.N)rJ   rP   rQ   rR   rN   r   r   r   is_abs   s   zPythonSpec.is_absr<   c                 C  s   g }| j | j| jfD ]}|du r n|| q
|sdS ddd |D }|jdu r-dS tt2 t	
| |jD ] }| |}|du sLt||k rMq;||s[ W d   dS q;W d   dS 1 sgw   Y  dS )z(Check if version specifier is satisfied.NTr   c                 s  s    | ]}t |V  qd S r   )r   )r   partr   r   r   r      s    z6PythonSpec._check_version_specifier.<locals>.<genexpr>F)r&   r'   r:   appendjoinr=   
contextlibsuppressInvalidVersionVersionrB   _get_required_precisionr!   contains)rN   r<   
componentsrh   version_stritemrequired_precisionr   r   r   _check_version_specifier   s2   





z#PythonSpec._check_version_specifierrs   r   c                 C  sJ   | j du rdS ttt t| j jW  d   S 1 sw   Y  dS )z0Get the required precision for a specifier item.N)r   rk   rl   AttributeErrorr"   r!   release)rs   r   r   r   ro      s   

 z"PythonSpec._get_required_precisionc                 C  s   |j r| j r| j|jkrdS |jdur$| jdur$|j | j kr$dS |jdur1|j| jkr1dS |jdurC| jdurC|j| jkrCdS |jdurP|j| jkrPdS |jdur\| |s\dS t	dd t
| j| j| jf|j|j|jfD S )z
        Check if this spec is compatible with the given *spec* (e.g. PEP-514 on Windows).

        :param spec: the requirement to check against.
        FNc                 s  s,    | ]\}}|d u p|d u p||kV  qd S r   r   )r   ourreqr   r   r   r      s
    
z'PythonSpec.satisfies.<locals>.<genexpr>)rg   rJ   rH   lowerrI   r0   r1   r=   ru   allzipr&   r'   r:   )rN   r<   r   r   r   	satisfies   s"   

  zPythonSpec.satisfiesc                   s2   t  j}d}| dd fdd|D  dS )N)	rH   r&   r'   r:   rI   r0   rJ   r1   r=   (z, c                 3  s2    | ]}t  |d ur| dt  | V  qd S )N=)getattr)r   krf   r   r   r      s   0 z&PythonSpec.__repr__.<locals>.<genexpr>r[   )type__name__rj   )rN   nameparamsr   rf   r   __repr__   s   
$zPythonSpec.__repr__)rG   r   rH   r   r&   r   r'   r   r:   r   rI   r   rJ   r   r1   rK   r0   r   r=   rL   r   rM   )r)   r   r   r8   )rV   r6   r   rW   )r   r6   )r<   r8   r   r6   )rs   r   r   r   )r   r   )r   
__module____qualname____doc__rO   classmethodrU   re   propertyrg   ru   staticmethodro   r}   r   r   r   r   r   r8   e   s     


r8   )rC   rm   r8   r@   rn   )r   r   r   r   )r   r   r   r   )r)   r   r   r*   ) r   
__future__r   rk   rP   r2   typingr   _py_infor   
_specifierr   r   r   r`   VERBOSEr4   r>   r
   __annotations__r   r@   rn   r"   rC   rm   r   r(   r;   rF   r8   __all__r   r   r   r   <module>   s:    	




 