o
    Wi                     @  s   d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZ dd	lmZmZmZ erJdd
lmZ ddlmZ G dd dZG dd dZG dd deZG dd deeZG dd deZG dd deeZg dZ dS )aE  Virtual environments in the traditional sense are built as reference to the host python. This file allows declarative references to elements on the file system, allowing our system to automatically detect what modes it can support given the constraints: e.g. can the file system symlink, can the files be read, executed, etc.    )annotationsN)ABCabstractmethod)OrderedDict)S_IXGRPS_IXOTHS_IXUSR)TYPE_CHECKING)fs_is_case_sensitivefs_supports_symlink)copymake_exesymlink)Callable)Pathc                   @     e Zd ZdZdZdZdS )RefMustNAr   r   N)__name__
__module____qualname__r   COPYSYMLINK r   r   `/home/ubuntu/.local/lib/python3.10/site-packages/virtualenv/create/via_global_ref/builtin/ref.pyr          r   c                   @  r   )RefWhenANYr   r   N)r   r   r   r   r   r   r   r   r   r   r      r   r   c                   @  s|   e Zd ZdZe Ze Zej	e
jfdd	d
ZdddZedddZedddZedddZed ddZd!ddZdS )"PathRefzABase class that checks if a file reference can be symlink/copied.srcr   muststrwhenreturnNonec                 C  st   || _ || _|| _z| | _W n ty   d| _Y nw | jr"d nd| _| jr*d nd| _| jr5d | _d S d| _d S NF)r    r"   r   existsOSError	_can_read	_can_copy_can_symlinkselfr   r    r"   r   r   r   __init__%   s   
zPathRef.__init__c                 C  s   | j j d| j dS )N(src=))	__class__r   r   r,   r   r   r   __repr__1   s   zPathRef.__repr__boolc                 C  s   | j d u rE| j r:z!| jd d| _ W d    n1 sw   Y  W | j S W | j S  ty9   d| _ Y | j S w tt| jtj| _ | j S )NrbTF)	r(   r   is_fileopenr'   osaccessr!   R_OKr1   r   r   r   can_read4   s   

zPathRef.can_readc                 C  s2   | j d u r| jtjkr| j| _ | j S | j| _ | j S N)r)   r    r   r   can_symlinkr:   r1   r   r   r   can_copyA   s   
zPathRef.can_copyc                 C  s8   | j d u r| jtjkr| j| _ | j S | jo| j| _ | j S r;   )r*   r    r   r   r=   FS_SUPPORTS_SYMLINKr:   r1   r   r   r   r<   J   s   
zPathRef.can_symlinkcreatorobjectsymlinksc                 C  s   t r;   )NotImplementedError)r,   r?   rA   r   r   r   runS   s   zPathRef.runCallable[..., None]c                 C  s,   | j tjkrtS | j tjkrtS |rtS tS r;   )r    r   r   r   r   r   )r,   rA   r   r   r   methodW   s
   zPathRef.methodNr   r   r    r!   r"   r!   r#   r$   r#   r!   r#   r3   r?   r@   rA   r3   r#   r$   )rA   r3   r#   rD   )r   r   r   __doc__r   r>   r
   FS_CASE_SENSITIVEr   r   r   r   r-   r2   propertyr:   r=   r<   r   rC   rE   r   r   r   r   r      s    
r   c                      sH   e Zd ZdZejejfd fd	d
Ze	dddZ
e	dddZ  ZS )
ExePathRefzJBase class that checks if a executable can be references via symlink/copy.r   r   r    r!   r"   r#   r$   c                   s   t  ||| d | _d S r;   )superr-   _can_runr+   r0   r   r   r-   b      
zExePathRef.__init__r3   c                 C  s   | j r| jS dS r%   )r>   can_runr1   r   r   r   r<   f   s   zExePathRef.can_symlinkc                 C  sF   | j d u r | j j}tttfD ]}||@ rd| _  | j S d| _ | j S )NTF)rO   r   statst_moder   r   r   )r,   modekeyr   r   r   rR   l   s   
zExePathRef.can_runrF   rH   )r   r   r   rJ   r   r   r   r   r-   rL   r<   rR   __classcell__r   r   rP   r   rM   _   s    rM   c                      s6   e Zd ZdZejejfd fddZdddZ	  Z
S )PathRefToDestzLink a path on the file system.r   r   destCallable[..., Path]r    r!   r"   r#   r$   c                   s   t  ||| || _d S r;   )rN   r-   rY   )r,   r   rY   r    r"   rP   r   r   r-   |   rQ   zPathRefToDest.__init__r?   r@   rA   r3   c                 C  s`   |  || j}| |}t|tr|n|f}|j s#|jjddd |D ]}|| j| q%d S )NT)parentsexist_ok)rY   r   rE   
isinstancelistparentr&   mkdir)r,   r?   rA   rY   rE   dest_iterabledstr   r   r   rC      s   

zPathRefToDest.run)
r   r   rY   rZ   r    r!   r"   r!   r#   r$   rI   )r   r   r   rJ   r   r   r   r   r-   rC   rW   r   r   rP   r   rX   y   s    rX   c                   @  s8   e Zd ZdZejejfdddZdddZ	dddZ
dS )ExePathRefToDestz#Link a exe path on the file system.r   r   targets	list[str]rY   rZ   r    r!   r"   r#   r$   c                 C  sd   t | ||| t| |||| | js!ttdd |D  }|d | _|dd  | _|| _	d S )Nc                 s  s    | ]	}|  d fV  qd S r;   )lower).0ir   r   r   	<genexpr>   s    z,ExePathRefToDest.__init__.<locals>.<genexpr>r      )
rM   r-   rX   rK   r^   r   keysbasealiasesrY   )r,   r   rd   rY   r    r"   r   r   r   r-      s   

zExePathRefToDest.__init__r?   r@   rA   r3   c                 C  s   |  || jj}|| j }| |}|| j| |st| | jD ]#}|| }| r/|  |r8|	| j nt
| j| |sDt| q!d S r;   )rY   r   r_   rl   rE   r   rm   r&   unlink
symlink_tor   )r,   r?   rA   bin_dirrY   rE   extra	link_filer   r   r   rC      s"   


zExePathRefToDest.runc                 C  s   | j j d| j d| j dS )Nr.   z, alias=r/   )r0   r   r   rm   r1   r   r   r   r2      s   zExePathRefToDest.__repr__N)r   r   rd   re   rY   rZ   r    r!   r"   r!   r#   r$   rI   rG   )r   r   r   rJ   r   r   r   r   r-   rC   r2   r   r   r   r   rc      s    
rc   )rM   rc   r   rX   r   r   )!rJ   
__future__r   r7   abcr   r   collectionsr   rS   r   r   r   typingr	   virtualenv.infor
   r   virtualenv.util.pathr   r   r   collections.abcr   pathlibr   r   r   r   rM   rX   rc   __all__r   r   r   r   <module>   s&    @$