
    çg                         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mZ d dlZddl	m
Z
 ddlmZ  ej        d          Z G d de
          Zd	 Zd
 ZdS )    N)S_ISDIRS_ISLNK   )AbstractFileSysteminfer_storage_optionszfsspec.sftpc                        e Zd ZdZdZ fdZd Zed             Ze	d             Z
dd	ZddZd Zd Ze	dd            ZddZddZd ZddZd Zd Z xZS )SFTPFileSystemzFiles over SFTP/SSH

    Peer-to-peer filesystem over SSH using paramiko.

    Note: if using this with the ``open`` or ``open_files``, with full URLs,
    there is no way to tell if a path is relative, so all paths are assumed
    to be absolute.
    )sftpsshc                     | j         rdS  t                      j        di | |                    dd          | _        || _        || _        |                                  dS )a  

        Parameters
        ----------
        host: str
            Hostname or IP as a string
        temppath: str
            Location on the server to put files, when within a transaction
        ssh_kwargs: dict
            Parameters passed on to connection. See details in
            https://docs.paramiko.org/en/3.3/api/client.html#paramiko.client.SSHClient.connect
            May include port, username, password...
        Ntemppathz/tmp )_cachedsuper__init__popr   host
ssh_kwargs_connect)selfr   r   	__class__s      W/var/www/html/ai-engine/env/lib/python3.11/site-packages/fsspec/implementations/sftp.pyr   zSFTPFileSystem.__init__   se     < 	F&&:&&&"z6::	$    c                 B   t                               d| j                   t          j                    | _        | j                            t          j                                | j        j        | j        fi | j	         | j        
                                | _        d S )NzConnecting to SFTP server %s)loggerdebugr   paramiko	SSHClientclientset_missing_host_key_policyAutoAddPolicyconnectr   	open_sftpftpr   s    r   r   zSFTPFileSystem._connect2   s    3TY???(**//0F0H0HIIIDI99999;((**r   c                 ,    t          |          d         S )Npathr   )clsr(   s     r   _strip_protocolzSFTPFileSystem._strip_protocol9   s    $T**622r   c                 |    t          |           }|                    dd            |                    dd            |S )Nr(   protocol)r   r   )urlpathouts     r   _get_kwargs_from_urlsz$SFTPFileSystem._get_kwargs_from_urls=   s<    #G,,
D!!!
r   T  c                     t                               d|           |                     |          rt          d|           |r|                     |           d S | j                            ||           d S )NzCreating folder %sFile exists: )r   r   existsFileExistsErrormakedirsr%   mkdir)r   r(   create_parentsmodes       r   r6   zSFTPFileSystem.mkdirD   s~    )4000;;t 	:!"8$"8"8999 	'MM$HNN4&&&&&r   Fc                 *   |                      |          r|st          d|           |                    d          }|d d         dk    rdnd}|D ]?}|r;|r| d| n|}|                      |          s| j                            ||           @d S )Nr2   /    )r3   r4   splitr%   r6   )r   r(   exist_okr8   partsnew_pathparts          r   r5   zSFTPFileSystem.makedirsN   s    ;;t 	:X 	:!"8$"8"8999

