o
    uJAfL6                     @  sP  d dl mZ d dlmZmZmZ d dlZd dlZd dlZ	ddl
mZmZmZ ddl
mZ ddlmZmZmZmZ dd	lmZmZmZmZ dd
lmZmZmZmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$ e	j%rddl
m&Z& ddlm'Z' ddl(m)Z) ddlm*Z* ddlm+Z+m,Z, d dl-m.Z. G dd de.Z/G dd de.Z0G dd deZ1dddZ2dS )    )annotations)PathPurePathPureWindowsPathN   )ExtensionModuleModuleReturnValue
ModuleInfo   )mlog)BuildTargetCustomTargetCustomTargetIndexInvalidArguments)
INSTALL_KWINSTALL_MODE_KWINSTALL_TAG_KWNoneType)
FeatureNew	KwargInfotyped_kwargstyped_pos_argsnoKwargs)FileMesonExceptionhas_path_seppath_is_in_rootrelpath)ModuleState)BuildTargetTypes)Interpreter)TYPE_kwargs)FileOrStringFileMode)	TypedDictc                   @  s   e Zd ZU dZded< dS )
ReadKwArgszKeyword Arguments for fs.read.strencodingN__name__
__module____qualname____doc____annotations__ r.   r.   :/usr/lib/python3.10/site-packages/mesonbuild/modules/fs.pyr%   %   s   
 r%   c                   @  s2   e Zd ZU dZded< ded< ded< ded< d	S )
CopyKwzKwargs for fs.copyboolinstallzT.Optional[str]install_dirr#   install_modeinstall_tagNr(   r.   r.   r.   r/   r0   *   s   
 r0   c                      s  e Zd ZeddZd_ fddZd`ddZd`ddZee	dde
dedaddZee	dde
deefdbddZee	d de
d edad!d"Zee
d#edcd$d%Zee
d&eefdbd'd(Zee
d)edcd*d+Zee
d,edcd-d.Zee
d/eefeddd1d2Zee
d3eefded5d6Zee
d7eefeefdfd9d:Zee
d;eefeddd<d=Zee
d>eefdgd?d@Zee
dAeefdgdBdCZee
dDeefe	dDddgdEdFZe	dGdHe
dGeefedGedIedJdKdhdMdNZe	dOdPe
dOeefegdQedOeee edRee!fdidWdXZ"e	dYdZe
dYeee#e$e%feee#e$e%fedjd]d^Z&  Z'S )kFSModulefsz0.53.0interpreter'Interpreter'returnNonec                   s   t  | | ji d| jd| jd| jd| jd| jd| j	d| j
d| jd	| jd
| jd| jd| jd| jd| jd| jd| jd| j d S )N
expanduseris_absoluteas_posixexists
is_symlinkis_fileis_dirhashsizeis_samepathreplace_suffixparentnamestemreadcopyfilerelative_to)super__init__methodsupdater<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   )selfr8   	__class__r.   r/   rN   8   sH   	

zFSModule.__init__state'ModuleState'arg'FileOrString'r   c                 C  sD   t |trt||j| jj S t|jt|j t|	  S )zX
        make an absolute path from a relative path, WITHOUT resolving symlinks
        )

isinstancer   r   absolute_pathsource_rootr8   environmentget_build_dirsubdirr<   )rQ   rT   rV   r.   r.   r/   _absolute_dirN   s   
 zFSModule._absolute_dirc                 C  s0   |  ||}z| }W |S  ty   Y |S w )z
        resolves symlinks and makes absolute a directory relative to calling meson.build,
        if not already absolute
        )r^   resolve	Exception)rQ   rT   rV   pathr.   r.   r/   _resolve_dirV   s   
