
    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y)    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yt        |   di | |j                  dd      | _        || _        || _        | j                          y)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      P/var/www/openai/venv/lib/python3.12/site-packages/fsspec/implementations/sftp.pyr   zSFTPFileSystem.__init__   sH     <<&:&"z6:	$    c                 p   t         j                  d| j                         t        j                         | _        | j
                  j                  t        j                                 | j
                  j                  | j                  fi | j                   | j
                  j                         | _        y )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   su    3TYY?((*//0F0F0HIDII99;;((*r   c                     t        |      d   S )Npathr   )clsr(   s     r   _strip_protocolzSFTPFileSystem._strip_protocol9   s    $T*622r   c                 d    t        |       }|j                  dd        |j                  dd        |S )Nr(   protocol)r   r   )urlpathouts     r   _get_kwargs_from_urlsz$SFTPFileSystem._get_kwargs_from_urls=   s-    #G,
D!
r   c                     t         j                  d|       | j                  |      rt        d|       |r| j	                  |       y | j
                  j                  ||       y )NzCreating folder %sFile exists: )r   r   existsFileExistsErrormakedirsr%   mkdir)r   r(   create_parentsmodes       r   r5   zSFTPFileSystem.mkdirD   sP    )40;;t!M$"899MM$HHNN4&r   c                    | j                  |      r|st        d|       |j                  d      }|d d dk(  rdnd}|D ]>  }|s|r| d| n|}| j                  |      r#| j                  j	                  ||       @ y )Nr1   /    )r2   r3   splitr%   r5   )r   r(   exist_okr7   partsnew_pathparts          r   r4   zSFTPFileSystem.makedirsN   s    ;;tX!M$"899

3r(c/3rD3;hZq/{{8,HHNN8T2	 r   c                 f    t         j                  d|       | j                  j                  |       y )NzRemoving folder %s)r   r   r%   rmdirr   r(   s     r   rB   zSFTPFileSystem.rmdir[   s!    )40tr   c                 d    | j                  | j                  j                  |            }||d<   |S )Nname)_decode_statr%   stat)r   r(   rG   s      r   infozSFTPFileSystem.info_   s-      t!45Vr   c           
          t        | j                        rd}nt        | j                        rd}nd}d| j                  || j                  | j
                  t        j                  j                  | j                  t        j                  j                        t        j                  j                  | j                  t        j                  j                        d}|r/dj                  |j                  d      | j                  g      |d<   |S )	N	directorylinkfiler;   )tz)rE   sizetypeuidgidtimemtimer9   rE   )r   st_moder   st_sizest_uidst_giddatetimefromtimestampst_atimetimezoneutcst_mtimejoinrstripfilename)rG   parent_pathtr.   s       r   rF   zSFTPFileSystem._decode_statd   s    4<< AT\\"AALL;;;;%%33("3"3"7"7 4  &&44("3"3"7"7 5 
 ((K$6$6s$;T]]#KLCK
r   c                     t         j                  d|       | j                  j                  |      D cg c]  }| j	                  ||       }}|r|S |D cg c]  }|d   	 }}t        |      S c c}w c c}w )NzListing folder %srE   )r   r   r%   listdir_iterrF   sorted)r   r(   detailrG   statspathss         r   lszSFTPFileSystem.ls}   sy    ($/;?88;P;PQU;VW;V4""4.;VWL.34edT&\eE4%=  X 5s   A0A5c                 j    t         j                  d||       | j                  j                  ||       y )NzPut file %s into %s)r   r   r%   put)r   lpathrpathcallbackkwargss        r   rk   zSFTPFileSystem.put   s%    *E59UE"r   c                     | j                  |      rt        j                  |d       y | j                  j	                  | j                  |      |       y )NT)r=   )isdirosr4   r%   getr*   )r   rm   rl   ro   s       r   get_filezSFTPFileSystem.get_file   s9    ::eKK-HHLL--e4e<r   c                    t         j                  d|       |j                  dd      du rdj                  | j                  t        t        j                               g      }| j                  j                  |||r|nd      }||_        ||_
        | |_        t        j                  t        |      |_        t        j                  t         |      |_        |S | j                  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
autocommitTFr9   )bufsize)r   r   rs   r^   r   struuiduuid4r%   open
targetpathfstypes
MethodTypecommit_a_filecommitdiscard_a_filediscard)r   r(   r7   
block_sizero   path2fs          r   _openzSFTPFileSystem._open   s     	&-::lD)U2HHdmmS->?@EeT:QSTAAJALAD''q9AH((;AI  dD
*PRSAr   c                     | j                  |      r| j                  j                  |       y | j                  j                  |       y N)rq   r%   rB   removerC   s     r   _rmzSFTPFileSystem._rm   s.    ::dHHNN4 HHOOD!r   c                 j    t         j                  d||       | j                  j                  ||       y )NzRenaming %s into %s)r   r   r%   posix_rename)r   oldnews      r   mvzSFTPFileSystem.mv   s'    *C5c3'r   )T  )Fr   r   )F)rbN)__name__
__module____qualname____doc__r,   r   r   classmethodr*   staticmethodr/   r5   r4   rB   rH   rF   ri   rk   rt   r   r   r   __classcell__)r   s   @r   r
   r
      s     H,+ 3 3  '3
  0!#=("(r   r
   c                 d    | j                   j                  | j                  | j                         y r   )r~   r   r   r}   r&   s    r   r   r      s    GGJJt}}doo.r   c                 N    | j                   j                  | j                         y r   )r~   r   r   r&   s    r   r   r      s    GGKKr   )rX   loggingrr   r   rz   rG   r   r   r   r;   r   utilsr   	getLoggerr   r
   r   r   r   r   r   <module>r      sL      	   !  ! )			=	)\(' \(~/r   