3rr(c//33r 	3 	3D 33;Eh/////{{8,, 3HNN8T222		3 	3r   c                 p    t                               d|           | j                            |           d S )NzRemoving folder %s)r   r   r%   rmdirr   r(   s     r   rC   zSFTPFileSystem.rmdir[   s1    )4000tr   c                 j    |                      | j                            |                    }||d<   |S )Nname)_decode_statr%   stat)r   r(   rH   s      r   infozSFTPFileSystem.info_   s1      t!4!455Vr   Nc           
         t          | j                  rd}nt          | j                  rd}nd}d| j        || j        | j        t          j                            | j        t          j	        j
                  t          j                            | j        t          j	        j
                  d}|r2d                    |                    d          | j        g          |d<   |S )	N	directorylinkfiler<   )tz)rF   sizetypeuidgidtimemtimer:   rF   )r   st_moder   st_sizest_uidst_giddatetimefromtimestampst_atimetimezoneutcst_mtimejoinrstripfilename)rH   parent_pathtr.   s       r   rG   zSFTPFileSystem._decode_statd   s    4<   	AAT\"" 	AAAL;;%33("3"7 4   &44("3"7 5  
 
  	M((K$6$6s$;$;T]#KLLCK
r   c                      t                               d            fd j                                      D             }|r|S d |D             }t	          |          S )NzListing folder %sc                 <    g | ]}                     |          S r   )rG   ).0rH   r(   r   s     r   
<listcomp>z%SFTPFileSystem.ls.<locals>.<listcomp>   s)    WWW4""4..WWWr   c                     g | ]
}|d          S )rF   r   )rf   rH   s     r   rg   z%SFTPFileSystem.ls.<locals>.<listcomp>   s    444dT&\444r   )r   r   r%   listdir_itersorted)r   r(   detailstatspathss   ``   r   lszSFTPFileSystem.ls}   sr    ($///WWWWW48;P;PQU;V;VWWW 	!L44e444E%== r   c                 t    t                               d||           | j                            ||           d S )NzPut file %s into %s)r   r   r%   put)r   lpathrpathcallbackkwargss        r   rp   zSFTPFileSystem.put   s5    *E5999UE"""""r   c                     |                      |          rt          j        |d           d S | j                            |                     |          |           d S )NT)r>   )isdirosr5   r%   getr*   )r   rr   rq   rt   s       r   get_filezSFTPFileSystem.get_file   s[    ::e 	=K------HLL--e44e<<<<<r   rbc                    t                               d|           |                    dd          du rd                    | j        t          t          j                              g          }| j        	                    |||r|nd          }||_        ||_
        | |_        t          j        t          |          |_        t          j        t           |          |_        n!| j        	                    |||r|nd          }|S )z
        block_size: int or None
            If 0, no buffering, if 1, line buffering, if >1, buffer that many
            bytes, if None use default from paramiko.
        zOpening file %s
autocommitTFr:   )bufsize)r   r   rx   r_   r   struuiduuid4r%   open
targetpathfstypes
MethodTypecommit_a_filecommitdiscard_a_filediscard)r   r(   r8   
block_sizert   path2fs          r   _openzSFTPFileSystem._open   s     	&---::lD))U22HHdmS->->?@@EeT3S::QSTTAAJALAD'q99AH(;;AIIdD
2R**PRSSAr   c                     |                      |          r| j                            |           d S | j                            |           d S N)rv   r%   rC   removerD   s     r   _rmzSFTPFileSystem._rm   sJ    ::d 	"HNN4     HOOD!!!!!r   c                 t    t                               d||           | j                            ||           d S )NzRenaming %s into %s)r   r   r%   posix_rename)r   oldnews      r   mvzSFTPFileSystem.mv   s7    *C555c3'''''r   )Tr0   )Fr0   r   )F)rz   N)__name__
__module____qualname____doc__r,   r   r   classmethodr*   staticmethodr/   r6   r5   rC   rI   rG   rn   rp   ry   r   r   r   __classcell__)r   s   @r   r
   r
      s]         H    ,+ + + 3 3 [3   \' ' ' '3 3 3 3    
    \0! ! ! !# # # #= = =   (" " "( ( ( ( ( ( (r   r
   c                 P    | j                             | j        | j                   d S r   )r   r   r   r   r&   s    r   r   r      s"    GJJt}do.....r   c                 D    | j                             | j                   d S r   )r   r   r   r&   s    r   r   r      s    GKKr   )rY   loggingrw   r   r   rH   r   r   r   r<   r   utilsr   	getLoggerr   r
   r   r   r   r   r   <module>r      s      				   ! ! ! ! ! ! ! !  ! ! ! ! ! ! ) ) ) ) ) )		=	)	)\( \( \( \( \(' \( \( \(~/ / /    r   