zFSModule._resolve_dirzfs.expanduserz0.54.0argsT.Tuple[str]kwargsT.Dict[str, T.Any]r&   c                 C  s   t t|d  S Nr   )r&   r   r<   rQ   rT   rc   re   r.   r.   r/   r<   d   s   zFSModule.expanduserzfs.is_absoluteT.Tuple['FileOrString']r1   c                 C  s4   t |d trtdd|j tt|d  S )Nr   zfs.is_absolute_file0.59.0)rX   r   r   use
subprojectr   r&   r=   rh   r.   r.   r/   r=   j   s   zFSModule.is_absolutezfs.as_posixc                 C  s   t |d  S )z
        this function assumes you are passing a Windows path, even if on a Unix-like system
        and so ALL '' are turned to '/', even if you meant to escape a character
        r   )r   r>   rh   r.   r.   r/   r>   r   s   zFSModule.as_posixz	fs.existsc                 C     |  ||d  S rg   )rb   r?   rh   r.   r.   r/   r?   |      zFSModule.existszfs.is_symlinkc                 C  s4   t |d trtdd|j | ||d  S )Nr   zfs.is_symlink_filerj   )rX   r   r   rk   rl   r^   r@   rh   r.   r.   r/   r@      s   zFSModule.is_symlinkz
fs.is_filec                 C  rm   rg   )rb   rA   rh   r.   r.   r/   rA      rn   zFSModule.is_filez	fs.is_dirc                 C  rm   rg   )rb   rB   rh   r.   r.   r/   rB      rn   zFSModule.is_dirzfs.hashT.Tuple['FileOrString', str]c                 C  s   t |d trtdd|j | ||d }| s#t| dz	t	|d }W n t
y<   td|d w td|d || j ||  | S )Nr   zfs.hash_filerj   z- is not a file and therefore cannot be hashedr   z"hash algorithm {} is not availablez$computing {} sum of {} size {} bytes)rX   r   r   rk   rl   rb   rA   r   hashlibnew
ValueErrorformatr   debugstatst_sizerP   
read_bytes	hexdigest)rQ   rT   rc   re   filehr.   r.   r/   rC      s   zFSModule.hashzfs.sizeintc                 C  st   t |d trtdd|j | ||d }| s#t| dz| j	W S  t
y9   td|d w )Nr   zfs.size_filerj   z, is not a file and therefore cannot be sizedz{} size could not be determined)rX   r   r   rk   rl   rb   rA   r   ru   rv   rr   rs   )rQ   rT   rc   re   ry   r.   r.   r/   rD      s   zFSModule.sizezfs.is_samepath'T.Tuple['FileOrString', 'FileOrString']c                 C  s   t |d tst |d trtdd|j | ||d }| ||d }| s-dS | s3dS z||W S  tyC   Y dS w )Nr   r   zfs.is_samepath_filerj   F)	rX   r   r   rk   rl   rb   r?   samefileOSError)rQ   rT   rc   re   Zfile1Zfile2r.   r.   r/   rE      s   zFSModule.is_samepathzfs.replace_suffixc                 C  sF   t |d trtdd|j tt|d }||d }t|S )Nr   zfs.replace_suffix_filerj   r   )rX   r   r   rk   rl   r   r&   with_suffixrQ   rT   rc   re   Zoriginalrq   r.   r.   r/   rF      s
   zFSModule.replace_suffixz	fs.parentc                 C  >   t |d trtdd|j tt|d }|j}t|S )Nr   zfs.parent_filerj   )rX   r   r   rk   rl   r   r&   rG   r   r.   r.   r/   rG      
   zFSModule.parentzfs.namec                 C  r   )Nr   zfs.name_filerj   )rX   r   r   rk   rl   r   r&   rH   r   r.   r.   r/   rH      r   zFSModule.namezfs.stemc                 C  r   )Nr   zfs.stem_filerj   )rX   r   r   rk   rl   r   r&   rI   r   r.   r.   r/   rI      s
   zFSModule.stemzfs.readz0.57.0r'   zutf-8)default'ReadKwArgs'c                 C  s.  |d }|d }| j jj}| j j}| j j }t|tr-|jr#tdt	j
|| }n|r6t	j
||}t	j
||}t	j
|}tt|t|ddrRtdzt||d}	|	 }
W d   n1 shw   Y  W n  ty   td	|d  d
 ty   td|d  w | j | |
