U q`@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)ClosingContextManagerc CsXz |dWSttfk rRz|dWYStk rL|YYSXYnXdS)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-target-nv4zd3e_/lib/python/paramiko/sftp_client.py _to_unicodeHs r7/cs^eZdZdZddZedQddZfddZd d Zd d Z dRddZ dSddZ dTddZ dUddZ e ZddZeZddZddZefdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdVd5d6Zd7d8Z d9d:Z!dWd=d>Z"dXd?d@Z#dYdAdBZ$dZdCdDZ%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. cCst|||_d|_d|_t|_d|_t |_ t |t krj|j}t|d|_||_z |}Wntk rtdYnX|td|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|d||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_transportcsdt|tr(|D]}|j||f|qn8|dd}tt|j|d|f|jgt|dS)N%z%%z [chan %s] ) isinstancelistrHreplacesuperr9r<get_name)rJlevelmsgargsm __class__r5r6rHs   zSFTPClient._logcCs|td|jdS)zc Close the SFTP session and its underlying channel. .. versionadded:: 1.4 zsftp session closed.N)rHrr<closerJr5r5r6r^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<r_r5r5r6 get_channelszSFTPClient.get_channel.cCsdd||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_attrrJpathr5r5r6listdirs zSFTPClient.listdirc Cs||}|td||t|\}}|tkr1``) 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+waxrkzopen({!r}, {!r}) -> {})rnrHrrIrrrrrrr+rorrrrpr rr-) rJrbmodebufsizeZimodeZ attrblockrPrYrvr5r5r6openFs6     zSFTPClient.opencCs,||}|td||t|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)rnrHrrIrorrgr5r5r6removes zSFTPClient.removecCs:||}||}|td|||t||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)rnrHrrIrorrJZoldpathnewpathr5r5r6renames  zSFTPClient.renamecCs<||}||}|td|||td||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)rnrHrrIror&rr5r5r6 posix_renames  zSFTPClient.posix_renamecCs<||}|td||t}||_|t||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)rnrHrrIr+st_moderorrJrhrr{r5r5r6mkdirs zSFTPClient.mkdircCs,||}|td||t|dS)zj Remove the folder named ``path``. :param str path: name of the folder to remove z rmdir({!r})N)rnrHrrIrorrgr5r5r6rmdirs zSFTPClient.rmdircCsF||}|td||t|\}}|tkr` of bytes written to the opened file object .. versionadded:: 1.10 rrN)rrrZprefetchr)rJrrrrrr5r5r6getfos  zSFTPClient.getfoc CsNt|d}||||}W5QRXt|}|j|krJtd|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 rzsize mismatch in get! {} != {}N)rrrrrrrI)rJrrrrrr4r5r5r6gets    zSFTPClient.getcGs |jtd|f|}||SN)r~rE_read_response)rJrPargrr5r5r6ro+szSFTPClient._requestcGs|jzt}||j|D]P}t|tr<||q"t|t rR||q"t|t rh| |q"| |q"|j}||j |<|jd7_W5|jX||||S)Nr:)r@acquirereleaserZadd_intr=rSr Z add_int64intr+_packZ add_stringrDZ _send_packet)rJfileobjrPrrYitemrr5r5r6r~/s$            zSFTPClient._async_requestc Csz|\}}Wn0tk r@}ztd|W5d}~XYnXt|}|}|jzF||j kr| t d||dkrWqWq|j |}|j |=W5|jX||kr|t kr| |||fS|tdk r|||||dkrqqdS)NzServer connection dropped: {}zUnexpected response #{})NN)rrGr,rIrrqr@rrrDrHrr%rrEZ_async_response)rJZwaitforrPrerYrrr5r5r6rHs2        zSFTPClient._read_responsecCs$||jkr ||qdSr)rDvaluesrZ_check_exception)rJrr5r5r6_finish_responsesmszSFTPClient._finish_responsescCsf|}|}|tkrdS|tkr.t|n4|tkrDttj|n|t krZttj |nt|dS)zU Raises EOFError or IOError on error status; otherwise does nothing. N) rqrsr'r(rGr)rrENOENTr*EACCES)rJrYrtextr5r5r6rrs 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_slashrgr5r5r6rns   zSFTPClient._adjust_cwd)NN)ra)ra)rar|)rr)N)rNT)NT)N)N)N)-__name__ __module__ __qualname____doc__r; classmethodrQrHr^r`rirfrrrrunlinkrrrrrrrrrrrrrrrrrrrrrror~rrrrn __classcell__r5r5r\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    "    @