o
    3gQ,                     @  s   d dl m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 d dlmZmZ G dd dZG dd	 d	eZG d
d deZdS )    )annotationsN)contextmanager)ProcessExecutionErrorshquote)FSUserPathc                   @  s   e Zd ZdZdd Zdd Zedd  ZZedd  Z	Z
ed	d  ZZed
d  ZZedd  ZZedd  ZZedd  ZZedd  ZZedd  ZZedd  ZZdS )StatReszPOSIX-like stat resultc                 C  s   t || _d S N)tuple_tup)selftup r   K/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/path/remote.py__init__   s   zStatRes.__init__c                 C  s
   | j | S r	   )r   )r   indexr   r   r   __getitem__   s   
zStatRes.__getitem__c                 C     | d S )Nr   r   r   r   r   r   <lambda>       zStatRes.<lambda>c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N   r   r   r   r   r   r      r   c                 C  r   )N	   r   r   r   r   r   r      r   N)__name__
__module____qualname____doc__r   r   propertyst_modemodest_inoinost_devdevst_nlinknlinkst_uiduidst_gidgidst_sizesizest_atimeatimest_mtimemtimest_ctimectimer   r   r   r   r      s    r   c                      s  e Zd ZdZ fddZdd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' ZdWd)d*Zd+d, Zd-d. ZeZd/d0 ZdXd2d3ZdYd6d7ZdZd8d9ZdZd:d;Z d<d= Z!d[d>d?Z"d@dA Z#d\dCdDZ$dEdF Z%dGdH Z&d]d4dKdLdMZ'd^dOdPZ(edQdR Z)edSdT Z*edUdV Z+  Z,S )_
RemotePathz+The class implementing remote-machine pathsc           
        sB  |st d|j dk}g }ttt|}|d r:|d d dvr:t|dr*|jn	|j	dd 
 }|g|R }|D ]@}|rLt|dd	d	}nt|d	}|d sa|d |d d = |D ]}|d
v rjqc|dkrv|ru|d qc|| qcq<|rt | d|}	d|	_nt | d	d	| }	d|	_||	_|	S )Nz/At least one path part is required (none given)windowsr   )/\_cwdpwdr   r<   r;   >    .z..FT)	TypeErrorunamelowerr
   mapstrhasattrr=   _sessionrunstripreplacesplitpopappendsuper__new__joinCASE_SENSITIVEremote)
clsrS   partsr:   normedcwdpplistitemr   	__class__r   r   rP   $   sH   