S )aO  Read a file from the source tree and return its value as a decoded
        string.

        If the encoding is not specified, the file is assumed to be utf-8
        encoded. Paths must be relative by default (to prevent accidents) and
        are forbidden to be read from the build directory (to prevent build
        loops)
        r   r'   z2fs.read_file does not accept built files() objectsT)r_   z"path must not be in the build tree)r'   NzFile z does not exist.zdecoding failed for )r8   r[   
source_dirr]   r\   rX   r   is_builtr   osra   joinrelative_nameabspathr   r   openrJ   FileNotFoundErrorUnicodeDecodeErroradd_build_def_file)rQ   rT   rc   re   ra   r'   src_dirZsub_dir	build_dirfdatar.   r.   r/   rJ      s:   


zFSModule.readzfs.copyfilez0.64.0)optargsr3   r   &T.Tuple[FileOrString, T.Optional[str]]r0   r   c                 C  s   |d r|d st d| j|d gd }|d r|d ntj|j}t|r.t dt||j	|j
|j|j g d |g|gd|d |d g|d	 |d
 g|jdd}t||gS )z3Copy a file into the build directory at build time.r2   r3   z6"install_dir" must be specified when "install" is truer   r   z-Destination path may not have path separators)z
--internalcopyz@INPUT@z@OUTPUT@Tr4   r5   zCopying file {})build_by_defaultr2   r3   r4   r5   backenddescription)r   r8   source_strings_to_filesr   ra   basenamefnamer   r   r]   rl   r[   get_build_commandr   r   )rQ   rT   rc   re   srcdestctr.   r.   r/   rK     s.   zFSModule.copyfilezfs.relative_toz1.3.0YT.Tuple[T.Union[FileOrString, BuildTargetTypes], T.Union[FileOrString, BuildTargetTypes]]r!   c                   s0   d	 fdd}||d }||d }t ||S )
NrV   CT.Union[FileOrString, CustomTarget, CustomTargetIndex, BuildTarget]r:   r&   c                   sP   t | tr|  jj jjS t | tttfr j	
| S tj jj j| S N)rX   r   rY   r[   r   r   r   r   r   r   get_target_filename_absr   ra   r   r]   )rV   rT   r.   r/   to_path>  s
   
z%FSModule.relative_to.<locals>.to_pathr   r   )rV   r   r:   r&   )r   )rQ   rT   rc   re   r   tr   r.   r   r/   rL   :  s   
zFSModule.relative_to)r8   r9   r:   r;   )rT   rU   rV   rW   r:   r   )rT   rU   rc   rd   re   rf   r:   r&   )rT   rU   rc   ri   re   rf   r:   r1   )rT   rU   rc   rd   re   rf   r:   r1   )rT   rU   rc   ro   re   rf   r:   r&   )rT   rU   rc   ri   re   rf   r:   r{   )rT   rU   rc   r|   re   rf   r:   r1   )rT   rU   rc   ri   re   rf   r:   r&   )rT   rU   rc   ri   re   r   r:   r&   )rT   r   rc   r   re   r0   r:   r   )rT   r   rc   r   re   r!   r:   r&   )(r)   r*   r+   r	   INFOrN   r^   rb   r   r   r   r&   r<   r   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   r   r   rJ   r   r   r   r   rK   r   r   r   rL   __classcell__r.   r.   rR   r/   r6   4   s    


)!r6   rc   T.Anyre   r:   c                  O  s   t | i |S r   )r6   )rc   re   r.   r.   r/   
initializeL  s   r   )rc   r   re   r   r:   r6   )3
__future__r   pathlibr   r   r   rp   r   typingT r   r   r	   r   buildr   r   r   r   Zinterpreter.type_checkingr   r   r   r   interpreterbaser   r   r   r   r   mesonlibr   r   r   r   r   TYPE_CHECKINGr   r   r8   r    r!   r"   r#   Ztyping_extensionsr$   r%   r0   r6   r   r.   r.   r.   r/   <module>   s0   
  