3 L]@snddlmZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddlmZmZmZddlmZmZmZddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5dd l6m7Z7dd l8m9Z9dd l:m;Z;dd ldZ?Gdddee=Z@Gddde@ZAdS))hexlifyN)util)Channel)Message)INFODEBUGo777)bulong)BaseSFTP CMD_OPENDIR CMD_HANDLE SFTPError CMD_READDIRCMD_NAME CMD_CLOSESFTP_FLAG_READSFTP_FLAG_WRITESFTP_FLAG_CREATESFTP_FLAG_TRUNCSFTP_FLAG_APPENDSFTP_FLAG_EXCLCMD_OPEN CMD_REMOVE CMD_RENAME CMD_MKDIR CMD_RMDIRCMD_STAT CMD_ATTRS CMD_LSTAT CMD_SYMLINK CMD_SETSTAT CMD_READLINK CMD_REALPATH CMD_STATUS CMD_EXTENDEDSFTP_OKSFTP_EOFSFTP_NO_SUCH_FILESFTP_PERMISSION_DENIED)SFTPAttributes) SSHException)SFTPFile)ClosingContextManagercCsHy |jdSttfk rBy |jdStk r<|SXYnXdS)z decode a string as ascii or utf8 if possible (as required by the sftp protocol). if neither works, just return a byte string because the server probably doesn't know the filename's encoding. asciizutf-8N)encode UnicodeErrorAttributeErrordecode)sr5:/tmp/pip-install-wfra5znf/paramiko/paramiko/sftp_client.py _to_unicodeHs  r7/cs^eZdZdZddZedQddZfddZd d Zd d Z dRddZ dSddZ dTddZ dVddZ e ZddZeZddZddZefdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdWd5d6Zd7d8Z d9d:Z!dXd=d>Z"dYd?d@Z#dZdAdBZ$d[dCdDZ%dEdFZ&dGdHZ'd\dIdJZ(dKdLZ)dMdNZ*dOdPZ+Z,S)] SFTPClientz SFTP client object. Used to open an SFTP session across an open SSH `.Transport` and perform remote file operations. Instances of this class may be used as context managers. c Cstj|||_d|_d|_tj|_d|_t j |_ t |t krj|jj}tj|jd|_|j|_y |j}Wntk rtdYnX|jtdj|dS)a Create an SFTP client from an existing `.Channel`. The channel should already have requested the ``"sftp"`` subsystem. An alternate way to create an SFTP client context is by using `from_transport`. :param .Channel sock: an open `.Channel` using the ``"sftp"`` subsystem :raises: `.SSHException` -- if there's an exception while negotiating sftp FNz.sftpzEOF during negotiationz*Opened sftp connection (server version {}))r __init__sockZ ultra_debugrequest_number threadingLock_lock_cwdweakrefWeakValueDictionary _expectingtyperZ get_transportrZ get_loggerZget_log_channelloggerZ get_hexdumpZ _send_versionEOFErrorr,_logrformat)selfr< transportZserver_versionr5r5r6r;ds(       zSFTPClient.__init__NcCs,|j||d}|dkrdS|jd||S)a\ Create an SFTP client channel from an open `.Transport`. Setting the window and packet sizes might affect the transfer speed. The default settings in the `.Transport` class are the same as in OpenSSH and should work adequately for both files transfers and interactive sessions. :param .Transport t: an open `.Transport` which is already authenticated :param int window_size: optional window size for the `.SFTPClient` session. :param int max_packet_size: optional max packet size for the `.SFTPClient` session.. :return: a new `.SFTPClient` object, referring to an sftp session (channel) across the transport .. versionchanged:: 1.15 Added the ``window_size`` and ``max_packet_size`` arguments. ) window_sizemax_packet_sizeNsftp)Z open_sessionZinvoke_subsystem)clstrLrMZchanr5r5r6from_transports   zSFTPClient.from_transportcsht|tr,xX|D]}|j||f|qWn8|jdd}tt|j|d|f|jjgt|dS)N%z%%z [chan %s] ) isinstancelistrHreplacesuperr9r<get_name)rJlevelmsgargsm) __class__r5r6rHs    zSFTPClient._logcCs|jtd|jjdS)zc Close the SFTP session and its underlying channel. .. versionadded:: 1.4 zsftp session closed.N)rHrr<close)rJr5r5r6r]s zSFTPClient.closecCs|jS)z Return the underlying `.Channel` object for this SFTP session. This might be useful for doing things like setting a timeout on the channel. .. versionadded:: 1.7.1 )r<)rJr5r5r6 get_channelszSFTPClient.get_channel.cCsdd|j|DS)a Return a list containing the names of the entries in the given ``path``. The list is in arbitrary order. It does not include the special entries ``'.'`` and ``'..'`` even if they are present in the folder. This method is meant to mirror ``os.listdir`` as closely as possible. For a list of full `.SFTPAttributes` objects, see `listdir_attr`. :param str path: path to list (defaults to ``'.'``) cSsg|] }|jqSr5)filename).0fr5r5r6 sz&SFTPClient.listdir..) listdir_attr)rJpathr5r5r6listdirs zSFTPClient.listdirc Cs|j|}|jtdj||jt|\}}|tkr       zSFTPClient.listdir_iterrr:c Cs|j|}|jtdj||d}d|ks2d|kr:|tO}d|ksRd|ksRd|krZ|tO}d|krn|ttBO}d|kr|ttBO}d|kr|tt BO}t }|j t |||\}}|t krtd|j}|jtd j||tt|t||||S) a[ Open a file on the remote server. The arguments are the same as for Python's built-in `python:file` (aka `python:open`). A file-like object is returned, which closely mimics the behavior of a normal Python file object, including the ability to be used as a context manager. The mode indicates how the file is to be opened: ``'r'`` for reading, ``'w'`` for writing (truncating an existing file), ``'a'`` for appending, ``'r+'`` for reading/writing, ``'w+'`` for reading/writing (truncating an existing file), ``'a+'`` for reading/appending. The Python ``'b'`` flag is ignored, since SSH treats all files as binary. The ``'U'`` flag is supported in a compatible way. Since 1.5.2, an ``'x'`` flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to Python's file flags, but is commonly known as the ``O_EXCL`` flag in posix. The file will be buffered in standard Python style by default, but can be altered with the ``bufsize`` parameter. ``0`` turns off buffering, ``1`` uses line buffering, and any number greater than 1 (``>1``) uses that specific buffer size. :param str filename: name of the file to open :param str mode: mode (Python-style) to open in :param int bufsize: desired buffering (-1 = default buffer size) :return: an `.SFTPFile` object representing the open file :raises: ``IOError`` -- if the file could not be opened. zopen({!r}, {!r})rr}+waxzExpected handlezopen({!r}, {!r}) -> {})rgrHrrIrrrrrrr+rhrrrrir rr-) rJr`modebufsizeZimodeZ attrblockrPrYror5r5r6openFs.    zSFTPClient.opencCs,|j|}|jtdj||jt|dS)a  Remove the file at the given path. This only works on files; for removing folders (directories), use `rmdir`. :param str path: path (absolute or relative) of the file to remove :raises: ``IOError`` -- if the path refers to a folder (directory) z remove({!r})N)rgrHrrIrhr)rJrer5r5r6removes zSFTPClient.removecCs:|j|}|j|}|jtdj|||jt||dS)a3 Rename a file or folder from ``oldpath`` to ``newpath``. .. note:: This method implements 'standard' SFTP ``RENAME`` behavior; those seeking the OpenSSH "POSIX rename" extension behavior should use `posix_rename`. :param str oldpath: existing name of the file or folder :param str newpath: new name for the file or folder, must not exist already :raises: ``IOError`` -- if ``newpath`` is a folder, or something else goes wrong zrename({!r}, {!r})N)rgrHrrIrhr)rJoldpathnewpathr5r5r6renames  zSFTPClient.renamecCs<|j|}|j|}|jtdj|||jtd||dS)a Rename a file or folder from ``oldpath`` to ``newpath``, following posix conventions. :param str oldpath: existing name of the file or folder :param str newpath: new name for the file or folder, will be overwritten if it already exists :raises: ``IOError`` -- if ``newpath`` is a folder, posix-rename is not supported by the server or something else goes wrong :versionadded: 2.2 zposix_rename({!r}, {!r})zposix-rename@openssh.comN)rgrHrrIrhr&)rJrrr5r5r6 posix_renames   zSFTPClient.posix_renamecCs<|j|}|jtdj||t}||_|jt||dS)as Create a folder (directory) named ``path`` with numeric mode ``mode``. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. :param str path: name of the folder to create :param int mode: permissions (posix-style) for the newly-created folder zmkdir({!r}, {!r})N)rgrHrrIr+st_moderhr)rJrerrtr5r5r6mkdirs zSFTPClient.mkdircCs,|j|}|jtdj||jt|dS)zj Remove the folder named ``path``. :param str path: name of the folder to remove z rmdir({!r})N)rgrHrrIrhr)rJrer5r5r6rmdirs zSFTPClient.rmdircCsF|j|}|jtdj||jt|\}}|tkr` of bytes written to the opened file object .. versionadded:: 1.10 r)rrrrN)rrrprefetchr)rJrrrrrr5r5r6getfos   zSFTPClient.getfoc CsNt|d}|j|||}WdQRXtj|}|j|krJtdj|j|dS)a Copy a remote file (``remotepath``) from the SFTP server to the local host as ``localpath``. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. :param str remotepath: the remote file to copy :param str localpath: the destination path on the local host :param callable callback: optional callback function (form: ``func(int, int)``) that accepts the bytes transferred so far and the total bytes to be transferred .. versionadded:: 1.4 .. versionchanged:: 1.7.4 Added the ``callback`` param rNzsize mismatch in get! {} != {})rrrrrrrI)rJrrrrrr4r5r5r6gets    zSFTPClient.getcGs |jtd|f|}|j|S)N)rwrE_read_response)rJrPargr{r5r5r6rh+szSFTPClient._requestc Gs|jjzt}|j|jxX|D]P}t|tr>|j|q$t|trT|j|q$t|t rj|j |q$|j |q$W|j}||j |<|jd7_Wd|jj X|j|||S)Nr:)r@acquirerZadd_intr=rSr Z add_int64intr+_packZ add_stringrDreleaseZ _send_packet)rJfileobjrPrrYitemr{r5r5r6rw/s$            zSFTPClient._async_requestcCsxy|j\}}Wn0tk rB}ztdj|WYdd}~XnXt|}|j}|jjz>||jkr|j t dj||dkrPw|j|}|j|=Wd|jj X||kr|t kr|j |||fS|tdk r|j||||dkrPqWdS)NzServer connection dropped: {}zUnexpected response #{})NN)rxrGr,rIrrjr@rrDrHrrr%ryrEZ_async_response)rJZwaitforrPrerYr{rr5r5r6rHs4        zSFTPClient._read_responsecCs(x"||jjkr"|j|jqWdS)N)rDvaluesrZ_check_exception)rJrr5r5r6_finish_responsesmszSFTPClient._finish_responsescCsf|j}|j}|tkrdS|tkr.t|n4|tkrDttj|n|t krZttj |nt|dS)zU Raises EOFError or IOError on error status; otherwise does nothing. N) rjrlr'r(rGr)rrENOENTr*EACCES)rJrYrtextr5r5r6ryrs zSFTPClient._convert_statuscCsTt|}|jdkr|St|r2|ddtkr2|S|jtkrF|j|S|jt|S)zr Return an adjusted path if we're emulating a "current working directory" for the server. Nrr:)r rArb_slash)rJrer5r5r6rgs   zSFTPClient._adjust_cwd)NN)r_)r_)r_ru)r}r)N)rNT)NT)N)N)N)-__name__ __module__ __qualname____doc__r; classmethodrQrHr]r^rfrdr|rrrunlinkrrrrrrrrrrrrrrrrrrrrrrhrwrrryrg __classcell__r5r5)r\r6r9ZsP(     * @ ;       *   %r9c@seZdZdZdS)SFTPzA An alias for `.SFTPClient` for backwards compatibility. N)rrrrr5r5r5r6rsr)Bbinasciirrrrr>rrBZparamikorZparamiko.channelrZparamiko.messagerZparamiko.commonrrrZparamiko.py3compatr r r Z paramiko.sftpr r rrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*Zparamiko.sftp_attrr+Zparamiko.ssh_exceptionr,Zparamiko.sftp_filer-Z paramiko.utilr.r7rr9rr5r5r5r6s4    "    @