zRemotePath.__new__c                 G  s   t | jg|R  S r	   r9   rS   r   rU   r   r   r   _formP      zRemotePath._formc                 C  s   t | S r	   )rF   r   r   r   r   _pathS   s   zRemotePath._pathc                 C  s(   dt | vr
t | S t | ddd S )Nr;   r   )rF   rsplitr   r   r   r   nameW   s   zRemotePath.namec                 C  s2   dt | vr
t | S | | jt | ddd S )Nr;   r   r   )rF   r\   rS   rb   r   r   r   r   dirname]   s   zRemotePath.dirnamec                 C  s   d| j ddd  S Nr@   r   rc   rb   r   r   r   r   suffixc   s   zRemotePath.suffixc                 C  sD   | j }g }d|v r|dd\}}|d|  d|v s	tt|S re   )rc   rb   rN   listreversed)r   rc   extsextr   r   r   suffixesg   s   zRemotePath.suffixesc                 C     | j | \}}tt||S r	   )rS   _path_getuidr   int)r   r.   rc   r   r   r   r.   p      zRemotePath.uidc                 C  rm   r	   )rS   _path_getgidr   ro   )r   r0   rc   r   r   r   r0   u   rp   zRemotePath.gidc                 C  s   | j | jfS r	   )rS   ra   r   r   r   r   	_get_infoz   s   zRemotePath._get_infoc                 G  s   t | j| g|R  S r	   r]   r^   r   r   r   rQ   }      zRemotePath.joinc                   s&      sg S  fdd j D S )Nc                   s   g | ]}  |qS r   rQ   .0fnr   r   r   
<listcomp>   s    z#RemotePath.list.<locals>.<listcomp>is_dirrS   _path_listdirr   r   r   r   rh         zRemotePath.listc                   s&      sdS  fdd j D S )Nr   c                 3  s    | ]}  |V  qd S r	   rt   ru   r   r   r   	<genexpr>   s    z%RemotePath.iterdir.<locals>.<genexpr>ry   r   r   r   r   iterdir   r|   zRemotePath.iterdirc                 C     | j | }|s
dS |jdkS )NF	directoryrS   
_path_stat	text_moder   resr   r   r   rz         
zRemotePath.is_dirc                 C  s   | j | }|s
dS |jdv S )NF)zregular filezregular empty filer   r   r   r   r   is_file   r   zRemotePath.is_filec                 C  r   )NFzsymbolic linkr   r   r   r   r   
is_symlink   r   zRemotePath.is_symlinkc                 C  s   | j | d uS r	   )rS   r   r   r   r   r   exists   s   zRemotePath.existsc                 C  s.   | j | }|d u rttjttjd|S Nr?   )rS   r   OSErrorerrnoENOENTosstrerrorr   r   r   r   stat   s   zRemotePath.statc                 C  s   |  | j| j| S r	   )r\   rS   rd   )r   rc   r   r   r   	with_name   rs   zRemotePath.with_namer   c                 C  s   |r| dr|dkrtd|| j}|d u rt| jnt|t| j}t|D ]
}|dd\}}q*| | j	| j
||  S )Nr@   zInvalid suffix r   )
startswith
ValueErrorrc   lenrl   minrangerb   r\   rS   rd   )r   rg   depthrc   _r   r   r   with_suffix   s   "zRemotePath.with_suffixc                   s     | fddS )Nc                   s    fdd j  | D S )Nc                   s   g | ]}t  j|qS r   r]   )rv   mr   r   r   rx      s    z5RemotePath.glob.<locals>.<lambda>.<locals>.<listcomp>)rS   
_path_glob)patr   r   r   r      s   
 z!RemotePath.glob.<locals>.<lambda>)_glob)r   patternr   r   r   glob   s   
zRemotePath.globc                 C  s   |   sd S | j|  d S r	   )r   rS   _path_deleter   r   r   r   delete   s   zRemotePath.deletec                 C  sJ   t |tr|j| jurtdnt |tstd|| j| | d S )N(dst points to a different remote machineGdst must be a string or a RemotePath (to the same remote machine), got )
isinstancer9   rS   rB   rF   
_path_mover   dstr   r   r   move   s   

zRemotePath.moveFc                 C  s   t |tr|j| jurtdnt |tstd||r.t |tr)t| j|}|  nt |tr9t| j|}| rAtd| j| | d S )Nr   r   z%Override not specified and dst exists)r   r9   rS   rB   rF   r   r   
_path_copy)r   r   overrider   r   r   copy   s"   




zRemotePath.copyNTc              
   C  s   |r|r| j j| |dd d S |r"t| jdkr"| j j| j|dd z| j j| |dd W d S  tyS } zd|jvr< |sHttj	dt
