a °…bJ‡ã@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?Gdd„dee=ƒZ@Gdd„de@ƒZAdS)é)ÚhexlifyN)Úutil)ÚChannel)ÚMessage)ÚINFOÚDEBUGÚo777)ÚbÚuÚlong)ÚBaseSFTPÚ CMD_OPENDIRÚ CMD_HANDLEÚ SFTPErrorÚ CMD_READDIRÚCMD_NAMEÚ CMD_CLOSEÚSFTP_FLAG_READÚSFTP_FLAG_WRITEÚSFTP_FLAG_CREATEÚSFTP_FLAG_TRUNCÚSFTP_FLAG_APPENDÚSFTP_FLAG_EXCLÚCMD_OPENÚ CMD_REMOVEÚ CMD_RENAMEÚ CMD_MKDIRÚ CMD_RMDIRÚCMD_STATÚ CMD_ATTRSÚ CMD_LSTATÚ CMD_SYMLINKÚ CMD_SETSTATÚ CMD_READLINKÚ CMD_REALPATHÚ CMD_STATUSÚ CMD_EXTENDEDÚSFTP_OKÚSFTP_EOFÚSFTP_NO_SUCH_FILEÚSFTP_PERMISSION_DENIED)ÚSFTPAttributes)Ú SSHException)ÚSFTPFile)ÚClosingContextManagerc CsTz | d¡WSttfyNz| d¡WYStyH|YYS0Yn0dS)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Ú UnicodeErrorÚAttributeErrorÚdecode)Ús©r5ú;/tmp/pip-target-98j97qn4/lib/python/paramiko/sftp_client.pyÚ _to_unicodeHs  r7ó/cs^eZdZdZdd„ZedQdd„ƒZ‡fdd„Zd d „Zd d „Z dRdd„Z dSdd„Z dTdd„Z dUdd„Z e Zdd„ZeZdd„Zdd„Zefdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„ZdVd5d6„Zd7d8„Z d9d:„Z!dWd=d>„Z"dXd?d@„Z#dYdAdB„Z$dZdCdD„Z%dEdF„Z&dGdH„Z'd[dIdJ„Z(dKdL„Z)dMdN„Z*dOdP„Z+‡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. cCs¨t |¡||_d|_d|_t ¡|_d|_t   ¡|_ t |ƒt urj|j ¡}t | ¡d¡|_| ¡|_z | ¡}Wntytdƒ‚Yn0| 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 FéNz.sftpzEOF during negotiationz*Opened sftp connection (server version {}))r Ú__init__ÚsockZ ultra_debugÚrequest_numberÚ threadingÚLockÚ_lockÚ_cwdÚweakrefÚWeakValueDictionaryÚ _expectingÚtyperZ get_transportrZ get_loggerZget_log_channelÚloggerZ get_hexdumpZ _send_versionÚEOFErrorr,Ú_logrÚformat)Úselfr<Ú transportZserver_versionr5r5r6r;ds.      ÿ   ÿþzSFTPClient.__init__NcCs,|j||d}|durdS| 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_sizeÚmax_packet_sizeNÚsftp)Z open_sessionZinvoke_subsystem)ÚclsÚtrLrMZchanr5r5r6Úfrom_transportŒsÿ zSFTPClient.from_transportcsht|tƒr*|D]}|j||g|¢RŽqn:| dd¡}tt|ƒj|d|g|j ¡gt|ƒ¢RŽdS)Nú%z%%z [chan %s] )Ú isinstanceÚlistrHÚreplaceÚsuperr9r<Úget_name)rJÚlevelÚmsgÚargsÚm©Ú __class__r5r6rH¬s   þýzSFTPClient._logcCs| td¡|j ¡dS)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<r_r5r5r6Ú get_channelÅszSFTPClient.get_channelÚ.cCsdd„| |¡DƒS)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|] }|j‘qSr5)Úfilename)Ú.0Úfr5r5r6Ú Úóz&SFTPClient.listdir..)Ú listdir_attr©rJÚpathr5r5r6ÚlistdirÎs zSFTPClient.listdirc Csä| |¡}| td |¡¡| t|¡\}}|tkr      ÿ   zSFTPClient.listdir_iterÚréÿÿÿÿc Csô| |¡}| td ||¡¡d}d|vs2d|vr:|tO}d|vsRd|vsRd|vrZ|tO}d|vrn|ttBO}d|vr‚|ttBO}d|vr–|tt BO}t ƒ}|  t |||¡\}}|t krÀtdƒ‚| ¡}| td  ||tt|ƒƒ¡¡t||||ƒS) aT 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 (default: ``-1``) :return: an `.SFTPFile` object representing the open file :raises: ``IOError`` -- if the file could not be opened. zopen({!r}, {!r})rr…ú+ÚwÚaÚxrlzopen({!r}, {!r}) -> {})rorHrrIrrrrrrr+rprrrrqr rr-) rJrbÚmodeÚbufsizeZimodeZ attrblockrPrYrwr5r5r6ÚopenFs2    ÿþ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)rorHrrIrprrhr5r5r6Úremoveƒs 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)rorHrrIrpr©rJZoldpathÚnewpathr5r5r6Úrename’s  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)rorHrrIrpr&rr5r5r6Ú posix_rename©s   ÿ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)rorHrrIr+Úst_moderpr©rJrir‹r|r5r5r6Úmkdir¿s zSFTPClient.mkdircCs,| |¡}| td |¡¡| t|¡dS)zj Remove the folder named ``path``. :param str path: name of the folder to remove z rmdir({!r})N)rorHrrIrprrhr5r5r6ÚrmdirÎs zSFTPClient.rmdircCsF| |¡}| td |¡¡| t|¡\}}|tkr0Y|rt| |¡}|j|krztd |j|¡ƒ‚ntƒ}|S)aï Copy the contents of an open file object (``fl``) to the SFTP server as ``remotepath``. Any exception raised by operations will be passed through. The SFTP operations use pipelining for speed. :param fl: opened file or file-like object to copy :param str remotepath: the destination path on the SFTP server :param int file_size: optional size parameter passed to callback. If none is specified, size defaults to 0 :param callable callback: optional callback function (form: ``func(int, int)``) that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4) :param bool confirm: whether to do a stat() on the file afterwards to confirm the file size (since 1.7.7) :return: an `.SFTPAttributes` object containing attributes about the given file. .. versionadded:: 1.10 ÚwbT©rºr»r¼r½Nzsize mismatch in put! {} != {})ÚfileZ set_pipelinedr¿r™r©ÚIOErrorrIr+) rJÚflÚ remotepathr¼r½ÚconfirmÚfrrªr4r5r5r6Úputfo¯s ÿ$   ÿzSFTPClient.putfocCsLt |¡j}t|dƒ"}| |||||¡WdƒS1s>0YdS)an Copy a local file (``localpath``) to the SFTP server as ``remotepath``. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. The SFTP operations use pipelining for speed. :param str localpath: the local file to copy :param str remotepath: the destination path on the SFTP server. Note that the filename should be included. Only specifying a directory may result in an error. :param callable callback: optional callback function (form: ``func(int, int)``) that accepts the bytes transferred so far and the total bytes to be transferred :param bool confirm: whether to do a stat() on the file afterwards to confirm the file size :return: an `.SFTPAttributes` object containing attributes about the given file .. versionadded:: 1.4 .. versionchanged:: 1.7.4 ``callback`` and rich attribute return value added. .. versionchanged:: 1.7.7 ``confirm`` param added. ÚrbN)r²r™r©rrÈ)rJÚ localpathrÅr½rÆr¼rÄr5r5r6ÚputÙs  zSFTPClient.putcCs\| |¡j}| |d¡0}|r(| |¡|j||||dWdƒS1sN0YdS)až Copy a remote file (``remotepath``) from the SFTP server and write to an open file or file-like object, ``fl``. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. :param object remotepath: opened file or file-like object to copy to :param str fl: the destination path on the local host or open file object :param callable callback: optional callback function (form: ``func(int, int)``) that accepts the bytes transferred so far and the total bytes to be transferred :param bool prefetch: controls whether prefetching is performed (default: True) :return: the `number ` of bytes written to the opened file object .. versionadded:: 1.10 .. versionchanged:: 2.8 Added the ``prefetch`` keyword argument. rÉrÁN)r™r©rÚprefetchr¿)rJrÅrÄr½rÌr¼rÇr5r5r6Úgetfoùs  ÿzSFTPClient.getfocCsdt|dƒ }| ||||¡}Wdƒn1s00Yt |¡}|j|kr`td |j|¡ƒ‚dS)aO 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 :param bool prefetch: controls whether prefetching is performed (default: True) .. versionadded:: 1.4 .. versionchanged:: 1.7.4 Added the ``callback`` param .. versionchanged:: 2.8 Added the ``prefetch`` keyword argument. rÀNzsize mismatch in get! {} != {})rrÍr²r™r©rÃrI)rJrÅrÊr½rÌrÄrªr4r5r5r6Úgets .   ÿzSFTPClient.getcGs"|jtdƒ|g|¢RŽ}| |¡S©N)rrEÚ_read_response)rJrPÚargrƒr5r5r6rp4szSFTPClient._requestcGs¼|j ¡z”tƒ}| |j¡|D]P}t|tƒr<| |¡q"t|tƒrR| |¡q"t|t ƒrh|  |¡q"|  |¡q"|j}||j |<|jd7_W|j  ¡n |j  ¡0| ||¡|S)Nr:)r@ÚacquirerZadd_intr=rSr Z add_int64Úintr+Ú_packZ add_stringrDÚreleaseZ _send_packet)rJÚfileobjrPrÑrYÚitemrƒr5r5r6r8s$           zSFTPClient._async_requestc Csz| ¡\}}Wn2tyB}ztd |¡ƒ‚WYd}~n d}~00t|ƒ}| ¡}|j ¡zb||jvr¢|  t d |¡¡|dur”W|j  ¡qW|j  ¡q|j|}|j|=W|j  ¡n |j  ¡0||krð|t krè|  |¡||fS|tdƒur | |||¡|durqqdS)NzServer connection dropped: {}zUnexpected response #{})NN)r€rGr,rIrrrr@rÒrDrHrrÕr%rrEZ_async_response)rJZwaitforrPr¾ÚerYrƒrÖr5r5r6rÐQs:$   û ü   zSFTPClient._read_responsecCs$||j ¡vr | ¡| ¡qdSrÏ)rDÚvaluesrÐZ_check_exception)rJrÖr5r5r6Ú_finish_responsesvszSFTPClient._finish_responsescCsf| ¡}| ¡}|tkrdS|tkr.t|ƒ‚n4|tkrDttj|ƒ‚n|t krZttj |ƒ‚nt|ƒ‚dS)zU Raises EOFError or IOError on error status; otherwise does nothing. N) rrrtr'r(rGr)rÃr°ÚENOENTr*ÚEACCES)rJrYr´Útextr5r5r6r{s zSFTPClient._convert_statuscCsTt|ƒ}|jdur|St|ƒr2|dd…tkr2|S|jtkrF|j|S|jt|S)zr Return an adjusted path if we're emulating a "current working directory" for the server. Nrr:)r rAr¹Úb_slashrhr5r5r6ros   zSFTPClient._adjust_cwd)NN)ra)ra)rar})r…r†)N)rNT)NT)NT)NT)N)-Ú__name__Ú __module__Ú __qualname__Ú__doc__r;Ú classmethodrQrHr^r`rjrgr„rrÂrŽÚunlinkr‘r’rr•r–r™ršrržr£r¨r«r­r®rµr¶r¿rÈrËrÍrÎrprrÐrÚrroÚ __classcell__r5r5r\r6r9ZsP (     * @ ;       *   %r9c@seZdZdZdS)ÚSFTPzA An alias for `.SFTPClient` for backwards compatibility. N)rßràrárâr5r5r5r6ræsræ)BÚbinasciirr°r²r™r>r¤rBZparamikorZparamiko.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.r7rÞr9rær5r5r5r6Ús4    „"    I