| d W Y d }~d S d }~ww )NT)r&   minus_pr   FzFile existszFile exists (on remote end))rS   _path_mkdirr   rU   parentr   stderrr   r   EEXISTrF   )r   r&   parentsexist_okexr   r   r   mkdir   s$   
zRemotePath.mkdirc                 C  s   | j | }|r||S |S r	   )rS   
_path_readdecode)r   encodingdatar   r   r   read   s   
zRemotePath.readc                 C  s    |r| |}| j| | d S r	   )encoderS   _path_write)r   r   r   r   r   r   write   s   
zRemotePath.writec                 C  s   | j t|  d S r	   )rS   _path_touchrF   r   r   r   r   touch   rs   zRemotePath.touchc                 C  s,   | j | |||d u r|   d S | d S r	   )rS   _path_chownrz   )r   ownergroup	recursiver   r   r   chown   s
   zRemotePath.chownc                 C  s   | j ||  d S r	   )rS   _path_chmod)r   r&   r   r   r   chmod  r`   zRemotePath.chmodr   c                 C  sD   |  |}| j| }|d u rdS |jd@ }|d? |@ p!|d? |@ S )NFi  r   r   )_access_mode_to_flagsrS   r   r%   )r   r&   r   maskr   r   r   access  s   

zRemotePath.accessc                 C  sL   t |tr|j| jurtdnt |tstd|| j| |d d S )Nr   r   Fr   r9   rS   rB   rF   
_path_linkr   r   r   r   link  s   

zRemotePath.linkc                 C  sF   t |tr|j| jurtdn	t |tstd| j| |d d S )Nr   zNdst must be a string or a RemotePath (to the same remote machine), got {dst!r}Tr   r   r   r   r   symlink  s   

zRemotePath.symlinkrrA   )r   c                C  sD   |durt dt| jdrt| jjdr| jj| ||S t d)zn
        Opens this path as a file.

        Only works for ParamikoMachine-associated paths for now.
        Nz0encoding not supported for ParamikoMachine pathssftpopenzGRemotePath.open only works for ParamikoMachine-associated paths for now)NotImplementedErrorrG   rS   r   r   )r   r&   bufsizer   r   r   r   r   !  s   zRemotePath.opensshc                 C  s$   t t| }| d| jj | S )Nz://)urllibpathname2urlrF   rS   _fqhost)r   schemerg   r   r   r   as_uri3  s   zRemotePath.as_uric                 C  s   | j dd S )Nr@   r   rf   r   r   r   r   stem7  s   zRemotePath.stemc                 C     dS )Nr;   r   r   r   r   r   root;     zRemotePath.rootc                 C  r   r   r   r   r   r   r   drive?  r   zRemotePath.drive)r   )F)NTTr	   )NNN)r   )r   rA   )r   )-r    r!   r"   r#   rP   r_   r$   ra   rc   rd   rg   rl   r.   r0   rr   rQ   rh   r~   rz   r   r   r   r   r   r   r   r   unlinkr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r[   r   r9   !   sf    ,







	











r9   c                      sD   e Zd ZdZ fddZdd Zdd Zdd	 Zed
d Z	  Z
S )RemoteWorkdirz$Remote working directory manipulatorc                   s    t  | ||jdd  S )Nr>   r   )rO   rP   rH   rI   rJ   )rT   rS   r[   r   r   rP   G  s    zRemoteWorkdir.__new__c                 C  s   t d)Nzunhashable type)rB   r   r   r   r   __hash__J  s   zRemoteWorkdir.__hash__c                 C  s6   | j jdt|  t| j dr| j `| | j S )z6Changes the current working directory to the given onezcd r=   )rS   rH   rI   r   rG   r=   r\   )r   newdirr   r   r   chdirM  s   zRemoteWorkdir.chdirc                 C  s   t | j| S )zhReturns the current working directory as a
        `remote path <plumbum.path.remote.RemotePath>` objectr]   r   r   r   r   getpathT  s   zRemoteWorkdir.getpathc              	   c  s6    | j }| |}z|V  W | | dS | | w )a"  A context manager used to ``chdir`` into a directory and then ``chdir`` back to
        the previous location; much like ``pushd``/``popd``.

        :param newdir: The destination director (a string or a
                       :class:`RemotePath <plumbum.path.remote.RemotePath>`)
        N)ra   r   )r   r   prevchanged_dirr   r   r   __call__Y  s   
zRemoteWorkdir.__call__)r    r!   r"   r#   rP   r   r   r   r   r   r   r   r   r[   r   r   D  s    r   )
__future__r   r   r   urllib.requestrequestr   
contextlibr   plumbum.commandsr   r   plumbum.path.baser   r   r   r9   r   r   r   r   r   <module>   s      %